blob: e653654c10bcd8e49c2809891966448dc9975a41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust39d43d12020-10-07 18:24:18 -040066#include "sysfs.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040067#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050068#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000069#include "fscache.h"
Frank van der Linden012a2112020-06-23 22:39:03 +000070#include "nfs42.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Trond Myklebustc6d01c62013-08-09 11:51:26 -040072#include "nfs4trace.h"
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFSDBG_FACILITY NFSDBG_PROC
75
Trond Myklebust30846df2018-04-07 13:44:28 -040076#define NFS4_BITMASK_SZ 3
77
Trond Myklebust2066fe82006-09-15 08:30:46 -040078#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define NFS4_POLL_RETRY_MAX (15*HZ)
80
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020081/* file attributes which can be mapped to nfs attributes */
82#define NFS4_VALID_ATTRS (ATTR_MODE \
83 | ATTR_UID \
84 | ATTR_GID \
85 | ATTR_SIZE \
86 | ATTR_ATIME \
87 | ATTR_MTIME \
88 | ATTR_CTIME \
89 | ATTR_ATIME_SET \
90 | ATTR_MTIME_SET)
91
Trond Myklebustcdd4e682006-01-03 09:55:12 +010092struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080093static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050095static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040096static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110097static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040098 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110099 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -0400100 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400101#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400102static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100103 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400104 struct nfs4_slot *slot,
105 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400106static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100107 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400108static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100109 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400110#endif
Trond Myklebust332d1a02021-03-25 18:15:36 -0400111static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ],
112 const __u32 *src, struct inode *inode,
113 struct nfs_server *server,
114 struct nfs4_label *label);
David Quigleyaa9c2662013-05-22 12:50:44 -0400115
116#ifdef CONFIG_NFS_V4_SECURITY_LABEL
117static inline struct nfs4_label *
118nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
119 struct iattr *sattr, struct nfs4_label *label)
120{
121 int err;
122
123 if (label == NULL)
124 return NULL;
125
126 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
127 return NULL;
128
David Quigleyaa9c2662013-05-22 12:50:44 -0400129 err = security_dentry_init_security(dentry, sattr->ia_mode,
130 &dentry->d_name, (void **)&label->label, &label->len);
131 if (err == 0)
132 return label;
133
134 return NULL;
135}
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{
139 if (label)
140 security_release_secctx(label->label, label->len);
141}
142static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
143{
144 if (label)
145 return server->attr_bitmask;
146
147 return server->attr_bitmask_nl;
148}
149#else
150static inline struct nfs4_label *
151nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
152 struct iattr *sattr, struct nfs4_label *l)
153{ return NULL; }
154static inline void
155nfs4_label_release_security(struct nfs4_label *label)
156{ return; }
157static inline u32 *
158nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
159{ return server->attr_bitmask; }
160#endif
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500163static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 if (err >= -1000)
166 return err;
167 switch (err) {
168 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500169 case -NFS4ERR_LAYOUTTRYLATER:
170 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400171 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000172 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400173 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000174 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800175 case -NFS4ERR_BADOWNER:
176 case -NFS4ERR_BADNAME:
177 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400178 case -NFS4ERR_SHARE_DENIED:
179 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400180 case -NFS4ERR_MINOR_VERS_MISMATCH:
181 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400182 case -NFS4ERR_FILE_OPEN:
183 return -EBUSY;
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500184 case -NFS4ERR_NOT_SAME:
185 return -ENOTSYNC;
Trond Myklebust52567b02009-10-23 14:46:42 -0400186 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700188 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400191 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
195 * This is our standard bitmap for GETATTR requests.
196 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400197const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 FATTR4_WORD0_TYPE
199 | FATTR4_WORD0_CHANGE
200 | FATTR4_WORD0_SIZE
201 | FATTR4_WORD0_FSID
202 | FATTR4_WORD0_FILEID,
203 FATTR4_WORD1_MODE
204 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER
206 | FATTR4_WORD1_OWNER_GROUP
207 | FATTR4_WORD1_RAWDEV
208 | FATTR4_WORD1_SPACE_USED
209 | FATTR4_WORD1_TIME_ACCESS
210 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400211 | FATTR4_WORD1_TIME_MODIFY
212 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400213#ifdef CONFIG_NFS_V4_SECURITY_LABEL
214 FATTR4_WORD2_SECURITY_LABEL
215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
Trond Myklebust1549210f2012-06-05 09:16:47 -0400218static const u32 nfs4_pnfs_open_bitmap[3] = {
219 FATTR4_WORD0_TYPE
220 | FATTR4_WORD0_CHANGE
221 | FATTR4_WORD0_SIZE
222 | FATTR4_WORD0_FSID
223 | FATTR4_WORD0_FILEID,
224 FATTR4_WORD1_MODE
225 | FATTR4_WORD1_NUMLINKS
226 | FATTR4_WORD1_OWNER
227 | FATTR4_WORD1_OWNER_GROUP
228 | FATTR4_WORD1_RAWDEV
229 | FATTR4_WORD1_SPACE_USED
230 | FATTR4_WORD1_TIME_ACCESS
231 | FATTR4_WORD1_TIME_METADATA
232 | FATTR4_WORD1_TIME_MODIFY,
233 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500234#ifdef CONFIG_NFS_V4_SECURITY_LABEL
235 | FATTR4_WORD2_SECURITY_LABEL
236#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400237};
238
Andy Adamsone23008e2012-10-02 21:07:32 -0400239static const u32 nfs4_open_noattr_bitmap[3] = {
240 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400241 | FATTR4_WORD0_FILEID,
242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 FATTR4_WORD0_FILES_AVAIL
246 | FATTR4_WORD0_FILES_FREE
247 | FATTR4_WORD0_FILES_TOTAL,
248 FATTR4_WORD1_SPACE_AVAIL
249 | FATTR4_WORD1_SPACE_FREE
250 | FATTR4_WORD1_SPACE_TOTAL
251};
252
David Quigleya09df2c2013-05-22 12:50:41 -0400253const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 FATTR4_WORD0_MAXLINK
255 | FATTR4_WORD0_MAXNAME,
256 0
257};
258
Fred Isamandae100c2011-07-30 20:52:37 -0400259const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 | FATTR4_WORD0_MAXREAD
261 | FATTR4_WORD0_MAXWRITE
262 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700263 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400264 | FATTR4_WORD1_FS_LAYOUT_TYPES,
265 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800266 | FATTR4_WORD2_CLONE_BLKSIZE
Trond Myklebust7f08a332021-03-26 09:50:19 -0400267 | FATTR4_WORD2_CHANGE_ATTR_TYPE
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000268 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
David Quigleya09df2c2013-05-22 12:50:41 -0400271const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500272 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400273 | FATTR4_WORD0_SIZE
274 | FATTR4_WORD0_FSID
275 | FATTR4_WORD0_FILEID
276 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500277 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400278 | FATTR4_WORD1_OWNER_GROUP
279 | FATTR4_WORD1_RAWDEV
280 | FATTR4_WORD1_SPACE_USED
281 | FATTR4_WORD1_TIME_ACCESS
282 | FATTR4_WORD1_TIME_METADATA
283 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400284 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400285};
286
Trond Myklebust30846df2018-04-07 13:44:28 -0400287static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
Trond Myklebusta71029b2021-04-10 00:23:03 -0400288 struct inode *inode, unsigned long flags)
Trond Myklebust30846df2018-04-07 13:44:28 -0400289{
290 unsigned long cache_validity;
291
292 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
293 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
294 return;
295
Trond Myklebusta71029b2021-04-10 00:23:03 -0400296 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
Trond Myklebust30846df2018-04-07 13:44:28 -0400297
Trond Myklebusta71029b2021-04-10 00:23:03 -0400298 /* Remove the attributes over which we have full control */
299 dst[1] &= ~FATTR4_WORD1_RAWDEV;
Trond Myklebust30846df2018-04-07 13:44:28 -0400300 if (!(cache_validity & NFS_INO_INVALID_SIZE))
301 dst[0] &= ~FATTR4_WORD0_SIZE;
302
303 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
304 dst[0] &= ~FATTR4_WORD0_CHANGE;
Trond Myklebust30846df2018-04-07 13:44:28 -0400305
Trond Myklebust720869e2021-04-13 09:41:16 -0400306 if (!(cache_validity & NFS_INO_INVALID_MODE))
307 dst[1] &= ~FATTR4_WORD1_MODE;
Trond Myklebusta71029b2021-04-10 00:23:03 -0400308 if (!(cache_validity & NFS_INO_INVALID_OTHER))
Trond Myklebust720869e2021-04-13 09:41:16 -0400309 dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
Trond Myklebust30846df2018-04-07 13:44:28 -0400310}
311
Al Virobc4785c2006-10-19 23:28:51 -0700312static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct nfs4_readdir_arg *readdir)
314{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400315 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700316 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000319 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
321 return;
322 }
323
324 readdir->cookie = 0;
325 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
326 if (cookie == 2)
327 return;
328
329 /*
330 * NFSv4 servers do not return entries for '.' and '..'
331 * Therefore, we fake these entries here. We let '.'
332 * have cookie 0 and '..' have cookie 1. Note that
333 * when talking to the server, we always send cookie 0
334 * instead of 1 or 2.
335 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800336 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (cookie == 0) {
339 *p++ = xdr_one; /* next */
340 *p++ = xdr_zero; /* cookie, first word */
341 *p++ = xdr_one; /* cookie, second word */
342 *p++ = xdr_one; /* entry len */
343 memcpy(p, ".\0\0\0", 4); /* entry */
344 p++;
345 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400346 *p++ = htonl(attrs); /* bitmap */
347 *p++ = htonl(12); /* attribute buffer length */
348 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000349 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 *p++ = xdr_one; /* next */
353 *p++ = xdr_zero; /* cookie, first word */
354 *p++ = xdr_two; /* cookie, second word */
355 *p++ = xdr_two; /* entry len */
356 memcpy(p, "..\0\0", 4); /* entry */
357 p++;
358 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400359 *p++ = htonl(attrs); /* bitmap */
360 *p++ = htonl(12); /* attribute buffer length */
361 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000362 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 readdir->pgbase = (char *)p - (char *)start;
365 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800366 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Trond Myklebust26d36302016-09-22 13:39:05 -0400369static void nfs4_test_and_free_stateid(struct nfs_server *server,
370 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100371 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400372{
373 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
374
375 ops->test_and_free_expired(server, stateid, cred);
376}
377
378static void __nfs4_free_revoked_stateid(struct nfs_server *server,
379 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100380 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400381{
382 stateid->type = NFS4_REVOKED_STATEID_TYPE;
383 nfs4_test_and_free_stateid(server, stateid, cred);
384}
385
386static void nfs4_free_revoked_stateid(struct nfs_server *server,
387 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100388 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400389{
390 nfs4_stateid tmp;
391
392 nfs4_stateid_copy(&tmp, stateid);
393 __nfs4_free_revoked_stateid(server, &tmp, cred);
394}
395
NeilBrown8478eaa2014-09-18 16:09:27 +1000396static long nfs4_update_delay(long *timeout)
397{
398 long ret;
399 if (!timeout)
400 return NFS4_POLL_RETRY_MAX;
401 if (*timeout <= 0)
402 *timeout = NFS4_POLL_RETRY_MIN;
403 if (*timeout > NFS4_POLL_RETRY_MAX)
404 *timeout = NFS4_POLL_RETRY_MAX;
405 ret = *timeout;
406 *timeout <<= 1;
407 return ret;
408}
409
Trond Myklebust0688e642019-04-07 13:59:09 -0400410static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500411{
Trond Myklebust65de8722008-12-23 15:21:44 -0500412 might_sleep();
413
NeilBrown8478eaa2014-09-18 16:09:27 +1000414 freezable_schedule_timeout_killable_unsafe(
415 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400416 if (!__fatal_signal_pending(current))
417 return 0;
418 return -EINTR;
419}
420
421static int nfs4_delay_interruptible(long *timeout)
422{
423 might_sleep();
424
He Zhe59679d92020-07-06 17:52:24 +0800425 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400426 if (!signal_pending(current))
427 return 0;
428 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
429}
430
431static int nfs4_delay(long *timeout, bool interruptible)
432{
433 if (interruptible)
434 return nfs4_delay_interruptible(timeout);
435 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500436}
437
Trond Myklebust50c80002019-07-11 19:02:18 -0400438static const nfs4_stateid *
439nfs4_recoverable_stateid(const nfs4_stateid *stateid)
440{
441 if (!stateid)
442 return NULL;
443 switch (stateid->type) {
444 case NFS4_OPEN_STATEID_TYPE:
445 case NFS4_LOCK_STATEID_TYPE:
446 case NFS4_DELEGATION_STATEID_TYPE:
447 return stateid;
448 default:
449 break;
450 }
451 return NULL;
452}
453
Trond Myklebust65de8722008-12-23 15:21:44 -0500454/* This is the error handling routine for processes that are allowed
455 * to sleep.
456 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400457static int nfs4_do_handle_exception(struct nfs_server *server,
458 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500459{
460 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500461 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400462 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500463 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500464 int ret = errorcode;
465
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400466 exception->delay = 0;
467 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400469
Trond Myklebust50c80002019-07-11 19:02:18 -0400470 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400471 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400472 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400473
Trond Myklebust65de8722008-12-23 15:21:44 -0500474 switch(errorcode) {
475 case 0:
476 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400477 case -NFS4ERR_BADHANDLE:
478 case -ESTALE:
479 if (inode != NULL && S_ISREG(inode->i_mode))
480 pnfs_destroy_layout(NFS_I(inode));
481 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400482 case -NFS4ERR_DELEG_REVOKED:
483 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400484 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400485 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400486 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400487 if (inode != NULL && stateid != NULL) {
488 nfs_inode_find_state_and_recover(inode,
489 stateid);
490 goto wait_on_recovery;
491 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500492 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400493 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400494 if (inode) {
495 int err;
496
497 err = nfs_async_inode_return_delegation(inode,
498 stateid);
499 if (err == 0)
500 goto wait_on_recovery;
501 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
502 exception->retry = 1;
503 break;
504 }
505 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500506 if (state == NULL)
507 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400508 ret = nfs4_schedule_stateid_recovery(server, state);
509 if (ret < 0)
510 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500511 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500512 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500513 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500514 nfs4_schedule_lease_recovery(clp);
515 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400516 case -NFS4ERR_MOVED:
517 ret = nfs4_schedule_migration_recovery(server);
518 if (ret < 0)
519 break;
520 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400521 case -NFS4ERR_LEASE_MOVED:
522 nfs4_schedule_lease_moved_recovery(clp);
523 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500524#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400525 case -NFS4ERR_BADSESSION:
526 case -NFS4ERR_BADSLOT:
527 case -NFS4ERR_BAD_HIGH_SLOT:
528 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
529 case -NFS4ERR_DEADSESSION:
530 case -NFS4ERR_SEQ_FALSE_RETRY:
531 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100532 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400533 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500534#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500535 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500536 if (exception->timeout > HZ) {
537 /* We have retried a decent amount, time to
538 * fail
539 */
540 ret = -EBUSY;
541 break;
542 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500543 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500544 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400545 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500546 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400547 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400548 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400549 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400550 exception->delay = 1;
551 return 0;
552
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400553 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500554 case -NFS4ERR_OLD_STATEID:
555 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800556 break;
557 case -NFS4ERR_BADOWNER:
558 /* The following works around a Linux server bug! */
559 case -NFS4ERR_BADNAME:
560 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
561 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
562 exception->retry = 1;
563 printk(KERN_WARNING "NFS: v4 server %s "
564 "does not accept raw "
565 "uid/gids. "
566 "Reenabling the idmapper.\n",
567 server->nfs_client->cl_hostname);
568 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500569 }
570 /* We failed to handle the error */
571 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500572wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400573 exception->recovering = 1;
574 return 0;
575}
576
577/* This is the error handling routine for processes that are allowed
578 * to sleep.
579 */
580int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
581{
582 struct nfs_client *clp = server->nfs_client;
583 int ret;
584
585 ret = nfs4_do_handle_exception(server, errorcode, exception);
586 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400587 ret = nfs4_delay(&exception->timeout,
588 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400589 goto out_retry;
590 }
591 if (exception->recovering) {
Trond Myklebustdfe1fe72021-06-01 11:10:05 -0400592 if (exception->task_is_privileged)
593 return -EDEADLOCK;
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400594 ret = nfs4_wait_clnt_recover(clp);
595 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
596 return -EIO;
597 goto out_retry;
598 }
599 return ret;
600out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500601 if (ret == 0)
602 exception->retry = 1;
603 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500604}
605
Trond Myklebust037fc982015-09-20 15:51:00 -0400606static int
607nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
608 int errorcode, struct nfs4_exception *exception)
609{
610 struct nfs_client *clp = server->nfs_client;
611 int ret;
612
613 ret = nfs4_do_handle_exception(server, errorcode, exception);
614 if (exception->delay) {
615 rpc_delay(task, nfs4_update_delay(&exception->timeout));
616 goto out_retry;
617 }
618 if (exception->recovering) {
Trond Myklebustdfe1fe72021-06-01 11:10:05 -0400619 if (exception->task_is_privileged)
620 return -EDEADLOCK;
Trond Myklebust037fc982015-09-20 15:51:00 -0400621 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
622 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
623 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
624 goto out_retry;
625 }
626 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
627 ret = -EIO;
628 return ret;
629out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500630 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400631 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500632 /*
633 * For NFS4ERR_MOVED, the client transport will need to
634 * be recomputed after migration recovery has completed.
635 */
636 if (errorcode == -NFS4ERR_MOVED)
637 rpc_task_release_transport(task);
638 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400639 return ret;
640}
641
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400642int
Trond Myklebust037fc982015-09-20 15:51:00 -0400643nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
644 struct nfs4_state *state, long *timeout)
645{
646 struct nfs4_exception exception = {
647 .state = state,
648 };
649
650 if (task->tk_status >= 0)
651 return 0;
652 if (timeout)
653 exception.timeout = *timeout;
654 task->tk_status = nfs4_async_handle_exception(task, server,
655 task->tk_status,
656 &exception);
657 if (exception.delay && timeout)
658 *timeout = exception.timeout;
659 if (exception.retry)
660 return -EAGAIN;
661 return 0;
662}
663
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400664/*
665 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
666 * or 'false' otherwise.
667 */
668static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
669{
670 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500671 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400672}
Trond Myklebust65de8722008-12-23 15:21:44 -0500673
Trond Myklebust452e9352010-07-31 14:29:06 -0400674static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 spin_lock(&clp->cl_lock);
677 if (time_before(clp->cl_last_renewal,timestamp))
678 clp->cl_last_renewal = timestamp;
679 spin_unlock(&clp->cl_lock);
680}
681
Trond Myklebust452e9352010-07-31 14:29:06 -0400682static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
683{
Trond Myklebustbe824162015-07-05 14:50:46 -0400684 struct nfs_client *clp = server->nfs_client;
685
686 if (!nfs4_has_session(clp))
687 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400688}
689
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400690struct nfs4_call_sync_data {
691 const struct nfs_server *seq_server;
692 struct nfs4_sequence_args *seq_args;
693 struct nfs4_sequence_res *seq_res;
694};
695
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800696void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400697 struct nfs4_sequence_res *res, int cache_reply,
698 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400699{
700 args->sa_slot = NULL;
701 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400702 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400703
704 res->sr_slot = NULL;
705}
706
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400707static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400708{
709 struct nfs4_slot *slot = res->sr_slot;
710 struct nfs4_slot_table *tbl;
711
Chuck Lever3bd23842013-08-09 12:49:19 -0400712 tbl = slot->table;
713 spin_lock(&tbl->slot_tbl_lock);
714 if (!nfs41_wake_and_assign_slot(tbl, slot))
715 nfs4_free_slot(tbl, slot);
716 spin_unlock(&tbl->slot_tbl_lock);
717
718 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400719}
720
721static int nfs40_sequence_done(struct rpc_task *task,
722 struct nfs4_sequence_res *res)
723{
724 if (res->sr_slot != NULL)
725 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400726 return 1;
727}
728
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400729#if defined(CONFIG_NFS_V4_1)
730
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400731static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400732{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500733 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400734 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500735 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400736
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400737 if (!slot)
738 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500739 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500740 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500741
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400742 /* Bump the slot sequence number */
743 if (slot->seq_done)
744 slot->seq_nr++;
745 slot->seq_done = 0;
746
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500747 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500748 /* Be nice to the server: try to ensure that the last transmitted
749 * value for highest_user_slotid <= target_highest_slotid
750 */
751 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
752 send_new_highest_used_slotid = true;
753
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500754 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500755 send_new_highest_used_slotid = false;
756 goto out_unlock;
757 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500758 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500759
760 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
761 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500762out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500763 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500764 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400765 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400766 if (waitqueue_active(&tbl->slot_waitq))
767 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400768}
769
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400770static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
771{
772 nfs41_release_slot(res->sr_slot);
773 res->sr_slot = NULL;
774}
775
Trond Myklebust3453d572018-06-20 17:53:34 -0400776static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
777 u32 seqnr)
778{
779 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
780 slot->seq_nr_highest_sent = seqnr;
781}
782static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
783 u32 seqnr)
784{
785 slot->seq_nr_highest_sent = seqnr;
786 slot->seq_nr_last_acked = seqnr;
787}
788
Anna Schumaker913fadc2020-07-08 10:33:40 -0400789static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
790 struct nfs4_slot *slot)
791{
792 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
793 if (!IS_ERR(task))
794 rpc_put_task_async(task);
795}
796
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400797static int nfs41_sequence_process(struct rpc_task *task,
798 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400799{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500800 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500801 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400802 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100803 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500804 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400805
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500806 if (slot == NULL)
807 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400808 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500809 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400810 goto out;
811
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500812 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400813 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500814
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400815 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100816
817 status = res->sr_status;
818 if (task->tk_status == -NFS4ERR_DEADSESSION)
819 status = -NFS4ERR_DEADSESSION;
820
Andy Adamson691daf32009-12-04 15:55:39 -0500821 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100822 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400824 /* Mark this sequence number as having been acked */
825 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400826 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400827 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500828 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500829 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400830 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
831 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500832 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400833 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500834 case 1:
835 /*
836 * sr_status remains 1 if an RPC level error occurred.
837 * The server may or may not have processed the sequence
838 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500839 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400840 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
841 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500842 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400843 case -NFS4ERR_DELAY:
844 /* The server detected a resend of the RPC call and
845 * returned NFS4ERR_DELAY as per Section 2.10.6.2
846 * of RFC5661.
847 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500848 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400849 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500850 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500851 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400852 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400853 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400854 case -NFS4ERR_RETRY_UNCACHED_REP:
855 case -NFS4ERR_SEQ_FALSE_RETRY:
856 /*
857 * The server thinks we tried to replay a request.
858 * Retry the call after bumping the sequence ID.
859 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400860 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400861 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500862 case -NFS4ERR_BADSLOT:
863 /*
864 * The slot id we used was probably retired. Try again
865 * using a different slot id.
866 */
Trond Myklebust99589102018-06-09 12:50:50 -0400867 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400868 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500869 goto retry_nowait;
870 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400871 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500872 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400873 * Were one or more calls using this slot interrupted?
874 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400875 * transmitted slot sequence number may be too high. However,
876 * if the server did receive the request then it might
877 * accidentally give us a reply with a mismatched operation.
878 * We can sort this out by sending a lone sequence operation
879 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500880 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400881 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
882 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400883 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
884 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
885 res->sr_slot = NULL;
886 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500887 goto retry_nowait;
888 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400889 /*
890 * RFC5661:
891 * A retry might be sent while the original request is
892 * still in progress on the replier. The replier SHOULD
893 * deal with the issue by returning NFS4ERR_DELAY as the
894 * reply to SEQUENCE or CB_SEQUENCE operation, but
895 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
896 *
897 * Restart the search after a delay.
898 */
899 slot->seq_nr = slot->seq_nr_highest_sent;
900 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100901 case -NFS4ERR_BADSESSION:
902 case -NFS4ERR_DEADSESSION:
903 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
904 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400905 default:
906 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400907 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400908 }
909out:
910 /* The session may be reset by one of the error handlers. */
911 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500912out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500913 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400914session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100915 nfs4_schedule_session_recovery(session, status);
916 dprintk("%s ERROR: %d Reset session\n", __func__, status);
917 nfs41_sequence_free_slot(res);
918 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400919retry_new_seq:
920 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500921retry_nowait:
922 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400923 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500924 task->tk_status = 0;
925 ret = 0;
926 }
927 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400928out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400929 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400930 goto out;
931 rpc_delay(task, NFS4_POLL_RETRY_MAX);
932 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400933}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400934
935int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
936{
937 if (!nfs41_sequence_process(task, res))
938 return 0;
939 if (res->sr_slot != NULL)
940 nfs41_sequence_free_slot(res);
941 return 1;
942
943}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500944EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400945
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400946static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
947{
948 if (res->sr_slot == NULL)
949 return 1;
950 if (res->sr_slot->table->session != NULL)
951 return nfs41_sequence_process(task, res);
952 return nfs40_sequence_done(task, res);
953}
954
955static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
956{
957 if (res->sr_slot != NULL) {
958 if (res->sr_slot->table->session != NULL)
959 nfs41_sequence_free_slot(res);
960 else
961 nfs40_sequence_free_slot(res);
962 }
963}
964
Peng Tao2c4b1312014-06-11 05:24:16 +0800965int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400966{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500967 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400968 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400969 if (!res->sr_slot->table->session)
970 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400971 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400972}
Peng Tao2c4b1312014-06-11 05:24:16 +0800973EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400974
Andy Adamsonce5039c2009-04-01 09:22:13 -0400975static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
976{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400977 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400978
Trond Myklebust035168ab2010-06-16 09:52:26 -0400979 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
980
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500981 nfs4_setup_sequence(data->seq_server->nfs_client,
982 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400983}
984
Andy Adamson69ab40c2009-04-01 09:22:19 -0400985static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
986{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400987 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988
Trond Myklebust14516c32010-07-31 14:29:06 -0400989 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400990}
991
Trond Myklebust17280172012-03-11 13:11:00 -0400992static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400993 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400994 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400995};
996
Chuck Lever3bd23842013-08-09 12:49:19 -0400997#else /* !CONFIG_NFS_V4_1 */
998
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400999static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1000{
1001 return nfs40_sequence_done(task, res);
1002}
1003
1004static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1005{
1006 if (res->sr_slot != NULL)
1007 nfs40_sequence_free_slot(res);
1008}
1009
Peng Tao2c4b1312014-06-11 05:24:16 +08001010int nfs4_sequence_done(struct rpc_task *task,
1011 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001012{
Chuck Lever3bd23842013-08-09 12:49:19 -04001013 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001014}
Peng Tao2c4b1312014-06-11 05:24:16 +08001015EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001016
1017#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001018
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001019static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1020{
1021 res->sr_timestamp = jiffies;
1022 res->sr_status_flags = 0;
1023 res->sr_status = 1;
1024}
1025
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001026static
1027void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1028 struct nfs4_sequence_res *res,
1029 struct nfs4_slot *slot)
1030{
1031 if (!slot)
1032 return;
1033 slot->privileged = args->sa_privileged ? 1 : 0;
1034 args->sa_slot = slot;
1035
1036 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001037}
1038
1039int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001040 struct nfs4_sequence_args *args,
1041 struct nfs4_sequence_res *res,
1042 struct rpc_task *task)
1043{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001044 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001045 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001046 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001047
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001048 /* slot already allocated? */
1049 if (res->sr_slot != NULL)
1050 goto out_start;
1051
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001052 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001053 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001054
Trond Myklebust3453d572018-06-20 17:53:34 -04001055 spin_lock(&tbl->slot_tbl_lock);
1056 /* The state manager will wait until the slot table is empty */
1057 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1058 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001059
Trond Myklebust3453d572018-06-20 17:53:34 -04001060 slot = nfs4_alloc_slot(tbl);
1061 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001062 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001063 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001064 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001065 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001066 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001067
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001068 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001069
Anna Schumakerad05cc02017-01-11 13:37:06 -05001070 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001071out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001072 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001073 rpc_call_start(task);
1074 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001075out_sleep_timeout:
1076 /* Try again in 1/4 second */
1077 if (args->sa_privileged)
1078 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1079 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1080 else
1081 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1082 NULL, jiffies + (HZ >> 2));
1083 spin_unlock(&tbl->slot_tbl_lock);
1084 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001085out_sleep:
1086 if (args->sa_privileged)
1087 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001088 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001089 else
1090 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1091 spin_unlock(&tbl->slot_tbl_lock);
1092 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001093}
1094EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1095
Chuck Lever9915ea72013-08-09 12:48:27 -04001096static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1097{
1098 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001099 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001100 data->seq_args, data->seq_res, task);
1101}
1102
1103static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1104{
1105 struct nfs4_call_sync_data *data = calldata;
1106 nfs4_sequence_done(task, data->seq_res);
1107}
1108
1109static const struct rpc_call_ops nfs40_call_sync_ops = {
1110 .rpc_call_prepare = nfs40_call_sync_prepare,
1111 .rpc_call_done = nfs40_call_sync_done,
1112};
1113
Anna Schumaker48c05852019-08-14 15:27:00 -04001114static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1115{
1116 int ret;
1117 struct rpc_task *task;
1118
1119 task = rpc_run_task(task_setup);
1120 if (IS_ERR(task))
1121 return PTR_ERR(task);
1122
1123 ret = task->tk_status;
1124 rpc_put_task(task);
1125 return ret;
1126}
1127
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001128static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1129 struct nfs_server *server,
1130 struct rpc_message *msg,
1131 struct nfs4_sequence_args *args,
1132 struct nfs4_sequence_res *res,
1133 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001134{
Chuck Lever9915ea72013-08-09 12:48:27 -04001135 struct nfs_client *clp = server->nfs_client;
1136 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001137 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001138 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001140 };
1141 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001142 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001143 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001144 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001145 .callback_data = &data,
1146 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001147 };
1148
Anna Schumaker48c05852019-08-14 15:27:00 -04001149 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001150}
1151
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001152static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1153 struct nfs_server *server,
1154 struct rpc_message *msg,
1155 struct nfs4_sequence_args *args,
1156 struct nfs4_sequence_res *res)
1157{
1158 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1159}
1160
1161
Bryan Schumaker7c513052011-03-24 17:12:24 +00001162int nfs4_call_sync(struct rpc_clnt *clnt,
1163 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001164 struct rpc_message *msg,
1165 struct nfs4_sequence_args *args,
1166 struct nfs4_sequence_res *res,
1167 int cache_reply)
1168{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001169 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001170 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001171}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Trond Myklebust3c591172018-07-31 15:54:10 -04001173static void
1174nfs4_inc_nlink_locked(struct inode *inode)
1175{
Trond Myklebust82eae5a2021-04-01 14:59:59 -04001176 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1177 NFS_INO_INVALID_CTIME |
1178 NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001179 inc_nlink(inode);
1180}
1181
1182static void
Trond Myklebust1301e422021-04-01 14:57:56 -04001183nfs4_inc_nlink(struct inode *inode)
1184{
1185 spin_lock(&inode->i_lock);
1186 nfs4_inc_nlink_locked(inode);
1187 spin_unlock(&inode->i_lock);
1188}
1189
1190static void
Trond Myklebust3c591172018-07-31 15:54:10 -04001191nfs4_dec_nlink_locked(struct inode *inode)
1192{
Trond Myklebust82eae5a2021-04-01 14:59:59 -04001193 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1194 NFS_INO_INVALID_CTIME |
1195 NFS_INO_INVALID_NLINK);
Trond Myklebust3c591172018-07-31 15:54:10 -04001196 drop_nlink(inode);
1197}
1198
1199static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001200nfs4_update_changeattr_locked(struct inode *inode,
1201 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001202 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001204 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust6f9be832021-03-26 11:01:19 -04001205 u64 change_attr = inode_peek_iversion_raw(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001207 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001208
Trond Myklebust6f9be832021-03-26 11:01:19 -04001209 switch (NFS_SERVER(inode)->change_attr_type) {
1210 case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1211 break;
1212 case NFS4_CHANGE_TYPE_IS_TIME_METADATA:
1213 if ((s64)(change_attr - cinfo->after) > 0)
1214 goto out;
1215 break;
1216 default:
1217 if ((s64)(change_attr - cinfo->after) >= 0)
1218 goto out;
1219 }
1220
1221 if (cinfo->atomic && cinfo->before == change_attr) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001222 nfsi->attrtimeo_timestamp = jiffies;
1223 } else {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001224 if (S_ISDIR(inode->i_mode)) {
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001225 cache_validity |= NFS_INO_INVALID_DATA;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001226 nfs_force_lookup_revalidate(inode);
1227 } else {
1228 if (!NFS_PROTO(inode)->have_delegation(inode,
1229 FMODE_READ))
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001230 cache_validity |= NFS_INO_REVAL_PAGECACHE;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001231 }
1232
Trond Myklebust6f9be832021-03-26 11:01:19 -04001233 if (cinfo->before != change_attr)
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001234 cache_validity |= NFS_INO_INVALID_ACCESS |
1235 NFS_INO_INVALID_ACL |
1236 NFS_INO_INVALID_XATTR;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001237 }
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001238 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001239 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001240 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001241 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Trond Myklebust6f9be832021-03-26 11:01:19 -04001242out:
1243 nfs_set_cache_invalid(inode, cache_validity);
Trond Myklebust3c591172018-07-31 15:54:10 -04001244}
1245
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001246void
1247nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001248 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001249{
1250 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001251 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 spin_unlock(&dir->i_lock);
1253}
1254
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001255struct nfs4_open_createattrs {
1256 struct nfs4_label *label;
1257 struct iattr *sattr;
1258 const __u32 verf[2];
1259};
1260
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001261static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1262 int err, struct nfs4_exception *exception)
1263{
1264 if (err != -EINVAL)
1265 return false;
1266 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1267 return false;
1268 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1269 exception->retry = 1;
1270 return true;
1271}
1272
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001273static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1274{
1275 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1276}
1277
1278static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1279{
1280 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1281
1282 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1283}
1284
Trond Myklebust6ae37332015-01-30 14:21:14 -05001285static u32
1286nfs4_map_atomic_open_share(struct nfs_server *server,
1287 fmode_t fmode, int openflags)
1288{
1289 u32 res = 0;
1290
1291 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1292 case FMODE_READ:
1293 res = NFS4_SHARE_ACCESS_READ;
1294 break;
1295 case FMODE_WRITE:
1296 res = NFS4_SHARE_ACCESS_WRITE;
1297 break;
1298 case FMODE_READ|FMODE_WRITE:
1299 res = NFS4_SHARE_ACCESS_BOTH;
1300 }
1301 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1302 goto out;
1303 /* Want no delegation if we're using O_DIRECT */
1304 if (openflags & O_DIRECT)
1305 res |= NFS4_SHARE_WANT_NO_DELEG;
1306out:
1307 return res;
1308}
1309
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001310static enum open_claim_type4
1311nfs4_map_atomic_open_claim(struct nfs_server *server,
1312 enum open_claim_type4 claim)
1313{
1314 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1315 return claim;
1316 switch (claim) {
1317 default:
1318 return claim;
1319 case NFS4_OPEN_CLAIM_FH:
1320 return NFS4_OPEN_CLAIM_NULL;
1321 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1322 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1323 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1324 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1325 }
1326}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001329{
1330 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001331 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001332 p->o_res.seqid = p->o_arg.seqid;
1333 p->c_res.seqid = p->c_arg.seqid;
1334 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001335 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001336 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001337 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001338}
1339
Al Viro82a2c1b2011-06-22 18:30:55 -04001340static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001341 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001342 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001343 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001344 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001345{
Al Viro82a2c1b2011-06-22 18:30:55 -04001346 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001347 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001348 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001349 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001350 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001351 struct nfs4_opendata *p;
1352
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001353 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001354 if (p == NULL)
1355 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001356
1357 p->f_label = nfs4_label_alloc(server, gfp_mask);
1358 if (IS_ERR(p->f_label))
1359 goto err_free_p;
1360
Kinglong Meea49c2692015-07-27 15:31:38 +08001361 p->a_label = nfs4_label_alloc(server, gfp_mask);
1362 if (IS_ERR(p->a_label))
1363 goto err_free_f;
1364
Trond Myklebust63f5f792015-01-23 19:19:25 -05001365 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1366 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001367 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001368 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001369 nfs_sb_active(dentry->d_sb);
1370 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001371 p->dir = parent;
1372 p->owner = sp;
1373 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001374 p->o_arg.open_flags = flags;
1375 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001376 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001377 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1378 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001379 if (flags & O_CREAT) {
1380 p->o_arg.umask = current_umask();
1381 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1382 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1383 p->o_arg.u.attrs = &p->attrs;
1384 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1385
1386 memcpy(p->o_arg.u.verifier.data, c->verf,
1387 sizeof(p->o_arg.u.verifier.data));
1388 }
1389 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001390 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1391 * will return permission denied for all bits until close */
1392 if (!(flags & O_EXCL)) {
1393 /* ask server to check for all possible rights as results
1394 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001395 switch (p->o_arg.claim) {
1396 default:
1397 break;
1398 case NFS4_OPEN_CLAIM_NULL:
1399 case NFS4_OPEN_CLAIM_FH:
1400 p->o_arg.access = NFS4_ACCESS_READ |
1401 NFS4_ACCESS_MODIFY |
1402 NFS4_ACCESS_EXTEND |
1403 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001404#ifdef CONFIG_NFS_V4_2
1405 if (server->caps & NFS_CAP_XATTR)
1406 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1407 NFS4_ACCESS_XAWRITE |
1408 NFS4_ACCESS_XALIST;
1409#endif
Trond Myklebust536585c2016-11-10 15:40:34 -05001410 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001411 }
David Howells7539bba2006-08-22 20:06:09 -04001412 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001413 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1414 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001415 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001416 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001417 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001418 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001419 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001420 case NFS4_OPEN_CLAIM_NULL:
1421 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1422 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1423 p->o_arg.fh = NFS_FH(dir);
1424 break;
1425 case NFS4_OPEN_CLAIM_PREVIOUS:
1426 case NFS4_OPEN_CLAIM_FH:
1427 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1428 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001429 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001430 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001431 p->c_arg.fh = &p->o_res.fh;
1432 p->c_arg.stateid = &p->o_res.stateid;
1433 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001434 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001435 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001436 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001437
1438err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001439 nfs4_label_free(p->a_label);
1440err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001441 nfs4_label_free(p->f_label);
1442err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001443 kfree(p);
1444err:
1445 dput(parent);
1446 return NULL;
1447}
1448
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001449static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001450{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001451 struct nfs4_opendata *p = container_of(kref,
1452 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001453 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001454
Fred Isaman30ae2412016-10-18 13:39:51 -04001455 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001456 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001457 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001458 if (p->state != NULL)
1459 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001460 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001461
Kinglong Meea49c2692015-07-27 15:31:38 +08001462 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001463 nfs4_label_free(p->f_label);
1464
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001465 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001466 dput(p->dentry);
1467 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001468 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001469 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001470 kfree(p);
1471}
1472
1473static void nfs4_opendata_put(struct nfs4_opendata *p)
1474{
1475 if (p != NULL)
1476 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001477}
1478
Trond Myklebust24311f82015-09-20 10:50:17 -04001479static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1480 fmode_t fmode)
1481{
1482 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1483 case FMODE_READ|FMODE_WRITE:
1484 return state->n_rdwr != 0;
1485 case FMODE_WRITE:
1486 return state->n_wronly != 0;
1487 case FMODE_READ:
1488 return state->n_rdonly != 0;
1489 }
1490 WARN_ON_ONCE(1);
1491 return false;
1492}
1493
Trond Myklebustbe189f72018-09-27 17:12:33 -04001494static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1495 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001496{
1497 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001498
Trond Myklebust536e43d2012-01-17 22:04:26 -05001499 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001500 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001501 switch (claim) {
1502 case NFS4_OPEN_CLAIM_NULL:
1503 case NFS4_OPEN_CLAIM_FH:
1504 goto out;
1505 default:
1506 break;
1507 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001508 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001509 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001510 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1511 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001512 break;
1513 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001514 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1515 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001516 break;
1517 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001518 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1519 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001520 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001521out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001522 return ret;
1523}
1524
Trond Myklebust2a606182015-08-19 22:30:00 -05001525static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1526 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001527{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001528 if (delegation == NULL)
1529 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001530 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001531 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001532 switch (claim) {
1533 case NFS4_OPEN_CLAIM_NULL:
1534 case NFS4_OPEN_CLAIM_FH:
1535 break;
1536 case NFS4_OPEN_CLAIM_PREVIOUS:
1537 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1538 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001539 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001540 default:
1541 return 0;
1542 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001543 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001544 return 1;
1545}
1546
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001547static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001548{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001549 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001550 case FMODE_WRITE:
1551 state->n_wronly++;
1552 break;
1553 case FMODE_READ:
1554 state->n_rdonly++;
1555 break;
1556 case FMODE_READ|FMODE_WRITE:
1557 state->n_rdwr++;
1558 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001559 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001560}
1561
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001562#ifdef CONFIG_NFS_V4_1
1563static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1564{
1565 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1566 return true;
1567 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1568 return true;
1569 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1570 return true;
1571 return false;
1572}
1573#endif /* CONFIG_NFS_V4_1 */
1574
Trond Myklebustc9399f22017-11-06 15:28:01 -05001575static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1576{
1577 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1578 wake_up_all(&state->waitq);
1579}
1580
Trond Myklebust4f14c192014-02-12 19:15:06 -05001581static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001582{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001583 struct nfs_client *clp = state->owner->so_server->nfs_client;
1584 bool need_recover = false;
1585
1586 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1587 need_recover = true;
1588 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1589 need_recover = true;
1590 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1591 need_recover = true;
1592 if (need_recover)
1593 nfs4_state_mark_reclaim_nograce(clp, state);
1594}
1595
Trond Myklebustc9399f22017-11-06 15:28:01 -05001596/*
1597 * Check for whether or not the caller may update the open stateid
1598 * to the value passed in by stateid.
1599 *
1600 * Note: This function relies heavily on the server implementing
1601 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1602 * correctly.
1603 * i.e. The stateid seqids have to be initialised to 1, and
1604 * are then incremented on every state transition.
1605 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001606static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001607 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001608{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001609 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1610 /* The common case - we're updating to a new sequence number */
1611 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1612 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1613 return true;
1614 }
1615 } else {
1616 /* This is the first OPEN in this generation */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001617 if (stateid->seqid == cpu_to_be32(1))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001618 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001619 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001620 return false;
1621}
1622
Trond Myklebustf95549c2015-01-23 18:06:09 -05001623static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1624{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001625 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1626 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001627 if (state->n_wronly)
1628 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1629 if (state->n_rdonly)
1630 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1631 if (state->n_rdwr)
1632 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001633 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001634}
1635
Trond Myklebust226056c2014-02-11 10:41:07 -05001636static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1637 nfs4_stateid *stateid, fmode_t fmode)
1638{
1639 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1640 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1641 case FMODE_WRITE:
1642 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1643 break;
1644 case FMODE_READ:
1645 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1646 break;
1647 case 0:
1648 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1649 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1650 clear_bit(NFS_OPEN_STATE, &state->flags);
1651 }
1652 if (stateid == NULL)
1653 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001654 /* Handle OPEN+OPEN_DOWNGRADE races */
1655 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1656 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001657 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001658 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001659 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001660 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001661 nfs4_stateid_copy(&state->stateid, stateid);
1662 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001663 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001664out:
1665 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001666}
1667
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001668static void nfs_clear_open_stateid(struct nfs4_state *state,
1669 nfs4_stateid *arg_stateid,
1670 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001671{
1672 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001673 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1674 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1675 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001676 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001677 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1678 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001679}
1680
Trond Myklebust1393d962016-09-22 13:39:13 -04001681static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001682 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001683 __must_hold(&state->owner->so_lock)
1684 __must_hold(&state->seqlock)
1685 __must_hold(RCU)
1686
Trond Myklebust003707c2007-07-05 18:07:55 -04001687{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001688 DEFINE_WAIT(wait);
1689 int status = 0;
1690 for (;;) {
1691
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001692 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001693 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001694
Trond Myklebustc9399f22017-11-06 15:28:01 -05001695 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001696 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001697 /* Rely on seqids for serialisation with NFSv4.0 */
1698 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1699 break;
1700
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001701 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001702 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1703 /*
1704 * Ensure we process the state changes in the same order
1705 * in which the server processed them by delaying the
1706 * update of the stateid until we are in sequence.
1707 */
1708 write_sequnlock(&state->seqlock);
1709 spin_unlock(&state->owner->so_lock);
1710 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001711 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001712
zhouchuangaobb002382021-05-09 19:34:37 -07001713 if (!fatal_signal_pending(current)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001714 if (schedule_timeout(5*HZ) == 0)
1715 status = -EAGAIN;
1716 else
1717 status = 0;
1718 } else
1719 status = -EINTR;
1720 finish_wait(&state->waitq, &wait);
1721 rcu_read_lock();
1722 spin_lock(&state->owner->so_lock);
1723 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001724 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001725
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001726 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1727 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001728 nfs4_stateid_copy(freeme, &state->open_stateid);
1729 nfs_test_and_clear_all_open_stateid(state);
1730 }
1731
Trond Myklebuste999e802014-02-10 18:20:47 -05001732 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1733 nfs4_stateid_copy(&state->stateid, stateid);
1734 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001735 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001736 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001737}
1738
Trond Myklebustc9399f22017-11-06 15:28:01 -05001739static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001740 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001741 fmode_t fmode,
1742 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001744 /*
1745 * Protect the call to nfs4_state_set_mode_locked and
1746 * serialise the stateid update
1747 */
1748 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001749 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1750 switch (fmode) {
1751 case FMODE_READ:
1752 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1753 break;
1754 case FMODE_WRITE:
1755 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1756 break;
1757 case FMODE_READ|FMODE_WRITE:
1758 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001759 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001760 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001761 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001762}
1763
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001764static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1765{
1766 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1767 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1768 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1769 clear_bit(NFS_OPEN_STATE, &state->flags);
1770}
1771
Trond Myklebustc9399f22017-11-06 15:28:01 -05001772static void nfs_state_set_delegation(struct nfs4_state *state,
1773 const nfs4_stateid *deleg_stateid,
1774 fmode_t fmode)
1775{
1776 /*
1777 * Protect the call to nfs4_state_set_mode_locked and
1778 * serialise the stateid update
1779 */
1780 write_seqlock(&state->seqlock);
1781 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1782 set_bit(NFS_DELEGATED_STATE, &state->flags);
1783 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001786static void nfs_state_clear_delegation(struct nfs4_state *state)
1787{
1788 write_seqlock(&state->seqlock);
1789 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1790 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1791 write_sequnlock(&state->seqlock);
1792}
1793
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001794int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001795 const nfs4_stateid *open_stateid,
1796 const nfs4_stateid *delegation,
1797 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001798{
Trond Myklebust1393d962016-09-22 13:39:13 -04001799 struct nfs_server *server = NFS_SERVER(state->inode);
1800 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001801 struct nfs_inode *nfsi = NFS_I(state->inode);
1802 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001803 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001804 int ret = 0;
1805
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001806 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001807
1808 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001809 spin_lock(&state->owner->so_lock);
1810 if (open_stateid != NULL) {
1811 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1812 ret = 1;
1813 }
1814
Trond Myklebust333ac782019-10-22 12:12:17 -04001815 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001816 if (deleg_cur == NULL)
1817 goto no_delegation;
1818
1819 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001820 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001821 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001822 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001823 goto no_delegation_unlock;
1824
1825 if (delegation == NULL)
1826 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001827 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001828 goto no_delegation_unlock;
1829
Trond Myklebustb7391f42008-12-23 15:21:52 -05001830 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001831 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001832 ret = 1;
1833no_delegation_unlock:
1834 spin_unlock(&deleg_cur->lock);
1835no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001836 if (ret)
1837 update_open_stateflags(state, fmode);
1838 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001839 rcu_read_unlock();
1840
Trond Myklebust4f14c192014-02-12 19:15:06 -05001841 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001842 nfs4_schedule_state_manager(clp);
1843 if (freeme.type != 0)
1844 nfs4_test_and_free_stateid(server, &freeme,
1845 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001846
1847 return ret;
1848}
1849
Trond Myklebust39071e62015-01-24 15:07:56 -05001850static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1851 const nfs4_stateid *stateid)
1852{
1853 struct nfs4_state *state = lsp->ls_state;
1854 bool ret = false;
1855
1856 spin_lock(&state->state_lock);
1857 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1858 goto out_noupdate;
1859 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1860 goto out_noupdate;
1861 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1862 ret = true;
1863out_noupdate:
1864 spin_unlock(&state->state_lock);
1865 return ret;
1866}
Trond Myklebust34310432008-12-23 15:21:38 -05001867
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001868static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001869{
1870 struct nfs_delegation *delegation;
1871
Trond Myklebustf5086242018-03-20 16:43:13 -04001872 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001873 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001874 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001875 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001876 rcu_read_unlock();
1877 return;
1878 }
1879 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001880 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001881}
1882
Trond Myklebust6ee41262007-07-08 14:11:36 -04001883static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001884{
1885 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001886 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001887 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001888 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001889 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001890 nfs4_stateid stateid;
1891 int ret = -EAGAIN;
1892
Trond Myklebustaac00a82007-07-05 19:02:21 -04001893 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001894 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001895 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001896 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001897 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001898 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001899 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001900 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001901 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001902 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001903 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001904 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001905 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001906 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001907 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001908 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001909 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001910 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001911 if (!opendata->is_recover) {
1912 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1913 if (ret != 0)
1914 goto out;
1915 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001916 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001917
1918 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001919 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001920 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001921 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001922out:
1923 return ERR_PTR(ret);
1924out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001925 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001926 return state;
1927}
1928
Andy Adamsone23008e2012-10-02 21:07:32 -04001929static void
1930nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1931{
1932 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1933 struct nfs_delegation *delegation;
1934 int delegation_flags = 0;
1935
1936 rcu_read_lock();
1937 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1938 if (delegation)
1939 delegation_flags = delegation->flags;
1940 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001941 switch (data->o_arg.claim) {
1942 default:
1943 break;
1944 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1945 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001946 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1947 "returning a delegation for "
1948 "OPEN(CLAIM_DELEGATE_CUR)\n",
1949 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001950 return;
1951 }
1952 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001953 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001954 data->owner->so_cred,
1955 data->o_res.delegation_type,
1956 &data->o_res.delegation,
1957 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001958 else
1959 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001960 data->owner->so_cred,
1961 data->o_res.delegation_type,
1962 &data->o_res.delegation,
1963 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001964
1965 if (data->o_res.do_recall)
1966 nfs_async_inode_return_delegation(state->inode,
1967 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001968}
1969
1970/*
1971 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1972 * and update the nfs4_state.
1973 */
1974static struct nfs4_state *
1975_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1976{
1977 struct inode *inode = data->state->inode;
1978 struct nfs4_state *state = data->state;
1979 int ret;
1980
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001981 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001982 if (data->rpc_status)
1983 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001984 /* cached opens have already been processed */
1985 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001986 }
1987
Andy Adamsone23008e2012-10-02 21:07:32 -04001988 ret = nfs_refresh_inode(inode, &data->f_attr);
1989 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001990 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001991
1992 if (data->o_res.delegation_type != 0)
1993 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001994update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001995 if (!update_open_stateid(state, &data->o_res.stateid,
1996 NULL, data->o_arg.fmode))
1997 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001998 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001999
2000 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04002001}
2002
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04002003static struct inode *
2004nfs4_opendata_get_inode(struct nfs4_opendata *data)
2005{
2006 struct inode *inode;
2007
2008 switch (data->o_arg.claim) {
2009 case NFS4_OPEN_CLAIM_NULL:
2010 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2011 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2012 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2013 return ERR_PTR(-EAGAIN);
2014 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2015 &data->f_attr, data->f_label);
2016 break;
2017 default:
2018 inode = d_inode(data->dentry);
2019 ihold(inode);
2020 nfs_refresh_inode(inode, &data->f_attr);
2021 }
2022 return inode;
2023}
2024
Andy Adamsone23008e2012-10-02 21:07:32 -04002025static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04002026nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2027{
2028 struct nfs4_state *state;
2029 struct inode *inode;
2030
2031 inode = nfs4_opendata_get_inode(data);
2032 if (IS_ERR(inode))
2033 return ERR_CAST(inode);
2034 if (data->state != NULL && data->state->inode == inode) {
2035 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002036 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002037 } else
2038 state = nfs4_get_open_state(inode, data->owner);
2039 iput(inode);
2040 if (state == NULL)
2041 state = ERR_PTR(-ENOMEM);
2042 return state;
2043}
2044
Andy Adamsone23008e2012-10-02 21:07:32 -04002045static struct nfs4_state *
2046_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002047{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002048 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002049
Trond Myklebustaac00a82007-07-05 19:02:21 -04002050 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002051 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002052 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002053 goto out;
2054 }
2055
Trond Myklebust75e8c482017-08-08 10:38:07 -04002056 state = nfs4_opendata_find_nfs4_state(data);
2057 if (IS_ERR(state))
2058 goto out;
2059
Andy Adamsone23008e2012-10-02 21:07:32 -04002060 if (data->o_res.delegation_type != 0)
2061 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002062 if (!update_open_stateid(state, &data->o_res.stateid,
2063 NULL, data->o_arg.fmode)) {
2064 nfs4_put_open_state(state);
2065 state = ERR_PTR(-EAGAIN);
2066 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002067out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002068 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002069 return state;
2070}
2071
Andy Adamsone23008e2012-10-02 21:07:32 -04002072static struct nfs4_state *
2073nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2074{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002075 struct nfs4_state *ret;
2076
Andy Adamsone23008e2012-10-02 21:07:32 -04002077 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002078 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2079 else
2080 ret = _nfs4_opendata_to_nfs4_state(data);
2081 nfs4_sequence_free_slot(&data->o_res.seq_res);
2082 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002083}
2084
Trond Myklebust0de43972018-09-02 15:57:01 -04002085static struct nfs_open_context *
2086nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002087{
2088 struct nfs_inode *nfsi = NFS_I(state->inode);
2089 struct nfs_open_context *ctx;
2090
Trond Myklebust0de43972018-09-02 15:57:01 -04002091 rcu_read_lock();
2092 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002093 if (ctx->state != state)
2094 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002095 if ((ctx->mode & mode) != mode)
2096 continue;
2097 if (!get_nfs_open_context(ctx))
2098 continue;
2099 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002100 return ctx;
2101 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002102 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002103 return ERR_PTR(-ENOENT);
2104}
2105
Trond Myklebust0de43972018-09-02 15:57:01 -04002106static struct nfs_open_context *
2107nfs4_state_find_open_context(struct nfs4_state *state)
2108{
2109 struct nfs_open_context *ctx;
2110
2111 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2112 if (!IS_ERR(ctx))
2113 return ctx;
2114 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2115 if (!IS_ERR(ctx))
2116 return ctx;
2117 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2118}
2119
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002120static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2121 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002122{
2123 struct nfs4_opendata *opendata;
2124
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002125 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002126 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002127 if (opendata == NULL)
2128 return ERR_PTR(-ENOMEM);
2129 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002130 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002131 return opendata;
2132}
2133
Trond Myklebust24311f82015-09-20 10:50:17 -04002134static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2135 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002136{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002137 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002138 int ret;
2139
Trond Myklebust24311f82015-09-20 10:50:17 -04002140 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002141 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002142 opendata->o_arg.open_flags = 0;
2143 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002144 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2145 NFS_SB(opendata->dentry->d_sb),
2146 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002147 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2148 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2149 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002150 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002151 if (ret != 0)
2152 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002153 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002154 if (IS_ERR(newstate))
2155 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002156 if (newstate != opendata->state)
2157 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002158 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002159 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002160}
2161
2162static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2163{
Trond Myklebust864472e2006-01-03 09:55:15 +01002164 int ret;
2165
2166 /* memory barrier prior to reading state->n_* */
2167 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002168 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2169 if (ret != 0)
2170 return ret;
2171 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2172 if (ret != 0)
2173 return ret;
2174 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2175 if (ret != 0)
2176 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002177 /*
2178 * We may have performed cached opens for all three recoveries.
2179 * Check if we need to update the current stateid.
2180 */
2181 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002182 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002183 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002184 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002185 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002186 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002187 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002188 return 0;
2189}
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191/*
2192 * OPEN_RECLAIM:
2193 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002195static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002197 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002198 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002199 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 int status;
2201
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002202 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2203 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002204 if (IS_ERR(opendata))
2205 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002206 rcu_read_lock();
2207 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002208 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002209 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002210 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002211 opendata->o_arg.u.delegation_type = delegation_type;
2212 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002213 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 return status;
2215}
2216
Trond Myklebust539cd032007-06-05 11:46:42 -04002217static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 struct nfs_server *server = NFS_SERVER(state->inode);
2220 struct nfs4_exception exception = { };
2221 int err;
2222 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002223 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002224 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002225 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2226 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002227 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002228 break;
2229 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 } while (exception.retry);
2231 return err;
2232}
2233
Trond Myklebust864472e2006-01-03 09:55:15 +01002234static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2235{
2236 struct nfs_open_context *ctx;
2237 int ret;
2238
2239 ctx = nfs4_state_find_open_context(state);
2240 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002241 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002242 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2243 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002244 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002245 put_nfs_open_context(ctx);
2246 return ret;
2247}
2248
NeilBrowndce26302017-12-13 09:57:09 +11002249static 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 -07002250{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002251 switch (err) {
2252 default:
2253 printk(KERN_ERR "NFS: %s: unhandled error "
2254 "%d.\n", __func__, err);
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06002255 fallthrough;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002256 case 0:
2257 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002258 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002259 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002260 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002261 break;
2262 case -NFS4ERR_BADSESSION:
2263 case -NFS4ERR_BADSLOT:
2264 case -NFS4ERR_BAD_HIGH_SLOT:
2265 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2266 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002267 return -EAGAIN;
2268 case -NFS4ERR_STALE_CLIENTID:
2269 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002270 /* Don't recall a delegation if it was lost */
2271 nfs4_schedule_lease_recovery(server->nfs_client);
2272 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002273 case -NFS4ERR_MOVED:
2274 nfs4_schedule_migration_recovery(server);
2275 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002276 case -NFS4ERR_LEASE_MOVED:
2277 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2278 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002279 case -NFS4ERR_DELEG_REVOKED:
2280 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002281 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002282 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002283 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002284 nfs_inode_find_state_and_recover(state->inode,
2285 stateid);
2286 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002287 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002288 case -NFS4ERR_DELAY:
2289 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002290 ssleep(1);
2291 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002292 case -ENOMEM:
2293 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002294 if (fl) {
2295 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2296 if (lsp)
2297 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2298 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002299 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return err;
2302}
2303
Trond Myklebust24311f82015-09-20 10:50:17 -04002304int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002305 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002306{
2307 struct nfs_server *server = NFS_SERVER(state->inode);
2308 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002309 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002310
2311 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2312 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2313 if (IS_ERR(opendata))
2314 return PTR_ERR(opendata);
2315 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002316 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002317 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2318 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002319 goto out;
2320 }
2321 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002322 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2323 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002324 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002325 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002326 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2327 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2328 if (err)
2329 goto out;
2330 }
2331 nfs_state_clear_delegation(state);
2332out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002333 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002334 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002335}
2336
Chuck Leverbe05c862013-08-09 12:49:47 -04002337static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2338{
2339 struct nfs4_opendata *data = calldata;
2340
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002341 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2342 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002343}
2344
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002345static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2346{
2347 struct nfs4_opendata *data = calldata;
2348
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002349 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002350
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002351 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002352 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002353 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002354 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002355 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002356 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002357 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002358}
2359
2360static void nfs4_open_confirm_release(void *calldata)
2361{
2362 struct nfs4_opendata *data = calldata;
2363 struct nfs4_state *state = NULL;
2364
2365 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002366 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002367 goto out_free;
2368 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002369 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002370 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002371 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002372 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002373 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002374out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002375 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002376}
2377
2378static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002379 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002380 .rpc_call_done = nfs4_open_confirm_done,
2381 .rpc_release = nfs4_open_confirm_release,
2382};
2383
2384/*
2385 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2386 */
2387static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2388{
David Howells2b0143b2015-03-17 22:25:59 +00002389 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002390 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002391 struct rpc_message msg = {
2392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2393 .rpc_argp = &data->c_arg,
2394 .rpc_resp = &data->c_res,
2395 .rpc_cred = data->owner->so_cred,
2396 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002397 struct rpc_task_setup task_setup_data = {
2398 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002399 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002400 .callback_ops = &nfs4_open_confirm_ops,
2401 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002402 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002403 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002404 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 int status;
2406
Anna Schumakerfba83f32018-05-04 16:22:50 -04002407 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2408 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002409 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002410 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002411 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002412 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002413 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002414 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002415 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002416 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002417 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002418 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002419 smp_wmb();
2420 } else
2421 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002422 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return status;
2424}
2425
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002426static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002428 struct nfs4_opendata *data = calldata;
2429 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002430 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002431 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002432
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002433 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002434 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002435 /*
2436 * Check if we still need to send an OPEN call, or if we can use
2437 * a delegation instead.
2438 */
2439 if (data->state != NULL) {
2440 struct nfs_delegation *delegation;
2441
Trond Myklebustbe189f72018-09-27 17:12:33 -04002442 if (can_open_cached(data->state, data->o_arg.fmode,
2443 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002444 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002445 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002446 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002447 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002448 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002449 rcu_read_unlock();
2450 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002451 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002452 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002453 switch (claim) {
2454 default:
2455 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002456 case NFS4_OPEN_CLAIM_PREVIOUS:
2457 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2458 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002459 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002460 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002461 case NFS4_OPEN_CLAIM_FH:
2462 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002463 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002464 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002465 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002466 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002467 &data->o_res.seq_res,
2468 task) != 0)
2469 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002470
2471 /* Set the create mode (note dependency on the session type) */
2472 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2473 if (data->o_arg.open_flags & O_EXCL) {
2474 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2475 if (nfs4_has_persistent_session(clp))
2476 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2477 else if (clp->cl_mvops->minor_version > 0)
2478 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2479 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002480 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002481unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002482 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002483 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002484out_no_action:
2485 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002486out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002487 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002488}
2489
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002490static void nfs4_open_done(struct rpc_task *task, void *calldata)
2491{
2492 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002494 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002495
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002496 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002497 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002498
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002499 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002500 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2501 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002502 case S_IFREG:
2503 break;
2504 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002505 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002506 break;
2507 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002508 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002509 break;
2510 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002511 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002512 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002513 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002514 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002515 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2516 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002517 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002518 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002519}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002520
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002521static void nfs4_open_release(void *calldata)
2522{
2523 struct nfs4_opendata *data = calldata;
2524 struct nfs4_state *state = NULL;
2525
2526 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002527 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002528 goto out_free;
2529 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002530 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002531 goto out_free;
2532 /* In case we need an open_confirm, no cleanup! */
2533 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2534 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002535 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002536 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002537 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002538out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002539 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002540}
2541
2542static const struct rpc_call_ops nfs4_open_ops = {
2543 .rpc_call_prepare = nfs4_open_prepare,
2544 .rpc_call_done = nfs4_open_done,
2545 .rpc_release = nfs4_open_release,
2546};
2547
Fred Isaman3b65a302016-09-19 10:06:49 -04002548static int nfs4_run_open_task(struct nfs4_opendata *data,
2549 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002550{
David Howells2b0143b2015-03-17 22:25:59 +00002551 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002552 struct nfs_server *server = NFS_SERVER(dir);
2553 struct nfs_openargs *o_arg = &data->o_arg;
2554 struct nfs_openres *o_res = &data->o_res;
2555 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002556 struct rpc_message msg = {
2557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2558 .rpc_argp = o_arg,
2559 .rpc_resp = o_res,
2560 .rpc_cred = data->owner->so_cred,
2561 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002562 struct rpc_task_setup task_setup_data = {
2563 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002564 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002565 .callback_ops = &nfs4_open_ops,
2566 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002567 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002568 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002569 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002570 int status;
2571
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002572 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002573 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002574 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002575 data->cancelled = false;
2576 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002577 if (!ctx) {
2578 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002579 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002580 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002581 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002582 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002583 pnfs_lgopen_prepare(data, ctx);
2584 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002585 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002586 if (IS_ERR(task))
2587 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002588 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002589 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002590 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002591 smp_wmb();
2592 } else
2593 status = data->rpc_status;
2594 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002595
2596 return status;
2597}
2598
2599static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2600{
David Howells2b0143b2015-03-17 22:25:59 +00002601 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002602 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002603 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002604
Fred Isaman3b65a302016-09-19 10:06:49 -04002605 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002606 if (status != 0 || !data->rpc_done)
2607 return status;
2608
Trond Myklebust6926afd12012-01-07 13:22:46 -05002609 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2610
Anna Schumakerd7e98252017-01-11 16:13:29 -05002611 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002612 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002613
2614 return status;
2615}
2616
Trond Myklebustf3792d62014-07-10 08:54:32 -04002617/*
2618 * Additional permission checks in order to distinguish between an
2619 * open for read, and an open for execute. This works around the
2620 * fact that NFSv4 OPEN treats read and execute permissions as being
2621 * the same.
2622 * Note that in the non-execute case, we want to turn off permission
2623 * checking if we just created a new file (POSIX open() semantics).
2624 */
NeilBrowna52458b2018-12-03 11:30:31 +11002625static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002626 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002627 struct nfs4_state *state, fmode_t fmode,
2628 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002629{
2630 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002631 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002632
2633 /* access call failed or for some reason the server doesn't
2634 * support any access modes -- defer access call until later */
2635 if (opendata->o_res.access_supported == 0)
2636 return 0;
2637
2638 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002639 /*
2640 * Use openflags to check for exec, because fmode won't
2641 * always have FMODE_EXEC set when file open for exec.
2642 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002643 if (openflags & __FMODE_EXEC) {
2644 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002645 if (S_ISDIR(state->inode->i_mode))
2646 mask = NFS4_ACCESS_LOOKUP;
2647 else
2648 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002649 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002650 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002651
2652 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002653 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2654 nfs_access_add_cache(state->inode, &cache);
2655
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002656 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2657 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002658 return 0;
2659
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002660 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002661}
2662
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002663/*
2664 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2665 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002666static int _nfs4_proc_open(struct nfs4_opendata *data,
2667 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002668{
David Howells2b0143b2015-03-17 22:25:59 +00002669 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002670 struct nfs_server *server = NFS_SERVER(dir);
2671 struct nfs_openargs *o_arg = &data->o_arg;
2672 struct nfs_openres *o_res = &data->o_res;
2673 int status;
2674
Fred Isaman3b65a302016-09-19 10:06:49 -04002675 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002676 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002677 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002678 if (status != 0) {
2679 if (status == -NFS4ERR_BADNAME &&
2680 !(o_arg->open_flags & O_CREAT))
2681 return -ENOENT;
2682 return status;
2683 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002684
Trond Myklebust6926afd12012-01-07 13:22:46 -05002685 nfs_fattr_map_and_free_names(server, &data->f_attr);
2686
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002687 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002688 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002689 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002690 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002691 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002692 if (data->file_created ||
2693 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002694 nfs4_update_changeattr(dir, &o_res->cinfo,
2695 o_res->f_attr->time_start,
2696 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002697 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002698 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2699 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002701 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002703 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002705 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2706 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002707 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2708 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002709 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713/*
2714 * OPEN_EXPIRED:
2715 * reclaim state on the server after a network partition.
2716 * Assumes caller holds the appropriate lock
2717 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002718static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002720 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002721 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002723 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002724 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002725 if (IS_ERR(opendata))
2726 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002727 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002728 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002729 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002730 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002731 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002734static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002735{
Trond Myklebust539cd032007-06-05 11:46:42 -04002736 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002737 struct nfs4_exception exception = { };
2738 int err;
2739
2740 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002741 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002742 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002743 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2744 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002745 switch (err) {
2746 default:
2747 goto out;
2748 case -NFS4ERR_GRACE:
2749 case -NFS4ERR_DELAY:
2750 nfs4_handle_exception(server, err, &exception);
2751 err = 0;
2752 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002753 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002754out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002755 return err;
2756}
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2759{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002761 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Trond Myklebust864472e2006-01-03 09:55:15 +01002763 ctx = nfs4_state_find_open_context(state);
2764 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002765 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002766 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002767 put_nfs_open_context(ctx);
2768 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
Trond Myklebust41020b62016-09-22 13:38:58 -04002771static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2772 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002773{
Trond Myklebust41020b62016-09-22 13:38:58 -04002774 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002775 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002776}
2777
2778static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2779{
2780 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002781 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002782}
2783
2784static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2785{
2786 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2787 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002788 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002789 return nfs4_open_expired(sp, state);
2790}
2791
Trond Myklebust45870d62016-09-22 13:38:59 -04002792static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2793 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002794 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002795{
2796 return -NFS4ERR_BAD_STATEID;
2797}
2798
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002799#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002800static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2801 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002802 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002803{
2804 int status;
2805
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002806 switch (stateid->type) {
2807 default:
2808 break;
2809 case NFS4_INVALID_STATEID_TYPE:
2810 case NFS4_SPECIAL_STATEID_TYPE:
2811 return -NFS4ERR_BAD_STATEID;
2812 case NFS4_REVOKED_STATEID_TYPE:
2813 goto out_free;
2814 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002815
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002816 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002817 switch (status) {
2818 case -NFS4ERR_EXPIRED:
2819 case -NFS4ERR_ADMIN_REVOKED:
2820 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002821 break;
2822 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002823 return status;
2824 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002825out_free:
2826 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002827 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002828 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002829}
2830
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002831static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002832{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002833 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002834 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002835 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002836 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002837 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002838
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002839 /* Get the delegation credential for use by test/free_stateid */
2840 rcu_read_lock();
2841 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002842 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002843 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002844 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002845 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002846 }
2847
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002848 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002849 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002850
Trond Myklebust994b15b2018-09-05 14:07:14 -04002851 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2852 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002853 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002854 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002855 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002856 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002857
NeilBrowna52458b2018-12-03 11:30:31 +11002858 if (delegation->cred)
2859 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002860 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002861 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002862 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002863 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002864 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002865 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002866 else
2867 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002868
Trond Myklebust8c39a392019-07-19 13:48:44 -04002869 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002870 return ret;
2871}
2872
2873static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2874{
2875 nfs4_stateid tmp;
2876
2877 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2878 nfs4_copy_delegation_stateid(state->inode, state->state,
2879 &tmp, NULL) &&
2880 nfs4_stateid_match_other(&state->stateid, &tmp))
2881 nfs_state_set_delegation(state, &tmp, state->state);
2882 else
2883 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002884}
2885
2886/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002887 * nfs41_check_expired_locks - possibly free a lock stateid
2888 *
2889 * @state: NFSv4 state for an inode
2890 *
2891 * Returns NFS_OK if recovery for this stateid is now finished.
2892 * Otherwise a negative NFS4ERR value is returned.
2893 */
2894static int nfs41_check_expired_locks(struct nfs4_state *state)
2895{
2896 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002897 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002898 struct nfs_server *server = NFS_SERVER(state->inode);
2899
2900 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2901 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002902
2903 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002904 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2905 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002906 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002907
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002908 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002909 spin_unlock(&state->state_lock);
2910
2911 nfs4_put_lock_state(prev);
2912 prev = lsp;
2913
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002914 status = nfs41_test_and_free_expired_stateid(server,
2915 &lsp->ls_stateid,
2916 cred);
2917 trace_nfs4_test_lock_stateid(state, lsp, status);
2918 if (status == -NFS4ERR_EXPIRED ||
2919 status == -NFS4ERR_BAD_STATEID) {
2920 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002921 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002922 if (!recover_lost_locks)
2923 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2924 } else if (status != NFS_OK) {
2925 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002926 nfs4_put_lock_state(prev);
2927 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002928 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002929 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002930 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002931 }
2932 spin_unlock(&state->state_lock);
2933 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002934out:
2935 return ret;
2936}
2937
2938/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002939 * nfs41_check_open_stateid - possibly free an open stateid
2940 *
2941 * @state: NFSv4 state for an inode
2942 *
2943 * Returns NFS_OK if recovery for this stateid is now finished.
2944 * Otherwise a negative NFS4ERR value is returned.
2945 */
2946static int nfs41_check_open_stateid(struct nfs4_state *state)
2947{
2948 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002949 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002950 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002951 int status;
2952
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002953 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002954 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002955 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002956 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002957 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002958 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002959 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002960 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002961 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002962 if (nfs_open_stateid_recover_openmode(state))
2963 return -NFS4ERR_OPENMODE;
2964 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002965}
2966
2967static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2968{
Chuck Levereb64cf92012-07-11 16:30:05 -04002969 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002970
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002971 status = nfs41_check_delegation_stateid(state);
2972 if (status != NFS_OK)
2973 return status;
2974 nfs41_delegation_recover_stateid(state);
2975
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002976 status = nfs41_check_expired_locks(state);
2977 if (status != NFS_OK)
2978 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002979 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002980 if (status != NFS_OK)
2981 status = nfs4_open_expired(sp, state);
2982 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002983}
2984#endif
2985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002987 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2988 * fields corresponding to attributes that were used to store the verifier.
2989 * Make sure we clobber those fields in the later setattr call
2990 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002991static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002992 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002993{
Trond Myklebust609339c2018-03-28 16:18:17 -04002994 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2995 __u32 attrset[3];
2996 unsigned ret;
2997 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002998
Trond Myklebust609339c2018-03-28 16:18:17 -04002999 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3000 attrset[i] = opendata->o_res.attrset[i];
3001 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3002 attrset[i] &= ~bitmask[i];
3003 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003004
Trond Myklebust609339c2018-03-28 16:18:17 -04003005 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3006 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003007
Trond Myklebust609339c2018-03-28 16:18:17 -04003008 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3009 if (sattr->ia_valid & ATTR_ATIME_SET)
3010 ret |= ATTR_ATIME_SET;
3011 else
3012 ret |= ATTR_ATIME;
3013 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003014
Trond Myklebust609339c2018-03-28 16:18:17 -04003015 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3016 if (sattr->ia_valid & ATTR_MTIME_SET)
3017 ret |= ATTR_MTIME_SET;
3018 else
3019 ret |= ATTR_MTIME;
3020 }
3021
3022 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003023 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04003024 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003025}
3026
Trond Myklebustc21443c2013-02-07 14:26:21 -05003027static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003028 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003029{
3030 struct nfs4_state_owner *sp = opendata->owner;
3031 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003032 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003033 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003034 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003035 struct inode *dir = d_inode(opendata->dir);
3036 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003037 unsigned int seq;
3038 int ret;
3039
3040 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003041 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003042
Fred Isaman3b65a302016-09-19 10:06:49 -04003043 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003044 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003045 goto out;
3046
Trond Myklebustae55e592018-05-22 11:17:16 -04003047 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003048 ret = PTR_ERR(state);
3049 if (IS_ERR(state))
3050 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003051 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003052 if (server->caps & NFS_CAP_POSIX_LOCK)
3053 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003054 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3055 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003056
Trond Myklebust275bb302013-05-29 13:11:28 -04003057 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003058 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003059 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003060 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003061 alias = d_exact_alias(dentry, state->inode);
3062 if (!alias)
3063 alias = d_splice_alias(igrab(state->inode), dentry);
3064 /* d_splice_alias() can't fail here - it's a non-directory */
3065 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003066 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003067 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003068 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003069 }
3070
3071 switch(opendata->o_arg.claim) {
3072 default:
3073 break;
3074 case NFS4_OPEN_CLAIM_NULL:
3075 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3076 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3077 if (!opendata->rpc_done)
3078 break;
3079 if (opendata->o_res.delegation_type != 0)
3080 dir_verifier = nfs_save_change_attribute(dir);
3081 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003082 }
3083
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003084 /* Parse layoutget results before we check for access */
3085 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3086
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003087 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3088 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003089 if (ret != 0)
3090 goto out;
3091
David Howells2b0143b2015-03-17 22:25:59 +00003092 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003093 nfs_inode_attach_open_context(ctx);
3094 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3095 nfs4_schedule_stateid_recovery(server, state);
3096 }
Fred Isaman2409a972016-10-06 12:11:21 -04003097
Trond Myklebustc21443c2013-02-07 14:26:21 -05003098out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003099 if (!opendata->cancelled)
3100 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003101 return ret;
3102}
3103
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003104/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003105 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 */
Andy Adamson82be4172012-05-23 05:02:35 -04003107static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003108 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003109 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003110 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003111 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
3113 struct nfs4_state_owner *sp;
3114 struct nfs4_state *state = NULL;
3115 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003116 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003117 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003118 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003119 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003120 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003121 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003122 struct iattr *sattr = c->sattr;
3123 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003124 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003125 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003129 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3130 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3132 goto out_err;
3133 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003134 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003135 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003136 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003137 if (d_really_is_positive(dentry))
3138 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003139 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003140 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003141 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003142 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3143 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003144 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003145 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
David Quigley14c43f72013-05-22 12:50:43 -04003147 if (label) {
3148 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3149 if (IS_ERR(olabel)) {
3150 status = PTR_ERR(olabel);
3151 goto err_opendata_put;
3152 }
3153 }
3154
Trond Myklebuste911b812014-03-26 13:24:37 -07003155 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3156 if (!opendata->f_attr.mdsthreshold) {
3157 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3158 if (!opendata->f_attr.mdsthreshold)
3159 goto err_free_label;
3160 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003161 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003162 }
David Howells2b0143b2015-03-17 22:25:59 +00003163 if (d_really_is_positive(dentry))
3164 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003165
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003166 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003167 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003168 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003169 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003170
NeilBrownefcbc042015-07-30 13:00:56 +10003171 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003172 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003173 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003174 /*
3175 * send create attributes which was not set by open
3176 * with an extra setattr.
3177 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003178 if (attrs || label) {
3179 unsigned ia_old = sattr->ia_valid;
3180
3181 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003182 nfs_fattr_init(opendata->o_res.f_attr);
3183 status = nfs4_do_setattr(state->inode, cred,
3184 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003185 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003186 if (status == 0) {
3187 nfs_setattr_update_inode(state->inode, sattr,
3188 opendata->o_res.f_attr);
3189 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3190 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003191 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003192 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003193 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003194 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003195 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003196
Trond Myklebuste911b812014-03-26 13:24:37 -07003197 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003198 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003199 opendata->f_attr.mdsthreshold = NULL;
3200 }
Andy Adamson82be4172012-05-23 05:02:35 -04003201
David Quigley14c43f72013-05-22 12:50:43 -04003202 nfs4_label_free(olabel);
3203
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003204 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003207err_free_label:
3208 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003209err_opendata_put:
3210 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003211err_put_state_owner:
3212 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return status;
3215}
3216
3217
Andy Adamson82be4172012-05-23 05:02:35 -04003218static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003219 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003220 int flags,
3221 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003222 struct nfs4_label *label,
3223 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003225 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003226 struct nfs4_exception exception = {
3227 .interruptible = true,
3228 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003230 struct nfs4_open_createattrs c = {
3231 .label = label,
3232 .sattr = sattr,
3233 .verf = {
3234 [0] = (__u32)jiffies,
3235 [1] = (__u32)current->pid,
3236 },
3237 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 int status;
3239
3240 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003241 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003242 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003243 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (status == 0)
3245 break;
3246 /* NOTE: BAD_SEQID means the server and client disagree about the
3247 * book-keeping w.r.t. state-changing operations
3248 * (OPEN/CLOSE/LOCK/LOCKU...)
3249 * It is actually a sign of a bug on the client or on the server.
3250 *
3251 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003252 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 * have unhashed the old state_owner for us, and that we can
3254 * therefore safely retry using a new one. We should still warn
3255 * the user though...
3256 */
3257 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003258 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003259 " returned a bad sequence-id error!\n",
3260 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 exception.retry = 1;
3262 continue;
3263 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003264 /*
3265 * BAD_STATEID on OPEN means that the server cancelled our
3266 * state before it received the OPEN_CONFIRM.
3267 * Recover by retrying the request as per the discussion
3268 * on Page 181 of RFC3530.
3269 */
3270 if (status == -NFS4ERR_BAD_STATEID) {
3271 exception.retry = 1;
3272 continue;
3273 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003274 if (status == -NFS4ERR_EXPIRED) {
3275 nfs4_schedule_lease_recovery(server->nfs_client);
3276 exception.retry = 1;
3277 continue;
3278 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003279 if (status == -EAGAIN) {
3280 /* We must have found a delegation */
3281 exception.retry = 1;
3282 continue;
3283 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003284 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3285 continue;
3286 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 status, &exception));
3288 } while (exception.retry);
3289 return res;
3290}
3291
Trond Myklebust8487c472016-06-26 08:44:35 -04003292static int _nfs4_do_setattr(struct inode *inode,
3293 struct nfs_setattrargs *arg,
3294 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003295 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003296 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003297{
3298 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003299 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3301 .rpc_argp = arg,
3302 .rpc_resp = res,
3303 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003304 };
NeilBrowna52458b2018-12-03 11:30:31 +11003305 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003306 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003307 bool truncate;
3308 int status;
3309
3310 nfs_fattr_init(res->fattr);
3311
3312 /* Servers should only apply open mode checks for file size changes */
3313 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003314 if (!truncate) {
3315 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003316 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003317 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003318
Trond Myklebust991eedb2018-04-09 11:15:30 -04003319 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003320 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003321 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003322 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003323 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003324 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003325 l_ctx = nfs_get_lock_context(ctx);
3326 if (IS_ERR(l_ctx))
3327 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003328 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3329 &arg->stateid, &delegation_cred);
3330 nfs_put_lock_context(l_ctx);
3331 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003332 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003333 else if (status == -EAGAIN)
3334 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003335 } else {
3336zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003337 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003338 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003339 if (delegation_cred)
3340 msg.rpc_cred = delegation_cred;
3341
3342 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3343
NeilBrowna52458b2018-12-03 11:30:31 +11003344 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003345 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003346 renew_lease(server, timestamp);
3347 trace_nfs4_setattr(inode, &arg->stateid, status);
3348 return status;
3349}
3350
NeilBrowna52458b2018-12-03 11:30:31 +11003351static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003352 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003353 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003354 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003356 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003357 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003358 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003359 struct nfs_setattrargs arg = {
3360 .fh = NFS_FH(inode),
3361 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003363 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003364 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003365 };
3366 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003368 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003370 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003371 struct nfs4_exception exception = {
3372 .state = state,
3373 .inode = inode,
3374 .stateid = &arg.stateid,
3375 };
Trond Myklebusta71029b2021-04-10 00:23:03 -04003376 unsigned long adjust_flags = NFS_INO_INVALID_CHANGE;
Trond Myklebust8487c472016-06-26 08:44:35 -04003377 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Trond Myklebust720869e2021-04-13 09:41:16 -04003379 if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3380 adjust_flags |= NFS_INO_INVALID_MODE;
3381 if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
Trond Myklebusta71029b2021-04-10 00:23:03 -04003382 adjust_flags |= NFS_INO_INVALID_OTHER;
3383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 do {
Trond Myklebusta71029b2021-04-10 00:23:03 -04003385 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, olabel),
3386 inode, adjust_flags);
Trond Myklebust30846df2018-04-07 13:44:28 -04003387
NeilBrown29b59f92016-10-13 15:26:47 +11003388 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003389 switch (err) {
3390 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003391 if (!(sattr->ia_valid & ATTR_SIZE)) {
3392 pr_warn_once("NFSv4: server %s is incorrectly "
3393 "applying open mode checks to "
3394 "a SETATTR that is not "
3395 "changing file size.\n",
3396 server->nfs_client->cl_hostname);
3397 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003398 if (state && !(state->state & FMODE_WRITE)) {
3399 err = -EBADF;
3400 if (sattr->ia_valid & ATTR_OPEN)
3401 err = -EACCES;
3402 goto out;
3403 }
3404 }
3405 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003407out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 return err;
3409}
3410
Peng Tao500d7012015-09-22 11:35:22 +08003411static bool
3412nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3413{
3414 if (inode == NULL || !nfs_have_layout(inode))
3415 return false;
3416
3417 return pnfs_wait_on_layoutreturn(inode, task);
3418}
3419
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003420/*
3421 * Update the seqid of an open stateid
3422 */
3423static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3424 struct nfs4_state *state)
3425{
3426 __be32 seqid_open;
3427 u32 dst_seqid;
3428 int seq;
3429
3430 for (;;) {
3431 if (!nfs4_valid_open_stateid(state))
3432 break;
3433 seq = read_seqbegin(&state->seqlock);
3434 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3435 nfs4_stateid_copy(dst, &state->open_stateid);
3436 if (read_seqretry(&state->seqlock, seq))
3437 continue;
3438 break;
3439 }
3440 seqid_open = state->open_stateid.seqid;
3441 if (read_seqretry(&state->seqlock, seq))
3442 continue;
3443
3444 dst_seqid = be32_to_cpu(dst->seqid);
3445 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3446 dst->seqid = seqid_open;
3447 break;
3448 }
3449}
3450
3451/*
3452 * Update the seqid of an open stateid after receiving
3453 * NFS4ERR_OLD_STATEID
3454 */
3455static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3456 struct nfs4_state *state)
3457{
3458 __be32 seqid_open;
3459 u32 dst_seqid;
3460 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003461 int seq, status = -EAGAIN;
3462 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003463
3464 for (;;) {
3465 ret = false;
3466 if (!nfs4_valid_open_stateid(state))
3467 break;
3468 seq = read_seqbegin(&state->seqlock);
3469 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3470 if (read_seqretry(&state->seqlock, seq))
3471 continue;
3472 break;
3473 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003474
3475 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003476 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003477
3478 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003479
3480 /* Did another OPEN bump the state's seqid? try again: */
3481 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003482 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003483 write_sequnlock(&state->seqlock);
3484 ret = true;
3485 break;
3486 }
3487
3488 /* server says we're behind but we haven't seen the update yet */
3489 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3490 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3491 write_sequnlock(&state->seqlock);
3492 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3493
zhouchuangaobb002382021-05-09 19:34:37 -07003494 if (fatal_signal_pending(current))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003495 status = -EINTR;
3496 else
3497 if (schedule_timeout(5*HZ) != 0)
3498 status = 0;
3499
3500 finish_wait(&state->waitq, &wait);
3501
3502 if (!status)
3503 continue;
3504 if (status == -EINTR)
3505 break;
3506
3507 /* we slept the whole 5 seconds, we must have lost a seqid */
3508 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003509 ret = true;
3510 break;
3511 }
3512
3513 return ret;
3514}
3515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516struct nfs4_closedata {
3517 struct inode *inode;
3518 struct nfs4_state *state;
3519 struct nfs_closeargs arg;
3520 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003521 struct {
3522 struct nfs4_layoutreturn_args arg;
3523 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003524 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003525 u32 roc_barrier;
3526 bool roc;
3527 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003528 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003529 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530};
3531
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003532static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003533{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003534 struct nfs4_closedata *calldata = data;
3535 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003536 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003537
Trond Myklebustcf805162016-11-15 14:56:07 -05003538 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003539 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3540 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003541 nfs4_put_open_state(calldata->state);
3542 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003543 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003544 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003545 kfree(calldata);
3546}
3547
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003548static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003550 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003553 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003554 struct nfs4_exception exception = {
3555 .state = state,
3556 .inode = calldata->inode,
3557 .stateid = &calldata->arg.stateid,
3558 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Chuck Levera3ca5652012-03-01 17:00:40 -05003560 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003561 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3562 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003563 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003564
3565 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05003566 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3567 &calldata->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04003568 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003569
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003570 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 * the state_owner. we keep this around to process errors
3572 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 switch (task->tk_status) {
3574 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003575 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003576 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003577 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003578 case -NFS4ERR_ACCESS:
3579 if (calldata->arg.bitmask != NULL) {
3580 calldata->arg.bitmask = NULL;
3581 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003582 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003583
3584 }
3585 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003586 case -NFS4ERR_OLD_STATEID:
3587 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003588 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003589 state))
3590 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003591 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003592 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003594 case -NFS4ERR_EXPIRED:
3595 nfs4_free_revoked_stateid(server,
3596 &calldata->arg.stateid,
3597 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003598 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003599 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003600 if (calldata->arg.fmode == 0)
3601 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003602 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003604 task->tk_status = nfs4_async_handle_exception(task,
3605 server, task->tk_status, &exception);
3606 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003607 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003609 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3610 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003611out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003612 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003613 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003614 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003615 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003616 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003617out_restart:
3618 task->tk_status = 0;
3619 rpc_restart_call_prepare(task);
3620 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003623static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003625 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003626 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003627 struct inode *inode = calldata->inode;
Trond Myklebust332d1a02021-03-25 18:15:36 -04003628 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003629 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003630 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003631 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003632
Chuck Levera3ca5652012-03-01 17:00:40 -05003633 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003634 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003635 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003636
Trond Myklebust88069f72009-12-08 08:33:16 -05003637 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003638 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003639 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3640 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3641 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003642 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003643 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003644 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003645 if (state->n_rdonly == 0)
3646 call_close |= is_rdonly;
3647 else if (is_rdonly)
3648 calldata->arg.fmode |= FMODE_READ;
3649 if (state->n_wronly == 0)
3650 call_close |= is_wronly;
3651 else if (is_wronly)
3652 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003653 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3654 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003655 } else if (is_rdwr)
3656 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3657
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003658 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3659 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003660 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003661 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003662
3663 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003664 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003665 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003666 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003667
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003668 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003669 nfs_release_seqid(calldata->arg.seqid);
3670 goto out_wait;
3671 }
3672
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003673 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3674 if (lo && !pnfs_layout_is_valid(lo)) {
3675 calldata->arg.lr_args = NULL;
3676 calldata->res.lr_res = NULL;
3677 }
3678
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003679 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003680 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003681
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003682 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003683 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003684 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust332d1a02021-03-25 18:15:36 -04003685 nfs4_bitmask_set(calldata->arg.bitmask_store,
3686 server->cache_consistency_bitmask,
3687 inode, server, NULL);
3688 calldata->arg.bitmask = calldata->arg.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003689 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003690 calldata->arg.bitmask = NULL;
3691 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003692
Trond Myklebust6ae37332015-01-30 14:21:14 -05003693 calldata->arg.share_access =
3694 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3695 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003696
Trond Myklebustd8d84982016-12-19 12:14:44 -05003697 if (calldata->res.fattr == NULL)
3698 calldata->arg.bitmask = NULL;
3699 else if (calldata->arg.bitmask == NULL)
3700 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003701 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003702 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003703 &calldata->arg.seq_args,
3704 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003705 task) != 0)
3706 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003707 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003708 return;
3709out_no_action:
3710 task->tk_action = NULL;
3711out_wait:
3712 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713}
3714
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003715static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003716 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003717 .rpc_call_done = nfs4_close_done,
3718 .rpc_release = nfs4_free_closedata,
3719};
3720
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721/*
3722 * It is possible for data to be read/written from a mem-mapped file
3723 * after the sys_close call (which hits the vfs layer as a flush).
3724 * This means that we can't safely call nfsv4 close on a file until
3725 * the inode is cleared. This in turn means that we are not good
3726 * NFSv4 citizens - we do not indicate to the server to update the file's
3727 * share state even when we are done with one of the three share
3728 * stateid's in the inode.
3729 *
3730 * NOTE: Caller must be holding the sp->so_owner semaphore!
3731 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003732int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003734 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003735 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003737 struct nfs4_state_owner *sp = state->owner;
3738 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003739 struct rpc_message msg = {
3740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3741 .rpc_cred = state->owner->so_cred,
3742 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003743 struct rpc_task_setup task_setup_data = {
3744 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003745 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003746 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003747 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003748 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003749 };
Trond Myklebust95121352005-10-18 14:20:12 -07003750 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003752 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3753 &task_setup_data.rpc_client, &msg);
3754
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003755 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003757 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003758 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003759 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003761 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003762 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3763 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003765 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3766 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003767 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003768 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003769 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003770 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003771 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003772 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003773 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003774 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003775 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003776 calldata->lr.roc = pnfs_roc(state->inode,
3777 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3778 if (calldata->lr.roc) {
3779 calldata->arg.lr_args = &calldata->lr.arg;
3780 calldata->res.lr_res = &calldata->lr.res;
3781 }
Al Viro643168c2011-06-22 18:20:23 -04003782 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003783
Trond Myklebust1174dd12010-12-21 10:52:24 -05003784 msg.rpc_argp = &calldata->arg;
3785 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003786 task_setup_data.callback_data = calldata;
3787 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003788 if (IS_ERR(task))
3789 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003790 status = 0;
3791 if (wait)
3792 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003793 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003794 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003795out_free_calldata:
3796 kfree(calldata);
3797out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003798 nfs4_put_open_state(state);
3799 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003800 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801}
3802
Trond Myklebust2b484292010-09-17 10:56:51 -04003803static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003804nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3805 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003808 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3809
3810 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Trond Myklebust565277f2007-10-15 18:17:53 -04003812 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003813 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003814
3815 nfs4_label_release_security(label);
3816
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003817 if (IS_ERR(state))
3818 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003819 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
Trond Myklebust1185a552009-12-03 15:54:02 -05003822static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003823{
3824 if (ctx->state == NULL)
3825 return;
3826 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003827 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003828 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003829 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
Trond Myklebustb944dba2013-11-04 15:20:20 -05003832#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3833#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003834#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3837{
Kinglong Mee8c612822015-08-26 21:12:58 +08003838 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003839 struct nfs4_server_caps_arg args = {
3840 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003841 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003842 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 struct nfs4_server_caps_res res = {};
3844 struct rpc_message msg = {
3845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003846 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 .rpc_resp = &res,
3848 };
3849 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003850 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
Kinglong Mee8c612822015-08-26 21:12:58 +08003852 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3853 FATTR4_WORD0_FH_EXPIRE_TYPE |
3854 FATTR4_WORD0_LINK_SUPPORT |
3855 FATTR4_WORD0_SYMLINK_SUPPORT |
3856 FATTR4_WORD0_ACLSUPPORT;
3857 if (minorversion)
3858 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3859
Bryan Schumaker7c513052011-03-24 17:12:24 +00003860 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003862 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003863 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003864 case 0:
3865 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3866 res.attr_bitmask[2] = 0;
3867 break;
3868 case 1:
3869 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3870 break;
3871 case 2:
3872 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebustce62b112018-03-05 15:01:18 -05003875 server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3876 NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3877 server->fattr_valid = NFS_ATTR_FATTR_V4;
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003878 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3879 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 server->caps |= NFS_CAP_ACLS;
3881 if (res.has_links != 0)
3882 server->caps |= NFS_CAP_HARDLINKS;
3883 if (res.has_symlinks != 0)
3884 server->caps |= NFS_CAP_SYMLINKS;
Scott Mayhew0b4f1322021-06-02 13:13:11 -04003885#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3886 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3887 server->caps |= NFS_CAP_SECURITY_LABEL;
3888#endif
Trond Myklebustce62b112018-03-05 15:01:18 -05003889 if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3890 server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3891 if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
3892 server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
3893 if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
3894 server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
3895 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
3896 server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
3897 NFS_ATTR_FATTR_OWNER_NAME);
3898 if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
3899 server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
3900 NFS_ATTR_FATTR_GROUP_NAME);
3901 if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
3902 server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
3903 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
3904 server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
3905 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
3906 server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
3907 if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
3908 server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003909 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3910 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003911 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003912
Trond Myklebusta65318b2009-03-11 14:10:28 -04003913 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3914 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3915 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003916 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003917
3918 /* Avoid a regression due to buggy server */
3919 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3920 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003921 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3922 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003923
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003925 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003927
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 return status;
3929}
3930
Trond Myklebust55a97592006-06-09 09:34:19 -04003931int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932{
Trond Myklebust0688e642019-04-07 13:59:09 -04003933 struct nfs4_exception exception = {
3934 .interruptible = true,
3935 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 int err;
3937 do {
3938 err = nfs4_handle_exception(server,
3939 _nfs4_server_capabilities(server, fhandle),
3940 &exception);
3941 } while (exception.retry);
3942 return err;
3943}
3944
3945static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3946 struct nfs_fsinfo *info)
3947{
David Quigleyaa9c2662013-05-22 12:50:44 -04003948 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003950 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 };
3952 struct nfs4_lookup_res res = {
3953 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003954 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 .fh = fhandle,
3956 };
3957 struct rpc_message msg = {
3958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3959 .rpc_argp = &args,
3960 .rpc_resp = &res,
3961 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003962
David Quigleyaa9c2662013-05-22 12:50:44 -04003963 bitmask[0] = nfs4_fattr_bitmap[0];
3964 bitmask[1] = nfs4_fattr_bitmap[1];
3965 /*
3966 * Process the label in the upcoming getfattr
3967 */
3968 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3969
Trond Myklebust0e574af2005-10-27 22:12:38 -04003970 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003971 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972}
3973
3974static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3975 struct nfs_fsinfo *info)
3976{
Trond Myklebust0688e642019-04-07 13:59:09 -04003977 struct nfs4_exception exception = {
3978 .interruptible = true,
3979 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 int err;
3981 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003982 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003983 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003984 switch (err) {
3985 case 0:
3986 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003987 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003988 default:
3989 err = nfs4_handle_exception(server, err, &exception);
3990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003992out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 return err;
3994}
3995
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003996static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3997 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3998{
Trond Myklebustc2190662013-08-26 19:23:04 -04003999 struct rpc_auth_create_args auth_args = {
4000 .pseudoflavor = flavor,
4001 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004002 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004003
Trond Myklebustc2190662013-08-26 19:23:04 -04004004 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05004005 if (IS_ERR(auth))
4006 return -EACCES;
4007 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004008}
4009
Chuck Lever9a744ba2013-03-16 15:56:02 -04004010/*
4011 * Retry pseudoroot lookup with various security flavors. We do this when:
4012 *
4013 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4014 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4015 *
4016 * Returns zero on success, or a negative NFS4ERR value, or a
4017 * negative errno value.
4018 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004019static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04004020 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021{
Chuck Lever9a744ba2013-03-16 15:56:02 -04004022 /* Per 3530bis 15.33.5 */
4023 static const rpc_authflavor_t flav_array[] = {
4024 RPC_AUTH_GSS_KRB5P,
4025 RPC_AUTH_GSS_KRB5I,
4026 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04004027 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04004028 RPC_AUTH_NULL,
4029 };
4030 int status = -EPERM;
4031 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04004033 if (server->auth_info.flavor_len > 0) {
4034 /* try each flavor specified by user */
4035 for (i = 0; i < server->auth_info.flavor_len; i++) {
4036 status = nfs4_lookup_root_sec(server, fhandle, info,
4037 server->auth_info.flavors[i]);
4038 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4039 continue;
4040 break;
4041 }
4042 } else {
4043 /* no flavors specified by user, try default list */
4044 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4045 status = nfs4_lookup_root_sec(server, fhandle, info,
4046 flav_array[i]);
4047 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4048 continue;
4049 break;
4050 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004051 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004052
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004053 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004054 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004055 * to access the mount. It could also mean that we tried to mount
4056 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4057 * existing mount programs don't handle -EACCES very well so it should
4058 * be mapped to -EPERM instead.
4059 */
4060 if (status == -EACCES)
4061 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004062 return status;
4063}
4064
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004065/**
4066 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4067 * @server: initialized nfs_server handle
4068 * @fhandle: we fill in the pseudo-fs root file handle
4069 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004070 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004071 *
4072 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004073 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004074int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004075 struct nfs_fsinfo *info,
4076 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004077{
Andre Przywarac7757072015-04-23 17:17:40 +01004078 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004079
Andre Przywarac7757072015-04-23 17:17:40 +01004080 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004081 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004082
4083 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004084 status = server->nfs_client->cl_mvops->find_root_sec(server,
4085 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004086
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 if (status == 0)
4088 status = nfs4_server_capabilities(server, fhandle);
4089 if (status == 0)
4090 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004091
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004092 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093}
4094
Bryan Schumakerbae36242012-05-10 15:07:31 -04004095static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4096 struct nfs_fsinfo *info)
4097{
4098 int error;
4099 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004100 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004101
4102 error = nfs4_server_capabilities(server, mntfh);
4103 if (error < 0) {
4104 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4105 return error;
4106 }
4107
Trond Myklebusta841b542018-04-07 13:50:59 -04004108 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004109 if (error < 0) {
4110 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004111 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004112 }
4113
4114 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4115 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4116 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4117
Scott Mayhew779df6a2020-03-03 17:58:37 -05004118out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004119 return error;
4120}
4121
Manoj Naik6b97fd32006-06-09 09:34:29 -04004122/*
4123 * Get locations and (maybe) other attributes of a referral.
4124 * Note that we'll actually follow the referral later when
4125 * we detect fsid mismatch in inode revalidation
4126 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004127static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4128 const struct qstr *name, struct nfs_fattr *fattr,
4129 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004130{
4131 int status = -ENOMEM;
4132 struct page *page = NULL;
4133 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004134
4135 page = alloc_page(GFP_KERNEL);
4136 if (page == NULL)
4137 goto out;
4138 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4139 if (locations == NULL)
4140 goto out;
4141
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004142 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004143 if (status != 0)
4144 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004145
4146 /*
4147 * If the fsid didn't change, this is a migration event, not a
4148 * referral. Cause us to drop into the exception handler, which
4149 * will kick off migration recovery.
4150 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004151 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004152 dprintk("%s: server did not return a different fsid for"
4153 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004154 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004155 goto out;
4156 }
Andy Adamson533eb462011-06-13 18:25:56 -04004157 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4158 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004159
Andy Adamson533eb462011-06-13 18:25:56 -04004160 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004161 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004162 memset(fhandle, 0, sizeof(struct nfs_fh));
4163out:
4164 if (page)
4165 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004166 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004167 return status;
4168}
4169
David Quigley1775fd32013-05-22 12:50:42 -04004170static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004171 struct nfs_fattr *fattr, struct nfs4_label *label,
4172 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
Trond Myklebust771734f2018-04-07 13:54:23 -04004174 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 struct nfs4_getattr_arg args = {
4176 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004177 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 };
4179 struct nfs4_getattr_res res = {
4180 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004181 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 .server = server,
4183 };
4184 struct rpc_message msg = {
4185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4186 .rpc_argp = &args,
4187 .rpc_resp = &res,
4188 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004189 unsigned short task_flags = 0;
4190
4191 /* Is this is an attribute revalidation, subject to softreval? */
4192 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4193 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004194
Trond Myklebusta71029b2021-04-10 00:23:03 -04004195 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode, 0);
Trond Myklebust0e574af2005-10-27 22:12:38 -04004196 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004197 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4198 return nfs4_do_call_sync(server->client, server, &msg,
4199 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200}
4201
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004202int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004203 struct nfs_fattr *fattr, struct nfs4_label *label,
4204 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
Trond Myklebust0688e642019-04-07 13:59:09 -04004206 struct nfs4_exception exception = {
4207 .interruptible = true,
4208 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 int err;
4210 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004211 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004212 trace_nfs4_getattr(server, fhandle, fattr, err);
4213 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 &exception);
4215 } while (exception.retry);
4216 return err;
4217}
4218
4219/*
4220 * The file is not closed if it is opened due to the a request to change
4221 * the size of the file. The open call will not be needed once the
4222 * VFS layer lookup-intents are implemented.
4223 *
4224 * Close is called when the inode is destroyed.
4225 * If we haven't opened the file for O_WRONLY, we
4226 * need to in the size_change case to obtain a stateid.
4227 *
4228 * Got race?
4229 * Because OPEN is always done by name in nfsv4, it is
4230 * possible that we opened a different file by the same
4231 * name. We can recognize this race condition, but we
4232 * can't do anything about it besides returning an error.
4233 *
4234 * This will be fixed with VFS changes (lookup-intent).
4235 */
4236static int
4237nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4238 struct iattr *sattr)
4239{
David Howells2b0143b2015-03-17 22:25:59 +00004240 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004241 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004242 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004243 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 int status;
4245
Peng Tao88ac8152014-09-12 11:04:10 +08004246 if (pnfs_ld_layoutret_on_setattr(inode) &&
4247 sattr->ia_valid & ATTR_SIZE &&
4248 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004249 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004250
Trond Myklebust0e574af2005-10-27 22:12:38 -04004251 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Andy Adamson26699402012-05-30 16:12:24 -04004253 /* Deal with open(O_TRUNC) */
4254 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004255 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004256
4257 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004258 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004259 return 0;
4260
Trond Myklebustd5308382005-11-04 15:33:38 -05004261 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004262 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004263
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004264 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004265 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004266 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004267 }
4268
David Quigley14c43f72013-05-22 12:50:43 -04004269 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4270 if (IS_ERR(label))
4271 return PTR_ERR(label);
4272
Trond Myklebust199366f2018-03-20 16:43:18 -04004273 /* Return any delegations if we're going to change ACLs */
4274 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004275 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004276
NeilBrown29b59f92016-10-13 15:26:47 +11004277 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004278 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004279 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004280 nfs_setsecurity(inode, fattr, label);
4281 }
David Quigley14c43f72013-05-22 12:50:43 -04004282 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 return status;
4284}
4285
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004286static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004287 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004288 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004289{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004290 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004291 int status;
4292 struct nfs4_lookup_arg args = {
4293 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004294 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004295 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004296 };
4297 struct nfs4_lookup_res res = {
4298 .server = server,
4299 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004300 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004301 .fh = fhandle,
4302 };
4303 struct rpc_message msg = {
4304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4305 .rpc_argp = &args,
4306 .rpc_resp = &res,
4307 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004308 unsigned short task_flags = 0;
4309
4310 /* Is this is an attribute revalidation, subject to softreval? */
4311 if (nfs_lookup_is_soft_revalidate(dentry))
4312 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004313
David Quigleyaa9c2662013-05-22 12:50:44 -04004314 args.bitmask = nfs4_bitmask(server, label);
4315
David Howells2b3de442006-08-22 20:06:09 -04004316 nfs_fattr_init(fattr);
4317
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004318 dprintk("NFS call lookup %pd2\n", dentry);
4319 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4320 status = nfs4_do_call_sync(clnt, server, &msg,
4321 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 dprintk("NFS reply lookup: %d\n", status);
4323 return status;
4324}
4325
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004326static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004327{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004328 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004329 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004330 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4331 fattr->nlink = 2;
4332}
4333
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004334static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004335 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004336 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004337{
Trond Myklebust0688e642019-04-07 13:59:09 -04004338 struct nfs4_exception exception = {
4339 .interruptible = true,
4340 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004341 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004342 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004343 int err;
4344 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004345 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004346 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004347 switch (err) {
4348 case -NFS4ERR_BADNAME:
4349 err = -ENOENT;
4350 goto out;
4351 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004352 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004353 if (err == -NFS4ERR_MOVED)
4354 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004355 goto out;
4356 case -NFS4ERR_WRONGSEC:
4357 err = -EPERM;
4358 if (client != *clnt)
4359 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004360 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004361 if (IS_ERR(client))
4362 return PTR_ERR(client);
4363
4364 exception.retry = 1;
4365 break;
4366 default:
4367 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4368 }
4369 } while (exception.retry);
4370
4371out:
4372 if (err == 0)
4373 *clnt = client;
4374 else if (client != *clnt)
4375 rpc_shutdown_client(client);
4376
4377 return err;
4378}
4379
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004380static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004381 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4382 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004384 int status;
4385 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004386
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004387 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004388 if (client != NFS_CLIENT(dir)) {
4389 rpc_shutdown_client(client);
4390 nfs_fixup_secinfo_attributes(fattr);
4391 }
4392 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393}
4394
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004395struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004396nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004397 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4398{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004399 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004400 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004401
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004402 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004403 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004404 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004405 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004406}
4407
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004408static int _nfs4_proc_lookupp(struct inode *inode,
4409 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4410 struct nfs4_label *label)
4411{
4412 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4413 struct nfs_server *server = NFS_SERVER(inode);
4414 int status;
4415 struct nfs4_lookupp_arg args = {
4416 .bitmask = server->attr_bitmask,
4417 .fh = NFS_FH(inode),
4418 };
4419 struct nfs4_lookupp_res res = {
4420 .server = server,
4421 .fattr = fattr,
4422 .label = label,
4423 .fh = fhandle,
4424 };
4425 struct rpc_message msg = {
4426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4427 .rpc_argp = &args,
4428 .rpc_resp = &res,
4429 };
Trond Myklebust76998eb2020-10-20 14:30:35 -04004430 unsigned short task_flags = 0;
4431
4432 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4433 task_flags |= RPC_TASK_TIMEOUT;
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004434
4435 args.bitmask = nfs4_bitmask(server, label);
4436
4437 nfs_fattr_init(fattr);
4438
4439 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4440 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
Trond Myklebust76998eb2020-10-20 14:30:35 -04004441 &res.seq_res, task_flags);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004442 dprintk("NFS reply lookupp: %d\n", status);
4443 return status;
4444}
4445
4446static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4447 struct nfs_fattr *fattr, struct nfs4_label *label)
4448{
Trond Myklebust0688e642019-04-07 13:59:09 -04004449 struct nfs4_exception exception = {
4450 .interruptible = true,
4451 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004452 int err;
4453 do {
4454 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4455 trace_nfs4_lookupp(inode, err);
4456 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4457 &exception);
4458 } while (exception.retry);
4459 return err;
4460}
4461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4463{
Trond Myklebust76b32992007-08-10 17:45:11 -04004464 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 struct nfs4_accessargs args = {
4466 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004467 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004469 struct nfs4_accessres res = {
4470 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004471 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 struct rpc_message msg = {
4473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4474 .rpc_argp = &args,
4475 .rpc_resp = &res,
4476 .rpc_cred = entry->cred,
4477 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004478 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
Trond Myklebust7c672652018-06-04 15:00:53 -04004480 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004481 res.fattr = nfs_alloc_fattr();
4482 if (res.fattr == NULL)
4483 return -ENOMEM;
4484 args.bitmask = server->cache_consistency_bitmask;
4485 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004486 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004488 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004489 if (res.fattr)
4490 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004492 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 return status;
4494}
4495
4496static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4497{
Trond Myklebust0688e642019-04-07 13:59:09 -04004498 struct nfs4_exception exception = {
4499 .interruptible = true,
4500 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 int err;
4502 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004503 err = _nfs4_proc_access(inode, entry);
4504 trace_nfs4_access(inode, err);
4505 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 &exception);
4507 } while (exception.retry);
4508 return err;
4509}
4510
4511/*
4512 * TODO: For the time being, we don't try to get any attributes
4513 * along with any of the zero-copy operations READ, READDIR,
4514 * READLINK, WRITE.
4515 *
4516 * In the case of the first three, we want to put the GETATTR
4517 * after the read-type operation -- this is because it is hard
4518 * to predict the length of a GETATTR response in v4, and thus
4519 * align the READ data correctly. This means that the GETATTR
4520 * may end up partially falling into the page cache, and we should
4521 * shift it into the 'tail' of the xdr_buf before processing.
4522 * To do this efficiently, we need to know the total length
4523 * of data received, which doesn't seem to be available outside
4524 * of the RPC layer.
4525 *
4526 * In the case of WRITE, we also want to put the GETATTR after
4527 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004528 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 *
4530 * Both of these changes to the XDR layer would in fact be quite
4531 * minor, but I decided to leave them for a subsequent patch.
4532 */
4533static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4534 unsigned int pgbase, unsigned int pglen)
4535{
4536 struct nfs4_readlink args = {
4537 .fh = NFS_FH(inode),
4538 .pgbase = pgbase,
4539 .pglen = pglen,
4540 .pages = &page,
4541 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004542 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 struct rpc_message msg = {
4544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4545 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004546 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 };
4548
Bryan Schumaker7c513052011-03-24 17:12:24 +00004549 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 -07004550}
4551
4552static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4553 unsigned int pgbase, unsigned int pglen)
4554{
Trond Myklebust0688e642019-04-07 13:59:09 -04004555 struct nfs4_exception exception = {
4556 .interruptible = true,
4557 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 int err;
4559 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004560 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4561 trace_nfs4_readlink(inode, err);
4562 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 &exception);
4564 } while (exception.retry);
4565 return err;
4566}
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004569 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571static int
4572nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004573 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004575 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004576 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004577 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 int status = 0;
4580
NeilBrown532d4de2016-10-13 15:26:47 +11004581 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004582 if (IS_ERR(ctx))
4583 return PTR_ERR(ctx);
4584
David Quigleyaa9c2662013-05-22 12:50:44 -04004585 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4586
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004587 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4588 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004589 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 if (IS_ERR(state)) {
4591 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004595 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004596 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return status;
4598}
4599
Trond Myklebust3c591172018-07-31 15:54:10 -04004600static int
4601_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602{
Trond Myklebust16e42952005-10-27 22:12:44 -04004603 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004604 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004606 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004608 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004609 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004610 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4613 .rpc_argp = &args,
4614 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004616 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004617 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004618
Bryan Schumaker7c513052011-03-24 17:12:24 +00004619 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004620 if (status == 0) {
4621 spin_lock(&dir->i_lock);
Trond Myklebust3c591172018-07-31 15:54:10 -04004622 /* Removing a directory decrements nlink in the parent */
4623 if (ftype == NF4DIR && dir->i_nlink > 2)
4624 nfs4_dec_nlink_locked(dir);
Trond Myklebust82eae5a2021-04-01 14:59:59 -04004625 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4626 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004627 spin_unlock(&dir->i_lock);
4628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 return status;
4630}
4631
Trond Myklebust912678d2018-03-20 16:43:15 -04004632static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4633{
Trond Myklebust0688e642019-04-07 13:59:09 -04004634 struct nfs4_exception exception = {
4635 .interruptible = true,
4636 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004637 struct inode *inode = d_inode(dentry);
4638 int err;
4639
Trond Myklebustc01d3642018-03-20 16:43:20 -04004640 if (inode) {
4641 if (inode->i_nlink == 1)
4642 nfs4_inode_return_delegation(inode);
4643 else
4644 nfs4_inode_make_writeable(inode);
4645 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004646 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004647 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004648 trace_nfs4_remove(dir, &dentry->d_name, err);
4649 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4650 &exception);
4651 } while (exception.retry);
4652 return err;
4653}
4654
4655static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656{
Trond Myklebust0688e642019-04-07 13:59:09 -04004657 struct nfs4_exception exception = {
4658 .interruptible = true,
4659 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004661
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004663 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004664 trace_nfs4_remove(dir, name, err);
4665 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 &exception);
4667 } while (exception.retry);
4668 return err;
4669}
4670
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004671static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4672 struct dentry *dentry,
4673 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004675 struct nfs_removeargs *args = msg->rpc_argp;
4676 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Trond Myklebust977fcc22018-03-20 16:43:17 -04004678 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004680 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004681
4682 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004683
4684 if (inode)
4685 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686}
4687
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004688static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4689{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004690 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004691 &data->args.seq_args,
4692 &data->res.seq_res,
4693 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694}
4695
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004696static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004698 struct nfs_unlinkdata *data = task->tk_calldata;
4699 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004700
Trond Myklebust14516c32010-07-31 14:29:06 -04004701 if (!nfs4_sequence_done(task, &res->seq_res))
4702 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004703 if (nfs4_async_handle_error(task, res->server, NULL,
4704 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004705 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004706 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004707 nfs4_update_changeattr(dir, &res->cinfo,
4708 res->dir_attr->time_start,
4709 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004710 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711}
4712
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004713static void nfs4_proc_rename_setup(struct rpc_message *msg,
4714 struct dentry *old_dentry,
4715 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004716{
Jeff Laytond3d41522010-09-17 17:31:57 -04004717 struct nfs_renameargs *arg = msg->rpc_argp;
4718 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004719 struct inode *old_inode = d_inode(old_dentry);
4720 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004721
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004722 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004723 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004724 if (new_inode)
4725 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004726 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004727 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004728 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004729}
4730
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004731static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4732{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004733 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004734 &data->args.seq_args,
4735 &data->res.seq_res,
4736 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004737}
4738
4739static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4740 struct inode *new_dir)
4741{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004742 struct nfs_renamedata *data = task->tk_calldata;
4743 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004744
4745 if (!nfs4_sequence_done(task, &res->seq_res))
4746 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004747 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004748 return 0;
4749
Trond Myklebustc733c492017-01-11 12:32:26 -05004750 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004751 if (new_dir != old_dir) {
4752 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004753 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004754 res->old_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004755 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004756 NFS_INO_INVALID_DATA);
4757 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004758 res->new_fattr->time_start,
Trond Myklebustfabf2b32021-03-25 13:14:42 -04004759 NFS_INO_INVALID_NLINK |
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004760 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004761 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004762 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004763 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004764 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004765 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004766 return 1;
4767}
4768
Al Virobeffb8f2016-07-20 16:34:42 -04004769static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004771 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004772 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 struct nfs4_link_arg arg = {
4774 .fh = NFS_FH(inode),
4775 .dir_fh = NFS_FH(dir),
4776 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004777 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004779 struct nfs4_link_res res = {
4780 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004781 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004782 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 struct rpc_message msg = {
4784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4785 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004786 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004788 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
Trond Myklebust136f2622010-04-16 16:22:49 -04004790 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004791 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004792 goto out;
4793
David Quigley14c43f72013-05-22 12:50:43 -04004794 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4795 if (IS_ERR(res.label)) {
4796 status = PTR_ERR(res.label);
4797 goto out;
4798 }
4799
Trond Myklebustc01d3642018-03-20 16:43:20 -04004800 nfs4_inode_make_writeable(inode);
Trond Myklebusta71029b2021-04-10 00:23:03 -04004801 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.label), inode,
4802 NFS_INO_INVALID_CHANGE);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004803 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004804 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004805 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4806 NFS_INO_INVALID_DATA);
Trond Myklebust1301e422021-04-01 14:57:56 -04004807 nfs4_inc_nlink(inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004808 status = nfs_post_op_update_inode(inode, res.fattr);
4809 if (!status)
4810 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004811 }
David Quigley14c43f72013-05-22 12:50:43 -04004812
4813
4814 nfs4_label_free(res.label);
4815
Trond Myklebust136f2622010-04-16 16:22:49 -04004816out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004817 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 return status;
4819}
4820
Al Virobeffb8f2016-07-20 16:34:42 -04004821static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822{
Trond Myklebust0688e642019-04-07 13:59:09 -04004823 struct nfs4_exception exception = {
4824 .interruptible = true,
4825 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 int err;
4827 do {
4828 err = nfs4_handle_exception(NFS_SERVER(inode),
4829 _nfs4_proc_link(inode, dir, name),
4830 &exception);
4831 } while (exception.retry);
4832 return err;
4833}
4834
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004835struct nfs4_createdata {
4836 struct rpc_message msg;
4837 struct nfs4_create_arg arg;
4838 struct nfs4_create_res res;
4839 struct nfs_fh fh;
4840 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004841 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004842};
4843
4844static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004845 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004846{
4847 struct nfs4_createdata *data;
4848
4849 data = kzalloc(sizeof(*data), GFP_KERNEL);
4850 if (data != NULL) {
4851 struct nfs_server *server = NFS_SERVER(dir);
4852
David Quigley14c43f72013-05-22 12:50:43 -04004853 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4854 if (IS_ERR(data->label))
4855 goto out_free;
4856
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004857 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4858 data->msg.rpc_argp = &data->arg;
4859 data->msg.rpc_resp = &data->res;
4860 data->arg.dir_fh = NFS_FH(dir);
4861 data->arg.server = server;
4862 data->arg.name = name;
4863 data->arg.attrs = sattr;
4864 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004865 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004866 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004867 data->res.server = server;
4868 data->res.fh = &data->fh;
4869 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004870 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004871 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004872 }
4873 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004874out_free:
4875 kfree(data);
4876 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004877}
4878
4879static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4880{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004881 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004882 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004883 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004884 spin_lock(&dir->i_lock);
Trond Myklebust3c591172018-07-31 15:54:10 -04004885 /* Creating a directory bumps nlink in the parent */
4886 if (data->arg.ftype == NF4DIR)
4887 nfs4_inc_nlink_locked(dir);
Trond Myklebust82eae5a2021-04-01 14:59:59 -04004888 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4889 data->res.fattr->time_start,
4890 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004891 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004892 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004893 }
4894 return status;
4895}
4896
4897static void nfs4_free_createdata(struct nfs4_createdata *data)
4898{
David Quigley14c43f72013-05-22 12:50:43 -04004899 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004900 kfree(data);
4901}
4902
Chuck Lever4f390c12006-08-22 20:06:22 -04004903static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004904 struct page *page, unsigned int len, struct iattr *sattr,
4905 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004907 struct nfs4_createdata *data;
4908 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Chuck Lever94a6d752006-08-22 20:06:23 -04004910 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004911 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004912
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004913 status = -ENOMEM;
4914 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4915 if (data == NULL)
4916 goto out;
4917
4918 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4919 data->arg.u.symlink.pages = &page;
4920 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004921 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004923 status = nfs4_do_create(dir, dentry, data);
4924
4925 nfs4_free_createdata(data);
4926out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 return status;
4928}
4929
Chuck Lever4f390c12006-08-22 20:06:22 -04004930static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004931 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
Trond Myklebust0688e642019-04-07 13:59:09 -04004933 struct nfs4_exception exception = {
4934 .interruptible = true,
4935 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004936 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004938
4939 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4940
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004942 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4943 trace_nfs4_symlink(dir, &dentry->d_name, err);
4944 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 &exception);
4946 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004947
4948 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return err;
4950}
4951
4952static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004953 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004955 struct nfs4_createdata *data;
4956 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004958 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4959 if (data == NULL)
4960 goto out;
4961
David Quigley1775fd32013-05-22 12:50:42 -04004962 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004963 status = nfs4_do_create(dir, dentry, data);
4964
4965 nfs4_free_createdata(data);
4966out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 return status;
4968}
4969
4970static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4971 struct iattr *sattr)
4972{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004973 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004974 struct nfs4_exception exception = {
4975 .interruptible = true,
4976 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004977 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004979
David Quigleyaa9c2662013-05-22 12:50:44 -04004980 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4981
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004982 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4983 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004985 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4986 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4987 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 &exception);
4989 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004990 nfs4_label_release_security(label);
4991
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 return err;
4993}
4994
Trond Myklebust82e22a52020-11-02 17:34:23 -05004995static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4996 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997{
Trond Myklebust82e22a52020-11-02 17:34:23 -05004998 struct inode *dir = d_inode(nr_arg->dentry);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004999 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 struct nfs4_readdir_arg args = {
5001 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -05005002 .pages = nr_arg->pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 .pgbase = 0,
Trond Myklebust82e22a52020-11-02 17:34:23 -05005004 .count = nr_arg->page_len,
5005 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 };
5007 struct nfs4_readdir_res res;
5008 struct rpc_message msg = {
5009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5010 .rpc_argp = &args,
5011 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -05005012 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 };
5014 int status;
5015
Trond Myklebust82e22a52020-11-02 17:34:23 -05005016 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5017 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05005018 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5019 args.bitmask = server->attr_bitmask_nl;
5020 else
5021 args.bitmask = server->attr_bitmask;
5022
Trond Myklebust82e22a52020-11-02 17:34:23 -05005023 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 res.pgbase = args.pgbase;
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05005025 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5026 &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05005027 if (status >= 0) {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005028 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05005029 status += args.pgbase;
5030 }
Trond Myklebustc4812992007-09-28 17:11:45 -04005031
5032 nfs_invalidate_atime(dir);
5033
Harvey Harrison3110ff82008-05-02 13:42:44 -07005034 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 return status;
5036}
5037
Trond Myklebust82e22a52020-11-02 17:34:23 -05005038static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5039 struct nfs_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040{
Trond Myklebust0688e642019-04-07 13:59:09 -04005041 struct nfs4_exception exception = {
5042 .interruptible = true,
5043 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 int err;
5045 do {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005046 err = _nfs4_proc_readdir(arg, res);
5047 trace_nfs4_readdir(d_inode(arg->dentry), err);
5048 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5049 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 } while (exception.retry);
5051 return err;
5052}
5053
5054static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005055 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005057 struct nfs4_createdata *data;
5058 int mode = sattr->ia_mode;
5059 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005061 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5062 if (data == NULL)
5063 goto out;
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005066 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005068 data->arg.ftype = NF4BLK;
5069 data->arg.u.device.specdata1 = MAJOR(rdev);
5070 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 }
5072 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005073 data->arg.ftype = NF4CHR;
5074 data->arg.u.device.specdata1 = MAJOR(rdev);
5075 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005076 } else if (!S_ISSOCK(mode)) {
5077 status = -EINVAL;
5078 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 }
David Quigley1775fd32013-05-22 12:50:42 -04005080
David Quigleyaa9c2662013-05-22 12:50:44 -04005081 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005082 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005083out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005084 nfs4_free_createdata(data);
5085out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 return status;
5087}
5088
5089static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5090 struct iattr *sattr, dev_t rdev)
5091{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005092 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005093 struct nfs4_exception exception = {
5094 .interruptible = true,
5095 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005096 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005098
David Quigleyaa9c2662013-05-22 12:50:44 -04005099 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5100
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005101 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5102 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005104 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5105 trace_nfs4_mknod(dir, &dentry->d_name, err);
5106 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 &exception);
5108 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005109
5110 nfs4_label_release_security(label);
5111
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 return err;
5113}
5114
5115static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5116 struct nfs_fsstat *fsstat)
5117{
5118 struct nfs4_statfs_arg args = {
5119 .fh = fhandle,
5120 .bitmask = server->attr_bitmask,
5121 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005122 struct nfs4_statfs_res res = {
5123 .fsstat = fsstat,
5124 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 struct rpc_message msg = {
5126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5127 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005128 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 };
5130
Trond Myklebust0e574af2005-10-27 22:12:38 -04005131 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005132 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133}
5134
5135static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5136{
Trond Myklebust0688e642019-04-07 13:59:09 -04005137 struct nfs4_exception exception = {
5138 .interruptible = true,
5139 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 int err;
5141 do {
5142 err = nfs4_handle_exception(server,
5143 _nfs4_proc_statfs(server, fhandle, fsstat),
5144 &exception);
5145 } while (exception.retry);
5146 return err;
5147}
5148
5149static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5150 struct nfs_fsinfo *fsinfo)
5151{
5152 struct nfs4_fsinfo_arg args = {
5153 .fh = fhandle,
5154 .bitmask = server->attr_bitmask,
5155 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005156 struct nfs4_fsinfo_res res = {
5157 .fsinfo = fsinfo,
5158 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 struct rpc_message msg = {
5160 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5161 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005162 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 };
5164
Bryan Schumaker7c513052011-03-24 17:12:24 +00005165 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166}
5167
5168static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5169{
Trond Myklebust0688e642019-04-07 13:59:09 -04005170 struct nfs4_exception exception = {
5171 .interruptible = true,
5172 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 int err;
5174
5175 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005176 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005177 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005178 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005179 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005180 break;
5181 }
5182 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 } while (exception.retry);
5184 return err;
5185}
5186
5187static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5188{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005189 int error;
5190
Trond Myklebust0e574af2005-10-27 22:12:38 -04005191 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005192 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005193 if (error == 0) {
5194 /* block layout checks this! */
5195 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005196 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005197 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005198
5199 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200}
5201
5202static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5203 struct nfs_pathconf *pathconf)
5204{
5205 struct nfs4_pathconf_arg args = {
5206 .fh = fhandle,
5207 .bitmask = server->attr_bitmask,
5208 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005209 struct nfs4_pathconf_res res = {
5210 .pathconf = pathconf,
5211 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 struct rpc_message msg = {
5213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5214 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005215 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 };
5217
5218 /* None of the pathconf attributes are mandatory to implement */
5219 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5220 memset(pathconf, 0, sizeof(*pathconf));
5221 return 0;
5222 }
5223
Trond Myklebust0e574af2005-10-27 22:12:38 -04005224 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005225 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226}
5227
5228static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5229 struct nfs_pathconf *pathconf)
5230{
Trond Myklebust0688e642019-04-07 13:59:09 -04005231 struct nfs4_exception exception = {
5232 .interruptible = true,
5233 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 int err;
5235
5236 do {
5237 err = nfs4_handle_exception(server,
5238 _nfs4_proc_pathconf(server, fhandle, pathconf),
5239 &exception);
5240 } while (exception.retry);
5241 return err;
5242}
5243
Trond Myklebust5521abf2013-03-16 20:54:34 -04005244int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005245 const struct nfs_open_context *ctx,
5246 const struct nfs_lock_context *l_ctx,
5247 fmode_t fmode)
5248{
NeilBrown17393472016-10-13 15:26:47 +11005249 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005250}
5251EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5252
Trond Myklebust5521abf2013-03-16 20:54:34 -04005253static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5254 const struct nfs_open_context *ctx,
5255 const struct nfs_lock_context *l_ctx,
5256 fmode_t fmode)
5257{
Ben Dooksd49dd112019-10-16 17:28:21 +01005258 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005259
Trond Myklebuste1253be2014-03-05 08:44:23 -05005260 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005261 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005262 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005263 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005264}
5265
5266static bool nfs4_error_stateid_expired(int err)
5267{
5268 switch (err) {
5269 case -NFS4ERR_DELEG_REVOKED:
5270 case -NFS4ERR_ADMIN_REVOKED:
5271 case -NFS4ERR_BAD_STATEID:
5272 case -NFS4ERR_STALE_STATEID:
5273 case -NFS4ERR_OLD_STATEID:
5274 case -NFS4ERR_OPENMODE:
5275 case -NFS4ERR_EXPIRED:
5276 return true;
5277 }
5278 return false;
5279}
5280
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005281static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005283 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005285 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005286 if (task->tk_status < 0) {
5287 struct nfs4_exception exception = {
5288 .inode = hdr->inode,
5289 .state = hdr->args.context->state,
5290 .stateid = &hdr->args.stateid,
5291 };
5292 task->tk_status = nfs4_async_handle_exception(task,
5293 server, task->tk_status, &exception);
5294 if (exception.retry) {
5295 rpc_restart_call_prepare(task);
5296 return -EAGAIN;
5297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005299
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005301 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303}
5304
Trond Myklebust5521abf2013-03-16 20:54:34 -04005305static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005306 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005307{
5308
5309 if (!nfs4_error_stateid_expired(task->tk_status) ||
5310 nfs4_stateid_is_current(&args->stateid,
5311 args->context,
5312 args->lock_context,
5313 FMODE_READ))
5314 return false;
5315 rpc_restart_call_prepare(task);
5316 return true;
5317}
5318
Anna Schumakerc5675522014-05-28 13:41:22 -04005319static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5320 struct nfs_pgio_header *hdr)
5321{
5322 struct nfs_server *server = NFS_SERVER(hdr->inode);
5323 struct rpc_message *msg = &task->tk_msg;
5324
5325 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5326 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5327 server->caps &= ~NFS_CAP_READ_PLUS;
5328 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5329 rpc_restart_call_prepare(task);
5330 return true;
5331 }
5332 return false;
5333}
5334
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005335static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005336{
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005337 dprintk("--> %s\n", __func__);
5338
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005339 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005340 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005341 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005342 return -EAGAIN;
Anna Schumakerc5675522014-05-28 13:41:22 -04005343 if (nfs4_read_plus_not_supported(task, hdr))
5344 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005345 if (task->tk_status > 0)
5346 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005347 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5348 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005349}
5350
Anna Schumaker21e31402020-12-03 15:18:39 -05005351#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005352static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5353 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005354{
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005355 /* Note: We don't use READ_PLUS with pNFS yet */
5356 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
Anna Schumakerc5675522014-05-28 13:41:22 -04005357 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
Anna Schumakerc5675522014-05-28 13:41:22 -04005358}
5359#else
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005360static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5361 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005362{
Anna Schumakerc5675522014-05-28 13:41:22 -04005363}
5364#endif /* CONFIG_NFS_V4_2 */
5365
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005366static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5367 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005369 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005370 if (!hdr->pgio_done_cb)
5371 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005372 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5373 nfs42_read_plus_support(hdr, msg);
Anna Schumakerfba83f32018-05-04 16:22:50 -04005374 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375}
5376
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005377static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5378 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005379{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005380 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005381 &hdr->args.seq_args,
5382 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005383 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005384 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005385 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5386 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005387 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005388 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005389 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005390 return -EIO;
5391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392}
5393
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005394static int nfs4_write_done_cb(struct rpc_task *task,
5395 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005397 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005398
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005399 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005400 if (task->tk_status < 0) {
5401 struct nfs4_exception exception = {
5402 .inode = hdr->inode,
5403 .state = hdr->args.context->state,
5404 .stateid = &hdr->args.stateid,
5405 };
5406 task->tk_status = nfs4_async_handle_exception(task,
5407 NFS_SERVER(inode), task->tk_status,
5408 &exception);
5409 if (exception.retry) {
5410 rpc_restart_call_prepare(task);
5411 return -EAGAIN;
5412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005414 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005415 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005416 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005417 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419}
5420
Trond Myklebust5521abf2013-03-16 20:54:34 -04005421static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005422 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005423{
5424
5425 if (!nfs4_error_stateid_expired(task->tk_status) ||
5426 nfs4_stateid_is_current(&args->stateid,
5427 args->context,
5428 args->lock_context,
5429 FMODE_WRITE))
5430 return false;
5431 rpc_restart_call_prepare(task);
5432 return true;
5433}
5434
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005435static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005436{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005437 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005438 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005439 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005440 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005441 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5442 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005443}
5444
Trond Myklebust5a37f852012-04-28 14:55:16 -04005445static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005446bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005447{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005448 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005449 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005450 return false;
5451 /* Otherwise, request attributes if and only if we don't hold
5452 * a delegation
5453 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005454 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005455}
Fred Isamana69aef12011-03-03 15:13:47 +00005456
Trond Myklebust332d1a02021-03-25 18:15:36 -04005457static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5458 struct inode *inode, struct nfs_server *server,
5459 struct nfs4_label *label)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005460{
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005461 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
Trond Myklebust332d1a02021-03-25 18:15:36 -04005462 unsigned int i;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005463
Trond Myklebust332d1a02021-03-25 18:15:36 -04005464 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005465
Trond Myklebust45901a22021-02-08 08:49:32 -05005466 if (cache_validity & NFS_INO_INVALID_CHANGE)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005467 bitmask[0] |= FATTR4_WORD0_CHANGE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005468 if (cache_validity & NFS_INO_INVALID_ATIME)
5469 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
Trond Myklebust720869e2021-04-13 09:41:16 -04005470 if (cache_validity & NFS_INO_INVALID_MODE)
5471 bitmask[1] |= FATTR4_WORD1_MODE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005472 if (cache_validity & NFS_INO_INVALID_OTHER)
Trond Myklebust720869e2021-04-13 09:41:16 -04005473 bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
Trond Myklebustfabf2b32021-03-25 13:14:42 -04005474 if (cache_validity & NFS_INO_INVALID_NLINK)
5475 bitmask[1] |= FATTR4_WORD1_NUMLINKS;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005476 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
5477 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust45901a22021-02-08 08:49:32 -05005478 if (cache_validity & NFS_INO_INVALID_CTIME)
5479 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005480 if (cache_validity & NFS_INO_INVALID_MTIME)
5481 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005482 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5483 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
Trond Myklebust332d1a02021-03-25 18:15:36 -04005484
Trond Myklebustcc7f2da2021-04-11 14:31:24 -04005485 if (cache_validity & NFS_INO_INVALID_SIZE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005486 bitmask[0] |= FATTR4_WORD0_SIZE;
5487
5488 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5489 bitmask[i] &= server->attr_bitmask[i];
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005490}
5491
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005492static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005493 struct rpc_message *msg,
5494 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005496 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005497
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005498 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5499 hdr->args.bitmask = NULL;
5500 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005501 } else {
Trond Myklebust332d1a02021-03-25 18:15:36 -04005502 nfs4_bitmask_set(hdr->args.bitmask_store,
5503 server->cache_consistency_bitmask,
5504 hdr->inode, server, NULL);
5505 hdr->args.bitmask = hdr->args.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005506 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005507
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005508 if (!hdr->pgio_done_cb)
5509 hdr->pgio_done_cb = nfs4_write_done_cb;
5510 hdr->res.server = server;
5511 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005513 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005514 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005515 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516}
5517
Fred Isaman0b7c0152012-04-20 14:47:39 -04005518static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5519{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005520 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005521 &data->args.seq_args,
5522 &data->res.seq_res,
5523 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524}
5525
Fred Isaman0b7c0152012-04-20 14:47:39 -04005526static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005529
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005530 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005531 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5532 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005533 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005534 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537}
5538
Fred Isaman0b7c0152012-04-20 14:47:39 -04005539static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005540{
5541 if (!nfs4_sequence_done(task, &data->res.seq_res))
5542 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005543 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005544}
5545
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005546static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5547 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005549 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005550
Fred Isaman0b7c0152012-04-20 14:47:39 -04005551 if (data->commit_done_cb == NULL)
5552 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005553 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005554 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005555 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005556 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557}
5558
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005559static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5560 struct nfs_commitres *res)
5561{
5562 struct inode *dst_inode = file_inode(dst);
5563 struct nfs_server *server = NFS_SERVER(dst_inode);
5564 struct rpc_message msg = {
5565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5566 .rpc_argp = args,
5567 .rpc_resp = res,
5568 };
5569
5570 args->fh = NFS_FH(dst_inode);
5571 return nfs4_call_sync(server->client, server, &msg,
5572 &args->seq_args, &res->seq_res, 1);
5573}
5574
5575int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5576{
5577 struct nfs_commitargs args = {
5578 .offset = offset,
5579 .count = count,
5580 };
5581 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5582 struct nfs4_exception exception = { };
5583 int status;
5584
5585 do {
5586 status = _nfs4_proc_commit(dst, &args, res);
5587 status = nfs4_handle_exception(dst_server, status, &exception);
5588 } while (exception.retry);
5589
5590 return status;
5591}
5592
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005593struct nfs4_renewdata {
5594 struct nfs_client *client;
5595 unsigned long timestamp;
5596};
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598/*
5599 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5600 * standalone procedure for queueing an asynchronous RENEW.
5601 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005602static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005603{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005604 struct nfs4_renewdata *data = calldata;
5605 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005606
Elena Reshetova212bf412017-10-20 12:53:38 +03005607 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005608 nfs4_schedule_state_renewal(clp);
5609 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005610 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005611}
5612
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005613static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005615 struct nfs4_renewdata *data = calldata;
5616 struct nfs_client *clp = data->client;
5617 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005619 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005620 switch (task->tk_status) {
5621 case 0:
5622 break;
5623 case -NFS4ERR_LEASE_MOVED:
5624 nfs4_schedule_lease_moved_recovery(clp);
5625 break;
5626 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005627 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005628 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5629 return;
5630 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005631 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005632 return;
5633 }
5634 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005636 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637}
5638
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005639static const struct rpc_call_ops nfs4_renew_ops = {
5640 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005641 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005642};
5643
NeilBrowna52458b2018-12-03 11:30:31 +11005644static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645{
5646 struct rpc_message msg = {
5647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5648 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005649 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005651 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005653 if (renew_flags == 0)
5654 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005655 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005656 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005657 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005658 if (data == NULL) {
5659 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005660 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005661 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005662 data->client = clp;
5663 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005664 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005665 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666}
5667
NeilBrowna52458b2018-12-03 11:30:31 +11005668static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669{
5670 struct rpc_message msg = {
5671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5672 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005673 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 };
5675 unsigned long now = jiffies;
5676 int status;
5677
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005678 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 if (status < 0)
5680 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005681 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 return 0;
5683}
5684
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005685static inline int nfs4_server_supports_acls(struct nfs_server *server)
5686{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005687 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005688}
5689
Trond Myklebust21f498c2012-08-24 10:59:25 -04005690/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5691 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005692 * the stack.
5693 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005694#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005695
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005696int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005697 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005698{
5699 struct page *newpage, **spages;
5700 int rc = 0;
5701 size_t len;
5702 spages = pages;
5703
5704 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005705 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005706 newpage = alloc_page(GFP_KERNEL);
5707
5708 if (newpage == NULL)
5709 goto unwind;
5710 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005711 buf += len;
5712 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005713 *pages++ = newpage;
5714 rc++;
5715 } while (buflen != 0);
5716
5717 return rc;
5718
5719unwind:
5720 for(; rc > 0; rc--)
5721 __free_page(spages[rc-1]);
5722 return -ENOMEM;
5723}
5724
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005725struct nfs4_cached_acl {
5726 int cached;
5727 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005728 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005729};
5730
5731static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005732{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005733 struct nfs_inode *nfsi = NFS_I(inode);
5734
5735 spin_lock(&inode->i_lock);
5736 kfree(nfsi->nfs4_acl);
5737 nfsi->nfs4_acl = acl;
5738 spin_unlock(&inode->i_lock);
5739}
5740
5741static void nfs4_zap_acl_attr(struct inode *inode)
5742{
5743 nfs4_set_cached_acl(inode, NULL);
5744}
5745
5746static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5747{
5748 struct nfs_inode *nfsi = NFS_I(inode);
5749 struct nfs4_cached_acl *acl;
5750 int ret = -ENOENT;
5751
5752 spin_lock(&inode->i_lock);
5753 acl = nfsi->nfs4_acl;
5754 if (acl == NULL)
5755 goto out;
5756 if (buf == NULL) /* user is just asking for length */
5757 goto out_len;
5758 if (acl->cached == 0)
5759 goto out;
5760 ret = -ERANGE; /* see getxattr(2) man page */
5761 if (acl->len > buflen)
5762 goto out;
5763 memcpy(buf, acl->data, acl->len);
5764out_len:
5765 ret = acl->len;
5766out:
5767 spin_unlock(&inode->i_lock);
5768 return ret;
5769}
5770
Sachin Prabhu5794d212012-04-17 14:36:40 +01005771static 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 +00005772{
5773 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005774 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005775
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005776 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005777 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005778 if (acl == NULL)
5779 goto out;
5780 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005781 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005782 } else {
5783 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5784 if (acl == NULL)
5785 goto out;
5786 acl->cached = 0;
5787 }
5788 acl->len = acl_len;
5789out:
5790 nfs4_set_cached_acl(inode, acl);
5791}
5792
Andy Adamsonbf118a32011-12-07 11:55:27 -05005793/*
5794 * The getxattr API returns the required buffer length when called with a
5795 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5796 * the required buf. On a NULL buf, we send a page of data to the server
5797 * guessing that the ACL request can be serviced by a page. If so, we cache
5798 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5799 * the cache. If not so, we throw away the page, and cache the required
5800 * length. The next getxattr call will then produce another round trip to
5801 * the server, this time with the input buf of the required size.
5802 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005803static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005804{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005805 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005806 struct nfs_getaclargs args = {
5807 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005808 .acl_len = buflen,
5809 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005810 struct nfs_getaclres res = {
5811 .acl_len = buflen,
5812 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005813 struct rpc_message msg = {
5814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5815 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005816 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005817 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005818 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005819 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005820 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005821
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005822 if (buflen == 0)
5823 buflen = server->rsize;
5824
5825 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5826 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5827 if (!pages)
5828 return -ENOMEM;
5829
5830 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005831
Andy Adamsonbf118a32011-12-07 11:55:27 -05005832 for (i = 0; i < npages; i++) {
5833 pages[i] = alloc_page(GFP_KERNEL);
5834 if (!pages[i])
5835 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005836 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005837
5838 /* for decoding across pages */
5839 res.acl_scratch = alloc_page(GFP_KERNEL);
5840 if (!res.acl_scratch)
5841 goto out_free;
5842
Andy Adamsonbf118a32011-12-07 11:55:27 -05005843 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005844
Peng Taode040be2012-01-10 22:42:47 +08005845 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005846 __func__, buf, buflen, npages, args.acl_len);
5847 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5848 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005849 if (ret)
5850 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005851
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005852 /* Handle the case where the passed-in buffer is too short */
5853 if (res.acl_flags & NFS4_ACL_TRUNC) {
5854 /* Did the user only issue a request for the acl length? */
5855 if (buf == NULL)
5856 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005857 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005858 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005859 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005860 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005861 if (buf) {
5862 if (res.acl_len > buflen) {
5863 ret = -ERANGE;
5864 goto out_free;
5865 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005866 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005867 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005868out_ok:
5869 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005870out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005871 for (i = 0; i < npages; i++)
5872 if (pages[i])
5873 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005874 if (res.acl_scratch)
5875 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005876 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005877 return ret;
5878}
5879
Trond Myklebust16b42892006-08-24 12:27:15 -04005880static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5881{
Trond Myklebust0688e642019-04-07 13:59:09 -04005882 struct nfs4_exception exception = {
5883 .interruptible = true,
5884 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005885 ssize_t ret;
5886 do {
5887 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005888 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005889 if (ret >= 0)
5890 break;
5891 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5892 } while (exception.retry);
5893 return ret;
5894}
5895
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005896static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5897{
5898 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005899 int ret;
5900
5901 if (!nfs4_server_supports_acls(server))
5902 return -EOPNOTSUPP;
Trond Myklebust1f3208b2021-03-25 11:04:34 -04005903 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005904 if (ret < 0)
5905 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005906 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5907 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005908 ret = nfs4_read_cached_acl(inode, buf, buflen);
5909 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005910 /* -ENOENT is returned if there is no ACL or if there is an ACL
5911 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005912 return ret;
5913 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005914}
5915
Trond Myklebust16b42892006-08-24 12:27:15 -04005916static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005917{
5918 struct nfs_server *server = NFS_SERVER(inode);
5919 struct page *pages[NFS4ACL_MAXPAGES];
5920 struct nfs_setaclargs arg = {
5921 .fh = NFS_FH(inode),
5922 .acl_pages = pages,
5923 .acl_len = buflen,
5924 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005925 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005926 struct rpc_message msg = {
5927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5928 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005929 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005930 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005931 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005932 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005933
J. Bruce Fields4f8be1f2021-01-28 17:36:38 -05005934 /* You can't remove system.nfs4_acl: */
5935 if (buflen == 0)
5936 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005937 if (!nfs4_server_supports_acls(server))
5938 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005939 if (npages > ARRAY_SIZE(pages))
5940 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005941 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005942 if (i < 0)
5943 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005944 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005945 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005946
5947 /*
5948 * Free each page after tx, so the only ref left is
5949 * held by the network stack
5950 */
5951 for (; i > 0; i--)
5952 put_page(pages[i-1]);
5953
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005954 /*
5955 * Acl update can result in inode attribute update.
5956 * so mark the attribute cache invalid.
5957 */
5958 spin_lock(&inode->i_lock);
Trond Myklebustb6f80a22021-03-08 14:42:55 -05005959 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
5960 NFS_INO_INVALID_CTIME |
5961 NFS_INO_REVAL_FORCED);
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005962 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005963 nfs_access_zap_cache(inode);
5964 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005965 return ret;
5966}
5967
Trond Myklebust16b42892006-08-24 12:27:15 -04005968static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5969{
5970 struct nfs4_exception exception = { };
5971 int err;
5972 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005973 err = __nfs4_proc_set_acl(inode, buf, buflen);
5974 trace_nfs4_set_acl(inode, err);
Dai Ngof8849e22021-05-19 17:15:10 -04005975 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
5976 /*
5977 * no need to retry since the kernel
5978 * isn't involved in encoding the ACEs.
5979 */
5980 err = -EINVAL;
5981 break;
5982 }
Trond Myklebustc1578b72013-08-12 16:58:42 -04005983 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005984 &exception);
5985 } while (exception.retry);
5986 return err;
5987}
5988
David Quigleyaa9c2662013-05-22 12:50:44 -04005989#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5990static int _nfs4_get_security_label(struct inode *inode, void *buf,
5991 size_t buflen)
5992{
5993 struct nfs_server *server = NFS_SERVER(inode);
5994 struct nfs_fattr fattr;
5995 struct nfs4_label label = {0, 0, buflen, buf};
5996
5997 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005998 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005999 .fh = NFS_FH(inode),
6000 .bitmask = bitmask,
6001 };
6002 struct nfs4_getattr_res res = {
6003 .fattr = &fattr,
6004 .label = &label,
6005 .server = server,
6006 };
6007 struct rpc_message msg = {
6008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04006009 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04006010 .rpc_resp = &res,
6011 };
6012 int ret;
6013
6014 nfs_fattr_init(&fattr);
6015
Trond Myklebustfcb63a92013-11-01 12:42:25 -04006016 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04006017 if (ret)
6018 return ret;
6019 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6020 return -ENOENT;
Ondrej Mosnacek53cb2452021-01-15 18:43:56 +01006021 return label.len;
David Quigleyaa9c2662013-05-22 12:50:44 -04006022}
6023
6024static int nfs4_get_security_label(struct inode *inode, void *buf,
6025 size_t buflen)
6026{
Trond Myklebust0688e642019-04-07 13:59:09 -04006027 struct nfs4_exception exception = {
6028 .interruptible = true,
6029 };
David Quigleyaa9c2662013-05-22 12:50:44 -04006030 int err;
6031
6032 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6033 return -EOPNOTSUPP;
6034
6035 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006036 err = _nfs4_get_security_label(inode, buf, buflen);
6037 trace_nfs4_get_security_label(inode, err);
6038 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006039 &exception);
6040 } while (exception.retry);
6041 return err;
6042}
6043
6044static int _nfs4_do_set_security_label(struct inode *inode,
6045 struct nfs4_label *ilabel,
6046 struct nfs_fattr *fattr,
6047 struct nfs4_label *olabel)
6048{
6049
6050 struct iattr sattr = {0};
6051 struct nfs_server *server = NFS_SERVER(inode);
6052 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04006053 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006054 .fh = NFS_FH(inode),
6055 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006056 .server = server,
6057 .bitmask = bitmask,
6058 .label = ilabel,
6059 };
6060 struct nfs_setattrres res = {
6061 .fattr = fattr,
6062 .label = olabel,
6063 .server = server,
6064 };
6065 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6067 .rpc_argp = &arg,
6068 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04006069 };
6070 int status;
6071
Jeff Layton12207f62013-11-01 10:49:32 -04006072 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04006073
Jeff Layton12207f62013-11-01 10:49:32 -04006074 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04006075 if (status)
6076 dprintk("%s failed: %d\n", __func__, status);
6077
6078 return status;
6079}
6080
6081static int nfs4_do_set_security_label(struct inode *inode,
6082 struct nfs4_label *ilabel,
6083 struct nfs_fattr *fattr,
6084 struct nfs4_label *olabel)
6085{
6086 struct nfs4_exception exception = { };
6087 int err;
6088
6089 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006090 err = _nfs4_do_set_security_label(inode, ilabel,
6091 fattr, olabel);
6092 trace_nfs4_set_security_label(inode, err);
6093 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006094 &exception);
6095 } while (exception.retry);
6096 return err;
6097}
6098
6099static int
Al Viro59301222016-05-27 10:19:30 -04006100nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006101{
6102 struct nfs4_label ilabel, *olabel = NULL;
6103 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006104 int status;
6105
6106 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6107 return -EOPNOTSUPP;
6108
6109 nfs_fattr_init(&fattr);
6110
6111 ilabel.pi = 0;
6112 ilabel.lfs = 0;
6113 ilabel.label = (char *)buf;
6114 ilabel.len = buflen;
6115
David Quigleyaa9c2662013-05-22 12:50:44 -04006116 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6117 if (IS_ERR(olabel)) {
6118 status = -PTR_ERR(olabel);
6119 goto out;
6120 }
6121
6122 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6123 if (status == 0)
6124 nfs_setsecurity(inode, &fattr, olabel);
6125
6126 nfs4_label_free(olabel);
6127out:
David Quigleyaa9c2662013-05-22 12:50:44 -04006128 return status;
6129}
6130#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6131
6132
Chuck Leverf0920752012-05-21 22:45:41 -04006133static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6134 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006135{
6136 __be32 verf[2];
6137
Chuck Lever2c820d92012-05-21 22:45:33 -04006138 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6139 /* An impossible timestamp guarantees this value
6140 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006141 verf[0] = cpu_to_be32(U32_MAX);
6142 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006143 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006144 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006145 u64 ns = ktime_to_ns(nn->boot_time);
6146
6147 verf[0] = cpu_to_be32(ns >> 32);
6148 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006149 }
Chuck Levercd937102012-03-02 17:14:31 -05006150 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6151}
6152
Trond Myklebust1aee5512020-10-07 18:24:17 -04006153static size_t
Trond Myklebust39d43d12020-10-07 18:24:18 -04006154nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
Trond Myklebust1aee5512020-10-07 18:24:17 -04006155{
Trond Myklebust39d43d12020-10-07 18:24:18 -04006156 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6157 struct nfs_netns_client *nn_clp = nn->nfs_client;
6158 const char *id;
6159
Trond Myklebust1aee5512020-10-07 18:24:17 -04006160 buf[0] = '\0';
6161
Trond Myklebust39d43d12020-10-07 18:24:18 -04006162 if (nn_clp) {
6163 rcu_read_lock();
6164 id = rcu_dereference(nn_clp->identifier);
6165 if (id)
6166 strscpy(buf, id, buflen);
6167 rcu_read_unlock();
6168 }
6169
6170 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
Trond Myklebust1aee5512020-10-07 18:24:17 -04006171 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6172
6173 return strlen(buf);
6174}
6175
Jeff Laytona3192682015-06-09 19:43:59 -04006176static int
6177nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006178{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006179 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6180 size_t buflen;
Jeff Laytona3192682015-06-09 19:43:59 -04006181 size_t len;
6182 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006183
Trond Myklebustceb3a162015-01-03 15:16:04 -05006184 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006185 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006186
Jeff Laytona3192682015-06-09 19:43:59 -04006187 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006188 len = 14 +
6189 strlen(clp->cl_rpcclient->cl_nodename) +
6190 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006191 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006192 1;
6193 rcu_read_unlock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006194
Trond Myklebust39d43d12020-10-07 18:24:18 -04006195 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006196 if (buflen)
6197 len += buflen + 1;
6198
Jeff Laytona3192682015-06-09 19:43:59 -04006199 if (len > NFS4_OPAQUE_LIMIT + 1)
6200 return -EINVAL;
6201
6202 /*
6203 * Since this string is allocated at mount time, and held until the
6204 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6205 * about a memory-reclaim deadlock.
6206 */
6207 str = kmalloc(len, GFP_KERNEL);
6208 if (!str)
6209 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006210
Chuck Levere984a552012-09-14 17:24:21 -04006211 rcu_read_lock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006212 if (buflen)
Chuck Lever025bb9f2018-06-04 10:53:34 -04006213 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Trond Myklebust1aee5512020-10-07 18:24:17 -04006214 clp->cl_rpcclient->cl_nodename, buf,
Chuck Lever848a4eb2018-06-04 10:53:29 -04006215 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006216 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006217 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006218 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006219 clp->cl_rpcclient->cl_nodename,
6220 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006221 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006222 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006223
Jeff Laytona3192682015-06-09 19:43:59 -04006224 clp->cl_owner_id = str;
6225 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006226}
6227
Jeff Layton873e3852015-06-09 19:44:00 -04006228static int
Jeff Layton873e3852015-06-09 19:44:00 -04006229nfs4_init_uniform_client_string(struct nfs_client *clp)
6230{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006231 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6232 size_t buflen;
Jeff Layton873e3852015-06-09 19:44:00 -04006233 size_t len;
6234 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006235
6236 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006237 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006238
Jeff Layton873e3852015-06-09 19:44:00 -04006239 len = 10 + 10 + 1 + 10 + 1 +
6240 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6241
Trond Myklebust39d43d12020-10-07 18:24:18 -04006242 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006243 if (buflen)
6244 len += buflen + 1;
6245
Jeff Layton873e3852015-06-09 19:44:00 -04006246 if (len > NFS4_OPAQUE_LIMIT + 1)
6247 return -EINVAL;
6248
6249 /*
6250 * Since this string is allocated at mount time, and held until the
6251 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6252 * about a memory-reclaim deadlock.
6253 */
6254 str = kmalloc(len, GFP_KERNEL);
6255 if (!str)
6256 return -ENOMEM;
6257
Trond Myklebust1aee5512020-10-07 18:24:17 -04006258 if (buflen)
6259 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6260 clp->rpc_ops->version, clp->cl_minorversion,
6261 buf, clp->cl_rpcclient->cl_nodename);
6262 else
6263 scnprintf(str, len, "Linux NFSv%u.%u %s",
6264 clp->rpc_ops->version, clp->cl_minorversion,
6265 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006266 clp->cl_owner_id = str;
6267 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006268}
6269
Chuck Lever706cb8d2014-03-12 12:51:47 -04006270/*
6271 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6272 * services. Advertise one based on the address family of the
6273 * clientaddr.
6274 */
6275static unsigned int
6276nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6277{
6278 if (strchr(clp->cl_ipaddr, ':') != NULL)
6279 return scnprintf(buf, len, "tcp6");
6280 else
6281 return scnprintf(buf, len, "tcp");
6282}
6283
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006284static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6285{
6286 struct nfs4_setclientid *sc = calldata;
6287
6288 if (task->tk_status == 0)
6289 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6290}
6291
6292static const struct rpc_call_ops nfs4_setclientid_ops = {
6293 .rpc_call_done = nfs4_setclientid_done,
6294};
6295
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006296/**
6297 * nfs4_proc_setclientid - Negotiate client ID
6298 * @clp: state data structure
6299 * @program: RPC program for NFSv4 callback service
6300 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006301 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006302 * @res: where to place the result
6303 *
6304 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6305 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006306int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006307 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006308 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309{
6310 nfs4_verifier sc_verifier;
6311 struct nfs4_setclientid setclientid = {
6312 .sc_verifier = &sc_verifier,
6313 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006314 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 };
6316 struct rpc_message msg = {
6317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6318 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006319 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006320 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006322 struct rpc_task_setup task_setup_data = {
6323 .rpc_client = clp->cl_rpcclient,
6324 .rpc_message = &msg,
6325 .callback_ops = &nfs4_setclientid_ops,
6326 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006327 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006328 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006329 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006330 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331
Chuck Leverde734832012-07-11 16:30:50 -04006332 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006333 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006334
6335 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6336 status = nfs4_init_uniform_client_string(clp);
6337 else
Jeff Laytona3192682015-06-09 19:43:59 -04006338 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006339
6340 if (status)
6341 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006342
Chuck Leverde734832012-07-11 16:30:50 -04006343 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006344 setclientid.sc_netid_len =
6345 nfs4_init_callback_netid(clp,
6346 setclientid.sc_netid,
6347 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006348 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006349 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 clp->cl_ipaddr, port >> 8, port & 255);
6351
Jeff Layton3a6bb732015-06-09 19:43:57 -04006352 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006353 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006354 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006355
6356 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006357 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006358 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006359 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6360 put_rpccred(setclientid.sc_cred);
6361 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006362
6363 if (status == 0)
6364 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006365out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006366 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006367 dprintk("NFS reply setclientid: %d\n", status);
6368 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369}
6370
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006371/**
6372 * nfs4_proc_setclientid_confirm - Confirm client ID
6373 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006374 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006375 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006376 *
6377 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6378 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006379int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006380 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006381 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 struct rpc_message msg = {
6384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006385 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006386 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 int status;
6389
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006390 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6391 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6392 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006393 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6394 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006395 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006396 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 return status;
6398}
6399
Trond Myklebustfe650402006-01-03 09:55:18 +01006400struct nfs4_delegreturndata {
6401 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006402 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006403 struct nfs_fh fh;
6404 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006405 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006406 struct {
6407 struct nfs4_layoutreturn_args arg;
6408 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006409 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006410 u32 roc_barrier;
6411 bool roc;
6412 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006413 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006414 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006415 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006416};
6417
Trond Myklebustfe650402006-01-03 09:55:18 +01006418static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6419{
6420 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006421 struct nfs4_exception exception = {
6422 .inode = data->inode,
6423 .stateid = &data->stateid,
Trond Myklebustdfe1fe72021-06-01 11:10:05 -04006424 .task_is_privileged = data->args.seq_args.sa_privileged,
Trond Myklebuste0dba012017-11-07 11:02:32 -05006425 };
Andy Adamson938e1012009-04-01 09:22:28 -04006426
Trond Myklebust14516c32010-07-31 14:29:06 -04006427 if (!nfs4_sequence_done(task, &data->res.seq_res))
6428 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006429
Trond Myklebustca8acf82013-08-13 10:36:56 -04006430 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006431
6432 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05006433 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6434 &data->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04006435 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006436
Ricardo Labiaga79708862009-12-07 09:23:21 -05006437 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006438 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006439 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006440 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006441 case -NFS4ERR_ADMIN_REVOKED:
6442 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006443 case -NFS4ERR_EXPIRED:
6444 nfs4_free_revoked_stateid(data->res.server,
6445 data->args.stateid,
6446 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006447 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006448 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006449 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006450 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006451 task->tk_status = 0;
6452 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006453 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006454 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6455 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006456 if (data->args.bitmask) {
6457 data->args.bitmask = NULL;
6458 data->res.fattr = NULL;
6459 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006460 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006461 case -NFS4ERR_ACCESS:
6462 if (data->args.bitmask) {
6463 data->args.bitmask = NULL;
6464 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006465 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006466 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006467 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006468 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006469 task->tk_status = nfs4_async_handle_exception(task,
6470 data->res.server, task->tk_status,
6471 &exception);
6472 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006473 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006474 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006475 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006476 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006477 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006478out_restart:
6479 task->tk_status = 0;
6480 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006481}
6482
6483static void nfs4_delegreturn_release(void *calldata)
6484{
Peng Tao039b7562014-07-03 13:05:02 +08006485 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006486 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006487
Trond Myklebust078000d2021-01-04 13:18:03 -05006488 if (data->lr.roc)
6489 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6490 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006491 if (inode) {
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006492 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006493 nfs_iput_and_deactive(inode);
6494 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006495 kfree(calldata);
6496}
6497
Andy Adamson938e1012009-04-01 09:22:28 -04006498static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6499{
6500 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006501 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006502
6503 d_data = (struct nfs4_delegreturndata *)data;
6504
Trond Myklebust5326de92019-11-13 09:39:36 +01006505 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6506 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006507 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006508 }
Peng Tao500d7012015-09-22 11:35:22 +08006509
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006510 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6511 if (lo && !pnfs_layout_is_valid(lo)) {
6512 d_data->args.lr_args = NULL;
6513 d_data->res.lr_res = NULL;
6514 }
6515
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006516 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006517 &d_data->args.seq_args,
6518 &d_data->res.seq_res,
6519 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006520}
Andy Adamson938e1012009-04-01 09:22:28 -04006521
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006522static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006523 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006524 .rpc_call_done = nfs4_delegreturn_done,
6525 .rpc_release = nfs4_delegreturn_release,
6526};
6527
NeilBrowna52458b2018-12-03 11:30:31 +11006528static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006529{
6530 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006531 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006532 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006533 struct rpc_message msg = {
6534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6535 .rpc_cred = cred,
6536 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006537 struct rpc_task_setup task_setup_data = {
6538 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006539 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006540 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebustf304a802020-05-11 10:42:04 -04006541 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006542 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006543 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006544
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006545 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006546 if (data == NULL)
6547 return -ENOMEM;
Andrew Elble99ade3c2015-12-02 09:39:51 -05006548
6549 nfs4_state_protect(server->nfs_client,
6550 NFS_SP4_MACH_CRED_CLEANUP,
6551 &task_setup_data.rpc_client, &msg);
6552
Trond Myklebustfe650402006-01-03 09:55:18 +01006553 data->args.fhandle = &data->fh;
6554 data->args.stateid = &data->stateid;
Trond Myklebust332d1a02021-03-25 18:15:36 -04006555 nfs4_bitmask_set(data->args.bitmask_store,
6556 server->cache_consistency_bitmask, inode, server,
6557 NULL);
6558 data->args.bitmask = data->args.bitmask_store;
Trond Myklebustfe650402006-01-03 09:55:18 +01006559 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006560 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006561 data->res.fattr = &data->fattr;
6562 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006563 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006564 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006565 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006566 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006567 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006568 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust078000d2021-01-04 13:18:03 -05006569 if (data->inode || issync) {
6570 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6571 cred);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006572 if (data->lr.roc) {
6573 data->args.lr_args = &data->lr.arg;
6574 data->res.lr_res = &data->lr.res;
6575 }
6576 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006577
Trond Myklebustdfe1fe72021-06-01 11:10:05 -04006578 if (!data->inode)
6579 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6580 1);
6581 else
6582 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6583 0);
Trond Myklebustc970aa82007-07-14 15:39:59 -04006584 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006585 msg.rpc_argp = &data->args;
6586 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006587 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006588 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006589 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006590 if (!issync)
6591 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006592 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006593 if (status != 0)
6594 goto out;
6595 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006596out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006597 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006598 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599}
6600
NeilBrowna52458b2018-12-03 11:30:31 +11006601int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602{
6603 struct nfs_server *server = NFS_SERVER(inode);
6604 struct nfs4_exception exception = { };
6605 int err;
6606 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006607 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006608 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 switch (err) {
6610 case -NFS4ERR_STALE_STATEID:
6611 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 case 0:
6613 return 0;
6614 }
6615 err = nfs4_handle_exception(server, err, &exception);
6616 } while (exception.retry);
6617 return err;
6618}
6619
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6621{
6622 struct inode *inode = state->inode;
6623 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006624 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006625 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006627 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006629 struct nfs_lockt_res res = {
6630 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 };
6632 struct rpc_message msg = {
6633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006634 .rpc_argp = &arg,
6635 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 .rpc_cred = state->owner->so_cred,
6637 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 struct nfs4_lock_state *lsp;
6639 int status;
6640
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006641 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006642 status = nfs4_set_lock_state(state, request);
6643 if (status != 0)
6644 goto out;
6645 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006646 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006647 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006648 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006649 switch (status) {
6650 case 0:
6651 request->fl_type = F_UNLCK;
6652 break;
6653 case -NFS4ERR_DENIED:
6654 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006656 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006657 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006658out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 return status;
6660}
6661
6662static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6663{
Trond Myklebust0688e642019-04-07 13:59:09 -04006664 struct nfs4_exception exception = {
6665 .interruptible = true,
6666 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 int err;
6668
6669 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006670 err = _nfs4_proc_getlk(state, cmd, request);
6671 trace_nfs4_get_lock(request, state, cmd, err);
6672 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673 &exception);
6674 } while (exception.retry);
6675 return err;
6676}
6677
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006678/*
6679 * Update the seqid of a lock stateid after receiving
6680 * NFS4ERR_OLD_STATEID
6681 */
6682static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6683 struct nfs4_lock_state *lsp)
6684{
6685 struct nfs4_state *state = lsp->ls_state;
6686 bool ret = false;
6687
6688 spin_lock(&state->state_lock);
6689 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6690 goto out;
6691 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6692 nfs4_stateid_seqid_inc(dst);
6693 else
6694 dst->seqid = lsp->ls_stateid.seqid;
6695 ret = true;
6696out:
6697 spin_unlock(&state->state_lock);
6698 return ret;
6699}
6700
6701static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6702 struct nfs4_lock_state *lsp)
6703{
6704 struct nfs4_state *state = lsp->ls_state;
6705 bool ret;
6706
6707 spin_lock(&state->state_lock);
6708 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6709 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6710 spin_unlock(&state->state_lock);
6711 return ret;
6712}
6713
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006714struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006715 struct nfs_locku_args arg;
6716 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006717 struct nfs4_lock_state *lsp;
6718 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006719 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006720 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006721 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006722 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006723};
6724
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006725static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6726 struct nfs_open_context *ctx,
6727 struct nfs4_lock_state *lsp,
6728 struct nfs_seqid *seqid)
6729{
6730 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006731 struct nfs4_state *state = lsp->ls_state;
6732 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006733
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006734 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006735 if (p == NULL)
6736 return NULL;
6737 p->arg.fh = NFS_FH(inode);
6738 p->arg.fl = &p->fl;
6739 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006740 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006741 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006742 /* Ensure we don't close file until we're done freeing locks! */
6743 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006744 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006745 locks_init_lock(&p->fl);
6746 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006747 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006748 spin_lock(&state->state_lock);
6749 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6750 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006751 return p;
6752}
6753
Trond Myklebust06f814a2006-01-03 09:55:07 +01006754static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006755{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006756 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006757 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006758 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006759 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006760 put_nfs_open_context(calldata->ctx);
6761 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006762}
6763
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006764static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006765{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006766 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006767 struct nfs4_exception exception = {
6768 .inode = calldata->lsp->ls_state->inode,
6769 .stateid = &calldata->arg.stateid,
6770 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006771
Trond Myklebust14516c32010-07-31 14:29:06 -04006772 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6773 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006774 switch (task->tk_status) {
6775 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006776 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006777 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006778 if (nfs4_update_lock_stateid(calldata->lsp,
6779 &calldata->res.stateid))
6780 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006781 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006782 case -NFS4ERR_ADMIN_REVOKED:
6783 case -NFS4ERR_EXPIRED:
6784 nfs4_free_revoked_stateid(calldata->server,
6785 &calldata->arg.stateid,
6786 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006787 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006788 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006789 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006790 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6791 calldata->lsp))
6792 rpc_restart_call_prepare(task);
6793 break;
6794 case -NFS4ERR_OLD_STATEID:
6795 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6796 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006797 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006798 break;
6799 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006800 task->tk_status = nfs4_async_handle_exception(task,
6801 calldata->server, task->tk_status,
6802 &exception);
6803 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006804 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006805 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006806 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006807}
6808
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006809static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006810{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006811 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006813 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6814 nfs_async_iocounter_wait(task, calldata->l_ctx))
6815 return;
6816
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006817 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006818 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006819 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006820 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006821 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006822 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006823 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006824 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006825 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006826 &calldata->res.seq_res,
6827 task) != 0)
6828 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006829 return;
6830out_no_action:
6831 task->tk_action = NULL;
6832out_wait:
6833 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834}
6835
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006836static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006837 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006838 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006839 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006840};
6841
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006842static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6843 struct nfs_open_context *ctx,
6844 struct nfs4_lock_state *lsp,
6845 struct nfs_seqid *seqid)
6846{
6847 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006848 struct rpc_message msg = {
6849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6850 .rpc_cred = ctx->cred,
6851 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006852 struct rpc_task_setup task_setup_data = {
6853 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006854 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006855 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006856 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006857 .flags = RPC_TASK_ASYNC,
6858 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006859
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006860 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6861 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6862
Frank Filz137d6ac2007-07-09 15:32:29 -07006863 /* Ensure this is an unlock - when canceling a lock, the
6864 * canceled lock is passed in, and it won't be an unlock.
6865 */
6866 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006867 if (fl->fl_flags & FL_CLOSE)
6868 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006869
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006870 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6871 if (data == NULL) {
6872 nfs_free_seqid(seqid);
6873 return ERR_PTR(-ENOMEM);
6874 }
6875
Anna Schumakerfba83f32018-05-04 16:22:50 -04006876 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006877 msg.rpc_argp = &data->arg;
6878 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006879 task_setup_data.callback_data = data;
6880 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006881}
6882
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6884{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006885 struct inode *inode = state->inode;
6886 struct nfs4_state_owner *sp = state->owner;
6887 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006888 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006889 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006890 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006891 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006892 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006893 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894
Trond Myklebust9b073572006-06-29 16:38:34 -04006895 status = nfs4_set_lock_state(state, request);
6896 /* Unlock _before_ we do the RPC call */
6897 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006898 /* Exclude nfs_delegation_claim_locks() */
6899 mutex_lock(&sp->so_delegreturn_mutex);
6900 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006901 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006902 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006903 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006904 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006905 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006906 }
6907 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006908 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006909 if (status != 0)
6910 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006911 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006912 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006913 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6914 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006915 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6916 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006917 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006918 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006919 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006920 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006921 status = PTR_ERR(task);
6922 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006923 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006924 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006925 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006926out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006927 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006928 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006929 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930}
6931
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006932struct nfs4_lockdata {
6933 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006934 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006935 struct nfs4_lock_state *lsp;
6936 struct nfs_open_context *ctx;
6937 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006938 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006939 int rpc_status;
6940 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006941 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006942};
6943
6944static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006945 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6946 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006947{
6948 struct nfs4_lockdata *p;
6949 struct inode *inode = lsp->ls_state->inode;
6950 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006951 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006952
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006953 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006954 if (p == NULL)
6955 return NULL;
6956
6957 p->arg.fh = NFS_FH(inode);
6958 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006959 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006960 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006961 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006962 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6963 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006964 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006965 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006966 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006967 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006968 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006969 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006970 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006971 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006972 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006973 locks_init_lock(&p->fl);
6974 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006975 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006976out_free_seqid:
6977 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006978out_free:
6979 kfree(p);
6980 return NULL;
6981}
6982
6983static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6984{
6985 struct nfs4_lockdata *data = calldata;
6986 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987
Harvey Harrison3110ff82008-05-02 13:42:44 -07006988 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006989 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006990 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006991 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006992 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006993 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006994 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006995 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006996 nfs4_stateid_copy(&data->arg.open_stateid,
6997 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006998 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006999 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007000 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007001 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007002 nfs4_stateid_copy(&data->arg.lock_stateid,
7003 &data->lsp->ls_stateid);
7004 }
Trond Myklebust5d422302013-03-14 16:57:48 -04007005 if (!nfs4_valid_open_stateid(state)) {
7006 data->rpc_status = -EBADF;
7007 task->tk_action = NULL;
7008 goto out_release_open_seqid;
7009 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01007010 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05007011 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04007012 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04007013 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007014 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04007015 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04007016out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04007017 nfs_release_seqid(data->arg.open_seqid);
7018out_release_lock_seqid:
7019 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05007020out_wait:
7021 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007022 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08007023}
7024
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007025static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7026{
7027 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05007028 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007029
Harvey Harrison3110ff82008-05-02 13:42:44 -07007030 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007031
Trond Myklebust14516c32010-07-31 14:29:06 -04007032 if (!nfs4_sequence_done(task, &data->res.seq_res))
7033 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04007034
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007035 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007036 switch (task->tk_status) {
7037 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00007038 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05007039 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007040 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05007041 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007042 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007043 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007044 }
Trond Myklebust39071e62015-01-24 15:07:56 -05007045 if (data->arg.new_lock_owner != 0) {
7046 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7047 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7048 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007049 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7050 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007051 break;
7052 case -NFS4ERR_BAD_STATEID:
7053 case -NFS4ERR_OLD_STATEID:
7054 case -NFS4ERR_STALE_STATEID:
7055 case -NFS4ERR_EXPIRED:
7056 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007057 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007058 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007059 goto out_restart;
7060 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007061 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007062 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007063 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007064out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07007065 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007066 return;
7067out_restart:
7068 if (!data->cancelled)
7069 rpc_restart_call_prepare(task);
7070 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007071}
7072
7073static void nfs4_lock_release(void *calldata)
7074{
7075 struct nfs4_lockdata *data = calldata;
7076
Harvey Harrison3110ff82008-05-02 13:42:44 -07007077 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007078 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007079 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007080 struct rpc_task *task;
7081 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7082 data->arg.lock_seqid);
7083 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08007084 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007085 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007086 } else
7087 nfs_free_seqid(data->arg.lock_seqid);
7088 nfs4_put_lock_state(data->lsp);
7089 put_nfs_open_context(data->ctx);
7090 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007091 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007092}
7093
7094static const struct rpc_call_ops nfs4_lock_ops = {
7095 .rpc_call_prepare = nfs4_lock_prepare,
7096 .rpc_call_done = nfs4_lock_done,
7097 .rpc_release = nfs4_lock_release,
7098};
7099
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007100static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7101{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007102 switch (error) {
7103 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007104 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007105 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007106 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007107 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007108 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007109 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007110 break;
7111 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007112 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007113 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007114 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007115}
7116
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007117static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007118{
7119 struct nfs4_lockdata *data;
7120 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007121 struct rpc_message msg = {
7122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7123 .rpc_cred = state->owner->so_cred,
7124 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007125 struct rpc_task_setup task_setup_data = {
7126 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007127 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007128 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007129 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007130 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007131 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007132 int ret;
7133
Harvey Harrison3110ff82008-05-02 13:42:44 -07007134 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007135 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007136 fl->fl_u.nfs4_fl.owner,
7137 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007138 if (data == NULL)
7139 return -ENOMEM;
7140 if (IS_SETLKW(cmd))
7141 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007142 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7143 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007144 msg.rpc_argp = &data->arg;
7145 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007146 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007147 if (recovery_type > NFS_LOCK_NEW) {
7148 if (recovery_type == NFS_LOCK_RECLAIM)
7149 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007150 } else
7151 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007152 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007153 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007154 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007155 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007156 if (ret == 0) {
7157 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007158 if (ret)
7159 nfs4_handle_setlk_error(data->server, data->lsp,
7160 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007161 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007162 data->cancelled = true;
Dave Wysochanski3d1a90a2020-12-11 05:12:51 -05007163 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007164 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007165 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007166 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167}
7168
7169static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7170{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007171 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007172 struct nfs4_exception exception = {
7173 .inode = state->inode,
7174 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007175 int err;
7176
7177 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007178 /* Cache the lock if possible... */
7179 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7180 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007181 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007182 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007183 break;
7184 nfs4_handle_exception(server, err, &exception);
7185 } while (exception.retry);
7186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187}
7188
7189static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7190{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007191 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007192 struct nfs4_exception exception = {
7193 .inode = state->inode,
7194 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007195 int err;
7196
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007197 err = nfs4_set_lock_state(state, request);
7198 if (err != 0)
7199 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007200 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007201 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7202 return 0;
7203 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007204 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007205 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7206 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007207 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007208 switch (err) {
7209 default:
7210 goto out;
7211 case -NFS4ERR_GRACE:
7212 case -NFS4ERR_DELAY:
7213 nfs4_handle_exception(server, err, &exception);
7214 err = 0;
7215 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007216 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007217out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007218 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219}
7220
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007221#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007222static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7223{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007224 struct nfs4_lock_state *lsp;
7225 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007226
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007227 status = nfs4_set_lock_state(state, request);
7228 if (status != 0)
7229 return status;
7230 lsp = request->fl_u.nfs4_fl.owner;
7231 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7232 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7233 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007234 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007235}
7236#endif
7237
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7239{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007240 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007241 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007242 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007243 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244
Trond Myklebust01c3b862006-06-29 16:38:39 -04007245 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007246 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007247 if (status < 0)
7248 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007249 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007250 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007251 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007252 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007253 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007254 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007255 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007256 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007257 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007258 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007259 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007260 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007261 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007262 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007263out:
7264 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 return status;
7266}
7267
7268static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7269{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007270 struct nfs4_exception exception = {
7271 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007272 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007273 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007274 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275 int err;
7276
7277 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007278 err = _nfs4_proc_setlk(state, cmd, request);
7279 if (err == -NFS4ERR_DENIED)
7280 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007282 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 } while (exception.retry);
7284 return err;
7285}
7286
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007287#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7288#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7289
7290static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007291nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7292 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007293{
7294 int status = -ERESTARTSYS;
7295 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7296
7297 while(!signalled()) {
7298 status = nfs4_proc_setlk(state, cmd, request);
7299 if ((status != -EAGAIN) || IS_SETLK(cmd))
7300 break;
7301 freezable_schedule_timeout_interruptible(timeout);
7302 timeout *= 2;
7303 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7304 status = -ERESTARTSYS;
7305 }
7306 return status;
7307}
7308
Jeff Laytona1d617d82016-09-17 18:17:39 -04007309#ifdef CONFIG_NFS_V4_1
7310struct nfs4_lock_waiter {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007311 struct inode *inode;
Trond Myklebust86438182021-02-02 14:22:51 -05007312 struct nfs_lowner owner;
7313 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007314};
7315
7316static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007317nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007318{
Trond Myklebust86438182021-02-02 14:22:51 -05007319 struct nfs4_lock_waiter *waiter =
7320 container_of(wait, struct nfs4_lock_waiter, wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007321
Jeff Layton57174592018-03-18 08:37:03 -04007322 /* NULL key means to wake up everyone */
7323 if (key) {
7324 struct cb_notify_lock_args *cbnl = key;
7325 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
Trond Myklebust86438182021-02-02 14:22:51 -05007326 *wowner = &waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007327
Jeff Layton57174592018-03-18 08:37:03 -04007328 /* Only wake if the callback was for the same owner. */
7329 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7330 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007331
Jeff Layton57174592018-03-18 08:37:03 -04007332 /* Make sure it's for the right inode */
7333 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7334 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007335 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007336
Trond Myklebust86438182021-02-02 14:22:51 -05007337 return woken_wake_function(wait, mode, flags, key);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007338}
7339
7340static int
7341nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7342{
Jeff Laytona1d617d82016-09-17 18:17:39 -04007343 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7344 struct nfs_server *server = NFS_SERVER(state->inode);
7345 struct nfs_client *clp = server->nfs_client;
7346 wait_queue_head_t *q = &clp->cl_lock_waitq;
Trond Myklebust86438182021-02-02 14:22:51 -05007347 struct nfs4_lock_waiter waiter = {
7348 .inode = state->inode,
7349 .owner = { .clientid = clp->cl_clientid,
7350 .id = lsp->ls_seqid.owner_id,
7351 .s_dev = server->s_dev },
7352 };
7353 int status;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007354
7355 /* Don't bother with waitqueue if we don't expect a callback */
7356 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7357 return nfs4_retry_setlk_simple(state, cmd, request);
7358
Trond Myklebust86438182021-02-02 14:22:51 -05007359 init_wait(&waiter.wait);
7360 waiter.wait.func = nfs4_wake_lock_waiter;
7361 add_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007362
Trond Myklebust86438182021-02-02 14:22:51 -05007363 do {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007364 status = nfs4_proc_setlk(state, cmd, request);
Trond Myklebust86438182021-02-02 14:22:51 -05007365 if (status != -EAGAIN || IS_SETLK(cmd))
Jeff Laytona1d617d82016-09-17 18:17:39 -04007366 break;
7367
7368 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007369 freezer_do_not_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007370 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7371 NFS4_LOCK_MAXTIMEOUT);
Yihao Wu52b042a2019-05-22 01:57:10 +08007372 freezer_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007373 } while (!signalled());
7374
7375 remove_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007376
Jeff Laytona1d617d82016-09-17 18:17:39 -04007377 return status;
7378}
7379#else /* !CONFIG_NFS_V4_1 */
7380static inline int
7381nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7382{
7383 return nfs4_retry_setlk_simple(state, cmd, request);
7384}
7385#endif
7386
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387static int
7388nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7389{
7390 struct nfs_open_context *ctx;
7391 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 int status;
7393
7394 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007395 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 state = ctx->state;
7397
Trond Myklebustd9531262009-07-21 19:22:38 -04007398 if (IS_GETLK(cmd)) {
7399 if (state != NULL)
7400 return nfs4_proc_getlk(state, F_GETLK, request);
7401 return 0;
7402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403
7404 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7405 return -EINVAL;
7406
Trond Myklebustd9531262009-07-21 19:22:38 -04007407 if (request->fl_type == F_UNLCK) {
7408 if (state != NULL)
7409 return nfs4_proc_unlck(state, cmd, request);
7410 return 0;
7411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
Trond Myklebustd9531262009-07-21 19:22:38 -04007413 if (state == NULL)
7414 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007415
7416 if ((request->fl_flags & FL_POSIX) &&
7417 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7418 return -ENOLCK;
7419
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007420 /*
7421 * Don't rely on the VFS having checked the file open mode,
7422 * since it won't do this for flock() locks.
7423 */
7424 switch (request->fl_type) {
7425 case F_RDLCK:
7426 if (!(filp->f_mode & FMODE_READ))
7427 return -EBADF;
7428 break;
7429 case F_WRLCK:
7430 if (!(filp->f_mode & FMODE_WRITE))
7431 return -EBADF;
7432 }
7433
Jeff Layton1ea67db2016-09-17 18:17:37 -04007434 status = nfs4_set_lock_state(state, request);
7435 if (status != 0)
7436 return status;
7437
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007438 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439}
7440
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007441int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007442{
7443 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007444 int err;
7445
7446 err = nfs4_set_lock_state(state, fl);
7447 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007448 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007449 do {
7450 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7451 if (err != -NFS4ERR_DELAY)
7452 break;
7453 ssleep(1);
7454 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007455 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007456}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007457
Trond Myklebustcf470c32012-03-07 13:49:12 -05007458struct nfs_release_lockowner_data {
7459 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007460 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007461 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007462 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007463 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007464};
7465
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007466static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7467{
7468 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007469 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007470 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7471 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007472 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007473 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007474}
7475
7476static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7477{
7478 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007479 struct nfs_server *server = data->server;
7480
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007481 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007482
7483 switch (task->tk_status) {
7484 case 0:
7485 renew_lease(server, data->timestamp);
7486 break;
7487 case -NFS4ERR_STALE_CLIENTID:
7488 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007489 nfs4_schedule_lease_recovery(server->nfs_client);
7490 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007491 case -NFS4ERR_LEASE_MOVED:
7492 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007493 if (nfs4_async_handle_error(task, server,
7494 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007495 rpc_restart_call_prepare(task);
7496 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007497}
7498
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007499static void nfs4_release_lockowner_release(void *calldata)
7500{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007501 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007502 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007503 kfree(calldata);
7504}
7505
Trond Myklebust17280172012-03-11 13:11:00 -04007506static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007507 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7508 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007509 .rpc_release = nfs4_release_lockowner_release,
7510};
7511
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007512static void
7513nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007514{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007515 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007516 struct rpc_message msg = {
7517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7518 };
7519
7520 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007521 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007522
Trond Myklebustcf470c32012-03-07 13:49:12 -05007523 data = kmalloc(sizeof(*data), GFP_NOFS);
7524 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007525 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007526 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007527 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007528 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7529 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7530 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007531
Trond Myklebustcf470c32012-03-07 13:49:12 -05007532 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007533 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007534 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007535 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007536}
7537
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007538#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7539
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007540static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007541 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007542 struct dentry *unused, struct inode *inode,
7543 const char *key, const void *buf,
7544 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007545{
Al Viro59301222016-05-27 10:19:30 -04007546 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007547}
7548
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007549static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007550 struct dentry *unused, struct inode *inode,
7551 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007552{
Al Virob2968212016-04-10 20:48:24 -04007553 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007554}
7555
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007556static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007557{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007558 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007559}
7560
David Quigleyc9bccef2013-05-22 12:50:45 -04007561#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007562
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007563static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007564 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007565 struct dentry *unused, struct inode *inode,
7566 const char *key, const void *buf,
7567 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007568{
7569 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007570 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007571
7572 return -EOPNOTSUPP;
7573}
7574
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007575static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007576 struct dentry *unused, struct inode *inode,
7577 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007578{
7579 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007580 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007581 return -EOPNOTSUPP;
7582}
7583
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007584static ssize_t
7585nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007586{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007587 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007588
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007589 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7590 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007591 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007592 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007593 }
7594 return len;
7595}
7596
7597static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7598 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007599 .get = nfs4_xattr_get_nfs4_label,
7600 .set = nfs4_xattr_set_nfs4_label,
7601};
David Quigleyc9bccef2013-05-22 12:50:45 -04007602
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007603#else
7604
7605static ssize_t
7606nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7607{
7608 return 0;
7609}
7610
7611#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007612
Frank van der Linden012a2112020-06-23 22:39:03 +00007613#ifdef CONFIG_NFS_V4_2
7614static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007615 struct user_namespace *mnt_userns,
Frank van der Linden012a2112020-06-23 22:39:03 +00007616 struct dentry *unused, struct inode *inode,
7617 const char *key, const void *buf,
7618 size_t buflen, int flags)
7619{
7620 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007621 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007622
7623 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7624 return -EOPNOTSUPP;
7625
7626 /*
7627 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7628 * flags right now. Handling of xattr operations use the normal
7629 * file read/write permissions.
7630 *
7631 * Just in case the server has other ideas (which RFC 8276 allows),
7632 * do a cached access check for the XA* flags to possibly avoid
7633 * doing an RPC and getting EACCES back.
7634 */
7635 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7636 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7637 return -EACCES;
7638 }
7639
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007640 if (buf == NULL) {
7641 ret = nfs42_proc_removexattr(inode, key);
7642 if (!ret)
7643 nfs4_xattr_cache_remove(inode, key);
7644 } else {
7645 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7646 if (!ret)
7647 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7648 }
7649
7650 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007651}
7652
7653static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7654 struct dentry *unused, struct inode *inode,
7655 const char *key, void *buf, size_t buflen)
7656{
7657 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007658 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007659
7660 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7661 return -EOPNOTSUPP;
7662
7663 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7664 if (!(cache.mask & NFS_ACCESS_XAREAD))
7665 return -EACCES;
7666 }
7667
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007668 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007669 if (ret)
7670 return ret;
7671
7672 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7673 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7674 return ret;
7675
7676 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7677
7678 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007679}
7680
7681static ssize_t
7682nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7683{
7684 u64 cookie;
7685 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007686 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007687 char *buf;
7688 size_t buflen;
7689 struct nfs_access_entry cache;
7690
7691 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7692 return 0;
7693
7694 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7695 if (!(cache.mask & NFS_ACCESS_XALIST))
7696 return 0;
7697 }
7698
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007699 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007700 if (ret)
7701 return ret;
7702
7703 ret = nfs4_xattr_cache_list(inode, list, list_len);
7704 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7705 return ret;
7706
Frank van der Linden012a2112020-06-23 22:39:03 +00007707 cookie = 0;
7708 eof = false;
7709 buflen = list_len ? list_len : XATTR_LIST_MAX;
7710 buf = list_len ? list : NULL;
7711 size = 0;
7712
7713 while (!eof) {
7714 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7715 &cookie, &eof);
7716 if (ret < 0)
7717 return ret;
7718
7719 if (list_len) {
7720 buf += ret;
7721 buflen -= ret;
7722 }
7723 size += ret;
7724 }
7725
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007726 if (list_len)
7727 nfs4_xattr_cache_set_list(inode, list, size);
7728
Frank van der Linden012a2112020-06-23 22:39:03 +00007729 return size;
7730}
7731
7732#else
7733
7734static ssize_t
7735nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7736{
7737 return 0;
7738}
7739#endif /* CONFIG_NFS_V4_2 */
7740
Andy Adamson533eb462011-06-13 18:25:56 -04007741/*
7742 * nfs_fhget will use either the mounted_on_fileid or the fileid
7743 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007744static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7745{
Andy Adamson533eb462011-06-13 18:25:56 -04007746 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7747 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7748 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007749 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007750 return;
7751
7752 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007753 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007754 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7755 fattr->nlink = 2;
7756}
7757
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007758static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7759 const struct qstr *name,
7760 struct nfs4_fs_locations *fs_locations,
7761 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007762{
7763 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007764 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007765 struct nfs4_fs_locations_arg args = {
7766 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007767 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007768 .page = page,
7769 .bitmask = bitmask,
7770 };
Benny Halevy22958462009-04-01 09:22:02 -04007771 struct nfs4_fs_locations_res res = {
7772 .fs_locations = fs_locations,
7773 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007774 struct rpc_message msg = {
7775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7776 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007777 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007778 };
7779 int status;
7780
Harvey Harrison3110ff82008-05-02 13:42:44 -07007781 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007782
Chuck Leverc05cefc2017-11-05 15:45:22 -05007783 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7784 bitmask[1] = nfs4_fattr_bitmap[1];
7785
Andy Adamson533eb462011-06-13 18:25:56 -04007786 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7787 * is not supported */
7788 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007789 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007790 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007791 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007792
Trond Myklebustc228fd32007-01-13 02:28:11 -05007793 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007794 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007795 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007796 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007797 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007798 return status;
7799}
7800
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007801int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7802 const struct qstr *name,
7803 struct nfs4_fs_locations *fs_locations,
7804 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007805{
Trond Myklebust0688e642019-04-07 13:59:09 -04007806 struct nfs4_exception exception = {
7807 .interruptible = true,
7808 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007809 int err;
7810 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007811 err = _nfs4_proc_fs_locations(client, dir, name,
7812 fs_locations, page);
7813 trace_nfs4_get_fs_locations(dir, name, err);
7814 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007815 &exception);
7816 } while (exception.retry);
7817 return err;
7818}
7819
Chuck Leverb03d7352013-10-17 14:12:50 -04007820/*
7821 * This operation also signals the server that this client is
7822 * performing migration recovery. The server can stop returning
7823 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7824 * appended to this compound to identify the client ID which is
7825 * performing recovery.
7826 */
7827static int _nfs40_proc_get_locations(struct inode *inode,
7828 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007829 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007830{
7831 struct nfs_server *server = NFS_SERVER(inode);
7832 struct rpc_clnt *clnt = server->client;
7833 u32 bitmask[2] = {
7834 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7835 };
7836 struct nfs4_fs_locations_arg args = {
7837 .clientid = server->nfs_client->cl_clientid,
7838 .fh = NFS_FH(inode),
7839 .page = page,
7840 .bitmask = bitmask,
7841 .migration = 1, /* skip LOOKUP */
7842 .renew = 1, /* append RENEW */
7843 };
7844 struct nfs4_fs_locations_res res = {
7845 .fs_locations = locations,
7846 .migration = 1,
7847 .renew = 1,
7848 };
7849 struct rpc_message msg = {
7850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7851 .rpc_argp = &args,
7852 .rpc_resp = &res,
7853 .rpc_cred = cred,
7854 };
7855 unsigned long now = jiffies;
7856 int status;
7857
7858 nfs_fattr_init(&locations->fattr);
7859 locations->server = server;
7860 locations->nlocations = 0;
7861
Anna Schumakerfba83f32018-05-04 16:22:50 -04007862 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007863 status = nfs4_call_sync_sequence(clnt, server, &msg,
7864 &args.seq_args, &res.seq_res);
7865 if (status)
7866 return status;
7867
7868 renew_lease(server, now);
7869 return 0;
7870}
7871
7872#ifdef CONFIG_NFS_V4_1
7873
7874/*
7875 * This operation also signals the server that this client is
7876 * performing migration recovery. The server can stop asserting
7877 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7878 * performing this operation is identified in the SEQUENCE
7879 * operation in this compound.
7880 *
7881 * When the client supports GETATTR(fs_locations_info), it can
7882 * be plumbed in here.
7883 */
7884static int _nfs41_proc_get_locations(struct inode *inode,
7885 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007886 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007887{
7888 struct nfs_server *server = NFS_SERVER(inode);
7889 struct rpc_clnt *clnt = server->client;
7890 u32 bitmask[2] = {
7891 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7892 };
7893 struct nfs4_fs_locations_arg args = {
7894 .fh = NFS_FH(inode),
7895 .page = page,
7896 .bitmask = bitmask,
7897 .migration = 1, /* skip LOOKUP */
7898 };
7899 struct nfs4_fs_locations_res res = {
7900 .fs_locations = locations,
7901 .migration = 1,
7902 };
7903 struct rpc_message msg = {
7904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7905 .rpc_argp = &args,
7906 .rpc_resp = &res,
7907 .rpc_cred = cred,
7908 };
7909 int status;
7910
7911 nfs_fattr_init(&locations->fattr);
7912 locations->server = server;
7913 locations->nlocations = 0;
7914
Anna Schumakerfba83f32018-05-04 16:22:50 -04007915 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007916 status = nfs4_call_sync_sequence(clnt, server, &msg,
7917 &args.seq_args, &res.seq_res);
7918 if (status == NFS4_OK &&
7919 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7920 status = -NFS4ERR_LEASE_MOVED;
7921 return status;
7922}
7923
7924#endif /* CONFIG_NFS_V4_1 */
7925
7926/**
7927 * nfs4_proc_get_locations - discover locations for a migrated FSID
7928 * @inode: inode on FSID that is migrating
7929 * @locations: result of query
7930 * @page: buffer
7931 * @cred: credential to use for this operation
7932 *
7933 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7934 * operation failed, or a negative errno if a local error occurred.
7935 *
7936 * On success, "locations" is filled in, but if the server has
7937 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7938 * asserted.
7939 *
7940 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7941 * from this client that require migration recovery.
7942 */
7943int nfs4_proc_get_locations(struct inode *inode,
7944 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007945 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007946{
7947 struct nfs_server *server = NFS_SERVER(inode);
7948 struct nfs_client *clp = server->nfs_client;
7949 const struct nfs4_mig_recovery_ops *ops =
7950 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007951 struct nfs4_exception exception = {
7952 .interruptible = true,
7953 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007954 int status;
7955
7956 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7957 (unsigned long long)server->fsid.major,
7958 (unsigned long long)server->fsid.minor,
7959 clp->cl_hostname);
7960 nfs_display_fhandle(NFS_FH(inode), __func__);
7961
7962 do {
7963 status = ops->get_locations(inode, locations, page, cred);
7964 if (status != -NFS4ERR_DELAY)
7965 break;
7966 nfs4_handle_exception(server, status, &exception);
7967 } while (exception.retry);
7968 return status;
7969}
7970
Chuck Lever44c99932013-10-17 14:13:30 -04007971/*
7972 * This operation also signals the server that this client is
7973 * performing "lease moved" recovery. The server can stop
7974 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7975 * is appended to this compound to identify the client ID which is
7976 * performing recovery.
7977 */
NeilBrowna52458b2018-12-03 11:30:31 +11007978static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007979{
7980 struct nfs_server *server = NFS_SERVER(inode);
7981 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7982 struct rpc_clnt *clnt = server->client;
7983 struct nfs4_fsid_present_arg args = {
7984 .fh = NFS_FH(inode),
7985 .clientid = clp->cl_clientid,
7986 .renew = 1, /* append RENEW */
7987 };
7988 struct nfs4_fsid_present_res res = {
7989 .renew = 1,
7990 };
7991 struct rpc_message msg = {
7992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7993 .rpc_argp = &args,
7994 .rpc_resp = &res,
7995 .rpc_cred = cred,
7996 };
7997 unsigned long now = jiffies;
7998 int status;
7999
8000 res.fh = nfs_alloc_fhandle();
8001 if (res.fh == NULL)
8002 return -ENOMEM;
8003
Anna Schumakerfba83f32018-05-04 16:22:50 -04008004 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04008005 status = nfs4_call_sync_sequence(clnt, server, &msg,
8006 &args.seq_args, &res.seq_res);
8007 nfs_free_fhandle(res.fh);
8008 if (status)
8009 return status;
8010
8011 do_renew_lease(clp, now);
8012 return 0;
8013}
8014
8015#ifdef CONFIG_NFS_V4_1
8016
8017/*
8018 * This operation also signals the server that this client is
8019 * performing "lease moved" recovery. The server can stop asserting
8020 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8021 * this operation is identified in the SEQUENCE operation in this
8022 * compound.
8023 */
NeilBrowna52458b2018-12-03 11:30:31 +11008024static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008025{
8026 struct nfs_server *server = NFS_SERVER(inode);
8027 struct rpc_clnt *clnt = server->client;
8028 struct nfs4_fsid_present_arg args = {
8029 .fh = NFS_FH(inode),
8030 };
8031 struct nfs4_fsid_present_res res = {
8032 };
8033 struct rpc_message msg = {
8034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8035 .rpc_argp = &args,
8036 .rpc_resp = &res,
8037 .rpc_cred = cred,
8038 };
8039 int status;
8040
8041 res.fh = nfs_alloc_fhandle();
8042 if (res.fh == NULL)
8043 return -ENOMEM;
8044
Anna Schumakerfba83f32018-05-04 16:22:50 -04008045 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04008046 status = nfs4_call_sync_sequence(clnt, server, &msg,
8047 &args.seq_args, &res.seq_res);
8048 nfs_free_fhandle(res.fh);
8049 if (status == NFS4_OK &&
8050 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8051 status = -NFS4ERR_LEASE_MOVED;
8052 return status;
8053}
8054
8055#endif /* CONFIG_NFS_V4_1 */
8056
8057/**
8058 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8059 * @inode: inode on FSID to check
8060 * @cred: credential to use for this operation
8061 *
8062 * Server indicates whether the FSID is present, moved, or not
8063 * recognized. This operation is necessary to clear a LEASE_MOVED
8064 * condition for this client ID.
8065 *
8066 * Returns NFS4_OK if the FSID is present on this server,
8067 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8068 * NFS4ERR code if some error occurred on the server, or a
8069 * negative errno if a local failure occurred.
8070 */
NeilBrowna52458b2018-12-03 11:30:31 +11008071int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008072{
8073 struct nfs_server *server = NFS_SERVER(inode);
8074 struct nfs_client *clp = server->nfs_client;
8075 const struct nfs4_mig_recovery_ops *ops =
8076 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008077 struct nfs4_exception exception = {
8078 .interruptible = true,
8079 };
Chuck Lever44c99932013-10-17 14:13:30 -04008080 int status;
8081
8082 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8083 (unsigned long long)server->fsid.major,
8084 (unsigned long long)server->fsid.minor,
8085 clp->cl_hostname);
8086 nfs_display_fhandle(NFS_FH(inode), __func__);
8087
8088 do {
8089 status = ops->fsid_present(inode, cred);
8090 if (status != -NFS4ERR_DELAY)
8091 break;
8092 nfs4_handle_exception(server, status, &exception);
8093 } while (exception.retry);
8094 return status;
8095}
8096
Trond Myklebust302fad72019-02-18 13:32:38 -05008097/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008098 * If 'use_integrity' is true and the state managment nfs_client
8099 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8100 * and the machine credential as per RFC3530bis and RFC5661 Security
8101 * Considerations sections. Otherwise, just use the user cred with the
8102 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008103 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008104static 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 +00008105{
8106 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008107 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8108 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008109 struct nfs4_secinfo_arg args = {
8110 .dir_fh = NFS_FH(dir),
8111 .name = name,
8112 };
8113 struct nfs4_secinfo_res res = {
8114 .flavors = flavors,
8115 };
8116 struct rpc_message msg = {
8117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8118 .rpc_argp = &args,
8119 .rpc_resp = &res,
8120 };
Anna Schumaker50493362019-08-14 15:30:16 -04008121 struct nfs4_call_sync_data data = {
8122 .seq_server = NFS_SERVER(dir),
8123 .seq_args = &args.seq_args,
8124 .seq_res = &res.seq_res,
8125 };
8126 struct rpc_task_setup task_setup = {
8127 .rpc_client = clnt,
8128 .rpc_message = &msg,
8129 .callback_ops = clp->cl_mvops->call_sync_ops,
8130 .callback_data = &data,
8131 .flags = RPC_TASK_NO_ROUND_ROBIN,
8132 };
NeilBrowna52458b2018-12-03 11:30:31 +11008133 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008134
8135 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008136 clnt = clp->cl_rpcclient;
8137 task_setup.rpc_client = clnt;
8138
8139 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008140 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008141 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008142
8143 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008144
Anna Schumaker50493362019-08-14 15:30:16 -04008145 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8146 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8147 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008148
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008149 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008150
NeilBrowna52458b2018-12-03 11:30:31 +11008151 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008152 return status;
8153}
8154
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008155int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8156 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008157{
Trond Myklebust0688e642019-04-07 13:59:09 -04008158 struct nfs4_exception exception = {
8159 .interruptible = true,
8160 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008161 int err;
8162 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008163 err = -NFS4ERR_WRONGSEC;
8164
8165 /* try to use integrity protection with machine cred */
8166 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8167 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8168
8169 /*
8170 * if unable to use integrity protection, or SECINFO with
8171 * integrity protection returns NFS4ERR_WRONGSEC (which is
8172 * disallowed by spec, but exists in deployed servers) use
8173 * the current filesystem's rpc_client and the user cred.
8174 */
8175 if (err == -NFS4ERR_WRONGSEC)
8176 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8177
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008178 trace_nfs4_secinfo(dir, name, err);
8179 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008180 &exception);
8181 } while (exception.retry);
8182 return err;
8183}
8184
Andy Adamson557134a2009-04-01 09:21:53 -04008185#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008186/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008187 * Check the exchange flags returned by the server for invalid flags, having
8188 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8189 * DS flags set.
8190 */
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008191static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
Andy Adamson357f54d2010-12-14 10:11:57 -05008192{
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008193 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8194 goto out_inval;
8195 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
Andy Adamson357f54d2010-12-14 10:11:57 -05008196 goto out_inval;
8197 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8198 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8199 goto out_inval;
8200 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8201 goto out_inval;
8202 return NFS_OK;
8203out_inval:
8204 return -NFS4ERR_INVAL;
8205}
8206
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008207static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008208nfs41_same_server_scope(struct nfs41_server_scope *a,
8209 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008210{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008211 if (a->server_scope_sz != b->server_scope_sz)
8212 return false;
8213 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008214}
8215
Andy Adamson02a95de2016-02-05 16:08:37 -05008216static void
8217nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8218{
Trond Myklebust5c441542019-11-13 08:34:00 +01008219 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008220 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008221 struct nfs_client *clp = args->client;
8222
8223 switch (task->tk_status) {
8224 case -NFS4ERR_BADSESSION:
8225 case -NFS4ERR_DEADSESSION:
8226 nfs4_schedule_session_recovery(clp->cl_session,
8227 task->tk_status);
8228 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008229 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8230 res->dir != NFS4_CDFS4_BOTH) {
8231 rpc_task_close_connection(task);
8232 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8233 rpc_restart_call(task);
8234 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008235}
8236
8237static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008238 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008239};
8240
Andy Adamson357f54d2010-12-14 10:11:57 -05008241/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008242 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008243 *
8244 * The 4.1 client currently uses the same TCP connection for the
8245 * fore and backchannel.
8246 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008247static
8248int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8249 struct rpc_xprt *xprt,
8250 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008251 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008252{
8253 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008254 struct nfs41_bind_conn_to_session_args args = {
8255 .client = clp,
8256 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008257 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008258 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008259 struct nfs41_bind_conn_to_session_res res;
8260 struct rpc_message msg = {
8261 .rpc_proc =
8262 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008263 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008264 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008265 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008266 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008267 struct rpc_task_setup task_setup_data = {
8268 .rpc_client = clnt,
8269 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008270 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008271 .rpc_message = &msg,
8272 .flags = RPC_TASK_TIMEOUT,
8273 };
8274 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008275
Trond Myklebust71a097c2015-02-18 09:27:18 -08008276 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8277 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8278 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008279
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008280 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8281 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8282 args.dir = NFS4_CDFC4_FORE;
8283
8284 task = rpc_run_task(&task_setup_data);
8285 if (!IS_ERR(task)) {
8286 status = task->tk_status;
8287 rpc_put_task(task);
8288 } else
8289 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008290 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008291 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008292 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008293 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8294 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008295 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008296 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008297 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008298 dprintk("NFS: %s: Unexpected direction from server\n",
8299 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008300 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008301 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008302 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008303 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8304 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008305 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008306 }
8307 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008308
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008309 return status;
8310}
8311
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008312struct rpc_bind_conn_calldata {
8313 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008314 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008315};
8316
8317static int
8318nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8319 struct rpc_xprt *xprt,
8320 void *calldata)
8321{
8322 struct rpc_bind_conn_calldata *p = calldata;
8323
8324 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8325}
8326
NeilBrowna52458b2018-12-03 11:30:31 +11008327int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008328{
8329 struct rpc_bind_conn_calldata data = {
8330 .clp = clp,
8331 .cred = cred,
8332 };
8333 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8334 nfs4_proc_bind_conn_to_session_callback, &data);
8335}
8336
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008337/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008338 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8339 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008340 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008341static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8342 .how = SP4_MACH_CRED,
8343 .enforce.u.words = {
8344 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8345 1 << (OP_EXCHANGE_ID - 32) |
8346 1 << (OP_CREATE_SESSION - 32) |
8347 1 << (OP_DESTROY_SESSION - 32) |
8348 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008349 },
8350 .allow.u.words = {
8351 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008352 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008353 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008354 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008355 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008356 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008357 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008358 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008359 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008360 1 << (OP_FREE_STATEID - 32) |
8361 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008362 }
8363};
8364
8365/*
8366 * Select the state protection mode for client `clp' given the server results
8367 * from exchange_id in `sp'.
8368 *
8369 * Returns 0 on success, negative errno otherwise.
8370 */
8371static int nfs4_sp4_select_mode(struct nfs_client *clp,
8372 struct nfs41_state_protection *sp)
8373{
8374 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8375 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8376 1 << (OP_EXCHANGE_ID - 32) |
8377 1 << (OP_CREATE_SESSION - 32) |
8378 1 << (OP_DESTROY_SESSION - 32) |
8379 1 << (OP_DESTROY_CLIENTID - 32)
8380 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008381 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008382 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008383 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008384
8385 if (sp->how == SP4_MACH_CRED) {
8386 /* Print state protect result */
8387 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8388 for (i = 0; i <= LAST_NFS4_OP; i++) {
8389 if (test_bit(i, sp->enforce.u.longs))
8390 dfprintk(MOUNT, " enforce op %d\n", i);
8391 if (test_bit(i, sp->allow.u.longs))
8392 dfprintk(MOUNT, " allow op %d\n", i);
8393 }
8394
8395 /* make sure nothing is on enforce list that isn't supported */
8396 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8397 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8398 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008399 ret = -EINVAL;
8400 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008401 }
8402 }
8403
8404 /*
8405 * Minimal mode - state operations are allowed to use machine
8406 * credential. Note this already happens by default, so the
8407 * client doesn't have to do anything more than the negotiation.
8408 *
8409 * NOTE: we don't care if EXCHANGE_ID is in the list -
8410 * we're already using the machine cred for exchange_id
8411 * and will never use a different cred.
8412 */
8413 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8414 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8415 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8416 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8417 dfprintk(MOUNT, "sp4_mach_cred:\n");
8418 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008419 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008420 } else {
8421 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008422 ret = -EINVAL;
8423 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008424 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008425
8426 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008427 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8428 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008429 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8430 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008431 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008432 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008433
Andrew Elble99ade3c2015-12-02 09:39:51 -05008434 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8435 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008436 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008437 }
8438
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008439 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8440 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8441 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008442 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008443 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008444
8445 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8446 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8447 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008448 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008449 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008450
8451 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8452 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008453 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008454 }
8455
8456 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8457 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008458 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008459 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008460 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008461out:
8462 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008463 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008464}
8465
Andy Adamson8d89bd72016-09-09 09:22:18 -04008466struct nfs41_exchange_id_data {
8467 struct nfs41_exchange_id_res res;
8468 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008469};
8470
Andy Adamson8d89bd72016-09-09 09:22:18 -04008471static void nfs4_exchange_id_release(void *data)
8472{
8473 struct nfs41_exchange_id_data *cdata =
8474 (struct nfs41_exchange_id_data *)data;
8475
Olga Kornievskaia63513232017-03-13 10:36:19 -04008476 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008477 kfree(cdata->res.impl_id);
8478 kfree(cdata->res.server_scope);
8479 kfree(cdata->res.server_owner);
8480 kfree(cdata);
8481}
8482
8483static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008484 .rpc_release = nfs4_exchange_id_release,
8485};
8486
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008487/*
8488 * _nfs4_proc_exchange_id()
8489 *
8490 * Wrapper for EXCHANGE_ID operation.
8491 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008492static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008493nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008494 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008495{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008496 struct rpc_message msg = {
8497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008498 .rpc_cred = cred,
8499 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008500 struct rpc_task_setup task_setup_data = {
8501 .rpc_client = clp->cl_rpcclient,
8502 .callback_ops = &nfs4_exchange_id_call_ops,
8503 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008504 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008505 };
8506 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008507 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008508
Elena Reshetova212bf412017-10-20 12:53:38 +03008509 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008510 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008511
Trond Myklebust9c760d12017-07-31 18:38:50 -04008512 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008513 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008514 if (!calldata)
8515 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008516
Trond Myklebustfd405592017-08-01 16:02:47 -04008517 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008518
8519 status = nfs4_init_uniform_client_string(clp);
8520 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008521 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008522
Andy Adamson8d89bd72016-09-09 09:22:18 -04008523 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8524 GFP_NOFS);
8525 status = -ENOMEM;
8526 if (unlikely(calldata->res.server_owner == NULL))
8527 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008528
Andy Adamson8d89bd72016-09-09 09:22:18 -04008529 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008530 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008531 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008532 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008533
Andy Adamson8d89bd72016-09-09 09:22:18 -04008534 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8535 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008536 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008537
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008538 switch (sp4_how) {
8539 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008540 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008541 break;
8542
8543 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008544 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008545 break;
8546
8547 default:
8548 /* unsupported! */
8549 WARN_ON_ONCE(1);
8550 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008551 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008552 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008553 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008554 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008555 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008556 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8557 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008558 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008559 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008560 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8561 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008562#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008563 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008564#endif
8565 msg.rpc_argp = &calldata->args;
8566 msg.rpc_resp = &calldata->res;
8567 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008568
Trond Myklebust9c760d12017-07-31 18:38:50 -04008569 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008570
8571out_impl_id:
8572 kfree(calldata->res.impl_id);
8573out_server_scope:
8574 kfree(calldata->res.server_scope);
8575out_server_owner:
8576 kfree(calldata->res.server_owner);
8577out_calldata:
8578 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008579out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008580 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008581 return ERR_PTR(status);
8582}
8583
8584/*
8585 * _nfs4_proc_exchange_id()
8586 *
8587 * Wrapper for EXCHANGE_ID operation.
8588 */
NeilBrowna52458b2018-12-03 11:30:31 +11008589static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008590 u32 sp4_how)
8591{
8592 struct rpc_task *task;
8593 struct nfs41_exchange_id_args *argp;
8594 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008595 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008596 int status;
8597
8598 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8599 if (IS_ERR(task))
8600 return PTR_ERR(task);
8601
8602 argp = task->tk_msg.rpc_argp;
8603 resp = task->tk_msg.rpc_resp;
8604 status = task->tk_status;
8605 if (status != 0)
8606 goto out;
8607
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008608 status = nfs4_check_cl_exchange_flags(resp->flags,
8609 clp->cl_mvops->minor_version);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008610 if (status != 0)
8611 goto out;
8612
8613 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8614 if (status != 0)
8615 goto out;
8616
Robert Milkowski7dc29932020-01-30 09:43:25 +00008617 do_renew_lease(clp, now);
8618
Trond Myklebust9c760d12017-07-31 18:38:50 -04008619 clp->cl_clientid = resp->clientid;
8620 clp->cl_exchange_flags = resp->flags;
8621 clp->cl_seqid = resp->seqid;
8622 /* Client ID is not confirmed */
8623 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8624 clear_bit(NFS4_SESSION_ESTABLISHED,
8625 &clp->cl_session->session_state);
8626
8627 if (clp->cl_serverscope != NULL &&
8628 !nfs41_same_server_scope(clp->cl_serverscope,
8629 resp->server_scope)) {
8630 dprintk("%s: server_scope mismatch detected\n",
8631 __func__);
8632 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8633 }
8634
8635 swap(clp->cl_serverowner, resp->server_owner);
8636 swap(clp->cl_serverscope, resp->server_scope);
8637 swap(clp->cl_implid, resp->impl_id);
8638
8639 /* Save the EXCHANGE_ID verifier session trunk tests */
8640 memcpy(clp->cl_confirm.data, argp->verifier.data,
8641 sizeof(clp->cl_confirm.data));
8642out:
8643 trace_nfs4_exchange_id(clp, status);
8644 rpc_put_task(task);
8645 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008646}
8647
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008648/*
8649 * nfs4_proc_exchange_id()
8650 *
8651 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8652 *
8653 * Since the clientid has expired, all compounds using sessions
8654 * associated with the stale clientid will be returning
8655 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8656 * be in some phase of session reset.
8657 *
8658 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8659 */
NeilBrowna52458b2018-12-03 11:30:31 +11008660int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008661{
8662 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8663 int status;
8664
8665 /* try SP4_MACH_CRED if krb5i/p */
8666 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8667 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008668 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008669 if (!status)
8670 return 0;
8671 }
8672
8673 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008674 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008675}
8676
Andy Adamson04fa2c62016-09-09 09:22:29 -04008677/**
8678 * nfs4_test_session_trunk
8679 *
8680 * This is an add_xprt_test() test function called from
8681 * rpc_clnt_setup_test_and_add_xprt.
8682 *
8683 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8684 * and is dereferrenced in nfs4_exchange_id_release
8685 *
8686 * Upon success, add the new transport to the rpc_clnt
8687 *
8688 * @clnt: struct rpc_clnt to get new transport
8689 * @xprt: the rpc_xprt to test
8690 * @data: call data for _nfs4_proc_exchange_id.
8691 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308692void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008693 void *data)
8694{
8695 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008696 struct rpc_task *task;
8697 int status;
8698
Andy Adamson04fa2c62016-09-09 09:22:29 -04008699 u32 sp4_how;
8700
8701 dprintk("--> %s try %s\n", __func__,
8702 xprt->address_strings[RPC_DISPLAY_ADDR]);
8703
8704 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8705
8706 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008707 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8708 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308709 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008710
8711 status = task->tk_status;
8712 if (status == 0)
8713 status = nfs4_detect_session_trunking(adata->clp,
8714 task->tk_msg.rpc_resp, xprt);
8715
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308716 if (status == 0)
8717 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8718
Trond Myklebust9c760d12017-07-31 18:38:50 -04008719 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008720}
8721EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8722
Trond Myklebust66245532012-05-25 17:18:09 -04008723static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008724 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008725{
8726 struct rpc_message msg = {
8727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8728 .rpc_argp = clp,
8729 .rpc_cred = cred,
8730 };
8731 int status;
8732
NeilBrown5a0c2572019-05-30 10:41:28 +10008733 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8734 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008735 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008736 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008737 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008738 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8739 return status;
8740}
8741
8742static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008743 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008744{
8745 unsigned int loop;
8746 int ret;
8747
8748 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8749 ret = _nfs4_proc_destroy_clientid(clp, cred);
8750 switch (ret) {
8751 case -NFS4ERR_DELAY:
8752 case -NFS4ERR_CLIENTID_BUSY:
8753 ssleep(1);
8754 break;
8755 default:
8756 return ret;
8757 }
8758 }
8759 return 0;
8760}
8761
8762int nfs4_destroy_clientid(struct nfs_client *clp)
8763{
NeilBrowna52458b2018-12-03 11:30:31 +11008764 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008765 int ret = 0;
8766
8767 if (clp->cl_mvops->minor_version < 1)
8768 goto out;
8769 if (clp->cl_exchange_flags == 0)
8770 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008771 if (clp->cl_preserve_clid)
8772 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008773 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008774 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008775 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008776 switch (ret) {
8777 case 0:
8778 case -NFS4ERR_STALE_CLIENTID:
8779 clp->cl_exchange_flags = 0;
8780 }
8781out:
8782 return ret;
8783}
8784
Donald Buczek0efb01b2019-07-07 21:26:08 +02008785#endif /* CONFIG_NFS_V4_1 */
8786
Andy Adamson2050f0c2009-04-01 09:22:30 -04008787struct nfs4_get_lease_time_data {
8788 struct nfs4_get_lease_time_args *args;
8789 struct nfs4_get_lease_time_res *res;
8790 struct nfs_client *clp;
8791};
8792
8793static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8794 void *calldata)
8795{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008796 struct nfs4_get_lease_time_data *data =
8797 (struct nfs4_get_lease_time_data *)calldata;
8798
8799 dprintk("--> %s\n", __func__);
8800 /* just setup sequence, do not trigger session recovery
8801 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008802 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008803 &data->args->la_seq_args,
8804 &data->res->lr_seq_res,
8805 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008806 dprintk("<-- %s\n", __func__);
8807}
8808
8809/*
8810 * Called from nfs4_state_manager thread for session setup, so don't recover
8811 * from sequence operation or clientid errors.
8812 */
8813static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8814{
8815 struct nfs4_get_lease_time_data *data =
8816 (struct nfs4_get_lease_time_data *)calldata;
8817
8818 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008819 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008820 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008821 switch (task->tk_status) {
8822 case -NFS4ERR_DELAY:
8823 case -NFS4ERR_GRACE:
8824 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8825 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8826 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008827 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008828 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008829 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008830 return;
8831 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008832 dprintk("<-- %s\n", __func__);
8833}
8834
Trond Myklebust17280172012-03-11 13:11:00 -04008835static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008836 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8837 .rpc_call_done = nfs4_get_lease_time_done,
8838};
8839
8840int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8841{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008842 struct nfs4_get_lease_time_args args;
8843 struct nfs4_get_lease_time_res res = {
8844 .lr_fsinfo = fsinfo,
8845 };
8846 struct nfs4_get_lease_time_data data = {
8847 .args = &args,
8848 .res = &res,
8849 .clp = clp,
8850 };
8851 struct rpc_message msg = {
8852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8853 .rpc_argp = &args,
8854 .rpc_resp = &res,
8855 };
8856 struct rpc_task_setup task_setup = {
8857 .rpc_client = clp->cl_rpcclient,
8858 .rpc_message = &msg,
8859 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008860 .callback_data = &data,
8861 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008862 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008863
Anna Schumakerfba83f32018-05-04 16:22:50 -04008864 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008865 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008866}
8867
Donald Buczek0efb01b2019-07-07 21:26:08 +02008868#ifdef CONFIG_NFS_V4_1
8869
Andy Adamsonfc931582009-04-01 09:22:31 -04008870/*
8871 * Initialize the values to be used by the client in CREATE_SESSION
8872 * If nfs4_init_session set the fore channel request and response sizes,
8873 * use them.
8874 *
8875 * Set the back channel max_resp_sz_cached to zero to force the client to
8876 * always set csa_cachethis to FALSE because the current implementation
8877 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8878 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008879static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8880 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008881{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008882 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008883 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008884 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008885
Andy Adamson18aad3d2013-06-26 12:21:49 -04008886 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8887 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8888
Andy Adamsonfc931582009-04-01 09:22:31 -04008889 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008890 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8891 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008892 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008893 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008894
8895 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008896 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008897 __func__,
8898 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008899 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008900
8901 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008902 args->bc_attrs.max_rqst_sz = max_bc_payload;
8903 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008904 args->bc_attrs.max_resp_sz_cached = 0;
8905 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008906 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008907 if (args->bc_attrs.max_reqs > max_bc_slots)
8908 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008909
8910 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8911 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8912 __func__,
8913 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8914 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8915 args->bc_attrs.max_reqs);
8916}
8917
Trond Myklebust79969dd2015-02-18 11:30:18 -08008918static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8919 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008920{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008921 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008922 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008923
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008924 if (rcvd->max_resp_sz > sent->max_resp_sz)
8925 return -EINVAL;
8926 /*
8927 * Our requested max_ops is the minimum we need; we're not
8928 * prepared to break up compounds into smaller pieces than that.
8929 * So, no point even trying to continue if the server won't
8930 * cooperate:
8931 */
8932 if (rcvd->max_ops < sent->max_ops)
8933 return -EINVAL;
8934 if (rcvd->max_reqs == 0)
8935 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008936 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8937 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008938 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008939}
8940
Trond Myklebust79969dd2015-02-18 11:30:18 -08008941static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8942 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008943{
8944 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008945 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008946
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008947 if (!(res->flags & SESSION4_BACK_CHAN))
8948 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008949 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8950 return -EINVAL;
8951 if (rcvd->max_resp_sz < sent->max_resp_sz)
8952 return -EINVAL;
8953 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8954 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008955 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008956 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008957 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008958 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008959out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008960 return 0;
8961}
Andy Adamson8d353012009-04-01 09:22:32 -04008962
Andy Adamson8d353012009-04-01 09:22:32 -04008963static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008964 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008965{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008966 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008967
Trond Myklebust79969dd2015-02-18 11:30:18 -08008968 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008969 if (ret)
8970 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008971 return nfs4_verify_back_channel_attrs(args, res);
8972}
8973
8974static void nfs4_update_session(struct nfs4_session *session,
8975 struct nfs41_create_session_res *res)
8976{
8977 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008978 /* Mark client id and session as being confirmed */
8979 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8980 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008981 session->flags = res->flags;
8982 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008983 if (res->flags & SESSION4_BACK_CHAN)
8984 memcpy(&session->bc_attrs, &res->bc_attrs,
8985 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008986}
8987
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008988static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008989 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008990{
8991 struct nfs4_session *session = clp->cl_session;
8992 struct nfs41_create_session_args args = {
8993 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008994 .clientid = clp->cl_clientid,
8995 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008996 .cb_program = NFS4_CALLBACK,
8997 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008998 struct nfs41_create_session_res res;
8999
Andy Adamsonfc931582009-04-01 09:22:31 -04009000 struct rpc_message msg = {
9001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9002 .rpc_argp = &args,
9003 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009004 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04009005 };
9006 int status;
9007
Chuck Lever6b26cc82016-05-02 14:40:40 -04009008 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04009009 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04009010
NeilBrown5a0c2572019-05-30 10:41:28 +10009011 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9012 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009013 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009014
Trond Myklebustb519d402016-09-11 14:50:01 -04009015 switch (status) {
9016 case -NFS4ERR_STALE_CLIENTID:
9017 case -NFS4ERR_DELAY:
9018 case -ETIMEDOUT:
9019 case -EACCES:
9020 case -EAGAIN:
9021 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08009022 }
Trond Myklebustb519d402016-09-11 14:50:01 -04009023
9024 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05009025 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04009026 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009027 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009028 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009029 if (status)
9030 goto out;
9031 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009032 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08009033out:
Andy Adamsonfc931582009-04-01 09:22:31 -04009034 return status;
9035}
9036
9037/*
9038 * Issues a CREATE_SESSION operation to the server.
9039 * It is the responsibility of the caller to verify the session is
9040 * expired before calling this routine.
9041 */
NeilBrowna52458b2018-12-03 11:30:31 +11009042int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04009043{
9044 int status;
9045 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04009046 struct nfs4_session *session = clp->cl_session;
9047
9048 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9049
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009050 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04009051 if (status)
9052 goto out;
9053
Andy Adamsonaacd5532011-11-09 13:58:21 -05009054 /* Init or reset the session slot tables */
9055 status = nfs4_setup_session_slot_tables(session);
9056 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009057 if (status)
9058 goto out;
9059
9060 ptr = (unsigned *)&session->sess_id.data[0];
9061 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9062 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04009063out:
9064 dprintk("<-- %s\n", __func__);
9065 return status;
9066}
9067
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009068/*
9069 * Issue the over-the-wire RPC DESTROY_SESSION.
9070 * The caller must serialize access to this routine.
9071 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009072int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11009073 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009074{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009075 struct rpc_message msg = {
9076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9077 .rpc_argp = session,
9078 .rpc_cred = cred,
9079 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009080 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009081
9082 dprintk("--> nfs4_proc_destroy_session\n");
9083
9084 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05009085 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9086 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009087
NeilBrown5a0c2572019-05-30 10:41:28 +10009088 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9089 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009090 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009091
9092 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04009093 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009094 "Session has been destroyed regardless...\n", status);
9095
9096 dprintk("<-- nfs4_proc_destroy_session\n");
9097 return status;
9098}
9099
Trond Myklebust7b38c362012-05-23 13:23:31 -04009100/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009101 * Renew the cl_session lease.
9102 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009103struct nfs4_sequence_data {
9104 struct nfs_client *clp;
9105 struct nfs4_sequence_args args;
9106 struct nfs4_sequence_res res;
9107};
9108
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009109static void nfs41_sequence_release(void *data)
9110{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009111 struct nfs4_sequence_data *calldata = data;
9112 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009113
Elena Reshetova212bf412017-10-20 12:53:38 +03009114 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009115 nfs4_schedule_state_renewal(clp);
9116 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009117 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009118}
9119
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009120static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9121{
9122 switch(task->tk_status) {
9123 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009124 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9125 return -EAGAIN;
9126 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009127 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009128 }
9129 return 0;
9130}
9131
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009132static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009133{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009134 struct nfs4_sequence_data *calldata = data;
9135 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009136
Trond Myklebust14516c32010-07-31 14:29:06 -04009137 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9138 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009139
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009140 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009141 if (task->tk_status < 0) {
9142 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009143 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009144 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009145
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009146 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9147 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009148 return;
9149 }
9150 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009151 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08009152out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009153 dprintk("<-- %s\n", __func__);
9154}
9155
9156static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9157{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009158 struct nfs4_sequence_data *calldata = data;
9159 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009160 struct nfs4_sequence_args *args;
9161 struct nfs4_sequence_res *res;
9162
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009163 args = task->tk_msg.rpc_argp;
9164 res = task->tk_msg.rpc_resp;
9165
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009166 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009167}
9168
9169static const struct rpc_call_ops nfs41_sequence_ops = {
9170 .rpc_call_done = nfs41_sequence_call_done,
9171 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009172 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009173};
9174
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009175static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009176 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009177 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009178 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009179{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009180 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009181 struct rpc_message msg = {
9182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9183 .rpc_cred = cred,
9184 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009185 struct rpc_task_setup task_setup_data = {
9186 .rpc_client = clp->cl_rpcclient,
9187 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009188 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04009189 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009190 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009191 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009192
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009193 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009194 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009195 goto out_err;
9196
9197 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009198 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009199 if (calldata == NULL)
9200 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009201 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009202 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009203 msg.rpc_argp = &calldata->args;
9204 msg.rpc_resp = &calldata->res;
9205 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009206 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009207
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009208 ret = rpc_run_task(&task_setup_data);
9209 if (IS_ERR(ret))
9210 goto out_err;
9211 return ret;
9212out_put_clp:
9213 nfs_put_client(clp);
9214out_err:
9215 nfs41_release_slot(slot);
9216 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009217}
9218
NeilBrowna52458b2018-12-03 11:30:31 +11009219static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009220{
9221 struct rpc_task *task;
9222 int ret = 0;
9223
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009224 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009225 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009226 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009227 if (IS_ERR(task))
9228 ret = PTR_ERR(task);
9229 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009230 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009231 dprintk("<-- %s status=%d\n", __func__, ret);
9232 return ret;
9233}
9234
NeilBrowna52458b2018-12-03 11:30:31 +11009235static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009236{
9237 struct rpc_task *task;
9238 int ret;
9239
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009240 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009241 if (IS_ERR(task)) {
9242 ret = PTR_ERR(task);
9243 goto out;
9244 }
9245 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009246 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009247 ret = task->tk_status;
9248 rpc_put_task(task);
9249out:
9250 dprintk("<-- %s status=%d\n", __func__, ret);
9251 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009252}
9253
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009254struct nfs4_reclaim_complete_data {
9255 struct nfs_client *clp;
9256 struct nfs41_reclaim_complete_args arg;
9257 struct nfs41_reclaim_complete_res res;
9258};
9259
9260static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9261{
9262 struct nfs4_reclaim_complete_data *calldata = data;
9263
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009264 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009265 &calldata->arg.seq_args,
9266 &calldata->res.seq_res,
9267 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009268}
9269
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009270static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9271{
9272 switch(task->tk_status) {
9273 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009274 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009275 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009276 case -NFS4ERR_COMPLETE_ALREADY:
9277 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9278 break;
9279 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009280 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009281 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009282 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009283 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009284 case -NFS4ERR_BADSESSION:
9285 case -NFS4ERR_DEADSESSION:
9286 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009287 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009288 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009289 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009290 }
9291 return 0;
9292}
9293
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009294static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9295{
9296 struct nfs4_reclaim_complete_data *calldata = data;
9297 struct nfs_client *clp = calldata->clp;
9298 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9299
9300 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04009301 if (!nfs41_sequence_done(task, res))
9302 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009303
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009304 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009305 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9306 rpc_restart_call_prepare(task);
9307 return;
9308 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009309 dprintk("<-- %s\n", __func__);
9310}
9311
9312static void nfs4_free_reclaim_complete_data(void *data)
9313{
9314 struct nfs4_reclaim_complete_data *calldata = data;
9315
9316 kfree(calldata);
9317}
9318
9319static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9320 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9321 .rpc_call_done = nfs4_reclaim_complete_done,
9322 .rpc_release = nfs4_free_reclaim_complete_data,
9323};
9324
9325/*
9326 * Issue a global reclaim complete.
9327 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009328static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009329 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009330{
9331 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009332 struct rpc_message msg = {
9333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009334 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009335 };
9336 struct rpc_task_setup task_setup_data = {
9337 .rpc_client = clp->cl_rpcclient,
9338 .rpc_message = &msg,
9339 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009340 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009341 };
9342 int status = -ENOMEM;
9343
9344 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009345 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009346 if (calldata == NULL)
9347 goto out;
9348 calldata->clp = clp;
9349 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009350
Anna Schumakerfba83f32018-05-04 16:22:50 -04009351 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009352 msg.rpc_argp = &calldata->arg;
9353 msg.rpc_resp = &calldata->res;
9354 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009355 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009356out:
9357 dprintk("<-- %s status=%d\n", __func__, status);
9358 return status;
9359}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009360
9361static void
9362nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9363{
9364 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009365 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009366
9367 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009368 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009369 &lgp->res.seq_res, task);
9370 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009371}
9372
9373static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9374{
9375 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009376
9377 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009378 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009379 dprintk("<-- %s\n", __func__);
9380}
9381
9382static int
9383nfs4_layoutget_handle_exception(struct rpc_task *task,
9384 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9385{
Trond Myklebustee314c22012-10-01 17:25:48 -07009386 struct inode *inode = lgp->args.inode;
9387 struct nfs_server *server = NFS_SERVER(inode);
9388 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009389 int nfs4err = task->tk_status;
9390 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009391 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009392
Boaz Harroshed7e5422014-01-22 20:34:54 +02009393 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009394
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009395 nfs4_sequence_free_slot(&lgp->res.seq_res);
9396
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009397 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009398 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009399 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009400
9401 /*
9402 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9403 * on the file. set tk_status to -ENODATA to tell upper layer to
9404 * retry go inband.
9405 */
9406 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009407 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009408 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009409 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009410 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9411 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9412 */
9413 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009414 status = -EOVERFLOW;
9415 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009416 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009417 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009418 * (or clients) writing to the same RAID stripe except when
9419 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009420 *
9421 * Treat it like we would RECALLCONFLICT -- we retry for a little
9422 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009423 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009424 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009425 if (lgp->args.minlength == 0) {
9426 status = -EOVERFLOW;
9427 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009428 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009429 status = -EBUSY;
9430 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009431 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009432 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009433 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009434 case -NFS4ERR_DELEG_REVOKED:
9435 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009436 case -NFS4ERR_EXPIRED:
9437 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009438 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009439 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009440 lo = NFS_I(inode)->layout;
9441 /* If the open stateid was bad, then recover it. */
9442 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009443 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009444 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009445 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009446 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009447 break;
9448 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009449
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009450 /*
9451 * Mark the bad layout state as invalid, then retry
9452 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009453 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009454 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009455 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009456 pnfs_free_lseg_list(&head);
9457 status = -EAGAIN;
9458 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009459 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009460
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009461 err = nfs4_handle_exception(server, nfs4err, exception);
9462 if (!status) {
9463 if (exception->retry)
9464 status = -EAGAIN;
9465 else
9466 status = err;
9467 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009468out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009469 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009470 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009471}
9472
Fred Isamandacb4522016-09-19 17:47:09 -04009473size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009474{
9475 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9476 return nfs_page_array_len(0, max_resp_sz);
9477}
9478
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009479static void nfs4_layoutget_release(void *calldata)
9480{
9481 struct nfs4_layoutget *lgp = calldata;
9482
9483 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009484 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009485 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009486 dprintk("<-- %s\n", __func__);
9487}
9488
9489static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9490 .rpc_call_prepare = nfs4_layoutget_prepare,
9491 .rpc_call_done = nfs4_layoutget_done,
9492 .rpc_release = nfs4_layoutget_release,
9493};
9494
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009495struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009496nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009497{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009498 struct inode *inode = lgp->args.inode;
9499 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009500 struct rpc_task *task;
9501 struct rpc_message msg = {
9502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9503 .rpc_argp = &lgp->args,
9504 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009505 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009506 };
9507 struct rpc_task_setup task_setup_data = {
9508 .rpc_client = server->client,
9509 .rpc_message = &msg,
9510 .callback_ops = &nfs4_layoutget_call_ops,
9511 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009512 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009513 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009514 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009515 struct nfs4_exception exception = {
9516 .inode = inode,
9517 .timeout = *timeout,
9518 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009519 int status = 0;
9520
9521 dprintk("--> %s\n", __func__);
9522
Peng Tao4bd5a982014-11-17 11:05:17 +08009523 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9524 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9525
Anna Schumakerfba83f32018-05-04 16:22:50 -04009526 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009527
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009528 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009529
Anna Schumaker820bf852017-01-11 15:01:43 -05009530 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009531 if (status != 0)
9532 goto out;
9533
Trond Myklebust18c07782019-02-13 07:55:31 -05009534 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009535 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9536 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009537 } else if (lgp->res.layoutp->len == 0) {
9538 status = -EAGAIN;
9539 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009540 } else
9541 lseg = pnfs_layout_process(lgp);
9542out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009543 trace_nfs4_layoutget(lgp->args.ctx,
9544 &lgp->args.range,
9545 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009546 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009547 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009548
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009549 rpc_put_task(task);
9550 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009551 if (status)
9552 return ERR_PTR(status);
9553 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009554}
9555
Benny Halevycbe82602011-05-22 19:52:37 +03009556static void
9557nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9558{
9559 struct nfs4_layoutreturn *lrp = calldata;
9560
9561 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009562 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009563 &lrp->args.seq_args,
9564 &lrp->res.seq_res,
9565 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009566 if (!pnfs_layout_is_valid(lrp->args.layout))
9567 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009568}
9569
9570static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9571{
9572 struct nfs4_layoutreturn *lrp = calldata;
9573 struct nfs_server *server;
9574
9575 dprintk("--> %s\n", __func__);
9576
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009577 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009578 return;
9579
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009580 /*
9581 * Was there an RPC level error? Assume the call succeeded,
9582 * and that we need to release the layout
9583 */
9584 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9585 lrp->res.lrs_present = 0;
9586 return;
9587 }
9588
Benny Halevycbe82602011-05-22 19:52:37 +03009589 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009590 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009591 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009592 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009593 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009594 lrp->args.inode))
9595 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009596 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009597 default:
9598 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009599 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009600 case 0:
9601 break;
9602 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009603 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009604 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009605 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009606 }
Benny Halevycbe82602011-05-22 19:52:37 +03009607 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009608 return;
9609out_restart:
9610 task->tk_status = 0;
9611 nfs4_sequence_free_slot(&lrp->res.seq_res);
9612 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009613}
9614
9615static void nfs4_layoutreturn_release(void *calldata)
9616{
9617 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009618 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009619
9620 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009621 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009622 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009623 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009624 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9625 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009626 pnfs_put_layout_hdr(lrp->args.layout);
9627 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009628 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009629 kfree(calldata);
9630 dprintk("<-- %s\n", __func__);
9631}
9632
9633static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9634 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9635 .rpc_call_done = nfs4_layoutreturn_done,
9636 .rpc_release = nfs4_layoutreturn_release,
9637};
9638
Peng Tao6c166052014-11-17 09:30:40 +08009639int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009640{
9641 struct rpc_task *task;
9642 struct rpc_message msg = {
9643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9644 .rpc_argp = &lrp->args,
9645 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009646 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009647 };
9648 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009649 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009650 .rpc_message = &msg,
9651 .callback_ops = &nfs4_layoutreturn_call_ops,
9652 .callback_data = lrp,
9653 };
Peng Tao6c166052014-11-17 09:30:40 +08009654 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009655
Andrew Elble99ade3c2015-12-02 09:39:51 -05009656 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9657 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9658 &task_setup_data.rpc_client, &msg);
9659
Benny Halevycbe82602011-05-22 19:52:37 +03009660 dprintk("--> %s\n", __func__);
Trond Myklebustc3aba892021-06-01 11:35:56 -04009661 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009662 if (!sync) {
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009663 if (!lrp->inode) {
9664 nfs4_layoutreturn_release(lrp);
9665 return -EAGAIN;
9666 }
9667 task_setup_data.flags |= RPC_TASK_ASYNC;
9668 }
Trond Myklebustc3aba892021-06-01 11:35:56 -04009669 if (!lrp->inode)
9670 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9671 1);
9672 else
9673 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9674 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009675 task = rpc_run_task(&task_setup_data);
9676 if (IS_ERR(task))
9677 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009678 if (sync)
9679 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009680 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009681 dprintk("<-- %s status=%d\n", __func__, status);
9682 rpc_put_task(task);
9683 return status;
9684}
9685
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009686static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009687_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9688 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009689 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009690{
9691 struct nfs4_getdeviceinfo_args args = {
9692 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009693 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9694 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009695 };
9696 struct nfs4_getdeviceinfo_res res = {
9697 .pdev = pdev,
9698 };
9699 struct rpc_message msg = {
9700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9701 .rpc_argp = &args,
9702 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009703 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009704 };
9705 int status;
9706
9707 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009708 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009709 if (res.notification & ~args.notify_types)
9710 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009711 if (res.notification != args.notify_types)
9712 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009713
Trond Myklebustcac1d3a2020-12-16 16:31:26 -05009714 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9715
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009716 dprintk("<-- %s status=%d\n", __func__, status);
9717
9718 return status;
9719}
9720
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009721int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9722 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009723 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009724{
9725 struct nfs4_exception exception = { };
9726 int err;
9727
9728 do {
9729 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009730 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009731 &exception);
9732 } while (exception.retry);
9733 return err;
9734}
9735EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9736
Andy Adamson863a3c62011-03-23 13:27:54 +00009737static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9738{
9739 struct nfs4_layoutcommit_data *data = calldata;
9740 struct nfs_server *server = NFS_SERVER(data->args.inode);
9741
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009742 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009743 &data->args.seq_args,
9744 &data->res.seq_res,
9745 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009746}
9747
9748static void
9749nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9750{
9751 struct nfs4_layoutcommit_data *data = calldata;
9752 struct nfs_server *server = NFS_SERVER(data->args.inode);
9753
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009754 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009755 return;
9756
9757 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009758 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9759 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9760 case -NFS4ERR_BADLAYOUT: /* no layout */
9761 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009762 task->tk_status = 0;
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06009763 break;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009764 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009765 break;
9766 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009767 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009768 rpc_restart_call_prepare(task);
9769 return;
9770 }
9771 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009772}
9773
9774static void nfs4_layoutcommit_release(void *calldata)
9775{
9776 struct nfs4_layoutcommit_data *data = calldata;
9777
Andy Adamsondb29c082011-07-30 20:52:38 -04009778 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009779 nfs_post_op_update_inode_force_wcc(data->args.inode,
9780 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009781 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009782 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009783 kfree(data);
9784}
9785
9786static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9787 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9788 .rpc_call_done = nfs4_layoutcommit_done,
9789 .rpc_release = nfs4_layoutcommit_release,
9790};
9791
9792int
Andy Adamsonef311532011-03-12 02:58:10 -05009793nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009794{
9795 struct rpc_message msg = {
9796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9797 .rpc_argp = &data->args,
9798 .rpc_resp = &data->res,
9799 .rpc_cred = data->cred,
9800 };
9801 struct rpc_task_setup task_setup_data = {
9802 .task = &data->task,
9803 .rpc_client = NFS_CLIENT(data->args.inode),
9804 .rpc_message = &msg,
9805 .callback_ops = &nfs4_layoutcommit_ops,
9806 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009807 };
9808 struct rpc_task *task;
9809 int status = 0;
9810
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009811 dprintk("NFS: initiating layoutcommit call. sync %d "
9812 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009813 data->args.lastbytewritten,
9814 data->args.inode->i_ino);
9815
Trond Myklebust472e2592015-02-05 16:50:30 -05009816 if (!sync) {
9817 data->inode = nfs_igrab_and_active(data->args.inode);
9818 if (data->inode == NULL) {
9819 nfs4_layoutcommit_release(data);
9820 return -EAGAIN;
9821 }
9822 task_setup_data.flags = RPC_TASK_ASYNC;
9823 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009824 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009825 task = rpc_run_task(&task_setup_data);
9826 if (IS_ERR(task))
9827 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009828 if (sync)
9829 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009830 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009831 dprintk("%s: status %d\n", __func__, status);
9832 rpc_put_task(task);
9833 return status;
9834}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009835
Trond Myklebust302fad72019-02-18 13:32:38 -05009836/*
Andy Adamson97431202013-08-08 10:57:56 -04009837 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9838 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9839 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009840static int
9841_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009842 struct nfs_fsinfo *info,
9843 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009844{
9845 struct nfs41_secinfo_no_name_args args = {
9846 .style = SECINFO_STYLE_CURRENT_FH,
9847 };
9848 struct nfs4_secinfo_res res = {
9849 .flavors = flavors,
9850 };
9851 struct rpc_message msg = {
9852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9853 .rpc_argp = &args,
9854 .rpc_resp = &res,
9855 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009856 struct nfs4_call_sync_data data = {
9857 .seq_server = server,
9858 .seq_args = &args.seq_args,
9859 .seq_res = &res.seq_res,
9860 };
9861 struct rpc_task_setup task_setup = {
9862 .rpc_client = server->client,
9863 .rpc_message = &msg,
9864 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9865 .callback_data = &data,
9866 .flags = RPC_TASK_NO_ROUND_ROBIN,
9867 };
NeilBrowna52458b2018-12-03 11:30:31 +11009868 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009869 int status;
9870
9871 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009872 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009873
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009874 cred = nfs4_get_clid_cred(server->nfs_client);
9875 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009876 }
9877
9878 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009879 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9880 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009881 dprintk("<-- %s status=%d\n", __func__, status);
9882
NeilBrowna52458b2018-12-03 11:30:31 +11009883 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009884
9885 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009886}
9887
9888static int
9889nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9890 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9891{
Trond Myklebust0688e642019-04-07 13:59:09 -04009892 struct nfs4_exception exception = {
9893 .interruptible = true,
9894 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009895 int err;
9896 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009897 /* first try using integrity protection */
9898 err = -NFS4ERR_WRONGSEC;
9899
9900 /* try to use integrity protection with machine cred */
9901 if (_nfs4_is_integrity_protected(server->nfs_client))
9902 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9903 flavors, true);
9904
9905 /*
9906 * if unable to use integrity protection, or SECINFO with
9907 * integrity protection returns NFS4ERR_WRONGSEC (which is
9908 * disallowed by spec, but exists in deployed servers) use
9909 * the current filesystem's rpc_client and the user cred.
9910 */
9911 if (err == -NFS4ERR_WRONGSEC)
9912 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9913 flavors, false);
9914
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009915 switch (err) {
9916 case 0:
9917 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009918 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009919 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009920 default:
9921 err = nfs4_handle_exception(server, err, &exception);
9922 }
9923 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009924out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009925 return err;
9926}
9927
9928static int
9929nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9930 struct nfs_fsinfo *info)
9931{
9932 int err;
9933 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009934 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009935 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009936 struct nfs4_secinfo4 *secinfo;
9937 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009938
9939 page = alloc_page(GFP_KERNEL);
9940 if (!page) {
9941 err = -ENOMEM;
9942 goto out;
9943 }
9944
9945 flavors = page_address(page);
9946 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9947
9948 /*
9949 * Fall back on "guess and check" method if
9950 * the server doesn't support SECINFO_NO_NAME
9951 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009952 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009953 err = nfs4_find_root_sec(server, fhandle, info);
9954 goto out_freepage;
9955 }
9956 if (err)
9957 goto out_freepage;
9958
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009959 for (i = 0; i < flavors->num_flavors; i++) {
9960 secinfo = &flavors->flavors[i];
9961
9962 switch (secinfo->flavor) {
9963 case RPC_AUTH_NULL:
9964 case RPC_AUTH_UNIX:
9965 case RPC_AUTH_GSS:
9966 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9967 &secinfo->flavor_info);
9968 break;
9969 default:
9970 flavor = RPC_AUTH_MAXFLAVOR;
9971 break;
9972 }
9973
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009974 if (!nfs_auth_info_match(&server->auth_info, flavor))
9975 flavor = RPC_AUTH_MAXFLAVOR;
9976
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009977 if (flavor != RPC_AUTH_MAXFLAVOR) {
9978 err = nfs4_lookup_root_sec(server, fhandle,
9979 info, flavor);
9980 if (!err)
9981 break;
9982 }
9983 }
9984
9985 if (flavor == RPC_AUTH_MAXFLAVOR)
9986 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009987
9988out_freepage:
9989 put_page(page);
9990 if (err == -EACCES)
9991 return -EPERM;
9992out:
9993 return err;
9994}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009995
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009996static int _nfs41_test_stateid(struct nfs_server *server,
9997 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009998 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009999{
10000 int status;
10001 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -050010002 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -040010003 };
10004 struct nfs41_test_stateid_res res;
10005 struct rpc_message msg = {
10006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10007 .rpc_argp = &args,
10008 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010009 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -040010010 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010011 struct rpc_clnt *rpc_client = server->client;
10012
10013 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10014 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -050010015
Chuck Lever38527b12012-07-11 16:30:23 -040010016 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -040010017 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010018 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -040010019 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -040010020 if (status != NFS_OK) {
10021 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -040010022 return status;
Chuck Lever38527b12012-07-11 16:30:23 -040010023 }
10024 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -040010025 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -040010026}
10027
Trond Myklebust43912bb2016-09-22 13:38:56 -040010028static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10029 int err, struct nfs4_exception *exception)
10030{
10031 exception->retry = 0;
10032 switch(err) {
10033 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -040010034 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -040010035 nfs4_handle_exception(server, err, exception);
10036 break;
10037 case -NFS4ERR_BADSESSION:
10038 case -NFS4ERR_BADSLOT:
10039 case -NFS4ERR_BAD_HIGH_SLOT:
10040 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10041 case -NFS4ERR_DEADSESSION:
10042 nfs4_do_handle_exception(server, err, exception);
10043 }
10044}
10045
Chuck Lever38527b12012-07-11 16:30:23 -040010046/**
10047 * nfs41_test_stateid - perform a TEST_STATEID operation
10048 *
10049 * @server: server / transport on which to perform the operation
10050 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010051 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -040010052 *
10053 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10054 * Otherwise a negative NFS4ERR value is returned if the operation
10055 * failed or the state ID is not currently valid.
10056 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010057static int nfs41_test_stateid(struct nfs_server *server,
10058 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010059 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010060{
Trond Myklebust0688e642019-04-07 13:59:09 -040010061 struct nfs4_exception exception = {
10062 .interruptible = true,
10063 };
Bryan Schumaker7d974792011-06-02 14:59:08 -040010064 int err;
10065 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010066 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -040010067 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -040010068 } while (exception.retry);
10069 return err;
10070}
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010071
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010072struct nfs_free_stateid_data {
10073 struct nfs_server *server;
10074 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010075 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010076};
10077
10078static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10079{
10080 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -050010081 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010082 &data->args.seq_args,
10083 &data->res.seq_res,
10084 task);
10085}
10086
10087static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10088{
10089 struct nfs_free_stateid_data *data = calldata;
10090
10091 nfs41_sequence_done(task, &data->res.seq_res);
10092
10093 switch (task->tk_status) {
10094 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +100010095 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010096 rpc_restart_call_prepare(task);
10097 }
10098}
10099
10100static void nfs41_free_stateid_release(void *calldata)
10101{
10102 kfree(calldata);
10103}
10104
Trond Myklebust17f26b12013-08-21 15:48:42 -040010105static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010106 .rpc_call_prepare = nfs41_free_stateid_prepare,
10107 .rpc_call_done = nfs41_free_stateid_done,
10108 .rpc_release = nfs41_free_stateid_release,
10109};
10110
Anna Schumaker2f261022018-05-15 13:03:39 -040010111/**
10112 * nfs41_free_stateid - perform a FREE_STATEID operation
10113 *
10114 * @server: server / transport on which to perform the operation
10115 * @stateid: state ID to release
10116 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010117 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010118 *
10119 * Note: this function is always asynchronous.
10120 */
10121static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010122 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010123 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010124 bool privileged)
10125{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010126 struct rpc_message msg = {
10127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010128 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010129 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010130 struct rpc_task_setup task_setup = {
10131 .rpc_client = server->client,
10132 .rpc_message = &msg,
10133 .callback_ops = &nfs41_free_stateid_ops,
10134 .flags = RPC_TASK_ASYNC,
10135 };
10136 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010137 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010138
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010139 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10140 &task_setup.rpc_client, &msg);
10141
Chuck Lever38527b12012-07-11 16:30:23 -040010142 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010143 data = kmalloc(sizeof(*data), GFP_NOFS);
10144 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010145 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010146 data->server = server;
10147 nfs4_stateid_copy(&data->args.stateid, stateid);
10148
10149 task_setup.callback_data = data;
10150
10151 msg.rpc_argp = &data->args;
10152 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010153 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010154 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010155 if (IS_ERR(task))
10156 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010157 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010158 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010159}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010160
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010161static void
10162nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010163{
NeilBrowna52458b2018-12-03 11:30:31 +110010164 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010165
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010166 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010167 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010168}
10169
Trond Myklebust36281ca2012-03-04 18:13:56 -050010170static bool nfs41_match_stateid(const nfs4_stateid *s1,
10171 const nfs4_stateid *s2)
10172{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010173 if (s1->type != s2->type)
10174 return false;
10175
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010176 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010177 return false;
10178
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010179 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010180 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010181
Anna Schumaker045c5512017-01-11 16:59:48 -050010182 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010183}
10184
Andy Adamson557134a2009-04-01 09:21:53 -040010185#endif /* CONFIG_NFS_V4_1 */
10186
Trond Myklebust36281ca2012-03-04 18:13:56 -050010187static bool nfs4_match_stateid(const nfs4_stateid *s1,
10188 const nfs4_stateid *s2)
10189{
Trond Myklebustf597c532012-03-04 18:13:56 -050010190 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010191}
10192
10193
Trond Myklebust17280172012-03-11 13:11:00 -040010194static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010195 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010196 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197 .recover_open = nfs4_open_reclaim,
10198 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010199 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010200 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010201};
10202
Andy Adamson591d71c2009-04-01 09:22:47 -040010203#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010204static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010205 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10206 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10207 .recover_open = nfs4_open_reclaim,
10208 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010209 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010210 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010211 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010212};
10213#endif /* CONFIG_NFS_V4_1 */
10214
Trond Myklebust17280172012-03-11 13:11:00 -040010215static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010216 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010217 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010218 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010219 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010220 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010221};
10222
Andy Adamson591d71c2009-04-01 09:22:47 -040010223#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010224static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010225 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10226 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010227 .recover_open = nfs41_open_expired,
10228 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010229 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010230};
10231#endif /* CONFIG_NFS_V4_1 */
10232
Trond Myklebust17280172012-03-11 13:11:00 -040010233static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010234 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010235 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010236 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010237};
10238
10239#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010240static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010241 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010242 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010243 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010244};
10245#endif
10246
Chuck Leverec011fe2013-10-17 14:12:39 -040010247static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010248 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010249 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010250};
10251
10252#if defined(CONFIG_NFS_V4_1)
10253static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010254 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010255 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010256};
10257#endif /* CONFIG_NFS_V4_1 */
10258
Trond Myklebust97dc1352010-06-16 09:52:26 -040010259static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10260 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010261 .init_caps = NFS_CAP_READDIRPLUS
10262 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010263 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010264 .init_client = nfs40_init_client,
10265 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010266 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010267 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010268 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010269 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010270 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010271 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010272 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10273 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10274 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010275 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010276};
10277
10278#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010279static struct nfs_seqid *
10280nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10281{
10282 return NULL;
10283}
10284
Trond Myklebust97dc1352010-06-16 09:52:26 -040010285static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10286 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010287 .init_caps = NFS_CAP_READDIRPLUS
10288 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010289 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010290 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010291 | NFS_CAP_ATOMIC_OPEN_V1
10292 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010293 .init_client = nfs41_init_client,
10294 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010295 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010296 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010297 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010298 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010299 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010300 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010301 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010302 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10303 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10304 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010305 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010306};
10307#endif
10308
Steve Dickson42c2c422013-05-22 12:50:38 -040010309#if defined(CONFIG_NFS_V4_2)
10310static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10311 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010312 .init_caps = NFS_CAP_READDIRPLUS
10313 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010314 | NFS_CAP_POSIX_LOCK
10315 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010316 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010317 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010318 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010319 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010320 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010321 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010322 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010323 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010324 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010325 | NFS_CAP_CLONE
Anna Schumakerc5675522014-05-28 13:41:22 -040010326 | NFS_CAP_LAYOUTERROR
10327 | NFS_CAP_READ_PLUS,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010328 .init_client = nfs41_init_client,
10329 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010330 .match_stateid = nfs41_match_stateid,
10331 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010332 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010333 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010334 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010335 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010336 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010337 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10338 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10339 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010340 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010341};
10342#endif
10343
Trond Myklebust97dc1352010-06-16 09:52:26 -040010344const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10345 [0] = &nfs_v4_0_minor_ops,
10346#if defined(CONFIG_NFS_V4_1)
10347 [1] = &nfs_v4_1_minor_ops,
10348#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010349#if defined(CONFIG_NFS_V4_2)
10350 [2] = &nfs_v4_2_minor_ops,
10351#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010352};
10353
Trond Myklebust13997822016-07-24 17:10:52 -040010354static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010355{
Frank van der Linden012a2112020-06-23 22:39:03 +000010356 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010357
10358 error = generic_listxattr(dentry, list, size);
10359 if (error < 0)
10360 return error;
10361 if (list) {
10362 list += error;
10363 size -= error;
10364 }
10365
10366 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10367 if (error2 < 0)
10368 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010369
10370 if (list) {
10371 list += error2;
10372 size -= error2;
10373 }
10374
10375 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10376 if (error3 < 0)
10377 return error3;
10378
10379 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010380}
10381
Trond Myklebust17f26b12013-08-21 15:48:42 -040010382static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010383 .create = nfs_create,
10384 .lookup = nfs_lookup,
10385 .atomic_open = nfs_atomic_open,
10386 .link = nfs_link,
10387 .unlink = nfs_unlink,
10388 .symlink = nfs_symlink,
10389 .mkdir = nfs_mkdir,
10390 .rmdir = nfs_rmdir,
10391 .mknod = nfs_mknod,
10392 .rename = nfs_rename,
10393 .permission = nfs_permission,
10394 .getattr = nfs_getattr,
10395 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010396 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010397};
10398
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010399static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010400 .permission = nfs_permission,
10401 .getattr = nfs_getattr,
10402 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010403 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010404};
10405
David Howells509de812006-08-22 20:06:11 -040010406const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010407 .version = 4, /* protocol version */
10408 .dentry_ops = &nfs4_dentry_operations,
10409 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010410 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010411 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010413 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010414 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010415 .getattr = nfs4_proc_getattr,
10416 .setattr = nfs4_proc_setattr,
10417 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010418 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419 .access = nfs4_proc_access,
10420 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010421 .create = nfs4_proc_create,
10422 .remove = nfs4_proc_remove,
10423 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010424 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010425 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010426 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010427 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010428 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010429 .link = nfs4_proc_link,
10430 .symlink = nfs4_proc_symlink,
10431 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010432 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010433 .readdir = nfs4_proc_readdir,
10434 .mknod = nfs4_proc_mknod,
10435 .statfs = nfs4_proc_statfs,
10436 .fsinfo = nfs4_proc_fsinfo,
10437 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010438 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010439 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010440 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010441 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010442 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010443 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010444 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010445 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010446 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010447 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010448 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010449 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010450 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010451 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010452 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010453 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010454 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010455 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010456 .create_server = nfs4_create_server,
10457 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010458};
10459
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010460static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010461 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010462 .list = nfs4_xattr_list_nfs4_acl,
10463 .get = nfs4_xattr_get_nfs4_acl,
10464 .set = nfs4_xattr_set_nfs4_acl,
10465};
10466
Frank van der Linden012a2112020-06-23 22:39:03 +000010467#ifdef CONFIG_NFS_V4_2
10468static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10469 .prefix = XATTR_USER_PREFIX,
10470 .get = nfs4_xattr_get_nfs4_user,
10471 .set = nfs4_xattr_set_nfs4_user,
10472};
10473#endif
10474
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010475const struct xattr_handler *nfs4_xattr_handlers[] = {
10476 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010477#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10478 &nfs4_xattr_nfs4_label_handler,
10479#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010480#ifdef CONFIG_NFS_V4_2
10481 &nfs4_xattr_nfs4_user_handler,
10482#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010483 NULL
10484};