blob: 8f1fcf87b80ca3f10954e83164b4e4b81f890685 [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
Anna Schumakerc5675522014-05-28 13:41:22 -040074#ifdef CONFIG_NFS_V4_2
75#include "nfs42.h"
76#endif /* CONFIG_NFS_V4_2 */
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define NFSDBG_FACILITY NFSDBG_PROC
79
Trond Myklebust30846df2018-04-07 13:44:28 -040080#define NFS4_BITMASK_SZ 3
81
Trond Myklebust2066fe82006-09-15 08:30:46 -040082#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define NFS4_POLL_RETRY_MAX (15*HZ)
84
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020085/* file attributes which can be mapped to nfs attributes */
86#define NFS4_VALID_ATTRS (ATTR_MODE \
87 | ATTR_UID \
88 | ATTR_GID \
89 | ATTR_SIZE \
90 | ATTR_ATIME \
91 | ATTR_MTIME \
92 | ATTR_CTIME \
93 | ATTR_ATIME_SET \
94 | ATTR_MTIME_SET)
95
Trond Myklebustcdd4e682006-01-03 09:55:12 +010096struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080097static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050099static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -0400100static 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 +1100101static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -0400102 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +1100103 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -0400104 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400105#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400106static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100107 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400108 struct nfs4_slot *slot,
109 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400110static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100111 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400112static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100113 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400114#endif
Trond Myklebustce2b4702021-03-25 18:15:36 -0400115static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ],
116 const __u32 *src, struct inode *inode,
117 struct nfs_server *server,
118 struct nfs4_label *label);
David Quigleyaa9c2662013-05-22 12:50:44 -0400119
120#ifdef CONFIG_NFS_V4_SECURITY_LABEL
121static inline struct nfs4_label *
122nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
123 struct iattr *sattr, struct nfs4_label *label)
124{
125 int err;
126
127 if (label == NULL)
128 return NULL;
129
130 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
131 return NULL;
132
David Quigleyaa9c2662013-05-22 12:50:44 -0400133 err = security_dentry_init_security(dentry, sattr->ia_mode,
134 &dentry->d_name, (void **)&label->label, &label->len);
135 if (err == 0)
136 return label;
137
138 return NULL;
139}
140static inline void
141nfs4_label_release_security(struct nfs4_label *label)
142{
143 if (label)
144 security_release_secctx(label->label, label->len);
145}
146static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
147{
148 if (label)
149 return server->attr_bitmask;
150
151 return server->attr_bitmask_nl;
152}
153#else
154static inline struct nfs4_label *
155nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
156 struct iattr *sattr, struct nfs4_label *l)
157{ return NULL; }
158static inline void
159nfs4_label_release_security(struct nfs4_label *label)
160{ return; }
161static inline u32 *
162nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
163{ return server->attr_bitmask; }
164#endif
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500167static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Trond Myklebust52567b02009-10-23 14:46:42 -0400169 if (err >= -1000)
170 return err;
171 switch (err) {
172 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500173 case -NFS4ERR_LAYOUTTRYLATER:
174 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400175 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000176 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400177 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000178 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800179 case -NFS4ERR_BADOWNER:
180 case -NFS4ERR_BADNAME:
181 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400182 case -NFS4ERR_SHARE_DENIED:
183 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400184 case -NFS4ERR_MINOR_VERS_MISMATCH:
185 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400186 case -NFS4ERR_FILE_OPEN:
187 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400188 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700190 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400193 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/*
197 * This is our standard bitmap for GETATTR requests.
198 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400199const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 FATTR4_WORD0_TYPE
201 | FATTR4_WORD0_CHANGE
202 | FATTR4_WORD0_SIZE
203 | FATTR4_WORD0_FSID
204 | FATTR4_WORD0_FILEID,
205 FATTR4_WORD1_MODE
206 | FATTR4_WORD1_NUMLINKS
207 | FATTR4_WORD1_OWNER
208 | FATTR4_WORD1_OWNER_GROUP
209 | FATTR4_WORD1_RAWDEV
210 | FATTR4_WORD1_SPACE_USED
211 | FATTR4_WORD1_TIME_ACCESS
212 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400213 | FATTR4_WORD1_TIME_MODIFY
214 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400215#ifdef CONFIG_NFS_V4_SECURITY_LABEL
216 FATTR4_WORD2_SECURITY_LABEL
217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
Trond Myklebust1549210f2012-06-05 09:16:47 -0400220static const u32 nfs4_pnfs_open_bitmap[3] = {
221 FATTR4_WORD0_TYPE
222 | FATTR4_WORD0_CHANGE
223 | FATTR4_WORD0_SIZE
224 | FATTR4_WORD0_FSID
225 | FATTR4_WORD0_FILEID,
226 FATTR4_WORD1_MODE
227 | FATTR4_WORD1_NUMLINKS
228 | FATTR4_WORD1_OWNER
229 | FATTR4_WORD1_OWNER_GROUP
230 | FATTR4_WORD1_RAWDEV
231 | FATTR4_WORD1_SPACE_USED
232 | FATTR4_WORD1_TIME_ACCESS
233 | FATTR4_WORD1_TIME_METADATA
234 | FATTR4_WORD1_TIME_MODIFY,
235 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500236#ifdef CONFIG_NFS_V4_SECURITY_LABEL
237 | FATTR4_WORD2_SECURITY_LABEL
238#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400239};
240
Andy Adamsone23008e2012-10-02 21:07:32 -0400241static const u32 nfs4_open_noattr_bitmap[3] = {
242 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400243 | FATTR4_WORD0_FILEID,
244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 FATTR4_WORD0_FILES_AVAIL
248 | FATTR4_WORD0_FILES_FREE
249 | FATTR4_WORD0_FILES_TOTAL,
250 FATTR4_WORD1_SPACE_AVAIL
251 | FATTR4_WORD1_SPACE_FREE
252 | FATTR4_WORD1_SPACE_TOTAL
253};
254
David Quigleya09df2c2013-05-22 12:50:41 -0400255const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 FATTR4_WORD0_MAXLINK
257 | FATTR4_WORD0_MAXNAME,
258 0
259};
260
Fred Isamandae100c2011-07-30 20:52:37 -0400261const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 | FATTR4_WORD0_MAXREAD
263 | FATTR4_WORD0_MAXWRITE
264 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700265 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400266 | FATTR4_WORD1_FS_LAYOUT_TYPES,
267 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800268 | FATTR4_WORD2_CLONE_BLKSIZE
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000269 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270};
271
David Quigleya09df2c2013-05-22 12:50:41 -0400272const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500273 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400274 | FATTR4_WORD0_SIZE
275 | FATTR4_WORD0_FSID
276 | FATTR4_WORD0_FILEID
277 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500278 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400279 | FATTR4_WORD1_OWNER_GROUP
280 | FATTR4_WORD1_RAWDEV
281 | FATTR4_WORD1_SPACE_USED
282 | FATTR4_WORD1_TIME_ACCESS
283 | FATTR4_WORD1_TIME_METADATA
284 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400285 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400286};
287
Trond Myklebust30846df2018-04-07 13:44:28 -0400288static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
289 struct inode *inode)
290{
291 unsigned long cache_validity;
292
293 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
294 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
295 return;
296
297 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
298 if (!(cache_validity & NFS_INO_REVAL_FORCED))
299 cache_validity &= ~(NFS_INO_INVALID_CHANGE
300 | NFS_INO_INVALID_SIZE);
301
302 if (!(cache_validity & NFS_INO_INVALID_SIZE))
303 dst[0] &= ~FATTR4_WORD0_SIZE;
304
305 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
306 dst[0] &= ~FATTR4_WORD0_CHANGE;
307}
308
309static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
310 const __u32 *src, struct inode *inode)
311{
312 nfs4_bitmap_copy_adjust(dst, src, inode);
313}
314
Al Virobc4785c2006-10-19 23:28:51 -0700315static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct nfs4_readdir_arg *readdir)
317{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400318 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700319 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000322 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
324 return;
325 }
326
327 readdir->cookie = 0;
328 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
329 if (cookie == 2)
330 return;
331
332 /*
333 * NFSv4 servers do not return entries for '.' and '..'
334 * Therefore, we fake these entries here. We let '.'
335 * have cookie 0 and '..' have cookie 1. Note that
336 * when talking to the server, we always send cookie 0
337 * instead of 1 or 2.
338 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800339 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 if (cookie == 0) {
342 *p++ = xdr_one; /* next */
343 *p++ = xdr_zero; /* cookie, first word */
344 *p++ = xdr_one; /* cookie, second word */
345 *p++ = xdr_one; /* entry len */
346 memcpy(p, ".\0\0\0", 4); /* entry */
347 p++;
348 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400349 *p++ = htonl(attrs); /* bitmap */
350 *p++ = htonl(12); /* attribute buffer length */
351 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000352 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
355 *p++ = xdr_one; /* next */
356 *p++ = xdr_zero; /* cookie, first word */
357 *p++ = xdr_two; /* cookie, second word */
358 *p++ = xdr_two; /* entry len */
359 memcpy(p, "..\0\0", 4); /* entry */
360 p++;
361 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400362 *p++ = htonl(attrs); /* bitmap */
363 *p++ = htonl(12); /* attribute buffer length */
364 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000365 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 readdir->pgbase = (char *)p - (char *)start;
368 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800369 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Trond Myklebust26d36302016-09-22 13:39:05 -0400372static void nfs4_test_and_free_stateid(struct nfs_server *server,
373 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100374 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400375{
376 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
377
378 ops->test_and_free_expired(server, stateid, cred);
379}
380
381static void __nfs4_free_revoked_stateid(struct nfs_server *server,
382 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100383 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400384{
385 stateid->type = NFS4_REVOKED_STATEID_TYPE;
386 nfs4_test_and_free_stateid(server, stateid, cred);
387}
388
389static void nfs4_free_revoked_stateid(struct nfs_server *server,
390 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100391 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400392{
393 nfs4_stateid tmp;
394
395 nfs4_stateid_copy(&tmp, stateid);
396 __nfs4_free_revoked_stateid(server, &tmp, cred);
397}
398
NeilBrown8478eaa2014-09-18 16:09:27 +1000399static long nfs4_update_delay(long *timeout)
400{
401 long ret;
402 if (!timeout)
403 return NFS4_POLL_RETRY_MAX;
404 if (*timeout <= 0)
405 *timeout = NFS4_POLL_RETRY_MIN;
406 if (*timeout > NFS4_POLL_RETRY_MAX)
407 *timeout = NFS4_POLL_RETRY_MAX;
408 ret = *timeout;
409 *timeout <<= 1;
410 return ret;
411}
412
Trond Myklebust0688e642019-04-07 13:59:09 -0400413static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500414{
Trond Myklebust65de8722008-12-23 15:21:44 -0500415 might_sleep();
416
NeilBrown8478eaa2014-09-18 16:09:27 +1000417 freezable_schedule_timeout_killable_unsafe(
418 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400419 if (!__fatal_signal_pending(current))
420 return 0;
421 return -EINTR;
422}
423
424static int nfs4_delay_interruptible(long *timeout)
425{
426 might_sleep();
427
He Zhe59679d92020-07-06 17:52:24 +0800428 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400429 if (!signal_pending(current))
430 return 0;
431 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
432}
433
434static int nfs4_delay(long *timeout, bool interruptible)
435{
436 if (interruptible)
437 return nfs4_delay_interruptible(timeout);
438 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500439}
440
Trond Myklebust50c80002019-07-11 19:02:18 -0400441static const nfs4_stateid *
442nfs4_recoverable_stateid(const nfs4_stateid *stateid)
443{
444 if (!stateid)
445 return NULL;
446 switch (stateid->type) {
447 case NFS4_OPEN_STATEID_TYPE:
448 case NFS4_LOCK_STATEID_TYPE:
449 case NFS4_DELEGATION_STATEID_TYPE:
450 return stateid;
451 default:
452 break;
453 }
454 return NULL;
455}
456
Trond Myklebust65de8722008-12-23 15:21:44 -0500457/* This is the error handling routine for processes that are allowed
458 * to sleep.
459 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400460static int nfs4_do_handle_exception(struct nfs_server *server,
461 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500462{
463 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500464 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400465 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500466 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500467 int ret = errorcode;
468
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400469 exception->delay = 0;
470 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500471 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400472
Trond Myklebust50c80002019-07-11 19:02:18 -0400473 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400474 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400475 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400476
Trond Myklebust65de8722008-12-23 15:21:44 -0500477 switch(errorcode) {
478 case 0:
479 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400480 case -NFS4ERR_BADHANDLE:
481 case -ESTALE:
482 if (inode != NULL && S_ISREG(inode->i_mode))
483 pnfs_destroy_layout(NFS_I(inode));
484 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400485 case -NFS4ERR_DELEG_REVOKED:
486 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400487 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400488 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400489 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400490 if (inode != NULL && stateid != NULL) {
491 nfs_inode_find_state_and_recover(inode,
492 stateid);
493 goto wait_on_recovery;
494 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500495 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400496 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400497 if (inode) {
498 int err;
499
500 err = nfs_async_inode_return_delegation(inode,
501 stateid);
502 if (err == 0)
503 goto wait_on_recovery;
504 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
505 exception->retry = 1;
506 break;
507 }
508 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500509 if (state == NULL)
510 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400511 ret = nfs4_schedule_stateid_recovery(server, state);
512 if (ret < 0)
513 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500514 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500515 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500516 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500517 nfs4_schedule_lease_recovery(clp);
518 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400519 case -NFS4ERR_MOVED:
520 ret = nfs4_schedule_migration_recovery(server);
521 if (ret < 0)
522 break;
523 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400524 case -NFS4ERR_LEASE_MOVED:
525 nfs4_schedule_lease_moved_recovery(clp);
526 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500527#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400528 case -NFS4ERR_BADSESSION:
529 case -NFS4ERR_BADSLOT:
530 case -NFS4ERR_BAD_HIGH_SLOT:
531 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
532 case -NFS4ERR_DEADSESSION:
533 case -NFS4ERR_SEQ_FALSE_RETRY:
534 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100535 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400536 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500537#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500538 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500539 if (exception->timeout > HZ) {
540 /* We have retried a decent amount, time to
541 * fail
542 */
543 ret = -EBUSY;
544 break;
545 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500546 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500547 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400548 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500549 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400550 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400551 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400552 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400553 exception->delay = 1;
554 return 0;
555
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400556 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500557 case -NFS4ERR_OLD_STATEID:
558 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800559 break;
560 case -NFS4ERR_BADOWNER:
561 /* The following works around a Linux server bug! */
562 case -NFS4ERR_BADNAME:
563 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
564 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
565 exception->retry = 1;
566 printk(KERN_WARNING "NFS: v4 server %s "
567 "does not accept raw "
568 "uid/gids. "
569 "Reenabling the idmapper.\n",
570 server->nfs_client->cl_hostname);
571 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500572 }
573 /* We failed to handle the error */
574 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500575wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400576 exception->recovering = 1;
577 return 0;
578}
579
580/* This is the error handling routine for processes that are allowed
581 * to sleep.
582 */
583int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
584{
585 struct nfs_client *clp = server->nfs_client;
586 int ret;
587
588 ret = nfs4_do_handle_exception(server, errorcode, exception);
589 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400590 ret = nfs4_delay(&exception->timeout,
591 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400592 goto out_retry;
593 }
594 if (exception->recovering) {
Trond Myklebustb4651ce2021-06-01 11:10:05 -0400595 if (exception->task_is_privileged)
596 return -EDEADLOCK;
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400597 ret = nfs4_wait_clnt_recover(clp);
598 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
599 return -EIO;
600 goto out_retry;
601 }
602 return ret;
603out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500604 if (ret == 0)
605 exception->retry = 1;
606 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500607}
608
Trond Myklebust037fc982015-09-20 15:51:00 -0400609static int
610nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
611 int errorcode, struct nfs4_exception *exception)
612{
613 struct nfs_client *clp = server->nfs_client;
614 int ret;
615
616 ret = nfs4_do_handle_exception(server, errorcode, exception);
617 if (exception->delay) {
618 rpc_delay(task, nfs4_update_delay(&exception->timeout));
619 goto out_retry;
620 }
621 if (exception->recovering) {
Trond Myklebustb4651ce2021-06-01 11:10:05 -0400622 if (exception->task_is_privileged)
623 return -EDEADLOCK;
Trond Myklebust037fc982015-09-20 15:51:00 -0400624 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
625 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
626 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
627 goto out_retry;
628 }
629 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
630 ret = -EIO;
631 return ret;
632out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500633 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400634 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500635 /*
636 * For NFS4ERR_MOVED, the client transport will need to
637 * be recomputed after migration recovery has completed.
638 */
639 if (errorcode == -NFS4ERR_MOVED)
640 rpc_task_release_transport(task);
641 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400642 return ret;
643}
644
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400645int
Trond Myklebust037fc982015-09-20 15:51:00 -0400646nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
647 struct nfs4_state *state, long *timeout)
648{
649 struct nfs4_exception exception = {
650 .state = state,
651 };
652
653 if (task->tk_status >= 0)
654 return 0;
655 if (timeout)
656 exception.timeout = *timeout;
657 task->tk_status = nfs4_async_handle_exception(task, server,
658 task->tk_status,
659 &exception);
660 if (exception.delay && timeout)
661 *timeout = exception.timeout;
662 if (exception.retry)
663 return -EAGAIN;
664 return 0;
665}
666
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400667/*
668 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
669 * or 'false' otherwise.
670 */
671static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
672{
673 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500674 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400675}
Trond Myklebust65de8722008-12-23 15:21:44 -0500676
Trond Myklebust452e9352010-07-31 14:29:06 -0400677static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 spin_lock(&clp->cl_lock);
680 if (time_before(clp->cl_last_renewal,timestamp))
681 clp->cl_last_renewal = timestamp;
682 spin_unlock(&clp->cl_lock);
683}
684
Trond Myklebust452e9352010-07-31 14:29:06 -0400685static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
686{
Trond Myklebustbe824162015-07-05 14:50:46 -0400687 struct nfs_client *clp = server->nfs_client;
688
689 if (!nfs4_has_session(clp))
690 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400691}
692
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400693struct nfs4_call_sync_data {
694 const struct nfs_server *seq_server;
695 struct nfs4_sequence_args *seq_args;
696 struct nfs4_sequence_res *seq_res;
697};
698
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800699void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400700 struct nfs4_sequence_res *res, int cache_reply,
701 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400702{
703 args->sa_slot = NULL;
704 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400705 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400706
707 res->sr_slot = NULL;
708}
709
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400710static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400711{
712 struct nfs4_slot *slot = res->sr_slot;
713 struct nfs4_slot_table *tbl;
714
Chuck Lever3bd23842013-08-09 12:49:19 -0400715 tbl = slot->table;
716 spin_lock(&tbl->slot_tbl_lock);
717 if (!nfs41_wake_and_assign_slot(tbl, slot))
718 nfs4_free_slot(tbl, slot);
719 spin_unlock(&tbl->slot_tbl_lock);
720
721 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400722}
723
724static int nfs40_sequence_done(struct rpc_task *task,
725 struct nfs4_sequence_res *res)
726{
727 if (res->sr_slot != NULL)
728 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400729 return 1;
730}
731
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400732#if defined(CONFIG_NFS_V4_1)
733
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400734static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400735{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500736 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400737 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500738 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400739
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400740 if (!slot)
741 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500742 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500743 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500744
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400745 /* Bump the slot sequence number */
746 if (slot->seq_done)
747 slot->seq_nr++;
748 slot->seq_done = 0;
749
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500750 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500751 /* Be nice to the server: try to ensure that the last transmitted
752 * value for highest_user_slotid <= target_highest_slotid
753 */
754 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
755 send_new_highest_used_slotid = true;
756
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500757 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500758 send_new_highest_used_slotid = false;
759 goto out_unlock;
760 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500761 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500762
763 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
764 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500765out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500766 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500767 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400768 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400769 if (waitqueue_active(&tbl->slot_waitq))
770 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400771}
772
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400773static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
774{
775 nfs41_release_slot(res->sr_slot);
776 res->sr_slot = NULL;
777}
778
Trond Myklebust3453d572018-06-20 17:53:34 -0400779static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
780 u32 seqnr)
781{
782 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
783 slot->seq_nr_highest_sent = seqnr;
784}
785static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
786 u32 seqnr)
787{
788 slot->seq_nr_highest_sent = seqnr;
789 slot->seq_nr_last_acked = seqnr;
790}
791
Anna Schumaker913fadc2020-07-08 10:33:40 -0400792static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
793 struct nfs4_slot *slot)
794{
795 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
796 if (!IS_ERR(task))
797 rpc_put_task_async(task);
798}
799
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400800static int nfs41_sequence_process(struct rpc_task *task,
801 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400802{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500803 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500804 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400805 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100806 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500807 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400808
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500809 if (slot == NULL)
810 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400811 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500812 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400813 goto out;
814
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500815 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400816 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500817
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400818 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100819
820 status = res->sr_status;
821 if (task->tk_status == -NFS4ERR_DEADSESSION)
822 status = -NFS4ERR_DEADSESSION;
823
Andy Adamson691daf32009-12-04 15:55:39 -0500824 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100825 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400826 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400827 /* Mark this sequence number as having been acked */
828 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400829 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400830 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500831 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500832 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400833 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
834 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500835 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400836 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500837 case 1:
838 /*
839 * sr_status remains 1 if an RPC level error occurred.
840 * The server may or may not have processed the sequence
841 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500842 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400843 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
844 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500845 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400846 case -NFS4ERR_DELAY:
847 /* The server detected a resend of the RPC call and
848 * returned NFS4ERR_DELAY as per Section 2.10.6.2
849 * of RFC5661.
850 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500851 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400852 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500853 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500854 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400855 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400856 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400857 case -NFS4ERR_RETRY_UNCACHED_REP:
858 case -NFS4ERR_SEQ_FALSE_RETRY:
859 /*
860 * The server thinks we tried to replay a request.
861 * Retry the call after bumping the sequence ID.
862 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400863 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400864 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500865 case -NFS4ERR_BADSLOT:
866 /*
867 * The slot id we used was probably retired. Try again
868 * using a different slot id.
869 */
Trond Myklebust99589102018-06-09 12:50:50 -0400870 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400871 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500872 goto retry_nowait;
873 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400874 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500875 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400876 * Were one or more calls using this slot interrupted?
877 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400878 * transmitted slot sequence number may be too high. However,
879 * if the server did receive the request then it might
880 * accidentally give us a reply with a mismatched operation.
881 * We can sort this out by sending a lone sequence operation
882 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500883 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400884 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
885 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400886 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
887 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
888 res->sr_slot = NULL;
889 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500890 goto retry_nowait;
891 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400892 /*
893 * RFC5661:
894 * A retry might be sent while the original request is
895 * still in progress on the replier. The replier SHOULD
896 * deal with the issue by returning NFS4ERR_DELAY as the
897 * reply to SEQUENCE or CB_SEQUENCE operation, but
898 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
899 *
900 * Restart the search after a delay.
901 */
902 slot->seq_nr = slot->seq_nr_highest_sent;
903 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100904 case -NFS4ERR_BADSESSION:
905 case -NFS4ERR_DEADSESSION:
906 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
907 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400908 default:
909 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400910 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400911 }
912out:
913 /* The session may be reset by one of the error handlers. */
914 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500915out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500916 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400917session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100918 nfs4_schedule_session_recovery(session, status);
919 dprintk("%s ERROR: %d Reset session\n", __func__, status);
920 nfs41_sequence_free_slot(res);
921 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400922retry_new_seq:
923 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500924retry_nowait:
925 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400926 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500927 task->tk_status = 0;
928 ret = 0;
929 }
930 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400931out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400932 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400933 goto out;
934 rpc_delay(task, NFS4_POLL_RETRY_MAX);
935 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400936}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400937
938int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
939{
940 if (!nfs41_sequence_process(task, res))
941 return 0;
942 if (res->sr_slot != NULL)
943 nfs41_sequence_free_slot(res);
944 return 1;
945
946}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500947EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400948
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400949static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
950{
951 if (res->sr_slot == NULL)
952 return 1;
953 if (res->sr_slot->table->session != NULL)
954 return nfs41_sequence_process(task, res);
955 return nfs40_sequence_done(task, res);
956}
957
958static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
959{
960 if (res->sr_slot != NULL) {
961 if (res->sr_slot->table->session != NULL)
962 nfs41_sequence_free_slot(res);
963 else
964 nfs40_sequence_free_slot(res);
965 }
966}
967
Peng Tao2c4b1312014-06-11 05:24:16 +0800968int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400969{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500970 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400971 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400972 if (!res->sr_slot->table->session)
973 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400974 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400975}
Peng Tao2c4b1312014-06-11 05:24:16 +0800976EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400977
Andy Adamsonce5039c2009-04-01 09:22:13 -0400978static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
979{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400980 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981
Trond Myklebust035168ab2010-06-16 09:52:26 -0400982 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
983
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500984 nfs4_setup_sequence(data->seq_server->nfs_client,
985 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400986}
987
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
989{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400990 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400991
Trond Myklebust14516c32010-07-31 14:29:06 -0400992 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400993}
994
Trond Myklebust17280172012-03-11 13:11:00 -0400995static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400996 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400997 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400998};
999
Chuck Lever3bd23842013-08-09 12:49:19 -04001000#else /* !CONFIG_NFS_V4_1 */
1001
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001002static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1003{
1004 return nfs40_sequence_done(task, res);
1005}
1006
1007static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1008{
1009 if (res->sr_slot != NULL)
1010 nfs40_sequence_free_slot(res);
1011}
1012
Peng Tao2c4b1312014-06-11 05:24:16 +08001013int nfs4_sequence_done(struct rpc_task *task,
1014 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001015{
Chuck Lever3bd23842013-08-09 12:49:19 -04001016 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001017}
Peng Tao2c4b1312014-06-11 05:24:16 +08001018EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001019
1020#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001021
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001022static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1023{
1024 res->sr_timestamp = jiffies;
1025 res->sr_status_flags = 0;
1026 res->sr_status = 1;
1027}
1028
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001029static
1030void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1031 struct nfs4_sequence_res *res,
1032 struct nfs4_slot *slot)
1033{
1034 if (!slot)
1035 return;
1036 slot->privileged = args->sa_privileged ? 1 : 0;
1037 args->sa_slot = slot;
1038
1039 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001040}
1041
1042int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001043 struct nfs4_sequence_args *args,
1044 struct nfs4_sequence_res *res,
1045 struct rpc_task *task)
1046{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001047 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001048 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001049 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001050
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001051 /* slot already allocated? */
1052 if (res->sr_slot != NULL)
1053 goto out_start;
1054
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001055 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001056 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001057
Trond Myklebust3453d572018-06-20 17:53:34 -04001058 spin_lock(&tbl->slot_tbl_lock);
1059 /* The state manager will wait until the slot table is empty */
1060 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1061 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001062
Trond Myklebust3453d572018-06-20 17:53:34 -04001063 slot = nfs4_alloc_slot(tbl);
1064 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001065 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001066 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001067 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001068 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001069 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001070
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001071 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001072
Anna Schumakerad05cc02017-01-11 13:37:06 -05001073 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001074out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001075 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001076 rpc_call_start(task);
1077 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001078out_sleep_timeout:
1079 /* Try again in 1/4 second */
1080 if (args->sa_privileged)
1081 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1082 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1083 else
1084 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1085 NULL, jiffies + (HZ >> 2));
1086 spin_unlock(&tbl->slot_tbl_lock);
1087 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001088out_sleep:
1089 if (args->sa_privileged)
1090 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001091 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001092 else
1093 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1094 spin_unlock(&tbl->slot_tbl_lock);
1095 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001096}
1097EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1098
Chuck Lever9915ea72013-08-09 12:48:27 -04001099static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1100{
1101 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001102 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001103 data->seq_args, data->seq_res, task);
1104}
1105
1106static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1107{
1108 struct nfs4_call_sync_data *data = calldata;
1109 nfs4_sequence_done(task, data->seq_res);
1110}
1111
1112static const struct rpc_call_ops nfs40_call_sync_ops = {
1113 .rpc_call_prepare = nfs40_call_sync_prepare,
1114 .rpc_call_done = nfs40_call_sync_done,
1115};
1116
Anna Schumaker48c05852019-08-14 15:27:00 -04001117static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1118{
1119 int ret;
1120 struct rpc_task *task;
1121
1122 task = rpc_run_task(task_setup);
1123 if (IS_ERR(task))
1124 return PTR_ERR(task);
1125
1126 ret = task->tk_status;
1127 rpc_put_task(task);
1128 return ret;
1129}
1130
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001131static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1132 struct nfs_server *server,
1133 struct rpc_message *msg,
1134 struct nfs4_sequence_args *args,
1135 struct nfs4_sequence_res *res,
1136 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001137{
Chuck Lever9915ea72013-08-09 12:48:27 -04001138 struct nfs_client *clp = server->nfs_client;
1139 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001140 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001141 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001142 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001143 };
1144 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001145 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001146 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001147 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001148 .callback_data = &data,
1149 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001150 };
1151
Anna Schumaker48c05852019-08-14 15:27:00 -04001152 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001153}
1154
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001155static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1156 struct nfs_server *server,
1157 struct rpc_message *msg,
1158 struct nfs4_sequence_args *args,
1159 struct nfs4_sequence_res *res)
1160{
1161 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1162}
1163
1164
Bryan Schumaker7c513052011-03-24 17:12:24 +00001165int nfs4_call_sync(struct rpc_clnt *clnt,
1166 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001167 struct rpc_message *msg,
1168 struct nfs4_sequence_args *args,
1169 struct nfs4_sequence_res *res,
1170 int cache_reply)
1171{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001172 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001173 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Trond Myklebust3c591172018-07-31 15:54:10 -04001176static void
1177nfs4_inc_nlink_locked(struct inode *inode)
1178{
1179 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1180 inc_nlink(inode);
1181}
1182
1183static void
1184nfs4_dec_nlink_locked(struct inode *inode)
1185{
1186 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1187 drop_nlink(inode);
1188}
1189
1190static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001191nfs4_update_changeattr_locked(struct inode *inode,
1192 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001193 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001195 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Trond Myklebust16e14372018-03-20 16:53:31 -04001197 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1198 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001199 | cache_validity;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001200
1201 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001202 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1203 nfsi->attrtimeo_timestamp = jiffies;
1204 } else {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001205 if (S_ISDIR(inode->i_mode)) {
1206 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1207 nfs_force_lookup_revalidate(inode);
1208 } else {
1209 if (!NFS_PROTO(inode)->have_delegation(inode,
1210 FMODE_READ))
1211 nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1212 }
1213
1214 if (cinfo->before != inode_peek_iversion_raw(inode))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001215 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
Frank van der Linden0f44da52020-06-23 22:39:00 +00001216 NFS_INO_INVALID_ACL |
1217 NFS_INO_INVALID_XATTR;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001218 }
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001219 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001220 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001221 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001222 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001223
1224 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1225 nfs_fscache_invalidate(inode);
Trond Myklebust3c591172018-07-31 15:54:10 -04001226}
1227
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001228void
1229nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001230 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001231{
1232 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001233 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 spin_unlock(&dir->i_lock);
1235}
1236
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001237struct nfs4_open_createattrs {
1238 struct nfs4_label *label;
1239 struct iattr *sattr;
1240 const __u32 verf[2];
1241};
1242
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001243static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1244 int err, struct nfs4_exception *exception)
1245{
1246 if (err != -EINVAL)
1247 return false;
1248 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1249 return false;
1250 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1251 exception->retry = 1;
1252 return true;
1253}
1254
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001255static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1256{
1257 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1258}
1259
1260static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1261{
1262 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1263
1264 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1265}
1266
Trond Myklebust6ae37332015-01-30 14:21:14 -05001267static u32
1268nfs4_map_atomic_open_share(struct nfs_server *server,
1269 fmode_t fmode, int openflags)
1270{
1271 u32 res = 0;
1272
1273 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1274 case FMODE_READ:
1275 res = NFS4_SHARE_ACCESS_READ;
1276 break;
1277 case FMODE_WRITE:
1278 res = NFS4_SHARE_ACCESS_WRITE;
1279 break;
1280 case FMODE_READ|FMODE_WRITE:
1281 res = NFS4_SHARE_ACCESS_BOTH;
1282 }
1283 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1284 goto out;
1285 /* Want no delegation if we're using O_DIRECT */
1286 if (openflags & O_DIRECT)
1287 res |= NFS4_SHARE_WANT_NO_DELEG;
1288out:
1289 return res;
1290}
1291
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001292static enum open_claim_type4
1293nfs4_map_atomic_open_claim(struct nfs_server *server,
1294 enum open_claim_type4 claim)
1295{
1296 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1297 return claim;
1298 switch (claim) {
1299 default:
1300 return claim;
1301 case NFS4_OPEN_CLAIM_FH:
1302 return NFS4_OPEN_CLAIM_NULL;
1303 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1304 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1305 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1306 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1307 }
1308}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001311{
1312 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001313 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001314 p->o_res.seqid = p->o_arg.seqid;
1315 p->c_res.seqid = p->c_arg.seqid;
1316 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001317 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001318 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001319 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001320}
1321
Al Viro82a2c1b2011-06-22 18:30:55 -04001322static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001323 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001324 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001325 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001326 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001327{
Al Viro82a2c1b2011-06-22 18:30:55 -04001328 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001329 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001330 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001331 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001332 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001333 struct nfs4_opendata *p;
1334
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001335 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001336 if (p == NULL)
1337 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001338
1339 p->f_label = nfs4_label_alloc(server, gfp_mask);
1340 if (IS_ERR(p->f_label))
1341 goto err_free_p;
1342
Kinglong Meea49c2692015-07-27 15:31:38 +08001343 p->a_label = nfs4_label_alloc(server, gfp_mask);
1344 if (IS_ERR(p->a_label))
1345 goto err_free_f;
1346
Trond Myklebust63f5f792015-01-23 19:19:25 -05001347 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1348 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001349 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001350 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001351 nfs_sb_active(dentry->d_sb);
1352 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001353 p->dir = parent;
1354 p->owner = sp;
1355 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001356 p->o_arg.open_flags = flags;
1357 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001358 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001359 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1360 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001361 if (flags & O_CREAT) {
1362 p->o_arg.umask = current_umask();
1363 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1364 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1365 p->o_arg.u.attrs = &p->attrs;
1366 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1367
1368 memcpy(p->o_arg.u.verifier.data, c->verf,
1369 sizeof(p->o_arg.u.verifier.data));
1370 }
1371 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001372 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1373 * will return permission denied for all bits until close */
1374 if (!(flags & O_EXCL)) {
1375 /* ask server to check for all possible rights as results
1376 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001377 switch (p->o_arg.claim) {
1378 default:
1379 break;
1380 case NFS4_OPEN_CLAIM_NULL:
1381 case NFS4_OPEN_CLAIM_FH:
1382 p->o_arg.access = NFS4_ACCESS_READ |
1383 NFS4_ACCESS_MODIFY |
1384 NFS4_ACCESS_EXTEND |
1385 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001386#ifdef CONFIG_NFS_V4_2
1387 if (server->caps & NFS_CAP_XATTR)
1388 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1389 NFS4_ACCESS_XAWRITE |
1390 NFS4_ACCESS_XALIST;
1391#endif
Trond Myklebust536585c2016-11-10 15:40:34 -05001392 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001393 }
David Howells7539bba2006-08-22 20:06:09 -04001394 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001395 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1396 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001397 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001398 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001399 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001400 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001401 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001402 case NFS4_OPEN_CLAIM_NULL:
1403 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1404 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1405 p->o_arg.fh = NFS_FH(dir);
1406 break;
1407 case NFS4_OPEN_CLAIM_PREVIOUS:
1408 case NFS4_OPEN_CLAIM_FH:
1409 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1410 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001411 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001412 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001413 p->c_arg.fh = &p->o_res.fh;
1414 p->c_arg.stateid = &p->o_res.stateid;
1415 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001416 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001417 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001418 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001419
1420err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001421 nfs4_label_free(p->a_label);
1422err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001423 nfs4_label_free(p->f_label);
1424err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001425 kfree(p);
1426err:
1427 dput(parent);
1428 return NULL;
1429}
1430
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001431static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001432{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001433 struct nfs4_opendata *p = container_of(kref,
1434 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001435 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001436
Fred Isaman30ae2412016-10-18 13:39:51 -04001437 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001438 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001439 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001440 if (p->state != NULL)
1441 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001442 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001443
Kinglong Meea49c2692015-07-27 15:31:38 +08001444 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001445 nfs4_label_free(p->f_label);
1446
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001447 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001448 dput(p->dentry);
1449 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001450 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001451 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001452 kfree(p);
1453}
1454
1455static void nfs4_opendata_put(struct nfs4_opendata *p)
1456{
1457 if (p != NULL)
1458 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001459}
1460
Trond Myklebust24311f82015-09-20 10:50:17 -04001461static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1462 fmode_t fmode)
1463{
1464 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1465 case FMODE_READ|FMODE_WRITE:
1466 return state->n_rdwr != 0;
1467 case FMODE_WRITE:
1468 return state->n_wronly != 0;
1469 case FMODE_READ:
1470 return state->n_rdonly != 0;
1471 }
1472 WARN_ON_ONCE(1);
1473 return false;
1474}
1475
Trond Myklebustbe189f72018-09-27 17:12:33 -04001476static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1477 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001478{
1479 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001480
Trond Myklebust536e43d2012-01-17 22:04:26 -05001481 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001482 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001483 switch (claim) {
1484 case NFS4_OPEN_CLAIM_NULL:
1485 case NFS4_OPEN_CLAIM_FH:
1486 goto out;
1487 default:
1488 break;
1489 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001490 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001491 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001492 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1493 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001494 break;
1495 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001496 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1497 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001498 break;
1499 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001500 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1501 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001502 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001503out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001504 return ret;
1505}
1506
Trond Myklebust2a606182015-08-19 22:30:00 -05001507static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1508 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001509{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001510 if (delegation == NULL)
1511 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001512 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001513 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001514 switch (claim) {
1515 case NFS4_OPEN_CLAIM_NULL:
1516 case NFS4_OPEN_CLAIM_FH:
1517 break;
1518 case NFS4_OPEN_CLAIM_PREVIOUS:
1519 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1520 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001521 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001522 default:
1523 return 0;
1524 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001525 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001526 return 1;
1527}
1528
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001529static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001530{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001531 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001532 case FMODE_WRITE:
1533 state->n_wronly++;
1534 break;
1535 case FMODE_READ:
1536 state->n_rdonly++;
1537 break;
1538 case FMODE_READ|FMODE_WRITE:
1539 state->n_rdwr++;
1540 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001541 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001542}
1543
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001544#ifdef CONFIG_NFS_V4_1
1545static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1546{
1547 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1548 return true;
1549 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1550 return true;
1551 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1552 return true;
1553 return false;
1554}
1555#endif /* CONFIG_NFS_V4_1 */
1556
Trond Myklebustc9399f22017-11-06 15:28:01 -05001557static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1558{
1559 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1560 wake_up_all(&state->waitq);
1561}
1562
Trond Myklebust4f14c192014-02-12 19:15:06 -05001563static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001564{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001565 struct nfs_client *clp = state->owner->so_server->nfs_client;
1566 bool need_recover = false;
1567
1568 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1569 need_recover = true;
1570 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1571 need_recover = true;
1572 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1573 need_recover = true;
1574 if (need_recover)
1575 nfs4_state_mark_reclaim_nograce(clp, state);
1576}
1577
Trond Myklebustc9399f22017-11-06 15:28:01 -05001578/*
1579 * Check for whether or not the caller may update the open stateid
1580 * to the value passed in by stateid.
1581 *
1582 * Note: This function relies heavily on the server implementing
1583 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1584 * correctly.
1585 * i.e. The stateid seqids have to be initialised to 1, and
1586 * are then incremented on every state transition.
1587 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001588static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001589 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001590{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001591 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1592 /* The common case - we're updating to a new sequence number */
Trond Myklebust8c3e2042021-10-26 21:56:40 -04001593 if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1594 if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1595 return true;
1596 return false;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001597 }
Trond Myklebust8c3e2042021-10-26 21:56:40 -04001598 /* The server returned a new stateid */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001599 }
Trond Myklebust8c3e2042021-10-26 21:56:40 -04001600 /* This is the first OPEN in this generation */
1601 if (stateid->seqid == cpu_to_be32(1))
1602 return true;
Trond Myklebuste999e802014-02-10 18:20:47 -05001603 return false;
1604}
1605
Trond Myklebustf95549c2015-01-23 18:06:09 -05001606static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1607{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001608 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1609 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001610 if (state->n_wronly)
1611 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1612 if (state->n_rdonly)
1613 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1614 if (state->n_rdwr)
1615 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001616 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001617}
1618
Trond Myklebust226056c2014-02-11 10:41:07 -05001619static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1620 nfs4_stateid *stateid, fmode_t fmode)
1621{
1622 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1623 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1624 case FMODE_WRITE:
1625 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1626 break;
1627 case FMODE_READ:
1628 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1629 break;
1630 case 0:
1631 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1632 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1633 clear_bit(NFS_OPEN_STATE, &state->flags);
1634 }
1635 if (stateid == NULL)
1636 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001637 /* Handle OPEN+OPEN_DOWNGRADE races */
1638 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1639 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001640 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001641 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001642 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001643 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001644 nfs4_stateid_copy(&state->stateid, stateid);
1645 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001646 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001647out:
1648 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001649}
1650
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001651static void nfs_clear_open_stateid(struct nfs4_state *state,
1652 nfs4_stateid *arg_stateid,
1653 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001654{
1655 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001656 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1657 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1658 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001659 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001660 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1661 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001662}
1663
Trond Myklebust1393d962016-09-22 13:39:13 -04001664static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001665 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001666 __must_hold(&state->owner->so_lock)
1667 __must_hold(&state->seqlock)
1668 __must_hold(RCU)
1669
Trond Myklebust003707c2007-07-05 18:07:55 -04001670{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001671 DEFINE_WAIT(wait);
1672 int status = 0;
1673 for (;;) {
1674
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001675 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001676 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001677
Trond Myklebustc9399f22017-11-06 15:28:01 -05001678 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001679 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001680 /* Rely on seqids for serialisation with NFSv4.0 */
1681 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1682 break;
1683
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001684 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001685 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1686 /*
1687 * Ensure we process the state changes in the same order
1688 * in which the server processed them by delaying the
1689 * update of the stateid until we are in sequence.
1690 */
1691 write_sequnlock(&state->seqlock);
1692 spin_unlock(&state->owner->so_lock);
1693 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001694 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001695
zhouchuangao1fbbcd12021-05-09 19:34:37 -07001696 if (!fatal_signal_pending(current)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001697 if (schedule_timeout(5*HZ) == 0)
1698 status = -EAGAIN;
1699 else
1700 status = 0;
1701 } else
1702 status = -EINTR;
1703 finish_wait(&state->waitq, &wait);
1704 rcu_read_lock();
1705 spin_lock(&state->owner->so_lock);
1706 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001707 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001708
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001709 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1710 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001711 nfs4_stateid_copy(freeme, &state->open_stateid);
1712 nfs_test_and_clear_all_open_stateid(state);
1713 }
1714
Trond Myklebuste999e802014-02-10 18:20:47 -05001715 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1716 nfs4_stateid_copy(&state->stateid, stateid);
1717 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001718 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001719 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001720}
1721
Trond Myklebustc9399f22017-11-06 15:28:01 -05001722static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001723 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001724 fmode_t fmode,
1725 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001727 /*
1728 * Protect the call to nfs4_state_set_mode_locked and
1729 * serialise the stateid update
1730 */
1731 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001732 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1733 switch (fmode) {
1734 case FMODE_READ:
1735 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1736 break;
1737 case FMODE_WRITE:
1738 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1739 break;
1740 case FMODE_READ|FMODE_WRITE:
1741 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001742 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001743 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001744 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001745}
1746
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001747static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1748{
1749 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1750 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1751 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1752 clear_bit(NFS_OPEN_STATE, &state->flags);
1753}
1754
Trond Myklebustc9399f22017-11-06 15:28:01 -05001755static void nfs_state_set_delegation(struct nfs4_state *state,
1756 const nfs4_stateid *deleg_stateid,
1757 fmode_t fmode)
1758{
1759 /*
1760 * Protect the call to nfs4_state_set_mode_locked and
1761 * serialise the stateid update
1762 */
1763 write_seqlock(&state->seqlock);
1764 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1765 set_bit(NFS_DELEGATED_STATE, &state->flags);
1766 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001769static void nfs_state_clear_delegation(struct nfs4_state *state)
1770{
1771 write_seqlock(&state->seqlock);
1772 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1773 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1774 write_sequnlock(&state->seqlock);
1775}
1776
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001777int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001778 const nfs4_stateid *open_stateid,
1779 const nfs4_stateid *delegation,
1780 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001781{
Trond Myklebust1393d962016-09-22 13:39:13 -04001782 struct nfs_server *server = NFS_SERVER(state->inode);
1783 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001784 struct nfs_inode *nfsi = NFS_I(state->inode);
1785 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001786 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001787 int ret = 0;
1788
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001789 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001790
1791 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001792 spin_lock(&state->owner->so_lock);
1793 if (open_stateid != NULL) {
1794 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1795 ret = 1;
1796 }
1797
Trond Myklebust333ac782019-10-22 12:12:17 -04001798 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001799 if (deleg_cur == NULL)
1800 goto no_delegation;
1801
1802 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001803 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001804 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001805 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001806 goto no_delegation_unlock;
1807
1808 if (delegation == NULL)
1809 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001810 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001811 goto no_delegation_unlock;
1812
Trond Myklebustb7391f42008-12-23 15:21:52 -05001813 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001814 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001815 ret = 1;
1816no_delegation_unlock:
1817 spin_unlock(&deleg_cur->lock);
1818no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001819 if (ret)
1820 update_open_stateflags(state, fmode);
1821 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001822 rcu_read_unlock();
1823
Trond Myklebust4f14c192014-02-12 19:15:06 -05001824 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001825 nfs4_schedule_state_manager(clp);
1826 if (freeme.type != 0)
1827 nfs4_test_and_free_stateid(server, &freeme,
1828 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001829
1830 return ret;
1831}
1832
Trond Myklebust39071e62015-01-24 15:07:56 -05001833static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1834 const nfs4_stateid *stateid)
1835{
1836 struct nfs4_state *state = lsp->ls_state;
1837 bool ret = false;
1838
1839 spin_lock(&state->state_lock);
1840 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1841 goto out_noupdate;
1842 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1843 goto out_noupdate;
1844 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1845 ret = true;
1846out_noupdate:
1847 spin_unlock(&state->state_lock);
1848 return ret;
1849}
Trond Myklebust34310432008-12-23 15:21:38 -05001850
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001851static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001852{
1853 struct nfs_delegation *delegation;
1854
Trond Myklebustf5086242018-03-20 16:43:13 -04001855 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001857 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001858 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001859 rcu_read_unlock();
1860 return;
1861 }
1862 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001863 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001864}
1865
Trond Myklebust6ee41262007-07-08 14:11:36 -04001866static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001867{
1868 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001869 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001870 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001871 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001872 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001873 nfs4_stateid stateid;
1874 int ret = -EAGAIN;
1875
Trond Myklebustaac00a82007-07-05 19:02:21 -04001876 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001877 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001878 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001879 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001880 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001881 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001882 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001883 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001884 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001885 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001886 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001887 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001888 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001889 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001890 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001891 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001892 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001893 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001894 if (!opendata->is_recover) {
1895 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1896 if (ret != 0)
1897 goto out;
1898 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001899 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001900
1901 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001902 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001903 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001904 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001905out:
1906 return ERR_PTR(ret);
1907out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001908 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001909 return state;
1910}
1911
Andy Adamsone23008e2012-10-02 21:07:32 -04001912static void
1913nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1914{
1915 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1916 struct nfs_delegation *delegation;
1917 int delegation_flags = 0;
1918
1919 rcu_read_lock();
1920 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1921 if (delegation)
1922 delegation_flags = delegation->flags;
1923 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001924 switch (data->o_arg.claim) {
1925 default:
1926 break;
1927 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1928 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001929 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1930 "returning a delegation for "
1931 "OPEN(CLAIM_DELEGATE_CUR)\n",
1932 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001933 return;
1934 }
1935 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001936 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001937 data->owner->so_cred,
1938 data->o_res.delegation_type,
1939 &data->o_res.delegation,
1940 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001941 else
1942 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001943 data->owner->so_cred,
1944 data->o_res.delegation_type,
1945 &data->o_res.delegation,
1946 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001947
1948 if (data->o_res.do_recall)
1949 nfs_async_inode_return_delegation(state->inode,
1950 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001951}
1952
1953/*
1954 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1955 * and update the nfs4_state.
1956 */
1957static struct nfs4_state *
1958_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1959{
1960 struct inode *inode = data->state->inode;
1961 struct nfs4_state *state = data->state;
1962 int ret;
1963
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001964 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001965 if (data->rpc_status)
1966 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001967 /* cached opens have already been processed */
1968 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001969 }
1970
Andy Adamsone23008e2012-10-02 21:07:32 -04001971 ret = nfs_refresh_inode(inode, &data->f_attr);
1972 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001973 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001974
1975 if (data->o_res.delegation_type != 0)
1976 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001977update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001978 if (!update_open_stateid(state, &data->o_res.stateid,
1979 NULL, data->o_arg.fmode))
1980 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001981 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001982
1983 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001984}
1985
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001986static struct inode *
1987nfs4_opendata_get_inode(struct nfs4_opendata *data)
1988{
1989 struct inode *inode;
1990
1991 switch (data->o_arg.claim) {
1992 case NFS4_OPEN_CLAIM_NULL:
1993 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1994 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1995 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1996 return ERR_PTR(-EAGAIN);
1997 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1998 &data->f_attr, data->f_label);
1999 break;
2000 default:
2001 inode = d_inode(data->dentry);
2002 ihold(inode);
2003 nfs_refresh_inode(inode, &data->f_attr);
2004 }
2005 return inode;
2006}
2007
Andy Adamsone23008e2012-10-02 21:07:32 -04002008static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04002009nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2010{
2011 struct nfs4_state *state;
2012 struct inode *inode;
2013
2014 inode = nfs4_opendata_get_inode(data);
2015 if (IS_ERR(inode))
2016 return ERR_CAST(inode);
2017 if (data->state != NULL && data->state->inode == inode) {
2018 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002019 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002020 } else
2021 state = nfs4_get_open_state(inode, data->owner);
2022 iput(inode);
2023 if (state == NULL)
2024 state = ERR_PTR(-ENOMEM);
2025 return state;
2026}
2027
Andy Adamsone23008e2012-10-02 21:07:32 -04002028static struct nfs4_state *
2029_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002030{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002031 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002032
Trond Myklebustaac00a82007-07-05 19:02:21 -04002033 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002034 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002035 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002036 goto out;
2037 }
2038
Trond Myklebust75e8c482017-08-08 10:38:07 -04002039 state = nfs4_opendata_find_nfs4_state(data);
2040 if (IS_ERR(state))
2041 goto out;
2042
Andy Adamsone23008e2012-10-02 21:07:32 -04002043 if (data->o_res.delegation_type != 0)
2044 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002045 if (!update_open_stateid(state, &data->o_res.stateid,
2046 NULL, data->o_arg.fmode)) {
2047 nfs4_put_open_state(state);
2048 state = ERR_PTR(-EAGAIN);
2049 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002050out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002051 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002052 return state;
2053}
2054
Andy Adamsone23008e2012-10-02 21:07:32 -04002055static struct nfs4_state *
2056nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2057{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002058 struct nfs4_state *ret;
2059
Andy Adamsone23008e2012-10-02 21:07:32 -04002060 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002061 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2062 else
2063 ret = _nfs4_opendata_to_nfs4_state(data);
2064 nfs4_sequence_free_slot(&data->o_res.seq_res);
2065 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002066}
2067
Trond Myklebust0de43972018-09-02 15:57:01 -04002068static struct nfs_open_context *
2069nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002070{
2071 struct nfs_inode *nfsi = NFS_I(state->inode);
2072 struct nfs_open_context *ctx;
2073
Trond Myklebust0de43972018-09-02 15:57:01 -04002074 rcu_read_lock();
2075 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002076 if (ctx->state != state)
2077 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002078 if ((ctx->mode & mode) != mode)
2079 continue;
2080 if (!get_nfs_open_context(ctx))
2081 continue;
2082 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002083 return ctx;
2084 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002085 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002086 return ERR_PTR(-ENOENT);
2087}
2088
Trond Myklebust0de43972018-09-02 15:57:01 -04002089static struct nfs_open_context *
2090nfs4_state_find_open_context(struct nfs4_state *state)
2091{
2092 struct nfs_open_context *ctx;
2093
2094 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2095 if (!IS_ERR(ctx))
2096 return ctx;
2097 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2098 if (!IS_ERR(ctx))
2099 return ctx;
2100 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2101}
2102
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002103static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2104 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002105{
2106 struct nfs4_opendata *opendata;
2107
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002108 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002109 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002110 if (opendata == NULL)
2111 return ERR_PTR(-ENOMEM);
2112 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002113 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002114 return opendata;
2115}
2116
Trond Myklebust24311f82015-09-20 10:50:17 -04002117static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2118 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002119{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002120 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002121 int ret;
2122
Trond Myklebust24311f82015-09-20 10:50:17 -04002123 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002124 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002125 opendata->o_arg.open_flags = 0;
2126 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002127 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2128 NFS_SB(opendata->dentry->d_sb),
2129 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002130 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2131 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2132 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002133 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002134 if (ret != 0)
2135 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002136 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002137 if (IS_ERR(newstate))
2138 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002139 if (newstate != opendata->state)
2140 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002141 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002142 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002143}
2144
2145static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2146{
Trond Myklebust864472e2006-01-03 09:55:15 +01002147 int ret;
2148
2149 /* memory barrier prior to reading state->n_* */
2150 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002151 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2152 if (ret != 0)
2153 return ret;
2154 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2155 if (ret != 0)
2156 return ret;
2157 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2158 if (ret != 0)
2159 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002160 /*
2161 * We may have performed cached opens for all three recoveries.
2162 * Check if we need to update the current stateid.
2163 */
2164 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002165 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002166 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002167 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002168 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002169 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002170 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002171 return 0;
2172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174/*
2175 * OPEN_RECLAIM:
2176 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002178static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002180 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002181 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002182 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 int status;
2184
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002185 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2186 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002187 if (IS_ERR(opendata))
2188 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002189 rcu_read_lock();
2190 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002191 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002192 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002193 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002194 opendata->o_arg.u.delegation_type = delegation_type;
2195 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002196 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return status;
2198}
2199
Trond Myklebust539cd032007-06-05 11:46:42 -04002200static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 struct nfs_server *server = NFS_SERVER(state->inode);
2203 struct nfs4_exception exception = { };
2204 int err;
2205 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002206 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002207 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002208 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2209 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002210 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002211 break;
2212 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 } while (exception.retry);
2214 return err;
2215}
2216
Trond Myklebust864472e2006-01-03 09:55:15 +01002217static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2218{
2219 struct nfs_open_context *ctx;
2220 int ret;
2221
2222 ctx = nfs4_state_find_open_context(state);
2223 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002224 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002225 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2226 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002227 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002228 put_nfs_open_context(ctx);
2229 return ret;
2230}
2231
NeilBrowndce26302017-12-13 09:57:09 +11002232static 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 -07002233{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002234 switch (err) {
2235 default:
2236 printk(KERN_ERR "NFS: %s: unhandled error "
2237 "%d.\n", __func__, err);
2238 case 0:
2239 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002240 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002241 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002242 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002243 break;
2244 case -NFS4ERR_BADSESSION:
2245 case -NFS4ERR_BADSLOT:
2246 case -NFS4ERR_BAD_HIGH_SLOT:
2247 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2248 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002249 return -EAGAIN;
2250 case -NFS4ERR_STALE_CLIENTID:
2251 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002252 /* Don't recall a delegation if it was lost */
2253 nfs4_schedule_lease_recovery(server->nfs_client);
2254 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002255 case -NFS4ERR_MOVED:
2256 nfs4_schedule_migration_recovery(server);
2257 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002258 case -NFS4ERR_LEASE_MOVED:
2259 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2260 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002261 case -NFS4ERR_DELEG_REVOKED:
2262 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002263 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002264 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002265 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002266 nfs_inode_find_state_and_recover(state->inode,
2267 stateid);
2268 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002269 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002270 case -NFS4ERR_DELAY:
2271 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002272 ssleep(1);
2273 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002274 case -ENOMEM:
2275 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002276 if (fl) {
2277 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2278 if (lsp)
2279 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2280 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002281 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return err;
2284}
2285
Trond Myklebust24311f82015-09-20 10:50:17 -04002286int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002287 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002288{
2289 struct nfs_server *server = NFS_SERVER(state->inode);
2290 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002291 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002292
2293 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2294 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2295 if (IS_ERR(opendata))
2296 return PTR_ERR(opendata);
2297 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002298 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002299 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2300 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002301 goto out;
2302 }
2303 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002304 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2305 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002306 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002307 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002308 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2309 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2310 if (err)
2311 goto out;
2312 }
2313 nfs_state_clear_delegation(state);
2314out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002315 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002316 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002317}
2318
Chuck Leverbe05c862013-08-09 12:49:47 -04002319static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2320{
2321 struct nfs4_opendata *data = calldata;
2322
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002323 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2324 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002325}
2326
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002327static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2328{
2329 struct nfs4_opendata *data = calldata;
2330
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002331 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002332
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002333 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002334 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002335 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002336 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002337 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002338 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002339 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002340}
2341
2342static void nfs4_open_confirm_release(void *calldata)
2343{
2344 struct nfs4_opendata *data = calldata;
2345 struct nfs4_state *state = NULL;
2346
2347 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002348 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002349 goto out_free;
2350 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002351 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002352 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002353 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002354 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002355 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002356out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002357 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002358}
2359
2360static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002361 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002362 .rpc_call_done = nfs4_open_confirm_done,
2363 .rpc_release = nfs4_open_confirm_release,
2364};
2365
2366/*
2367 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2368 */
2369static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2370{
David Howells2b0143b2015-03-17 22:25:59 +00002371 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002372 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002373 struct rpc_message msg = {
2374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2375 .rpc_argp = &data->c_arg,
2376 .rpc_resp = &data->c_res,
2377 .rpc_cred = data->owner->so_cred,
2378 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002379 struct rpc_task_setup task_setup_data = {
2380 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002381 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002382 .callback_ops = &nfs4_open_confirm_ops,
2383 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002384 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002385 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002386 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 int status;
2388
Anna Schumakerfba83f32018-05-04 16:22:50 -04002389 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2390 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002391 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002392 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002393 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002394 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002395 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002396 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002397 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002398 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002399 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002400 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002401 smp_wmb();
2402 } else
2403 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002404 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 return status;
2406}
2407
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002408static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002410 struct nfs4_opendata *data = calldata;
2411 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002412 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002413 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002414
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002415 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002416 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002417 /*
2418 * Check if we still need to send an OPEN call, or if we can use
2419 * a delegation instead.
2420 */
2421 if (data->state != NULL) {
2422 struct nfs_delegation *delegation;
2423
Trond Myklebustbe189f72018-09-27 17:12:33 -04002424 if (can_open_cached(data->state, data->o_arg.fmode,
2425 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002426 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002427 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002428 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002429 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002430 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002431 rcu_read_unlock();
2432 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002433 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002434 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002435 switch (claim) {
2436 default:
2437 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002438 case NFS4_OPEN_CLAIM_PREVIOUS:
2439 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2440 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002441 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002442 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002443 case NFS4_OPEN_CLAIM_FH:
2444 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002445 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002446 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002447 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002448 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002449 &data->o_res.seq_res,
2450 task) != 0)
2451 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002452
2453 /* Set the create mode (note dependency on the session type) */
2454 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2455 if (data->o_arg.open_flags & O_EXCL) {
2456 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2457 if (nfs4_has_persistent_session(clp))
2458 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2459 else if (clp->cl_mvops->minor_version > 0)
2460 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2461 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002462 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002463unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002464 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002465 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002466out_no_action:
2467 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002468out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002469 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002470}
2471
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002472static void nfs4_open_done(struct rpc_task *task, void *calldata)
2473{
2474 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002476 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002477
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002478 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002479 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002480
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002481 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002482 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2483 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002484 case S_IFREG:
2485 break;
2486 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002487 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002488 break;
2489 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002490 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002491 break;
2492 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002493 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002494 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002495 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002496 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002497 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2498 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002499 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002500 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002501}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002502
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002503static void nfs4_open_release(void *calldata)
2504{
2505 struct nfs4_opendata *data = calldata;
2506 struct nfs4_state *state = NULL;
2507
2508 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002509 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002510 goto out_free;
2511 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002512 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002513 goto out_free;
2514 /* In case we need an open_confirm, no cleanup! */
2515 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2516 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002517 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002518 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002519 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002520out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002521 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002522}
2523
2524static const struct rpc_call_ops nfs4_open_ops = {
2525 .rpc_call_prepare = nfs4_open_prepare,
2526 .rpc_call_done = nfs4_open_done,
2527 .rpc_release = nfs4_open_release,
2528};
2529
Fred Isaman3b65a302016-09-19 10:06:49 -04002530static int nfs4_run_open_task(struct nfs4_opendata *data,
2531 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002532{
David Howells2b0143b2015-03-17 22:25:59 +00002533 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002534 struct nfs_server *server = NFS_SERVER(dir);
2535 struct nfs_openargs *o_arg = &data->o_arg;
2536 struct nfs_openres *o_res = &data->o_res;
2537 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002538 struct rpc_message msg = {
2539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2540 .rpc_argp = o_arg,
2541 .rpc_resp = o_res,
2542 .rpc_cred = data->owner->so_cred,
2543 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002544 struct rpc_task_setup task_setup_data = {
2545 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002546 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002547 .callback_ops = &nfs4_open_ops,
2548 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002549 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002550 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002551 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002552 int status;
2553
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002554 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002555 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002556 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002557 data->cancelled = false;
2558 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002559 if (!ctx) {
2560 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002561 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002562 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002563 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002564 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002565 pnfs_lgopen_prepare(data, ctx);
2566 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002567 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002568 if (IS_ERR(task))
2569 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002570 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002571 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002572 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002573 smp_wmb();
2574 } else
2575 status = data->rpc_status;
2576 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002577
2578 return status;
2579}
2580
2581static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2582{
David Howells2b0143b2015-03-17 22:25:59 +00002583 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002584 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002585 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002586
Fred Isaman3b65a302016-09-19 10:06:49 -04002587 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002588 if (status != 0 || !data->rpc_done)
2589 return status;
2590
Trond Myklebust6926afd2012-01-07 13:22:46 -05002591 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2592
Anna Schumakerd7e98252017-01-11 16:13:29 -05002593 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002594 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002595
2596 return status;
2597}
2598
Trond Myklebustf3792d62014-07-10 08:54:32 -04002599/*
2600 * Additional permission checks in order to distinguish between an
2601 * open for read, and an open for execute. This works around the
2602 * fact that NFSv4 OPEN treats read and execute permissions as being
2603 * the same.
2604 * Note that in the non-execute case, we want to turn off permission
2605 * checking if we just created a new file (POSIX open() semantics).
2606 */
NeilBrowna52458b2018-12-03 11:30:31 +11002607static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002608 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002609 struct nfs4_state *state, fmode_t fmode,
2610 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002611{
2612 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002613 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002614
2615 /* access call failed or for some reason the server doesn't
2616 * support any access modes -- defer access call until later */
2617 if (opendata->o_res.access_supported == 0)
2618 return 0;
2619
2620 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002621 /*
2622 * Use openflags to check for exec, because fmode won't
2623 * always have FMODE_EXEC set when file open for exec.
2624 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002625 if (openflags & __FMODE_EXEC) {
2626 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002627 if (S_ISDIR(state->inode->i_mode))
2628 mask = NFS4_ACCESS_LOOKUP;
2629 else
2630 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002631 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002632 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002633
2634 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002635 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2636 nfs_access_add_cache(state->inode, &cache);
2637
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002638 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2639 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002640 return 0;
2641
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002642 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002643}
2644
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002645/*
2646 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2647 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002648static int _nfs4_proc_open(struct nfs4_opendata *data,
2649 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002650{
David Howells2b0143b2015-03-17 22:25:59 +00002651 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002652 struct nfs_server *server = NFS_SERVER(dir);
2653 struct nfs_openargs *o_arg = &data->o_arg;
2654 struct nfs_openres *o_res = &data->o_res;
2655 int status;
2656
Fred Isaman3b65a302016-09-19 10:06:49 -04002657 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002658 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002659 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002660 if (status != 0) {
2661 if (status == -NFS4ERR_BADNAME &&
2662 !(o_arg->open_flags & O_CREAT))
2663 return -ENOENT;
2664 return status;
2665 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002666
Trond Myklebust6926afd2012-01-07 13:22:46 -05002667 nfs_fattr_map_and_free_names(server, &data->f_attr);
2668
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002669 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002670 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002671 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002672 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002673 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002674 if (data->file_created ||
2675 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002676 nfs4_update_changeattr(dir, &o_res->cinfo,
2677 o_res->f_attr->time_start,
2678 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002679 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002680 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2681 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002683 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002685 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002687 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2688 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002689 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2690 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002691 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695/*
2696 * OPEN_EXPIRED:
2697 * reclaim state on the server after a network partition.
2698 * Assumes caller holds the appropriate lock
2699 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002700static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002702 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002703 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002705 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002706 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002707 if (IS_ERR(opendata))
2708 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002709 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002710 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002711 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002712 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002716static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002717{
Trond Myklebust539cd032007-06-05 11:46:42 -04002718 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002719 struct nfs4_exception exception = { };
2720 int err;
2721
2722 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002723 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002724 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002725 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2726 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002727 switch (err) {
2728 default:
2729 goto out;
2730 case -NFS4ERR_GRACE:
2731 case -NFS4ERR_DELAY:
2732 nfs4_handle_exception(server, err, &exception);
2733 err = 0;
2734 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002735 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002736out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002737 return err;
2738}
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002743 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Trond Myklebust864472e2006-01-03 09:55:15 +01002745 ctx = nfs4_state_find_open_context(state);
2746 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002747 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002748 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002749 put_nfs_open_context(ctx);
2750 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
Trond Myklebust41020b62016-09-22 13:38:58 -04002753static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2754 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002755{
Trond Myklebust41020b62016-09-22 13:38:58 -04002756 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002757 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002758}
2759
2760static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2761{
2762 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002763 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002764}
2765
2766static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2767{
2768 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2769 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002770 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002771 return nfs4_open_expired(sp, state);
2772}
2773
Trond Myklebust45870d62016-09-22 13:38:59 -04002774static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2775 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002776 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002777{
2778 return -NFS4ERR_BAD_STATEID;
2779}
2780
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002781#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002782static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2783 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002784 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002785{
2786 int status;
2787
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002788 switch (stateid->type) {
2789 default:
2790 break;
2791 case NFS4_INVALID_STATEID_TYPE:
2792 case NFS4_SPECIAL_STATEID_TYPE:
2793 return -NFS4ERR_BAD_STATEID;
2794 case NFS4_REVOKED_STATEID_TYPE:
2795 goto out_free;
2796 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002797
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002798 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002799 switch (status) {
2800 case -NFS4ERR_EXPIRED:
2801 case -NFS4ERR_ADMIN_REVOKED:
2802 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002803 break;
2804 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002805 return status;
2806 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002807out_free:
2808 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002809 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002810 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002811}
2812
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002813static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002814{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002815 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002816 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002817 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002818 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002819 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002820
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002821 /* Get the delegation credential for use by test/free_stateid */
2822 rcu_read_lock();
2823 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002824 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002825 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002826 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002827 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002828 }
2829
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002830 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002831 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002832
Trond Myklebust994b15b2018-09-05 14:07:14 -04002833 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2834 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002835 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002836 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002837 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002838 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002839
NeilBrowna52458b2018-12-03 11:30:31 +11002840 if (delegation->cred)
2841 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002842 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002843 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002844 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002845 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002846 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002847 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002848 else
2849 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002850
Trond Myklebust8c39a392019-07-19 13:48:44 -04002851 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002852 return ret;
2853}
2854
2855static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2856{
2857 nfs4_stateid tmp;
2858
2859 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2860 nfs4_copy_delegation_stateid(state->inode, state->state,
2861 &tmp, NULL) &&
2862 nfs4_stateid_match_other(&state->stateid, &tmp))
2863 nfs_state_set_delegation(state, &tmp, state->state);
2864 else
2865 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002866}
2867
2868/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002869 * nfs41_check_expired_locks - possibly free a lock stateid
2870 *
2871 * @state: NFSv4 state for an inode
2872 *
2873 * Returns NFS_OK if recovery for this stateid is now finished.
2874 * Otherwise a negative NFS4ERR value is returned.
2875 */
2876static int nfs41_check_expired_locks(struct nfs4_state *state)
2877{
2878 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002879 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002880 struct nfs_server *server = NFS_SERVER(state->inode);
2881
2882 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2883 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002884
2885 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002886 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2887 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002888 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002889
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002890 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002891 spin_unlock(&state->state_lock);
2892
2893 nfs4_put_lock_state(prev);
2894 prev = lsp;
2895
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002896 status = nfs41_test_and_free_expired_stateid(server,
2897 &lsp->ls_stateid,
2898 cred);
2899 trace_nfs4_test_lock_stateid(state, lsp, status);
2900 if (status == -NFS4ERR_EXPIRED ||
2901 status == -NFS4ERR_BAD_STATEID) {
2902 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002903 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002904 if (!recover_lost_locks)
2905 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2906 } else if (status != NFS_OK) {
2907 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002908 nfs4_put_lock_state(prev);
2909 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002910 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002911 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002912 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002913 }
2914 spin_unlock(&state->state_lock);
2915 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002916out:
2917 return ret;
2918}
2919
2920/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002921 * nfs41_check_open_stateid - possibly free an open stateid
2922 *
2923 * @state: NFSv4 state for an inode
2924 *
2925 * Returns NFS_OK if recovery for this stateid is now finished.
2926 * Otherwise a negative NFS4ERR value is returned.
2927 */
2928static int nfs41_check_open_stateid(struct nfs4_state *state)
2929{
2930 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002931 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002932 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002933 int status;
2934
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002935 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002936 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002937 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002938 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002939 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002940 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002941 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002942 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002943 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002944 if (nfs_open_stateid_recover_openmode(state))
2945 return -NFS4ERR_OPENMODE;
2946 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002947}
2948
2949static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2950{
Chuck Levereb64cf92012-07-11 16:30:05 -04002951 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002952
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002953 status = nfs41_check_delegation_stateid(state);
2954 if (status != NFS_OK)
2955 return status;
2956 nfs41_delegation_recover_stateid(state);
2957
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002958 status = nfs41_check_expired_locks(state);
2959 if (status != NFS_OK)
2960 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002961 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002962 if (status != NFS_OK)
2963 status = nfs4_open_expired(sp, state);
2964 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002965}
2966#endif
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002969 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2970 * fields corresponding to attributes that were used to store the verifier.
2971 * Make sure we clobber those fields in the later setattr call
2972 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002973static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002974 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002975{
Trond Myklebust609339c2018-03-28 16:18:17 -04002976 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2977 __u32 attrset[3];
2978 unsigned ret;
2979 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002980
Trond Myklebust609339c2018-03-28 16:18:17 -04002981 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2982 attrset[i] = opendata->o_res.attrset[i];
2983 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2984 attrset[i] &= ~bitmask[i];
2985 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002986
Trond Myklebust609339c2018-03-28 16:18:17 -04002987 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2988 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002989
Trond Myklebust609339c2018-03-28 16:18:17 -04002990 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2991 if (sattr->ia_valid & ATTR_ATIME_SET)
2992 ret |= ATTR_ATIME_SET;
2993 else
2994 ret |= ATTR_ATIME;
2995 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002996
Trond Myklebust609339c2018-03-28 16:18:17 -04002997 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2998 if (sattr->ia_valid & ATTR_MTIME_SET)
2999 ret |= ATTR_MTIME_SET;
3000 else
3001 ret |= ATTR_MTIME;
3002 }
3003
3004 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003005 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04003006 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003007}
3008
Trond Myklebustc21443c2013-02-07 14:26:21 -05003009static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003010 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003011{
3012 struct nfs4_state_owner *sp = opendata->owner;
3013 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003014 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003015 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003016 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003017 struct inode *dir = d_inode(opendata->dir);
3018 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003019 unsigned int seq;
3020 int ret;
3021
3022 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003023 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003024
Fred Isaman3b65a302016-09-19 10:06:49 -04003025 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003026 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003027 goto out;
3028
Trond Myklebustae55e592018-05-22 11:17:16 -04003029 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003030 ret = PTR_ERR(state);
3031 if (IS_ERR(state))
3032 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003033 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003034 if (server->caps & NFS_CAP_POSIX_LOCK)
3035 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003036 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3037 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003038
Trond Myklebust275bb302013-05-29 13:11:28 -04003039 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003040 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003041 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003042 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003043 alias = d_exact_alias(dentry, state->inode);
3044 if (!alias)
3045 alias = d_splice_alias(igrab(state->inode), dentry);
3046 /* d_splice_alias() can't fail here - it's a non-directory */
3047 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003048 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003049 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003050 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003051 }
3052
3053 switch(opendata->o_arg.claim) {
3054 default:
3055 break;
3056 case NFS4_OPEN_CLAIM_NULL:
3057 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3058 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3059 if (!opendata->rpc_done)
3060 break;
3061 if (opendata->o_res.delegation_type != 0)
3062 dir_verifier = nfs_save_change_attribute(dir);
3063 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003064 }
3065
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003066 /* Parse layoutget results before we check for access */
3067 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3068
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003069 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3070 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003071 if (ret != 0)
3072 goto out;
3073
David Howells2b0143b2015-03-17 22:25:59 +00003074 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003075 nfs_inode_attach_open_context(ctx);
3076 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3077 nfs4_schedule_stateid_recovery(server, state);
3078 }
Fred Isaman2409a972016-10-06 12:11:21 -04003079
Trond Myklebustc21443c2013-02-07 14:26:21 -05003080out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003081 if (!opendata->cancelled)
3082 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003083 return ret;
3084}
3085
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003086/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003087 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 */
Andy Adamson82be4172012-05-23 05:02:35 -04003089static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003090 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003091 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003092 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003093 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 struct nfs4_state_owner *sp;
3096 struct nfs4_state *state = NULL;
3097 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003098 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003099 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003100 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003101 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003102 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003103 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003104 struct iattr *sattr = c->sattr;
3105 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003106 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003107 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
3109 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003111 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3112 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3114 goto out_err;
3115 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003116 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003117 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003118 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003119 if (d_really_is_positive(dentry))
3120 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003121 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003122 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003123 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003124 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3125 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003126 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003127 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
David Quigley14c43f72013-05-22 12:50:43 -04003129 if (label) {
3130 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3131 if (IS_ERR(olabel)) {
3132 status = PTR_ERR(olabel);
3133 goto err_opendata_put;
3134 }
3135 }
3136
Trond Myklebuste911b812014-03-26 13:24:37 -07003137 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3138 if (!opendata->f_attr.mdsthreshold) {
3139 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3140 if (!opendata->f_attr.mdsthreshold)
3141 goto err_free_label;
3142 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003143 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003144 }
David Howells2b0143b2015-03-17 22:25:59 +00003145 if (d_really_is_positive(dentry))
3146 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003147
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003148 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003149 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003150 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003151 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003152
NeilBrownefcbc042015-07-30 13:00:56 +10003153 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003154 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003155 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003156 /*
3157 * send create attributes which was not set by open
3158 * with an extra setattr.
3159 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003160 if (attrs || label) {
3161 unsigned ia_old = sattr->ia_valid;
3162
3163 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003164 nfs_fattr_init(opendata->o_res.f_attr);
3165 status = nfs4_do_setattr(state->inode, cred,
3166 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003167 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003168 if (status == 0) {
3169 nfs_setattr_update_inode(state->inode, sattr,
3170 opendata->o_res.f_attr);
3171 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3172 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003173 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003174 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003175 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003176 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003177 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003178
Trond Myklebuste911b812014-03-26 13:24:37 -07003179 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003180 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003181 opendata->f_attr.mdsthreshold = NULL;
3182 }
Andy Adamson82be4172012-05-23 05:02:35 -04003183
David Quigley14c43f72013-05-22 12:50:43 -04003184 nfs4_label_free(olabel);
3185
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003186 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003189err_free_label:
3190 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003191err_opendata_put:
3192 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003193err_put_state_owner:
3194 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return status;
3197}
3198
3199
Andy Adamson82be4172012-05-23 05:02:35 -04003200static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003201 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003202 int flags,
3203 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003204 struct nfs4_label *label,
3205 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003207 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003208 struct nfs4_exception exception = {
3209 .interruptible = true,
3210 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003212 struct nfs4_open_createattrs c = {
3213 .label = label,
3214 .sattr = sattr,
3215 .verf = {
3216 [0] = (__u32)jiffies,
3217 [1] = (__u32)current->pid,
3218 },
3219 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 int status;
3221
3222 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003223 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003224 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003225 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (status == 0)
3227 break;
3228 /* NOTE: BAD_SEQID means the server and client disagree about the
3229 * book-keeping w.r.t. state-changing operations
3230 * (OPEN/CLOSE/LOCK/LOCKU...)
3231 * It is actually a sign of a bug on the client or on the server.
3232 *
3233 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003234 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 * have unhashed the old state_owner for us, and that we can
3236 * therefore safely retry using a new one. We should still warn
3237 * the user though...
3238 */
3239 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003240 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003241 " returned a bad sequence-id error!\n",
3242 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 exception.retry = 1;
3244 continue;
3245 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003246 /*
3247 * BAD_STATEID on OPEN means that the server cancelled our
3248 * state before it received the OPEN_CONFIRM.
3249 * Recover by retrying the request as per the discussion
3250 * on Page 181 of RFC3530.
3251 */
3252 if (status == -NFS4ERR_BAD_STATEID) {
3253 exception.retry = 1;
3254 continue;
3255 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003256 if (status == -NFS4ERR_EXPIRED) {
3257 nfs4_schedule_lease_recovery(server->nfs_client);
3258 exception.retry = 1;
3259 continue;
3260 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003261 if (status == -EAGAIN) {
3262 /* We must have found a delegation */
3263 exception.retry = 1;
3264 continue;
3265 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003266 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3267 continue;
3268 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 status, &exception));
3270 } while (exception.retry);
3271 return res;
3272}
3273
Trond Myklebust8487c472016-06-26 08:44:35 -04003274static int _nfs4_do_setattr(struct inode *inode,
3275 struct nfs_setattrargs *arg,
3276 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003277 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003278 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003279{
3280 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003281 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3283 .rpc_argp = arg,
3284 .rpc_resp = res,
3285 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003286 };
NeilBrowna52458b2018-12-03 11:30:31 +11003287 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003288 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003289 bool truncate;
3290 int status;
3291
3292 nfs_fattr_init(res->fattr);
3293
3294 /* Servers should only apply open mode checks for file size changes */
3295 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003296 if (!truncate) {
3297 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003298 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003299 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003300
Trond Myklebust991eedb2018-04-09 11:15:30 -04003301 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003302 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003303 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003304 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003305 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003306 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003307 l_ctx = nfs_get_lock_context(ctx);
3308 if (IS_ERR(l_ctx))
3309 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003310 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3311 &arg->stateid, &delegation_cred);
3312 nfs_put_lock_context(l_ctx);
3313 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003314 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003315 else if (status == -EAGAIN)
3316 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003317 } else {
3318zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003319 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003320 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003321 if (delegation_cred)
3322 msg.rpc_cred = delegation_cred;
3323
3324 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3325
NeilBrowna52458b2018-12-03 11:30:31 +11003326 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003327 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003328 renew_lease(server, timestamp);
3329 trace_nfs4_setattr(inode, &arg->stateid, status);
3330 return status;
3331}
3332
NeilBrowna52458b2018-12-03 11:30:31 +11003333static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003334 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003335 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003336 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003338 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003339 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003340 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003341 struct nfs_setattrargs arg = {
3342 .fh = NFS_FH(inode),
3343 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003345 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003346 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003347 };
3348 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003350 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003352 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003353 struct nfs4_exception exception = {
3354 .state = state,
3355 .inode = inode,
3356 .stateid = &arg.stateid,
3357 };
3358 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003361 nfs4_bitmap_copy_adjust_setattr(bitmask,
3362 nfs4_bitmask(server, olabel),
3363 inode);
3364
NeilBrown29b59f92016-10-13 15:26:47 +11003365 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003366 switch (err) {
3367 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003368 if (!(sattr->ia_valid & ATTR_SIZE)) {
3369 pr_warn_once("NFSv4: server %s is incorrectly "
3370 "applying open mode checks to "
3371 "a SETATTR that is not "
3372 "changing file size.\n",
3373 server->nfs_client->cl_hostname);
3374 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003375 if (state && !(state->state & FMODE_WRITE)) {
3376 err = -EBADF;
3377 if (sattr->ia_valid & ATTR_OPEN)
3378 err = -EACCES;
3379 goto out;
3380 }
3381 }
3382 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003384out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return err;
3386}
3387
Peng Tao500d7012015-09-22 11:35:22 +08003388static bool
3389nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3390{
3391 if (inode == NULL || !nfs_have_layout(inode))
3392 return false;
3393
3394 return pnfs_wait_on_layoutreturn(inode, task);
3395}
3396
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003397/*
3398 * Update the seqid of an open stateid
3399 */
3400static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3401 struct nfs4_state *state)
3402{
3403 __be32 seqid_open;
3404 u32 dst_seqid;
3405 int seq;
3406
3407 for (;;) {
3408 if (!nfs4_valid_open_stateid(state))
3409 break;
3410 seq = read_seqbegin(&state->seqlock);
3411 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3412 nfs4_stateid_copy(dst, &state->open_stateid);
3413 if (read_seqretry(&state->seqlock, seq))
3414 continue;
3415 break;
3416 }
3417 seqid_open = state->open_stateid.seqid;
3418 if (read_seqretry(&state->seqlock, seq))
3419 continue;
3420
3421 dst_seqid = be32_to_cpu(dst->seqid);
3422 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3423 dst->seqid = seqid_open;
3424 break;
3425 }
3426}
3427
3428/*
3429 * Update the seqid of an open stateid after receiving
3430 * NFS4ERR_OLD_STATEID
3431 */
3432static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3433 struct nfs4_state *state)
3434{
3435 __be32 seqid_open;
3436 u32 dst_seqid;
3437 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003438 int seq, status = -EAGAIN;
3439 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003440
3441 for (;;) {
3442 ret = false;
3443 if (!nfs4_valid_open_stateid(state))
3444 break;
3445 seq = read_seqbegin(&state->seqlock);
3446 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3447 if (read_seqretry(&state->seqlock, seq))
3448 continue;
3449 break;
3450 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003451
3452 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003453 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003454
3455 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003456
3457 /* Did another OPEN bump the state's seqid? try again: */
3458 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003459 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003460 write_sequnlock(&state->seqlock);
3461 ret = true;
3462 break;
3463 }
3464
3465 /* server says we're behind but we haven't seen the update yet */
3466 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3467 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3468 write_sequnlock(&state->seqlock);
3469 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3470
zhouchuangao1fbbcd12021-05-09 19:34:37 -07003471 if (fatal_signal_pending(current))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003472 status = -EINTR;
3473 else
3474 if (schedule_timeout(5*HZ) != 0)
3475 status = 0;
3476
3477 finish_wait(&state->waitq, &wait);
3478
3479 if (!status)
3480 continue;
3481 if (status == -EINTR)
3482 break;
3483
3484 /* we slept the whole 5 seconds, we must have lost a seqid */
3485 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003486 ret = true;
3487 break;
3488 }
3489
3490 return ret;
3491}
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493struct nfs4_closedata {
3494 struct inode *inode;
3495 struct nfs4_state *state;
3496 struct nfs_closeargs arg;
3497 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003498 struct {
3499 struct nfs4_layoutreturn_args arg;
3500 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003501 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003502 u32 roc_barrier;
3503 bool roc;
3504 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003505 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003506 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507};
3508
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003509static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003510{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003511 struct nfs4_closedata *calldata = data;
3512 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003513 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003514
Trond Myklebustcf805162016-11-15 14:56:07 -05003515 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003516 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3517 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003518 nfs4_put_open_state(calldata->state);
3519 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003520 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003521 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003522 kfree(calldata);
3523}
3524
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003525static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003527 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003530 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003531 struct nfs4_exception exception = {
3532 .state = state,
3533 .inode = calldata->inode,
3534 .stateid = &calldata->arg.stateid,
3535 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Chuck Levera3ca5652012-03-01 17:00:40 -05003537 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003538 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3539 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003540 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003541
3542 /* Handle Layoutreturn errors */
Trond Myklebustf128de12021-01-04 13:18:03 -05003543 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3544 &calldata->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04003545 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003546
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003547 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 * the state_owner. we keep this around to process errors
3549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 switch (task->tk_status) {
3551 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003552 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003553 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003554 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003555 case -NFS4ERR_ACCESS:
3556 if (calldata->arg.bitmask != NULL) {
3557 calldata->arg.bitmask = NULL;
3558 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003559 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003560
3561 }
3562 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003563 case -NFS4ERR_OLD_STATEID:
3564 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003565 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003566 state))
3567 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003568 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003569 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003571 case -NFS4ERR_EXPIRED:
3572 nfs4_free_revoked_stateid(server,
3573 &calldata->arg.stateid,
3574 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003575 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003576 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003577 if (calldata->arg.fmode == 0)
3578 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003579 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003581 task->tk_status = nfs4_async_handle_exception(task,
3582 server, task->tk_status, &exception);
3583 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003584 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003586 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3587 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003588out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003589 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003590 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003591 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003592 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003593 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003594out_restart:
3595 task->tk_status = 0;
3596 rpc_restart_call_prepare(task);
3597 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003600static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003602 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003603 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003604 struct inode *inode = calldata->inode;
Trond Myklebustce2b4702021-03-25 18:15:36 -04003605 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003606 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003607 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003608 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003609
Chuck Levera3ca5652012-03-01 17:00:40 -05003610 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003611 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003612 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003613
Trond Myklebust88069f72009-12-08 08:33:16 -05003614 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003615 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003616 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3617 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3618 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003619 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003620 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003621 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003622 if (state->n_rdonly == 0)
3623 call_close |= is_rdonly;
3624 else if (is_rdonly)
3625 calldata->arg.fmode |= FMODE_READ;
3626 if (state->n_wronly == 0)
3627 call_close |= is_wronly;
3628 else if (is_wronly)
3629 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003630 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3631 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003632 } else if (is_rdwr)
3633 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3634
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003635 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3636 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003637 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003638 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003639
3640 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003641 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003642 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003643 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003644
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003645 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003646 nfs_release_seqid(calldata->arg.seqid);
3647 goto out_wait;
3648 }
3649
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003650 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3651 if (lo && !pnfs_layout_is_valid(lo)) {
3652 calldata->arg.lr_args = NULL;
3653 calldata->res.lr_res = NULL;
3654 }
3655
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003656 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003657 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003658
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003659 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003660 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003661 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebustce2b4702021-03-25 18:15:36 -04003662 nfs4_bitmask_set(calldata->arg.bitmask_store,
3663 server->cache_consistency_bitmask,
3664 inode, server, NULL);
3665 calldata->arg.bitmask = calldata->arg.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003666 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003667 calldata->arg.bitmask = NULL;
3668 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003669
Trond Myklebust6ae37332015-01-30 14:21:14 -05003670 calldata->arg.share_access =
3671 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3672 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003673
Trond Myklebustd8d84982016-12-19 12:14:44 -05003674 if (calldata->res.fattr == NULL)
3675 calldata->arg.bitmask = NULL;
3676 else if (calldata->arg.bitmask == NULL)
3677 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003678 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003679 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003680 &calldata->arg.seq_args,
3681 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003682 task) != 0)
3683 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003684 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003685 return;
3686out_no_action:
3687 task->tk_action = NULL;
3688out_wait:
3689 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690}
3691
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003692static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003693 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003694 .rpc_call_done = nfs4_close_done,
3695 .rpc_release = nfs4_free_closedata,
3696};
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698/*
3699 * It is possible for data to be read/written from a mem-mapped file
3700 * after the sys_close call (which hits the vfs layer as a flush).
3701 * This means that we can't safely call nfsv4 close on a file until
3702 * the inode is cleared. This in turn means that we are not good
3703 * NFSv4 citizens - we do not indicate to the server to update the file's
3704 * share state even when we are done with one of the three share
3705 * stateid's in the inode.
3706 *
3707 * NOTE: Caller must be holding the sp->so_owner semaphore!
3708 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003709int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003711 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003712 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003714 struct nfs4_state_owner *sp = state->owner;
3715 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003716 struct rpc_message msg = {
3717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3718 .rpc_cred = state->owner->so_cred,
3719 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003720 struct rpc_task_setup task_setup_data = {
3721 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003722 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003723 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003724 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003725 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003726 };
Trond Myklebust95121352005-10-18 14:20:12 -07003727 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003729 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3730 &task_setup_data.rpc_client, &msg);
3731
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003732 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003734 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003735 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003736 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003738 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003739 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3740 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003742 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3743 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003744 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003745 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003746 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003747 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003748 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003749 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003750 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003751 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003752 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003753 calldata->lr.roc = pnfs_roc(state->inode,
3754 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3755 if (calldata->lr.roc) {
3756 calldata->arg.lr_args = &calldata->lr.arg;
3757 calldata->res.lr_res = &calldata->lr.res;
3758 }
Al Viro643168c2011-06-22 18:20:23 -04003759 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003760
Trond Myklebust1174dd12010-12-21 10:52:24 -05003761 msg.rpc_argp = &calldata->arg;
3762 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003763 task_setup_data.callback_data = calldata;
3764 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003765 if (IS_ERR(task))
3766 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003767 status = 0;
3768 if (wait)
3769 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003770 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003771 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003772out_free_calldata:
3773 kfree(calldata);
3774out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003775 nfs4_put_open_state(state);
3776 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003777 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778}
3779
Trond Myklebust2b484292010-09-17 10:56:51 -04003780static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003781nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3782 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003785 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3786
3787 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
Trond Myklebust565277f2007-10-15 18:17:53 -04003789 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003790 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003791
3792 nfs4_label_release_security(label);
3793
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003794 if (IS_ERR(state))
3795 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003796 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
Trond Myklebust1185a552009-12-03 15:54:02 -05003799static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003800{
3801 if (ctx->state == NULL)
3802 return;
3803 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003804 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003805 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003806 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003807}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Trond Myklebustb944dba2013-11-04 15:20:20 -05003809#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3810#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003811#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3814{
Kinglong Mee8c612822015-08-26 21:12:58 +08003815 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003816 struct nfs4_server_caps_arg args = {
3817 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003818 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003819 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 struct nfs4_server_caps_res res = {};
3821 struct rpc_message msg = {
3822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003823 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 .rpc_resp = &res,
3825 };
3826 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003827 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Kinglong Mee8c612822015-08-26 21:12:58 +08003829 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3830 FATTR4_WORD0_FH_EXPIRE_TYPE |
3831 FATTR4_WORD0_LINK_SUPPORT |
3832 FATTR4_WORD0_SYMLINK_SUPPORT |
3833 FATTR4_WORD0_ACLSUPPORT;
3834 if (minorversion)
3835 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3836
Bryan Schumaker7c513052011-03-24 17:12:24 +00003837 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003839 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003840 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003841 case 0:
3842 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3843 res.attr_bitmask[2] = 0;
3844 break;
3845 case 1:
3846 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3847 break;
3848 case 2:
3849 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003852 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3853 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3854 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3855 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003856 NFS_CAP_CTIME|NFS_CAP_MTIME|
3857 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003858 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3859 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 server->caps |= NFS_CAP_ACLS;
3861 if (res.has_links != 0)
3862 server->caps |= NFS_CAP_HARDLINKS;
3863 if (res.has_symlinks != 0)
3864 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003865 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3866 server->caps |= NFS_CAP_FILEID;
3867 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3868 server->caps |= NFS_CAP_MODE;
3869 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3870 server->caps |= NFS_CAP_NLINK;
3871 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3872 server->caps |= NFS_CAP_OWNER;
3873 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3874 server->caps |= NFS_CAP_OWNER_GROUP;
3875 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3876 server->caps |= NFS_CAP_ATIME;
3877 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3878 server->caps |= NFS_CAP_CTIME;
3879 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3880 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003881#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3882 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3883 server->caps |= NFS_CAP_SECURITY_LABEL;
3884#endif
3885 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3886 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003887 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003888
Trond Myklebusta65318b2009-03-11 14:10:28 -04003889 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3890 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3891 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003892 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003893
3894 /* Avoid a regression due to buggy server */
3895 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3896 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003897 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3898 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003899
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003901 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003903
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 return status;
3905}
3906
Trond Myklebust55a97592006-06-09 09:34:19 -04003907int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908{
Trond Myklebust0688e642019-04-07 13:59:09 -04003909 struct nfs4_exception exception = {
3910 .interruptible = true,
3911 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 int err;
3913 do {
3914 err = nfs4_handle_exception(server,
3915 _nfs4_server_capabilities(server, fhandle),
3916 &exception);
3917 } while (exception.retry);
3918 return err;
3919}
3920
3921static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3922 struct nfs_fsinfo *info)
3923{
David Quigleyaa9c2662013-05-22 12:50:44 -04003924 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003926 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 };
3928 struct nfs4_lookup_res res = {
3929 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003930 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 .fh = fhandle,
3932 };
3933 struct rpc_message msg = {
3934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3935 .rpc_argp = &args,
3936 .rpc_resp = &res,
3937 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003938
David Quigleyaa9c2662013-05-22 12:50:44 -04003939 bitmask[0] = nfs4_fattr_bitmap[0];
3940 bitmask[1] = nfs4_fattr_bitmap[1];
3941 /*
3942 * Process the label in the upcoming getfattr
3943 */
3944 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3945
Trond Myklebust0e574af2005-10-27 22:12:38 -04003946 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003947 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948}
3949
3950static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3951 struct nfs_fsinfo *info)
3952{
Trond Myklebust0688e642019-04-07 13:59:09 -04003953 struct nfs4_exception exception = {
3954 .interruptible = true,
3955 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 int err;
3957 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003958 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003959 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003960 switch (err) {
3961 case 0:
3962 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003963 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003964 default:
3965 err = nfs4_handle_exception(server, err, &exception);
3966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003968out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return err;
3970}
3971
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003972static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3973 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3974{
Trond Myklebustc2190662013-08-26 19:23:04 -04003975 struct rpc_auth_create_args auth_args = {
3976 .pseudoflavor = flavor,
3977 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003978 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003979
Trond Myklebustc2190662013-08-26 19:23:04 -04003980 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003981 if (IS_ERR(auth))
3982 return -EACCES;
3983 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003984}
3985
Chuck Lever9a744ba2013-03-16 15:56:02 -04003986/*
3987 * Retry pseudoroot lookup with various security flavors. We do this when:
3988 *
3989 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3990 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3991 *
3992 * Returns zero on success, or a negative NFS4ERR value, or a
3993 * negative errno value.
3994 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003995static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003996 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003998 /* Per 3530bis 15.33.5 */
3999 static const rpc_authflavor_t flav_array[] = {
4000 RPC_AUTH_GSS_KRB5P,
4001 RPC_AUTH_GSS_KRB5I,
4002 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04004003 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04004004 RPC_AUTH_NULL,
4005 };
4006 int status = -EPERM;
4007 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04004009 if (server->auth_info.flavor_len > 0) {
4010 /* try each flavor specified by user */
4011 for (i = 0; i < server->auth_info.flavor_len; i++) {
4012 status = nfs4_lookup_root_sec(server, fhandle, info,
4013 server->auth_info.flavors[i]);
4014 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4015 continue;
4016 break;
4017 }
4018 } else {
4019 /* no flavors specified by user, try default list */
4020 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4021 status = nfs4_lookup_root_sec(server, fhandle, info,
4022 flav_array[i]);
4023 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4024 continue;
4025 break;
4026 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004027 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004028
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004029 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004030 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004031 * to access the mount. It could also mean that we tried to mount
4032 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4033 * existing mount programs don't handle -EACCES very well so it should
4034 * be mapped to -EPERM instead.
4035 */
4036 if (status == -EACCES)
4037 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004038 return status;
4039}
4040
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004041/**
4042 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4043 * @server: initialized nfs_server handle
4044 * @fhandle: we fill in the pseudo-fs root file handle
4045 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004046 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004047 *
4048 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004049 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004050int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004051 struct nfs_fsinfo *info,
4052 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004053{
Andre Przywarac7757072015-04-23 17:17:40 +01004054 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004055
Andre Przywarac7757072015-04-23 17:17:40 +01004056 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004057 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004058
4059 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004060 status = server->nfs_client->cl_mvops->find_root_sec(server,
4061 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 if (status == 0)
4064 status = nfs4_server_capabilities(server, fhandle);
4065 if (status == 0)
4066 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004067
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004068 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
Bryan Schumakerbae36242012-05-10 15:07:31 -04004071static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4072 struct nfs_fsinfo *info)
4073{
4074 int error;
4075 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004076 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004077
4078 error = nfs4_server_capabilities(server, mntfh);
4079 if (error < 0) {
4080 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4081 return error;
4082 }
4083
Trond Myklebusta841b542018-04-07 13:50:59 -04004084 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004085 if (error < 0) {
4086 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004087 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004088 }
4089
4090 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4091 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4092 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4093
Scott Mayhew779df6a2020-03-03 17:58:37 -05004094out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004095 return error;
4096}
4097
Manoj Naik6b97fd32006-06-09 09:34:29 -04004098/*
4099 * Get locations and (maybe) other attributes of a referral.
4100 * Note that we'll actually follow the referral later when
4101 * we detect fsid mismatch in inode revalidation
4102 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004103static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4104 const struct qstr *name, struct nfs_fattr *fattr,
4105 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004106{
4107 int status = -ENOMEM;
4108 struct page *page = NULL;
4109 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004110
4111 page = alloc_page(GFP_KERNEL);
4112 if (page == NULL)
4113 goto out;
4114 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4115 if (locations == NULL)
4116 goto out;
4117
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004118 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004119 if (status != 0)
4120 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004121
4122 /*
4123 * If the fsid didn't change, this is a migration event, not a
4124 * referral. Cause us to drop into the exception handler, which
4125 * will kick off migration recovery.
4126 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004127 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004128 dprintk("%s: server did not return a different fsid for"
4129 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004130 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004131 goto out;
4132 }
Andy Adamson533eb462011-06-13 18:25:56 -04004133 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4134 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004135
Andy Adamson533eb462011-06-13 18:25:56 -04004136 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004137 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004138 memset(fhandle, 0, sizeof(struct nfs_fh));
4139out:
4140 if (page)
4141 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004142 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004143 return status;
4144}
4145
David Quigley1775fd32013-05-22 12:50:42 -04004146static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004147 struct nfs_fattr *fattr, struct nfs4_label *label,
4148 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
Trond Myklebust771734f2018-04-07 13:54:23 -04004150 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 struct nfs4_getattr_arg args = {
4152 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004153 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 };
4155 struct nfs4_getattr_res res = {
4156 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004157 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 .server = server,
4159 };
4160 struct rpc_message msg = {
4161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4162 .rpc_argp = &args,
4163 .rpc_resp = &res,
4164 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004165 unsigned short task_flags = 0;
4166
4167 /* Is this is an attribute revalidation, subject to softreval? */
4168 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4169 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004170
Trond Myklebust771734f2018-04-07 13:54:23 -04004171 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004172
Trond Myklebust0e574af2005-10-27 22:12:38 -04004173 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004174 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4175 return nfs4_do_call_sync(server->client, server, &msg,
4176 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177}
4178
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004179int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004180 struct nfs_fattr *fattr, struct nfs4_label *label,
4181 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182{
Trond Myklebust0688e642019-04-07 13:59:09 -04004183 struct nfs4_exception exception = {
4184 .interruptible = true,
4185 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 int err;
4187 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004188 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004189 trace_nfs4_getattr(server, fhandle, fattr, err);
4190 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 &exception);
4192 } while (exception.retry);
4193 return err;
4194}
4195
4196/*
4197 * The file is not closed if it is opened due to the a request to change
4198 * the size of the file. The open call will not be needed once the
4199 * VFS layer lookup-intents are implemented.
4200 *
4201 * Close is called when the inode is destroyed.
4202 * If we haven't opened the file for O_WRONLY, we
4203 * need to in the size_change case to obtain a stateid.
4204 *
4205 * Got race?
4206 * Because OPEN is always done by name in nfsv4, it is
4207 * possible that we opened a different file by the same
4208 * name. We can recognize this race condition, but we
4209 * can't do anything about it besides returning an error.
4210 *
4211 * This will be fixed with VFS changes (lookup-intent).
4212 */
4213static int
4214nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4215 struct iattr *sattr)
4216{
David Howells2b0143b2015-03-17 22:25:59 +00004217 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004218 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004219 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004220 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 int status;
4222
Peng Tao88ac8152014-09-12 11:04:10 +08004223 if (pnfs_ld_layoutret_on_setattr(inode) &&
4224 sattr->ia_valid & ATTR_SIZE &&
4225 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004226 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004227
Trond Myklebust0e574af2005-10-27 22:12:38 -04004228 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Andy Adamson26699402012-05-30 16:12:24 -04004230 /* Deal with open(O_TRUNC) */
4231 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004232 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004233
4234 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004235 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004236 return 0;
4237
Trond Myklebustd5308382005-11-04 15:33:38 -05004238 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004239 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004240
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004241 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004242 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004243 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004244 }
4245
David Quigley14c43f72013-05-22 12:50:43 -04004246 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4247 if (IS_ERR(label))
4248 return PTR_ERR(label);
4249
Trond Myklebust199366f2018-03-20 16:43:18 -04004250 /* Return any delegations if we're going to change ACLs */
4251 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004252 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004253
NeilBrown29b59f92016-10-13 15:26:47 +11004254 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004255 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004256 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004257 nfs_setsecurity(inode, fattr, label);
4258 }
David Quigley14c43f72013-05-22 12:50:43 -04004259 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 return status;
4261}
4262
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004263static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004264 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004265 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004266{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004267 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004268 int status;
4269 struct nfs4_lookup_arg args = {
4270 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004271 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004272 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004273 };
4274 struct nfs4_lookup_res res = {
4275 .server = server,
4276 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004277 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004278 .fh = fhandle,
4279 };
4280 struct rpc_message msg = {
4281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4282 .rpc_argp = &args,
4283 .rpc_resp = &res,
4284 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004285 unsigned short task_flags = 0;
4286
4287 /* Is this is an attribute revalidation, subject to softreval? */
4288 if (nfs_lookup_is_soft_revalidate(dentry))
4289 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004290
David Quigleyaa9c2662013-05-22 12:50:44 -04004291 args.bitmask = nfs4_bitmask(server, label);
4292
David Howells2b3de442006-08-22 20:06:09 -04004293 nfs_fattr_init(fattr);
4294
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004295 dprintk("NFS call lookup %pd2\n", dentry);
4296 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4297 status = nfs4_do_call_sync(clnt, server, &msg,
4298 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 dprintk("NFS reply lookup: %d\n", status);
4300 return status;
4301}
4302
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004303static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004304{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004305 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004306 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004307 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4308 fattr->nlink = 2;
4309}
4310
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004311static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004312 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004313 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004314{
Trond Myklebust0688e642019-04-07 13:59:09 -04004315 struct nfs4_exception exception = {
4316 .interruptible = true,
4317 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004318 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004319 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004320 int err;
4321 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004322 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004323 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004324 switch (err) {
4325 case -NFS4ERR_BADNAME:
4326 err = -ENOENT;
4327 goto out;
4328 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004329 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004330 if (err == -NFS4ERR_MOVED)
4331 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004332 goto out;
4333 case -NFS4ERR_WRONGSEC:
4334 err = -EPERM;
4335 if (client != *clnt)
4336 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004337 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004338 if (IS_ERR(client))
4339 return PTR_ERR(client);
4340
4341 exception.retry = 1;
4342 break;
4343 default:
4344 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4345 }
4346 } while (exception.retry);
4347
4348out:
4349 if (err == 0)
4350 *clnt = client;
4351 else if (client != *clnt)
4352 rpc_shutdown_client(client);
4353
4354 return err;
4355}
4356
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004357static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004358 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4359 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004361 int status;
4362 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004363
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004364 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004365 if (client != NFS_CLIENT(dir)) {
4366 rpc_shutdown_client(client);
4367 nfs_fixup_secinfo_attributes(fattr);
4368 }
4369 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370}
4371
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004372struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004373nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004374 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4375{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004376 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004377 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004378
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004379 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004380 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004381 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004382 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004383}
4384
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004385static int _nfs4_proc_lookupp(struct inode *inode,
4386 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4387 struct nfs4_label *label)
4388{
4389 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4390 struct nfs_server *server = NFS_SERVER(inode);
4391 int status;
4392 struct nfs4_lookupp_arg args = {
4393 .bitmask = server->attr_bitmask,
4394 .fh = NFS_FH(inode),
4395 };
4396 struct nfs4_lookupp_res res = {
4397 .server = server,
4398 .fattr = fattr,
4399 .label = label,
4400 .fh = fhandle,
4401 };
4402 struct rpc_message msg = {
4403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4404 .rpc_argp = &args,
4405 .rpc_resp = &res,
4406 };
4407
4408 args.bitmask = nfs4_bitmask(server, label);
4409
4410 nfs_fattr_init(fattr);
4411
4412 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4413 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4414 &res.seq_res, 0);
4415 dprintk("NFS reply lookupp: %d\n", status);
4416 return status;
4417}
4418
4419static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4420 struct nfs_fattr *fattr, struct nfs4_label *label)
4421{
Trond Myklebust0688e642019-04-07 13:59:09 -04004422 struct nfs4_exception exception = {
4423 .interruptible = true,
4424 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004425 int err;
4426 do {
4427 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4428 trace_nfs4_lookupp(inode, err);
4429 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4430 &exception);
4431 } while (exception.retry);
4432 return err;
4433}
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4436{
Trond Myklebust76b32992007-08-10 17:45:11 -04004437 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 struct nfs4_accessargs args = {
4439 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004440 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004442 struct nfs4_accessres res = {
4443 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004444 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 struct rpc_message msg = {
4446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4447 .rpc_argp = &args,
4448 .rpc_resp = &res,
4449 .rpc_cred = entry->cred,
4450 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004451 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
Trond Myklebust7c672652018-06-04 15:00:53 -04004453 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004454 res.fattr = nfs_alloc_fattr();
4455 if (res.fattr == NULL)
4456 return -ENOMEM;
4457 args.bitmask = server->cache_consistency_bitmask;
4458 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004459 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004461 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004462 if (res.fattr)
4463 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004465 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return status;
4467}
4468
4469static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4470{
Trond Myklebust0688e642019-04-07 13:59:09 -04004471 struct nfs4_exception exception = {
4472 .interruptible = true,
4473 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 int err;
4475 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004476 err = _nfs4_proc_access(inode, entry);
4477 trace_nfs4_access(inode, err);
4478 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 &exception);
4480 } while (exception.retry);
4481 return err;
4482}
4483
4484/*
4485 * TODO: For the time being, we don't try to get any attributes
4486 * along with any of the zero-copy operations READ, READDIR,
4487 * READLINK, WRITE.
4488 *
4489 * In the case of the first three, we want to put the GETATTR
4490 * after the read-type operation -- this is because it is hard
4491 * to predict the length of a GETATTR response in v4, and thus
4492 * align the READ data correctly. This means that the GETATTR
4493 * may end up partially falling into the page cache, and we should
4494 * shift it into the 'tail' of the xdr_buf before processing.
4495 * To do this efficiently, we need to know the total length
4496 * of data received, which doesn't seem to be available outside
4497 * of the RPC layer.
4498 *
4499 * In the case of WRITE, we also want to put the GETATTR after
4500 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004501 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 *
4503 * Both of these changes to the XDR layer would in fact be quite
4504 * minor, but I decided to leave them for a subsequent patch.
4505 */
4506static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4507 unsigned int pgbase, unsigned int pglen)
4508{
4509 struct nfs4_readlink args = {
4510 .fh = NFS_FH(inode),
4511 .pgbase = pgbase,
4512 .pglen = pglen,
4513 .pages = &page,
4514 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004515 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 struct rpc_message msg = {
4517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4518 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004519 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 };
4521
Bryan Schumaker7c513052011-03-24 17:12:24 +00004522 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 -07004523}
4524
4525static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4526 unsigned int pgbase, unsigned int pglen)
4527{
Trond Myklebust0688e642019-04-07 13:59:09 -04004528 struct nfs4_exception exception = {
4529 .interruptible = true,
4530 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 int err;
4532 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004533 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4534 trace_nfs4_readlink(inode, err);
4535 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 &exception);
4537 } while (exception.retry);
4538 return err;
4539}
4540
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004542 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544static int
4545nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004546 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004548 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004549 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004550 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 int status = 0;
4553
NeilBrown532d4de2016-10-13 15:26:47 +11004554 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004555 if (IS_ERR(ctx))
4556 return PTR_ERR(ctx);
4557
David Quigleyaa9c2662013-05-22 12:50:44 -04004558 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4559
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004560 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4561 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004562 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 if (IS_ERR(state)) {
4564 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004565 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004568 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004569 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 return status;
4571}
4572
Trond Myklebust3c591172018-07-31 15:54:10 -04004573static int
4574_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575{
Trond Myklebust16e42952005-10-27 22:12:44 -04004576 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004577 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004579 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004581 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004582 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004583 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4586 .rpc_argp = &args,
4587 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004589 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004590 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004591
Bryan Schumaker7c513052011-03-24 17:12:24 +00004592 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004593 if (status == 0) {
4594 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004595 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4596 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004597 /* Removing a directory decrements nlink in the parent */
4598 if (ftype == NF4DIR && dir->i_nlink > 2)
4599 nfs4_dec_nlink_locked(dir);
4600 spin_unlock(&dir->i_lock);
4601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 return status;
4603}
4604
Trond Myklebust912678d2018-03-20 16:43:15 -04004605static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4606{
Trond Myklebust0688e642019-04-07 13:59:09 -04004607 struct nfs4_exception exception = {
4608 .interruptible = true,
4609 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004610 struct inode *inode = d_inode(dentry);
4611 int err;
4612
Trond Myklebustc01d3642018-03-20 16:43:20 -04004613 if (inode) {
4614 if (inode->i_nlink == 1)
4615 nfs4_inode_return_delegation(inode);
4616 else
4617 nfs4_inode_make_writeable(inode);
4618 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004619 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004620 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004621 trace_nfs4_remove(dir, &dentry->d_name, err);
4622 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4623 &exception);
4624 } while (exception.retry);
4625 return err;
4626}
4627
4628static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629{
Trond Myklebust0688e642019-04-07 13:59:09 -04004630 struct nfs4_exception exception = {
4631 .interruptible = true,
4632 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004634
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004636 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004637 trace_nfs4_remove(dir, name, err);
4638 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 &exception);
4640 } while (exception.retry);
4641 return err;
4642}
4643
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004644static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4645 struct dentry *dentry,
4646 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004648 struct nfs_removeargs *args = msg->rpc_argp;
4649 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Trond Myklebust977fcc22018-03-20 16:43:17 -04004651 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004653 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004654
4655 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004656
4657 if (inode)
4658 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659}
4660
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004661static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4662{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004663 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004664 &data->args.seq_args,
4665 &data->res.seq_res,
4666 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667}
4668
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004669static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004671 struct nfs_unlinkdata *data = task->tk_calldata;
4672 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004673
Trond Myklebust14516c32010-07-31 14:29:06 -04004674 if (!nfs4_sequence_done(task, &res->seq_res))
4675 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004676 if (nfs4_async_handle_error(task, res->server, NULL,
4677 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004678 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004679 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004680 nfs4_update_changeattr(dir, &res->cinfo,
4681 res->dir_attr->time_start,
4682 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004683 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004686static void nfs4_proc_rename_setup(struct rpc_message *msg,
4687 struct dentry *old_dentry,
4688 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004689{
Jeff Laytond3d41522010-09-17 17:31:57 -04004690 struct nfs_renameargs *arg = msg->rpc_argp;
4691 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004692 struct inode *old_inode = d_inode(old_dentry);
4693 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004694
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004695 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004696 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004697 if (new_inode)
4698 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004699 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004700 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004701 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004702}
4703
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004704static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4705{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004706 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004707 &data->args.seq_args,
4708 &data->res.seq_res,
4709 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004710}
4711
4712static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4713 struct inode *new_dir)
4714{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004715 struct nfs_renamedata *data = task->tk_calldata;
4716 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004717
4718 if (!nfs4_sequence_done(task, &res->seq_res))
4719 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004720 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004721 return 0;
4722
Trond Myklebustc733c492017-01-11 12:32:26 -05004723 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004724 if (new_dir != old_dir) {
4725 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004726 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004727 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004728 NFS_INO_INVALID_OTHER |
4729 NFS_INO_INVALID_DATA);
4730 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004731 res->new_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004732 NFS_INO_INVALID_OTHER |
4733 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004734 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004735 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004736 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004737 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004738 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004739 return 1;
4740}
4741
Al Virobeffb8f2016-07-20 16:34:42 -04004742static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004744 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004745 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 struct nfs4_link_arg arg = {
4747 .fh = NFS_FH(inode),
4748 .dir_fh = NFS_FH(dir),
4749 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004750 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004752 struct nfs4_link_res res = {
4753 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004754 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004755 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 struct rpc_message msg = {
4757 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4758 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004759 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004761 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Trond Myklebust136f2622010-04-16 16:22:49 -04004763 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004764 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004765 goto out;
4766
David Quigley14c43f72013-05-22 12:50:43 -04004767 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4768 if (IS_ERR(res.label)) {
4769 status = PTR_ERR(res.label);
4770 goto out;
4771 }
4772
Trond Myklebustc01d3642018-03-20 16:43:20 -04004773 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004774 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004775
Bryan Schumaker7c513052011-03-24 17:12:24 +00004776 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004777 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004778 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4779 NFS_INO_INVALID_DATA);
David Quigleyaa9c2662013-05-22 12:50:44 -04004780 status = nfs_post_op_update_inode(inode, res.fattr);
4781 if (!status)
4782 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004783 }
David Quigley14c43f72013-05-22 12:50:43 -04004784
4785
4786 nfs4_label_free(res.label);
4787
Trond Myklebust136f2622010-04-16 16:22:49 -04004788out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004789 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 return status;
4791}
4792
Al Virobeffb8f2016-07-20 16:34:42 -04004793static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794{
Trond Myklebust0688e642019-04-07 13:59:09 -04004795 struct nfs4_exception exception = {
4796 .interruptible = true,
4797 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 int err;
4799 do {
4800 err = nfs4_handle_exception(NFS_SERVER(inode),
4801 _nfs4_proc_link(inode, dir, name),
4802 &exception);
4803 } while (exception.retry);
4804 return err;
4805}
4806
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004807struct nfs4_createdata {
4808 struct rpc_message msg;
4809 struct nfs4_create_arg arg;
4810 struct nfs4_create_res res;
4811 struct nfs_fh fh;
4812 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004813 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004814};
4815
4816static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004817 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004818{
4819 struct nfs4_createdata *data;
4820
4821 data = kzalloc(sizeof(*data), GFP_KERNEL);
4822 if (data != NULL) {
4823 struct nfs_server *server = NFS_SERVER(dir);
4824
David Quigley14c43f72013-05-22 12:50:43 -04004825 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4826 if (IS_ERR(data->label))
4827 goto out_free;
4828
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004829 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4830 data->msg.rpc_argp = &data->arg;
4831 data->msg.rpc_resp = &data->res;
4832 data->arg.dir_fh = NFS_FH(dir);
4833 data->arg.server = server;
4834 data->arg.name = name;
4835 data->arg.attrs = sattr;
4836 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004837 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004838 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004839 data->res.server = server;
4840 data->res.fh = &data->fh;
4841 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004842 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004843 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004844 }
4845 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004846out_free:
4847 kfree(data);
4848 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004849}
4850
4851static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4852{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004853 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004854 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004855 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004856 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004857 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4858 data->res.fattr->time_start,
4859 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004860 /* Creating a directory bumps nlink in the parent */
4861 if (data->arg.ftype == NF4DIR)
4862 nfs4_inc_nlink_locked(dir);
4863 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004864 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004865 }
4866 return status;
4867}
4868
4869static void nfs4_free_createdata(struct nfs4_createdata *data)
4870{
David Quigley14c43f72013-05-22 12:50:43 -04004871 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004872 kfree(data);
4873}
4874
Chuck Lever4f390c12006-08-22 20:06:22 -04004875static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004876 struct page *page, unsigned int len, struct iattr *sattr,
4877 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004879 struct nfs4_createdata *data;
4880 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Chuck Lever94a6d752006-08-22 20:06:23 -04004882 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004883 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004884
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004885 status = -ENOMEM;
4886 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4887 if (data == NULL)
4888 goto out;
4889
4890 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4891 data->arg.u.symlink.pages = &page;
4892 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004893 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004895 status = nfs4_do_create(dir, dentry, data);
4896
4897 nfs4_free_createdata(data);
4898out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 return status;
4900}
4901
Chuck Lever4f390c12006-08-22 20:06:22 -04004902static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004903 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904{
Trond Myklebust0688e642019-04-07 13:59:09 -04004905 struct nfs4_exception exception = {
4906 .interruptible = true,
4907 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004908 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004910
4911 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004914 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4915 trace_nfs4_symlink(dir, &dentry->d_name, err);
4916 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 &exception);
4918 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004919
4920 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 return err;
4922}
4923
4924static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004925 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004927 struct nfs4_createdata *data;
4928 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004930 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4931 if (data == NULL)
4932 goto out;
4933
David Quigley1775fd32013-05-22 12:50:42 -04004934 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004935 status = nfs4_do_create(dir, dentry, data);
4936
4937 nfs4_free_createdata(data);
4938out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 return status;
4940}
4941
4942static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4943 struct iattr *sattr)
4944{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004945 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004946 struct nfs4_exception exception = {
4947 .interruptible = true,
4948 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004949 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004951
David Quigleyaa9c2662013-05-22 12:50:44 -04004952 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4953
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004954 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4955 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004957 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4958 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4959 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 &exception);
4961 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004962 nfs4_label_release_security(label);
4963
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 return err;
4965}
4966
NeilBrown684f39b2018-12-03 11:30:30 +11004967static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004968 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969{
David Howells2b0143b2015-03-17 22:25:59 +00004970 struct inode *dir = d_inode(dentry);
Olga Kornievskaia78c90262020-11-06 16:03:38 -05004971 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 struct nfs4_readdir_arg args = {
4973 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004974 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 .pgbase = 0,
4976 .count = count,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004977 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 };
4979 struct nfs4_readdir_res res;
4980 struct rpc_message msg = {
4981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4982 .rpc_argp = &args,
4983 .rpc_resp = &res,
4984 .rpc_cred = cred,
4985 };
4986 int status;
4987
Al Viro6de14722013-09-16 10:53:17 -04004988 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4989 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004990 (unsigned long long)cookie);
Olga Kornievskaia78c90262020-11-06 16:03:38 -05004991 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4992 args.bitmask = server->attr_bitmask_nl;
4993 else
4994 args.bitmask = server->attr_bitmask;
4995
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004996 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 res.pgbase = args.pgbase;
Olga Kornievskaia78c90262020-11-06 16:03:38 -05004998 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4999 &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05005000 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04005001 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05005002 status += args.pgbase;
5003 }
Trond Myklebustc4812992007-09-28 17:11:45 -04005004
5005 nfs_invalidate_atime(dir);
5006
Harvey Harrison3110ff82008-05-02 13:42:44 -07005007 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 return status;
5009}
5010
NeilBrown684f39b2018-12-03 11:30:30 +11005011static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04005012 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013{
Trond Myklebust0688e642019-04-07 13:59:09 -04005014 struct nfs4_exception exception = {
5015 .interruptible = true,
5016 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 int err;
5018 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005019 err = _nfs4_proc_readdir(dentry, cred, cookie,
5020 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00005021 trace_nfs4_readdir(d_inode(dentry), err);
5022 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 &exception);
5024 } while (exception.retry);
5025 return err;
5026}
5027
5028static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005029 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005031 struct nfs4_createdata *data;
5032 int mode = sattr->ia_mode;
5033 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005035 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5036 if (data == NULL)
5037 goto out;
5038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005040 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005042 data->arg.ftype = NF4BLK;
5043 data->arg.u.device.specdata1 = MAJOR(rdev);
5044 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 }
5046 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005047 data->arg.ftype = NF4CHR;
5048 data->arg.u.device.specdata1 = MAJOR(rdev);
5049 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005050 } else if (!S_ISSOCK(mode)) {
5051 status = -EINVAL;
5052 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 }
David Quigley1775fd32013-05-22 12:50:42 -04005054
David Quigleyaa9c2662013-05-22 12:50:44 -04005055 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005056 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005057out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005058 nfs4_free_createdata(data);
5059out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 return status;
5061}
5062
5063static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5064 struct iattr *sattr, dev_t rdev)
5065{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005066 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005067 struct nfs4_exception exception = {
5068 .interruptible = true,
5069 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005070 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005072
David Quigleyaa9c2662013-05-22 12:50:44 -04005073 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5074
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005075 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5076 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005078 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5079 trace_nfs4_mknod(dir, &dentry->d_name, err);
5080 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 &exception);
5082 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005083
5084 nfs4_label_release_security(label);
5085
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 return err;
5087}
5088
5089static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5090 struct nfs_fsstat *fsstat)
5091{
5092 struct nfs4_statfs_arg args = {
5093 .fh = fhandle,
5094 .bitmask = server->attr_bitmask,
5095 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005096 struct nfs4_statfs_res res = {
5097 .fsstat = fsstat,
5098 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 struct rpc_message msg = {
5100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5101 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005102 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 };
5104
Trond Myklebust0e574af2005-10-27 22:12:38 -04005105 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005106 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107}
5108
5109static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5110{
Trond Myklebust0688e642019-04-07 13:59:09 -04005111 struct nfs4_exception exception = {
5112 .interruptible = true,
5113 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 int err;
5115 do {
5116 err = nfs4_handle_exception(server,
5117 _nfs4_proc_statfs(server, fhandle, fsstat),
5118 &exception);
5119 } while (exception.retry);
5120 return err;
5121}
5122
5123static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5124 struct nfs_fsinfo *fsinfo)
5125{
5126 struct nfs4_fsinfo_arg args = {
5127 .fh = fhandle,
5128 .bitmask = server->attr_bitmask,
5129 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005130 struct nfs4_fsinfo_res res = {
5131 .fsinfo = fsinfo,
5132 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 struct rpc_message msg = {
5134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5135 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005136 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 };
5138
Bryan Schumaker7c513052011-03-24 17:12:24 +00005139 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140}
5141
5142static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5143{
Trond Myklebust0688e642019-04-07 13:59:09 -04005144 struct nfs4_exception exception = {
5145 .interruptible = true,
5146 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 int err;
5148
5149 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005150 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005151 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005152 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005153 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005154 break;
5155 }
5156 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 } while (exception.retry);
5158 return err;
5159}
5160
5161static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5162{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005163 int error;
5164
Trond Myklebust0e574af2005-10-27 22:12:38 -04005165 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005166 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005167 if (error == 0) {
5168 /* block layout checks this! */
5169 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005170 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005171 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005172
5173 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174}
5175
5176static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5177 struct nfs_pathconf *pathconf)
5178{
5179 struct nfs4_pathconf_arg args = {
5180 .fh = fhandle,
5181 .bitmask = server->attr_bitmask,
5182 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005183 struct nfs4_pathconf_res res = {
5184 .pathconf = pathconf,
5185 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 struct rpc_message msg = {
5187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5188 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005189 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 };
5191
5192 /* None of the pathconf attributes are mandatory to implement */
5193 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5194 memset(pathconf, 0, sizeof(*pathconf));
5195 return 0;
5196 }
5197
Trond Myklebust0e574af2005-10-27 22:12:38 -04005198 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005199 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
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{
Trond Myklebust0688e642019-04-07 13:59:09 -04005205 struct nfs4_exception exception = {
5206 .interruptible = true,
5207 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 int err;
5209
5210 do {
5211 err = nfs4_handle_exception(server,
5212 _nfs4_proc_pathconf(server, fhandle, pathconf),
5213 &exception);
5214 } while (exception.retry);
5215 return err;
5216}
5217
Trond Myklebust5521abf2013-03-16 20:54:34 -04005218int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005219 const struct nfs_open_context *ctx,
5220 const struct nfs_lock_context *l_ctx,
5221 fmode_t fmode)
5222{
NeilBrown17393472016-10-13 15:26:47 +11005223 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005224}
5225EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5226
Trond Myklebust5521abf2013-03-16 20:54:34 -04005227static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5228 const struct nfs_open_context *ctx,
5229 const struct nfs_lock_context *l_ctx,
5230 fmode_t fmode)
5231{
Ben Dooksd49dd112019-10-16 17:28:21 +01005232 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005233
Trond Myklebuste1253be2014-03-05 08:44:23 -05005234 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005235 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005236 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005237 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005238}
5239
5240static bool nfs4_error_stateid_expired(int err)
5241{
5242 switch (err) {
5243 case -NFS4ERR_DELEG_REVOKED:
5244 case -NFS4ERR_ADMIN_REVOKED:
5245 case -NFS4ERR_BAD_STATEID:
5246 case -NFS4ERR_STALE_STATEID:
5247 case -NFS4ERR_OLD_STATEID:
5248 case -NFS4ERR_OPENMODE:
5249 case -NFS4ERR_EXPIRED:
5250 return true;
5251 }
5252 return false;
5253}
5254
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005255static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005257 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005259 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005260 if (task->tk_status < 0) {
5261 struct nfs4_exception exception = {
5262 .inode = hdr->inode,
5263 .state = hdr->args.context->state,
5264 .stateid = &hdr->args.stateid,
5265 };
5266 task->tk_status = nfs4_async_handle_exception(task,
5267 server, task->tk_status, &exception);
5268 if (exception.retry) {
5269 rpc_restart_call_prepare(task);
5270 return -EAGAIN;
5271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005273
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005275 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277}
5278
Trond Myklebust5521abf2013-03-16 20:54:34 -04005279static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005280 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005281{
5282
5283 if (!nfs4_error_stateid_expired(task->tk_status) ||
5284 nfs4_stateid_is_current(&args->stateid,
5285 args->context,
5286 args->lock_context,
5287 FMODE_READ))
5288 return false;
5289 rpc_restart_call_prepare(task);
5290 return true;
5291}
5292
Anna Schumakerc5675522014-05-28 13:41:22 -04005293static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5294 struct nfs_pgio_header *hdr)
5295{
5296 struct nfs_server *server = NFS_SERVER(hdr->inode);
5297 struct rpc_message *msg = &task->tk_msg;
5298
5299 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5300 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5301 server->caps &= ~NFS_CAP_READ_PLUS;
5302 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5303 rpc_restart_call_prepare(task);
5304 return true;
5305 }
5306 return false;
5307}
5308
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005309static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005310{
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005311 dprintk("--> %s\n", __func__);
5312
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005313 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005314 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005315 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005316 return -EAGAIN;
Anna Schumakerc5675522014-05-28 13:41:22 -04005317 if (nfs4_read_plus_not_supported(task, hdr))
5318 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005319 if (task->tk_status > 0)
5320 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005321 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5322 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005323}
5324
Anna Schumaker21e31402020-12-03 15:18:39 -05005325#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
Anna Schumakerc5675522014-05-28 13:41:22 -04005326static void nfs42_read_plus_support(struct nfs_server *server, struct rpc_message *msg)
5327{
5328 if (server->caps & NFS_CAP_READ_PLUS)
5329 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5330 else
5331 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5332}
5333#else
5334static void nfs42_read_plus_support(struct nfs_server *server, struct rpc_message *msg)
5335{
5336 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5337}
5338#endif /* CONFIG_NFS_V4_2 */
5339
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005340static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5341 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005343 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005344 if (!hdr->pgio_done_cb)
5345 hdr->pgio_done_cb = nfs4_read_done_cb;
Anna Schumakerc5675522014-05-28 13:41:22 -04005346 nfs42_read_plus_support(NFS_SERVER(hdr->inode), msg);
Anna Schumakerfba83f32018-05-04 16:22:50 -04005347 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348}
5349
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005350static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5351 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005352{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005353 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005354 &hdr->args.seq_args,
5355 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005356 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005357 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005358 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5359 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005360 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005361 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005362 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005363 return -EIO;
5364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365}
5366
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005367static int nfs4_write_done_cb(struct rpc_task *task,
5368 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005370 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005371
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005372 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005373 if (task->tk_status < 0) {
5374 struct nfs4_exception exception = {
5375 .inode = hdr->inode,
5376 .state = hdr->args.context->state,
5377 .stateid = &hdr->args.stateid,
5378 };
5379 task->tk_status = nfs4_async_handle_exception(task,
5380 NFS_SERVER(inode), task->tk_status,
5381 &exception);
5382 if (exception.retry) {
5383 rpc_restart_call_prepare(task);
5384 return -EAGAIN;
5385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005387 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005388 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005389 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005390 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392}
5393
Trond Myklebust5521abf2013-03-16 20:54:34 -04005394static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005395 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005396{
5397
5398 if (!nfs4_error_stateid_expired(task->tk_status) ||
5399 nfs4_stateid_is_current(&args->stateid,
5400 args->context,
5401 args->lock_context,
5402 FMODE_WRITE))
5403 return false;
5404 rpc_restart_call_prepare(task);
5405 return true;
5406}
5407
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005408static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005409{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005410 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005411 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005412 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005413 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005414 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5415 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005416}
5417
Trond Myklebust5a37f852012-04-28 14:55:16 -04005418static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005419bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005420{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005421 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005422 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005423 return false;
5424 /* Otherwise, request attributes if and only if we don't hold
5425 * a delegation
5426 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005427 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005428}
Fred Isamana69aef12011-03-03 15:13:47 +00005429
Trond Myklebustce2b4702021-03-25 18:15:36 -04005430static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5431 struct inode *inode, struct nfs_server *server,
5432 struct nfs4_label *label)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005433{
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005434 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
Trond Myklebustce2b4702021-03-25 18:15:36 -04005435 unsigned int i;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005436
Trond Myklebustce2b4702021-03-25 18:15:36 -04005437 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005438
Trond Myklebustce2b4702021-03-25 18:15:36 -04005439 if (cache_validity & (NFS_INO_INVALID_CHANGE | NFS_INO_REVAL_PAGECACHE))
5440 bitmask[0] |= FATTR4_WORD0_CHANGE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005441 if (cache_validity & NFS_INO_INVALID_ATIME)
5442 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
Trond Myklebust9468ab82021-02-08 08:49:32 -05005443 if (cache_validity & NFS_INO_INVALID_OTHER)
5444 bitmask[1] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
5445 FATTR4_WORD1_OWNER_GROUP |
5446 FATTR4_WORD1_NUMLINKS;
5447 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005448 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust9468ab82021-02-08 08:49:32 -05005449 if (cache_validity & NFS_INO_INVALID_CTIME)
5450 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005451 if (cache_validity & NFS_INO_INVALID_MTIME)
5452 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005453 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5454 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
Trond Myklebustce2b4702021-03-25 18:15:36 -04005455
5456 if (nfs4_have_delegation(inode, FMODE_READ) &&
5457 !(cache_validity & NFS_INO_REVAL_FORCED))
5458 bitmask[0] &= ~FATTR4_WORD0_SIZE;
5459 else if (cache_validity &
5460 (NFS_INO_INVALID_SIZE | NFS_INO_REVAL_PAGECACHE))
5461 bitmask[0] |= FATTR4_WORD0_SIZE;
5462
5463 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5464 bitmask[i] &= server->attr_bitmask[i];
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005465}
5466
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005467static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005468 struct rpc_message *msg,
5469 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005471 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005472
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005473 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5474 hdr->args.bitmask = NULL;
5475 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005476 } else {
Trond Myklebustce2b4702021-03-25 18:15:36 -04005477 nfs4_bitmask_set(hdr->args.bitmask_store,
5478 server->cache_consistency_bitmask,
5479 hdr->inode, server, NULL);
5480 hdr->args.bitmask = hdr->args.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005481 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005482
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005483 if (!hdr->pgio_done_cb)
5484 hdr->pgio_done_cb = nfs4_write_done_cb;
5485 hdr->res.server = server;
5486 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005488 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005489 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005490 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491}
5492
Fred Isaman0b7c0152012-04-20 14:47:39 -04005493static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5494{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005495 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005496 &data->args.seq_args,
5497 &data->res.seq_res,
5498 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499}
5500
Fred Isaman0b7c0152012-04-20 14:47:39 -04005501static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005504
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005505 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005506 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5507 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005508 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005509 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512}
5513
Fred Isaman0b7c0152012-04-20 14:47:39 -04005514static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005515{
5516 if (!nfs4_sequence_done(task, &data->res.seq_res))
5517 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005518 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005519}
5520
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005521static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5522 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005524 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005525
Fred Isaman0b7c0152012-04-20 14:47:39 -04005526 if (data->commit_done_cb == NULL)
5527 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005528 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005529 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005530 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005531 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532}
5533
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005534static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5535 struct nfs_commitres *res)
5536{
5537 struct inode *dst_inode = file_inode(dst);
5538 struct nfs_server *server = NFS_SERVER(dst_inode);
5539 struct rpc_message msg = {
5540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5541 .rpc_argp = args,
5542 .rpc_resp = res,
5543 };
5544
5545 args->fh = NFS_FH(dst_inode);
5546 return nfs4_call_sync(server->client, server, &msg,
5547 &args->seq_args, &res->seq_res, 1);
5548}
5549
5550int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5551{
5552 struct nfs_commitargs args = {
5553 .offset = offset,
5554 .count = count,
5555 };
5556 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5557 struct nfs4_exception exception = { };
5558 int status;
5559
5560 do {
5561 status = _nfs4_proc_commit(dst, &args, res);
5562 status = nfs4_handle_exception(dst_server, status, &exception);
5563 } while (exception.retry);
5564
5565 return status;
5566}
5567
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005568struct nfs4_renewdata {
5569 struct nfs_client *client;
5570 unsigned long timestamp;
5571};
5572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573/*
5574 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5575 * standalone procedure for queueing an asynchronous RENEW.
5576 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005577static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005578{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005579 struct nfs4_renewdata *data = calldata;
5580 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005581
Elena Reshetova212bf412017-10-20 12:53:38 +03005582 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005583 nfs4_schedule_state_renewal(clp);
5584 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005585 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005586}
5587
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005588static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005590 struct nfs4_renewdata *data = calldata;
5591 struct nfs_client *clp = data->client;
5592 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005594 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005595 switch (task->tk_status) {
5596 case 0:
5597 break;
5598 case -NFS4ERR_LEASE_MOVED:
5599 nfs4_schedule_lease_moved_recovery(clp);
5600 break;
5601 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005602 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005603 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5604 return;
5605 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005606 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005607 return;
5608 }
5609 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005611 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612}
5613
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005614static const struct rpc_call_ops nfs4_renew_ops = {
5615 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005616 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005617};
5618
NeilBrowna52458b2018-12-03 11:30:31 +11005619static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620{
5621 struct rpc_message msg = {
5622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5623 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005624 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005626 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005628 if (renew_flags == 0)
5629 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005630 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005631 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005632 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005633 if (data == NULL) {
5634 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005635 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005636 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005637 data->client = clp;
5638 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005639 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005640 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641}
5642
NeilBrowna52458b2018-12-03 11:30:31 +11005643static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644{
5645 struct rpc_message msg = {
5646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5647 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005648 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 };
5650 unsigned long now = jiffies;
5651 int status;
5652
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005653 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 if (status < 0)
5655 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005656 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 return 0;
5658}
5659
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005660static inline int nfs4_server_supports_acls(struct nfs_server *server)
5661{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005662 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005663}
5664
Trond Myklebust21f498c2012-08-24 10:59:25 -04005665/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5666 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005667 * the stack.
5668 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005669#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005670
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005671int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005672 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005673{
5674 struct page *newpage, **spages;
5675 int rc = 0;
5676 size_t len;
5677 spages = pages;
5678
5679 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005680 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005681 newpage = alloc_page(GFP_KERNEL);
5682
5683 if (newpage == NULL)
5684 goto unwind;
5685 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005686 buf += len;
5687 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005688 *pages++ = newpage;
5689 rc++;
5690 } while (buflen != 0);
5691
5692 return rc;
5693
5694unwind:
5695 for(; rc > 0; rc--)
5696 __free_page(spages[rc-1]);
5697 return -ENOMEM;
5698}
5699
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005700struct nfs4_cached_acl {
5701 int cached;
5702 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005703 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005704};
5705
5706static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005707{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005708 struct nfs_inode *nfsi = NFS_I(inode);
5709
5710 spin_lock(&inode->i_lock);
5711 kfree(nfsi->nfs4_acl);
5712 nfsi->nfs4_acl = acl;
5713 spin_unlock(&inode->i_lock);
5714}
5715
5716static void nfs4_zap_acl_attr(struct inode *inode)
5717{
5718 nfs4_set_cached_acl(inode, NULL);
5719}
5720
5721static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5722{
5723 struct nfs_inode *nfsi = NFS_I(inode);
5724 struct nfs4_cached_acl *acl;
5725 int ret = -ENOENT;
5726
5727 spin_lock(&inode->i_lock);
5728 acl = nfsi->nfs4_acl;
5729 if (acl == NULL)
5730 goto out;
5731 if (buf == NULL) /* user is just asking for length */
5732 goto out_len;
5733 if (acl->cached == 0)
5734 goto out;
5735 ret = -ERANGE; /* see getxattr(2) man page */
5736 if (acl->len > buflen)
5737 goto out;
5738 memcpy(buf, acl->data, acl->len);
5739out_len:
5740 ret = acl->len;
5741out:
5742 spin_unlock(&inode->i_lock);
5743 return ret;
5744}
5745
Sachin Prabhu5794d212012-04-17 14:36:40 +01005746static 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 +00005747{
5748 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005749 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005750
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005751 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005752 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005753 if (acl == NULL)
5754 goto out;
5755 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005756 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005757 } else {
5758 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5759 if (acl == NULL)
5760 goto out;
5761 acl->cached = 0;
5762 }
5763 acl->len = acl_len;
5764out:
5765 nfs4_set_cached_acl(inode, acl);
5766}
5767
Andy Adamsonbf118a32011-12-07 11:55:27 -05005768/*
5769 * The getxattr API returns the required buffer length when called with a
5770 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5771 * the required buf. On a NULL buf, we send a page of data to the server
5772 * guessing that the ACL request can be serviced by a page. If so, we cache
5773 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5774 * the cache. If not so, we throw away the page, and cache the required
5775 * length. The next getxattr call will then produce another round trip to
5776 * the server, this time with the input buf of the required size.
5777 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005778static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005779{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005780 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005781 struct nfs_getaclargs args = {
5782 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005783 .acl_len = buflen,
5784 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005785 struct nfs_getaclres res = {
5786 .acl_len = buflen,
5787 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005788 struct rpc_message msg = {
5789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5790 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005791 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005792 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005793 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005794 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005795 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005796
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005797 if (buflen == 0)
5798 buflen = server->rsize;
5799
5800 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5801 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5802 if (!pages)
5803 return -ENOMEM;
5804
5805 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005806
Andy Adamsonbf118a32011-12-07 11:55:27 -05005807 for (i = 0; i < npages; i++) {
5808 pages[i] = alloc_page(GFP_KERNEL);
5809 if (!pages[i])
5810 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005811 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005812
5813 /* for decoding across pages */
5814 res.acl_scratch = alloc_page(GFP_KERNEL);
5815 if (!res.acl_scratch)
5816 goto out_free;
5817
Andy Adamsonbf118a32011-12-07 11:55:27 -05005818 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005819
Peng Taode040be2012-01-10 22:42:47 +08005820 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005821 __func__, buf, buflen, npages, args.acl_len);
5822 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5823 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005824 if (ret)
5825 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005826
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005827 /* Handle the case where the passed-in buffer is too short */
5828 if (res.acl_flags & NFS4_ACL_TRUNC) {
5829 /* Did the user only issue a request for the acl length? */
5830 if (buf == NULL)
5831 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005832 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005833 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005834 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005835 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005836 if (buf) {
5837 if (res.acl_len > buflen) {
5838 ret = -ERANGE;
5839 goto out_free;
5840 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005841 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005842 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005843out_ok:
5844 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005845out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005846 for (i = 0; i < npages; i++)
5847 if (pages[i])
5848 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005849 if (res.acl_scratch)
5850 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005851 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005852 return ret;
5853}
5854
Trond Myklebust16b42892006-08-24 12:27:15 -04005855static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5856{
Trond Myklebust0688e642019-04-07 13:59:09 -04005857 struct nfs4_exception exception = {
5858 .interruptible = true,
5859 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005860 ssize_t ret;
5861 do {
5862 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005863 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005864 if (ret >= 0)
5865 break;
5866 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5867 } while (exception.retry);
5868 return ret;
5869}
5870
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005871static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5872{
5873 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005874 int ret;
5875
5876 if (!nfs4_server_supports_acls(server))
5877 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005878 ret = nfs_revalidate_inode(server, inode);
5879 if (ret < 0)
5880 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005881 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5882 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005883 ret = nfs4_read_cached_acl(inode, buf, buflen);
5884 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005885 /* -ENOENT is returned if there is no ACL or if there is an ACL
5886 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005887 return ret;
5888 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005889}
5890
Trond Myklebust16b42892006-08-24 12:27:15 -04005891static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005892{
5893 struct nfs_server *server = NFS_SERVER(inode);
5894 struct page *pages[NFS4ACL_MAXPAGES];
5895 struct nfs_setaclargs arg = {
5896 .fh = NFS_FH(inode),
5897 .acl_pages = pages,
5898 .acl_len = buflen,
5899 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005900 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005901 struct rpc_message msg = {
5902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5903 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005904 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005905 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005906 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005907 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005908
J. Bruce Fields9f704602021-01-28 17:36:38 -05005909 /* You can't remove system.nfs4_acl: */
5910 if (buflen == 0)
5911 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005912 if (!nfs4_server_supports_acls(server))
5913 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005914 if (npages > ARRAY_SIZE(pages))
5915 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005916 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005917 if (i < 0)
5918 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005919 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005920 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005921
5922 /*
5923 * Free each page after tx, so the only ref left is
5924 * held by the network stack
5925 */
5926 for (; i > 0; i--)
5927 put_page(pages[i-1]);
5928
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005929 /*
5930 * Acl update can result in inode attribute update.
5931 * so mark the attribute cache invalid.
5932 */
5933 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005934 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005935 | NFS_INO_INVALID_CTIME
5936 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005937 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005938 nfs_access_zap_cache(inode);
5939 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005940 return ret;
5941}
5942
Trond Myklebust16b42892006-08-24 12:27:15 -04005943static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5944{
5945 struct nfs4_exception exception = { };
5946 int err;
5947 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005948 err = __nfs4_proc_set_acl(inode, buf, buflen);
5949 trace_nfs4_set_acl(inode, err);
Dai Ngo6e13b9b2021-05-19 17:15:10 -04005950 if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
5951 /*
5952 * no need to retry since the kernel
5953 * isn't involved in encoding the ACEs.
5954 */
5955 err = -EINVAL;
5956 break;
5957 }
Trond Myklebustc1578b72013-08-12 16:58:42 -04005958 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005959 &exception);
5960 } while (exception.retry);
5961 return err;
5962}
5963
David Quigleyaa9c2662013-05-22 12:50:44 -04005964#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5965static int _nfs4_get_security_label(struct inode *inode, void *buf,
5966 size_t buflen)
5967{
5968 struct nfs_server *server = NFS_SERVER(inode);
5969 struct nfs_fattr fattr;
5970 struct nfs4_label label = {0, 0, buflen, buf};
5971
5972 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005973 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005974 .fh = NFS_FH(inode),
5975 .bitmask = bitmask,
5976 };
5977 struct nfs4_getattr_res res = {
5978 .fattr = &fattr,
5979 .label = &label,
5980 .server = server,
5981 };
5982 struct rpc_message msg = {
5983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005984 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005985 .rpc_resp = &res,
5986 };
5987 int ret;
5988
5989 nfs_fattr_init(&fattr);
5990
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005991 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005992 if (ret)
5993 return ret;
5994 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5995 return -ENOENT;
Ondrej Mosnacekcaa86902021-01-15 18:43:56 +01005996 return label.len;
David Quigleyaa9c2662013-05-22 12:50:44 -04005997}
5998
5999static int nfs4_get_security_label(struct inode *inode, void *buf,
6000 size_t buflen)
6001{
Trond Myklebust0688e642019-04-07 13:59:09 -04006002 struct nfs4_exception exception = {
6003 .interruptible = true,
6004 };
David Quigleyaa9c2662013-05-22 12:50:44 -04006005 int err;
6006
6007 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6008 return -EOPNOTSUPP;
6009
6010 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006011 err = _nfs4_get_security_label(inode, buf, buflen);
6012 trace_nfs4_get_security_label(inode, err);
6013 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006014 &exception);
6015 } while (exception.retry);
6016 return err;
6017}
6018
6019static int _nfs4_do_set_security_label(struct inode *inode,
6020 struct nfs4_label *ilabel,
6021 struct nfs_fattr *fattr,
6022 struct nfs4_label *olabel)
6023{
6024
6025 struct iattr sattr = {0};
6026 struct nfs_server *server = NFS_SERVER(inode);
6027 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04006028 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006029 .fh = NFS_FH(inode),
6030 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006031 .server = server,
6032 .bitmask = bitmask,
6033 .label = ilabel,
6034 };
6035 struct nfs_setattrres res = {
6036 .fattr = fattr,
6037 .label = olabel,
6038 .server = server,
6039 };
6040 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6042 .rpc_argp = &arg,
6043 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04006044 };
6045 int status;
6046
Jeff Layton12207f62013-11-01 10:49:32 -04006047 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04006048
Jeff Layton12207f62013-11-01 10:49:32 -04006049 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04006050 if (status)
6051 dprintk("%s failed: %d\n", __func__, status);
6052
6053 return status;
6054}
6055
6056static int nfs4_do_set_security_label(struct inode *inode,
6057 struct nfs4_label *ilabel,
6058 struct nfs_fattr *fattr,
6059 struct nfs4_label *olabel)
6060{
6061 struct nfs4_exception exception = { };
6062 int err;
6063
6064 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006065 err = _nfs4_do_set_security_label(inode, ilabel,
6066 fattr, olabel);
6067 trace_nfs4_set_security_label(inode, err);
6068 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006069 &exception);
6070 } while (exception.retry);
6071 return err;
6072}
6073
6074static int
Al Viro59301222016-05-27 10:19:30 -04006075nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006076{
6077 struct nfs4_label ilabel, *olabel = NULL;
6078 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006079 int status;
6080
6081 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6082 return -EOPNOTSUPP;
6083
6084 nfs_fattr_init(&fattr);
6085
6086 ilabel.pi = 0;
6087 ilabel.lfs = 0;
6088 ilabel.label = (char *)buf;
6089 ilabel.len = buflen;
6090
David Quigleyaa9c2662013-05-22 12:50:44 -04006091 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6092 if (IS_ERR(olabel)) {
6093 status = -PTR_ERR(olabel);
6094 goto out;
6095 }
6096
6097 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6098 if (status == 0)
6099 nfs_setsecurity(inode, &fattr, olabel);
6100
6101 nfs4_label_free(olabel);
6102out:
David Quigleyaa9c2662013-05-22 12:50:44 -04006103 return status;
6104}
6105#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6106
6107
Chuck Leverf0920752012-05-21 22:45:41 -04006108static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6109 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006110{
6111 __be32 verf[2];
6112
Chuck Lever2c820d92012-05-21 22:45:33 -04006113 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6114 /* An impossible timestamp guarantees this value
6115 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006116 verf[0] = cpu_to_be32(U32_MAX);
6117 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006118 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006119 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006120 u64 ns = ktime_to_ns(nn->boot_time);
6121
6122 verf[0] = cpu_to_be32(ns >> 32);
6123 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006124 }
Chuck Levercd937102012-03-02 17:14:31 -05006125 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6126}
6127
Trond Myklebust1aee5512020-10-07 18:24:17 -04006128static size_t
Trond Myklebust39d43d12020-10-07 18:24:18 -04006129nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
Trond Myklebust1aee5512020-10-07 18:24:17 -04006130{
Trond Myklebust39d43d12020-10-07 18:24:18 -04006131 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6132 struct nfs_netns_client *nn_clp = nn->nfs_client;
6133 const char *id;
6134
Trond Myklebust1aee5512020-10-07 18:24:17 -04006135 buf[0] = '\0';
6136
Trond Myklebust39d43d12020-10-07 18:24:18 -04006137 if (nn_clp) {
6138 rcu_read_lock();
6139 id = rcu_dereference(nn_clp->identifier);
6140 if (id)
6141 strscpy(buf, id, buflen);
6142 rcu_read_unlock();
6143 }
6144
6145 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
Trond Myklebust1aee5512020-10-07 18:24:17 -04006146 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6147
6148 return strlen(buf);
6149}
6150
Jeff Laytona3192682015-06-09 19:43:59 -04006151static int
6152nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006153{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006154 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6155 size_t buflen;
Jeff Laytona3192682015-06-09 19:43:59 -04006156 size_t len;
6157 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006158
Trond Myklebustceb3a162015-01-03 15:16:04 -05006159 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006160 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006161
Jeff Laytona3192682015-06-09 19:43:59 -04006162 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006163 len = 14 +
6164 strlen(clp->cl_rpcclient->cl_nodename) +
6165 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006166 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006167 1;
6168 rcu_read_unlock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006169
Trond Myklebust39d43d12020-10-07 18:24:18 -04006170 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006171 if (buflen)
6172 len += buflen + 1;
6173
Jeff Laytona3192682015-06-09 19:43:59 -04006174 if (len > NFS4_OPAQUE_LIMIT + 1)
6175 return -EINVAL;
6176
6177 /*
6178 * Since this string is allocated at mount time, and held until the
6179 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6180 * about a memory-reclaim deadlock.
6181 */
6182 str = kmalloc(len, GFP_KERNEL);
6183 if (!str)
6184 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006185
Chuck Levere984a552012-09-14 17:24:21 -04006186 rcu_read_lock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006187 if (buflen)
Chuck Lever025bb9f2018-06-04 10:53:34 -04006188 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Trond Myklebust1aee5512020-10-07 18:24:17 -04006189 clp->cl_rpcclient->cl_nodename, buf,
Chuck Lever848a4eb2018-06-04 10:53:29 -04006190 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006191 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006192 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006193 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006194 clp->cl_rpcclient->cl_nodename,
6195 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006196 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006197 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006198
Jeff Laytona3192682015-06-09 19:43:59 -04006199 clp->cl_owner_id = str;
6200 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006201}
6202
Jeff Layton873e3852015-06-09 19:44:00 -04006203static int
Jeff Layton873e3852015-06-09 19:44:00 -04006204nfs4_init_uniform_client_string(struct nfs_client *clp)
6205{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006206 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6207 size_t buflen;
Jeff Layton873e3852015-06-09 19:44:00 -04006208 size_t len;
6209 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006210
6211 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006212 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006213
Jeff Layton873e3852015-06-09 19:44:00 -04006214 len = 10 + 10 + 1 + 10 + 1 +
6215 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6216
Trond Myklebust39d43d12020-10-07 18:24:18 -04006217 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006218 if (buflen)
6219 len += buflen + 1;
6220
Jeff Layton873e3852015-06-09 19:44:00 -04006221 if (len > NFS4_OPAQUE_LIMIT + 1)
6222 return -EINVAL;
6223
6224 /*
6225 * Since this string is allocated at mount time, and held until the
6226 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6227 * about a memory-reclaim deadlock.
6228 */
6229 str = kmalloc(len, GFP_KERNEL);
6230 if (!str)
6231 return -ENOMEM;
6232
Trond Myklebust1aee5512020-10-07 18:24:17 -04006233 if (buflen)
6234 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6235 clp->rpc_ops->version, clp->cl_minorversion,
6236 buf, clp->cl_rpcclient->cl_nodename);
6237 else
6238 scnprintf(str, len, "Linux NFSv%u.%u %s",
6239 clp->rpc_ops->version, clp->cl_minorversion,
6240 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006241 clp->cl_owner_id = str;
6242 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006243}
6244
Chuck Lever706cb8d2014-03-12 12:51:47 -04006245/*
6246 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6247 * services. Advertise one based on the address family of the
6248 * clientaddr.
6249 */
6250static unsigned int
6251nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6252{
6253 if (strchr(clp->cl_ipaddr, ':') != NULL)
6254 return scnprintf(buf, len, "tcp6");
6255 else
6256 return scnprintf(buf, len, "tcp");
6257}
6258
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006259static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6260{
6261 struct nfs4_setclientid *sc = calldata;
6262
6263 if (task->tk_status == 0)
6264 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6265}
6266
6267static const struct rpc_call_ops nfs4_setclientid_ops = {
6268 .rpc_call_done = nfs4_setclientid_done,
6269};
6270
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006271/**
6272 * nfs4_proc_setclientid - Negotiate client ID
6273 * @clp: state data structure
6274 * @program: RPC program for NFSv4 callback service
6275 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006276 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006277 * @res: where to place the result
6278 *
6279 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6280 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006281int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006282 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006283 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284{
6285 nfs4_verifier sc_verifier;
6286 struct nfs4_setclientid setclientid = {
6287 .sc_verifier = &sc_verifier,
6288 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006289 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 };
6291 struct rpc_message msg = {
6292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6293 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006294 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006295 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006297 struct rpc_task_setup task_setup_data = {
6298 .rpc_client = clp->cl_rpcclient,
6299 .rpc_message = &msg,
6300 .callback_ops = &nfs4_setclientid_ops,
6301 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006302 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006303 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006304 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006305 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
Chuck Leverde734832012-07-11 16:30:50 -04006307 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006308 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006309
6310 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6311 status = nfs4_init_uniform_client_string(clp);
6312 else
Jeff Laytona3192682015-06-09 19:43:59 -04006313 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006314
6315 if (status)
6316 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006317
Chuck Leverde734832012-07-11 16:30:50 -04006318 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006319 setclientid.sc_netid_len =
6320 nfs4_init_callback_netid(clp,
6321 setclientid.sc_netid,
6322 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006323 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006324 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 clp->cl_ipaddr, port >> 8, port & 255);
6326
Jeff Layton3a6bb732015-06-09 19:43:57 -04006327 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006328 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006329 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006330
6331 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006332 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006333 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006334 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6335 put_rpccred(setclientid.sc_cred);
6336 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006337
6338 if (status == 0)
6339 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006340out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006341 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006342 dprintk("NFS reply setclientid: %d\n", status);
6343 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344}
6345
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006346/**
6347 * nfs4_proc_setclientid_confirm - Confirm client ID
6348 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006349 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006350 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006351 *
6352 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6353 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006354int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006355 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006356 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 struct rpc_message msg = {
6359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006360 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006361 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 int status;
6364
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006365 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6366 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6367 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006368 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6369 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006370 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006371 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 return status;
6373}
6374
Trond Myklebustfe650402006-01-03 09:55:18 +01006375struct nfs4_delegreturndata {
6376 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006377 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006378 struct nfs_fh fh;
6379 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006380 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006381 struct {
6382 struct nfs4_layoutreturn_args arg;
6383 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006384 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006385 u32 roc_barrier;
6386 bool roc;
6387 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006388 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006389 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006390 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006391};
6392
Trond Myklebustfe650402006-01-03 09:55:18 +01006393static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6394{
6395 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006396 struct nfs4_exception exception = {
6397 .inode = data->inode,
6398 .stateid = &data->stateid,
Trond Myklebustb4651ce2021-06-01 11:10:05 -04006399 .task_is_privileged = data->args.seq_args.sa_privileged,
Trond Myklebuste0dba012017-11-07 11:02:32 -05006400 };
Andy Adamson938e1012009-04-01 09:22:28 -04006401
Trond Myklebust14516c32010-07-31 14:29:06 -04006402 if (!nfs4_sequence_done(task, &data->res.seq_res))
6403 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006404
Trond Myklebustca8acf82013-08-13 10:36:56 -04006405 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006406
6407 /* Handle Layoutreturn errors */
Trond Myklebustf128de12021-01-04 13:18:03 -05006408 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6409 &data->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04006410 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006411
Ricardo Labiaga79708862009-12-07 09:23:21 -05006412 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006413 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006414 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006415 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006416 case -NFS4ERR_ADMIN_REVOKED:
6417 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006418 case -NFS4ERR_EXPIRED:
6419 nfs4_free_revoked_stateid(data->res.server,
6420 data->args.stateid,
6421 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006422 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006423 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006424 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006425 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006426 task->tk_status = 0;
6427 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006428 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006429 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6430 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006431 if (data->args.bitmask) {
6432 data->args.bitmask = NULL;
6433 data->res.fattr = NULL;
6434 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006435 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006436 case -NFS4ERR_ACCESS:
6437 if (data->args.bitmask) {
6438 data->args.bitmask = NULL;
6439 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006440 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006441 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006442 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006443 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006444 task->tk_status = nfs4_async_handle_exception(task,
6445 data->res.server, task->tk_status,
6446 &exception);
6447 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006448 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006449 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006450 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006451 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006452 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006453out_restart:
6454 task->tk_status = 0;
6455 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006456}
6457
6458static void nfs4_delegreturn_release(void *calldata)
6459{
Peng Tao039b7562014-07-03 13:05:02 +08006460 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006461 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006462
Trond Myklebustf128de12021-01-04 13:18:03 -05006463 if (data->lr.roc)
6464 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6465 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006466 if (inode) {
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006467 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006468 nfs_iput_and_deactive(inode);
6469 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006470 kfree(calldata);
6471}
6472
Andy Adamson938e1012009-04-01 09:22:28 -04006473static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6474{
6475 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006476 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006477
6478 d_data = (struct nfs4_delegreturndata *)data;
6479
Trond Myklebust5326de92019-11-13 09:39:36 +01006480 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6481 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006482 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006483 }
Peng Tao500d7012015-09-22 11:35:22 +08006484
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006485 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6486 if (lo && !pnfs_layout_is_valid(lo)) {
6487 d_data->args.lr_args = NULL;
6488 d_data->res.lr_res = NULL;
6489 }
6490
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006491 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006492 &d_data->args.seq_args,
6493 &d_data->res.seq_res,
6494 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006495}
Andy Adamson938e1012009-04-01 09:22:28 -04006496
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006497static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006498 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006499 .rpc_call_done = nfs4_delegreturn_done,
6500 .rpc_release = nfs4_delegreturn_release,
6501};
6502
NeilBrowna52458b2018-12-03 11:30:31 +11006503static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006504{
6505 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006506 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006507 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006508 struct rpc_message msg = {
6509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6510 .rpc_cred = cred,
6511 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006512 struct rpc_task_setup task_setup_data = {
6513 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006514 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006515 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebustf304a802020-05-11 10:42:04 -04006516 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006517 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006518 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006519
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006520 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006521 if (data == NULL)
6522 return -ENOMEM;
Andrew Elble99ade3c2015-12-02 09:39:51 -05006523
6524 nfs4_state_protect(server->nfs_client,
6525 NFS_SP4_MACH_CRED_CLEANUP,
6526 &task_setup_data.rpc_client, &msg);
6527
Trond Myklebustfe650402006-01-03 09:55:18 +01006528 data->args.fhandle = &data->fh;
6529 data->args.stateid = &data->stateid;
Trond Myklebustce2b4702021-03-25 18:15:36 -04006530 nfs4_bitmask_set(data->args.bitmask_store,
6531 server->cache_consistency_bitmask, inode, server,
6532 NULL);
6533 data->args.bitmask = data->args.bitmask_store;
Trond Myklebustfe650402006-01-03 09:55:18 +01006534 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006535 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006536 data->res.fattr = &data->fattr;
6537 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006538 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006539 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006540 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006541 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006542 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006543 data->inode = nfs_igrab_and_active(inode);
Trond Myklebustf128de12021-01-04 13:18:03 -05006544 if (data->inode || issync) {
6545 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6546 cred);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006547 if (data->lr.roc) {
6548 data->args.lr_args = &data->lr.arg;
6549 data->res.lr_res = &data->lr.res;
6550 }
6551 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006552
Trond Myklebustb4651ce2021-06-01 11:10:05 -04006553 if (!data->inode)
6554 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6555 1);
6556 else
6557 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6558 0);
Trond Myklebustc970aa82007-07-14 15:39:59 -04006559 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006560 msg.rpc_argp = &data->args;
6561 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006562 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006563 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006564 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006565 if (!issync)
6566 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006567 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006568 if (status != 0)
6569 goto out;
6570 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006571out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006572 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006573 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574}
6575
NeilBrowna52458b2018-12-03 11:30:31 +11006576int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577{
6578 struct nfs_server *server = NFS_SERVER(inode);
6579 struct nfs4_exception exception = { };
6580 int err;
6581 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006582 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006583 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 switch (err) {
6585 case -NFS4ERR_STALE_STATEID:
6586 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 case 0:
6588 return 0;
6589 }
6590 err = nfs4_handle_exception(server, err, &exception);
6591 } while (exception.retry);
6592 return err;
6593}
6594
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6596{
6597 struct inode *inode = state->inode;
6598 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006599 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006600 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006602 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006604 struct nfs_lockt_res res = {
6605 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 };
6607 struct rpc_message msg = {
6608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006609 .rpc_argp = &arg,
6610 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 .rpc_cred = state->owner->so_cred,
6612 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 struct nfs4_lock_state *lsp;
6614 int status;
6615
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006616 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006617 status = nfs4_set_lock_state(state, request);
6618 if (status != 0)
6619 goto out;
6620 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006621 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006622 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006623 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006624 switch (status) {
6625 case 0:
6626 request->fl_type = F_UNLCK;
6627 break;
6628 case -NFS4ERR_DENIED:
6629 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006631 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006632 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006633out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 return status;
6635}
6636
6637static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6638{
Trond Myklebust0688e642019-04-07 13:59:09 -04006639 struct nfs4_exception exception = {
6640 .interruptible = true,
6641 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 int err;
6643
6644 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006645 err = _nfs4_proc_getlk(state, cmd, request);
6646 trace_nfs4_get_lock(request, state, cmd, err);
6647 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 &exception);
6649 } while (exception.retry);
6650 return err;
6651}
6652
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006653/*
6654 * Update the seqid of a lock stateid after receiving
6655 * NFS4ERR_OLD_STATEID
6656 */
6657static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6658 struct nfs4_lock_state *lsp)
6659{
6660 struct nfs4_state *state = lsp->ls_state;
6661 bool ret = false;
6662
6663 spin_lock(&state->state_lock);
6664 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6665 goto out;
6666 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6667 nfs4_stateid_seqid_inc(dst);
6668 else
6669 dst->seqid = lsp->ls_stateid.seqid;
6670 ret = true;
6671out:
6672 spin_unlock(&state->state_lock);
6673 return ret;
6674}
6675
6676static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6677 struct nfs4_lock_state *lsp)
6678{
6679 struct nfs4_state *state = lsp->ls_state;
6680 bool ret;
6681
6682 spin_lock(&state->state_lock);
6683 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6684 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6685 spin_unlock(&state->state_lock);
6686 return ret;
6687}
6688
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006689struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006690 struct nfs_locku_args arg;
6691 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006692 struct nfs4_lock_state *lsp;
6693 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006694 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006695 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006696 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006697 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006698};
6699
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006700static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6701 struct nfs_open_context *ctx,
6702 struct nfs4_lock_state *lsp,
6703 struct nfs_seqid *seqid)
6704{
6705 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006706 struct nfs4_state *state = lsp->ls_state;
6707 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006708
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006709 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006710 if (p == NULL)
6711 return NULL;
6712 p->arg.fh = NFS_FH(inode);
6713 p->arg.fl = &p->fl;
6714 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006715 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006716 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006717 /* Ensure we don't close file until we're done freeing locks! */
6718 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006719 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006720 locks_init_lock(&p->fl);
6721 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006722 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006723 spin_lock(&state->state_lock);
6724 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6725 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006726 return p;
6727}
6728
Trond Myklebust06f814a2006-01-03 09:55:07 +01006729static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006730{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006731 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006732 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006733 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006734 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006735 put_nfs_open_context(calldata->ctx);
6736 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006737}
6738
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006739static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006740{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006741 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006742 struct nfs4_exception exception = {
6743 .inode = calldata->lsp->ls_state->inode,
6744 .stateid = &calldata->arg.stateid,
6745 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006746
Trond Myklebust14516c32010-07-31 14:29:06 -04006747 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6748 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006749 switch (task->tk_status) {
6750 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006751 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006752 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006753 if (nfs4_update_lock_stateid(calldata->lsp,
6754 &calldata->res.stateid))
6755 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006756 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006757 case -NFS4ERR_ADMIN_REVOKED:
6758 case -NFS4ERR_EXPIRED:
6759 nfs4_free_revoked_stateid(calldata->server,
6760 &calldata->arg.stateid,
6761 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006762 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006763 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006764 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006765 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6766 calldata->lsp))
6767 rpc_restart_call_prepare(task);
6768 break;
6769 case -NFS4ERR_OLD_STATEID:
6770 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6771 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006772 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006773 break;
6774 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006775 task->tk_status = nfs4_async_handle_exception(task,
6776 calldata->server, task->tk_status,
6777 &exception);
6778 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006779 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006780 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006781 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006782}
6783
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006784static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006785{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006786 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006788 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6789 nfs_async_iocounter_wait(task, calldata->l_ctx))
6790 return;
6791
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006792 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006793 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006794 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006795 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006796 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006797 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006798 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006799 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006800 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006801 &calldata->res.seq_res,
6802 task) != 0)
6803 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006804 return;
6805out_no_action:
6806 task->tk_action = NULL;
6807out_wait:
6808 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809}
6810
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006811static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006812 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006813 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006814 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006815};
6816
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006817static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6818 struct nfs_open_context *ctx,
6819 struct nfs4_lock_state *lsp,
6820 struct nfs_seqid *seqid)
6821{
6822 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006823 struct rpc_message msg = {
6824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6825 .rpc_cred = ctx->cred,
6826 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006827 struct rpc_task_setup task_setup_data = {
6828 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006829 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006830 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006831 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006832 .flags = RPC_TASK_ASYNC,
6833 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006834
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006835 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6836 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6837
Frank Filz137d6ac2007-07-09 15:32:29 -07006838 /* Ensure this is an unlock - when canceling a lock, the
6839 * canceled lock is passed in, and it won't be an unlock.
6840 */
6841 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006842 if (fl->fl_flags & FL_CLOSE)
6843 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006844
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006845 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6846 if (data == NULL) {
6847 nfs_free_seqid(seqid);
6848 return ERR_PTR(-ENOMEM);
6849 }
6850
Anna Schumakerfba83f32018-05-04 16:22:50 -04006851 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006852 msg.rpc_argp = &data->arg;
6853 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006854 task_setup_data.callback_data = data;
6855 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006856}
6857
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6859{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006860 struct inode *inode = state->inode;
6861 struct nfs4_state_owner *sp = state->owner;
6862 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006863 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006864 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006865 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006866 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006867 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006868 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
Trond Myklebust9b073572006-06-29 16:38:34 -04006870 status = nfs4_set_lock_state(state, request);
6871 /* Unlock _before_ we do the RPC call */
6872 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006873 /* Exclude nfs_delegation_claim_locks() */
6874 mutex_lock(&sp->so_delegreturn_mutex);
6875 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006876 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006877 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006878 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006879 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006880 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006881 }
6882 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006883 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006884 if (status != 0)
6885 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006886 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006887 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006888 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6889 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006890 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6891 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006892 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006893 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006894 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006895 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006896 status = PTR_ERR(task);
6897 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006898 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006899 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006900 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006901out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006902 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006903 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006904 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905}
6906
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006907struct nfs4_lockdata {
6908 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006909 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006910 struct nfs4_lock_state *lsp;
6911 struct nfs_open_context *ctx;
6912 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006913 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006914 int rpc_status;
6915 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006916 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006917};
6918
6919static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006920 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6921 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006922{
6923 struct nfs4_lockdata *p;
6924 struct inode *inode = lsp->ls_state->inode;
6925 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006926 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006927
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006928 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006929 if (p == NULL)
6930 return NULL;
6931
6932 p->arg.fh = NFS_FH(inode);
6933 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006934 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006935 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006936 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006937 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6938 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006939 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006940 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006941 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006942 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006943 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006944 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006945 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006946 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006947 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006948 locks_init_lock(&p->fl);
6949 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006950 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006951out_free_seqid:
6952 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006953out_free:
6954 kfree(p);
6955 return NULL;
6956}
6957
6958static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6959{
6960 struct nfs4_lockdata *data = calldata;
6961 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962
Harvey Harrison3110ff82008-05-02 13:42:44 -07006963 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006964 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006965 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006966 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006967 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006968 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006969 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006970 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006971 nfs4_stateid_copy(&data->arg.open_stateid,
6972 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006973 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006974 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006975 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006976 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006977 nfs4_stateid_copy(&data->arg.lock_stateid,
6978 &data->lsp->ls_stateid);
6979 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006980 if (!nfs4_valid_open_stateid(state)) {
6981 data->rpc_status = -EBADF;
6982 task->tk_action = NULL;
6983 goto out_release_open_seqid;
6984 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006985 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006986 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006987 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006988 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006989 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006990 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006991out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006992 nfs_release_seqid(data->arg.open_seqid);
6993out_release_lock_seqid:
6994 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006995out_wait:
6996 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006997 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006998}
6999
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007000static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7001{
7002 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05007003 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007004
Harvey Harrison3110ff82008-05-02 13:42:44 -07007005 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007006
Trond Myklebust14516c32010-07-31 14:29:06 -04007007 if (!nfs4_sequence_done(task, &data->res.seq_res))
7008 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04007009
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007010 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007011 switch (task->tk_status) {
7012 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00007013 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05007014 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007015 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05007016 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007017 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007018 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007019 }
Trond Myklebust39071e62015-01-24 15:07:56 -05007020 if (data->arg.new_lock_owner != 0) {
7021 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7022 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7023 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007024 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7025 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007026 break;
7027 case -NFS4ERR_BAD_STATEID:
7028 case -NFS4ERR_OLD_STATEID:
7029 case -NFS4ERR_STALE_STATEID:
7030 case -NFS4ERR_EXPIRED:
7031 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007032 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007033 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007034 goto out_restart;
7035 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007036 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007037 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007038 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007039out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07007040 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007041 return;
7042out_restart:
7043 if (!data->cancelled)
7044 rpc_restart_call_prepare(task);
7045 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007046}
7047
7048static void nfs4_lock_release(void *calldata)
7049{
7050 struct nfs4_lockdata *data = calldata;
7051
Harvey Harrison3110ff82008-05-02 13:42:44 -07007052 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007053 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007054 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007055 struct rpc_task *task;
7056 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7057 data->arg.lock_seqid);
7058 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08007059 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007060 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007061 } else
7062 nfs_free_seqid(data->arg.lock_seqid);
7063 nfs4_put_lock_state(data->lsp);
7064 put_nfs_open_context(data->ctx);
7065 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007066 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007067}
7068
7069static const struct rpc_call_ops nfs4_lock_ops = {
7070 .rpc_call_prepare = nfs4_lock_prepare,
7071 .rpc_call_done = nfs4_lock_done,
7072 .rpc_release = nfs4_lock_release,
7073};
7074
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007075static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7076{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007077 switch (error) {
7078 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007079 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007080 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007081 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007082 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007083 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007084 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007085 break;
7086 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007087 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007088 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007089 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007090}
7091
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007092static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007093{
7094 struct nfs4_lockdata *data;
7095 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007096 struct rpc_message msg = {
7097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7098 .rpc_cred = state->owner->so_cred,
7099 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007100 struct rpc_task_setup task_setup_data = {
7101 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007102 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007103 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007104 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007105 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007106 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007107 int ret;
7108
Harvey Harrison3110ff82008-05-02 13:42:44 -07007109 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007110 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007111 fl->fl_u.nfs4_fl.owner,
7112 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007113 if (data == NULL)
7114 return -ENOMEM;
7115 if (IS_SETLKW(cmd))
7116 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007117 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7118 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007119 msg.rpc_argp = &data->arg;
7120 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007121 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007122 if (recovery_type > NFS_LOCK_NEW) {
7123 if (recovery_type == NFS_LOCK_RECLAIM)
7124 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007125 } else
7126 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007127 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007128 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007129 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007130 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007131 if (ret == 0) {
7132 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007133 if (ret)
7134 nfs4_handle_setlk_error(data->server, data->lsp,
7135 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007136 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007137 data->cancelled = true;
Dave Wysochanski1b427122020-12-11 05:12:51 -05007138 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007139 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007140 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007141 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142}
7143
7144static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7145{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007146 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007147 struct nfs4_exception exception = {
7148 .inode = state->inode,
7149 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007150 int err;
7151
7152 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007153 /* Cache the lock if possible... */
7154 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7155 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007156 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007157 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007158 break;
7159 nfs4_handle_exception(server, err, &exception);
7160 } while (exception.retry);
7161 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162}
7163
7164static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7165{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007166 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007167 struct nfs4_exception exception = {
7168 .inode = state->inode,
7169 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007170 int err;
7171
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007172 err = nfs4_set_lock_state(state, request);
7173 if (err != 0)
7174 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007175 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007176 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7177 return 0;
7178 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007179 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007180 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7181 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007182 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007183 switch (err) {
7184 default:
7185 goto out;
7186 case -NFS4ERR_GRACE:
7187 case -NFS4ERR_DELAY:
7188 nfs4_handle_exception(server, err, &exception);
7189 err = 0;
7190 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007191 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007192out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194}
7195
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007196#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007197static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7198{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007199 struct nfs4_lock_state *lsp;
7200 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007201
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007202 status = nfs4_set_lock_state(state, request);
7203 if (status != 0)
7204 return status;
7205 lsp = request->fl_u.nfs4_fl.owner;
7206 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7207 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7208 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007209 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007210}
7211#endif
7212
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7214{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007215 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007216 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007217 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007218 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219
Trond Myklebust01c3b862006-06-29 16:38:39 -04007220 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007221 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007222 if (status < 0)
7223 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007224 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007225 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007226 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007227 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007228 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007229 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007230 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007231 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007232 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007233 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007234 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007235 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007236 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007237 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007238out:
7239 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 return status;
7241}
7242
7243static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7244{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007245 struct nfs4_exception exception = {
7246 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007247 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007248 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007249 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 int err;
7251
7252 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007253 err = _nfs4_proc_setlk(state, cmd, request);
7254 if (err == -NFS4ERR_DENIED)
7255 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007257 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 } while (exception.retry);
7259 return err;
7260}
7261
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007262#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7263#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7264
7265static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007266nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7267 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007268{
7269 int status = -ERESTARTSYS;
7270 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7271
7272 while(!signalled()) {
7273 status = nfs4_proc_setlk(state, cmd, request);
7274 if ((status != -EAGAIN) || IS_SETLK(cmd))
7275 break;
7276 freezable_schedule_timeout_interruptible(timeout);
7277 timeout *= 2;
7278 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7279 status = -ERESTARTSYS;
7280 }
7281 return status;
7282}
7283
Jeff Laytona1d617d82016-09-17 18:17:39 -04007284#ifdef CONFIG_NFS_V4_1
7285struct nfs4_lock_waiter {
7286 struct task_struct *task;
7287 struct inode *inode;
7288 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007289};
7290
7291static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007292nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007293{
7294 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007295 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007296
Jeff Layton57174592018-03-18 08:37:03 -04007297 /* NULL key means to wake up everyone */
7298 if (key) {
7299 struct cb_notify_lock_args *cbnl = key;
7300 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7301 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007302
Jeff Layton57174592018-03-18 08:37:03 -04007303 /* Only wake if the callback was for the same owner. */
7304 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7305 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007306
Jeff Layton57174592018-03-18 08:37:03 -04007307 /* Make sure it's for the right inode */
7308 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7309 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007310 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007311
7312 /* override "private" so we can use default_wake_function */
7313 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007314 ret = woken_wake_function(wait, mode, flags, key);
7315 if (ret)
7316 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007317 wait->private = waiter;
7318 return ret;
7319}
7320
7321static int
7322nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7323{
7324 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007325 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7326 struct nfs_server *server = NFS_SERVER(state->inode);
7327 struct nfs_client *clp = server->nfs_client;
7328 wait_queue_head_t *q = &clp->cl_lock_waitq;
7329 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7330 .id = lsp->ls_seqid.owner_id,
7331 .s_dev = server->s_dev };
7332 struct nfs4_lock_waiter waiter = { .task = current,
7333 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007334 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007335 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007336
7337 /* Don't bother with waitqueue if we don't expect a callback */
7338 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7339 return nfs4_retry_setlk_simple(state, cmd, request);
7340
7341 init_wait(&wait);
7342 wait.private = &waiter;
7343 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007344
7345 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007346 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007347 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007348 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7349 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007350 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007351 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007352
7353 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007354 freezer_do_not_count();
7355 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7356 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007357 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007358 }
7359
Jeff Laytona1d617d82016-09-17 18:17:39 -04007360 return status;
7361}
7362#else /* !CONFIG_NFS_V4_1 */
7363static inline int
7364nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7365{
7366 return nfs4_retry_setlk_simple(state, cmd, request);
7367}
7368#endif
7369
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370static int
7371nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7372{
7373 struct nfs_open_context *ctx;
7374 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 int status;
7376
7377 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007378 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 state = ctx->state;
7380
Trond Myklebustd9531262009-07-21 19:22:38 -04007381 if (IS_GETLK(cmd)) {
7382 if (state != NULL)
7383 return nfs4_proc_getlk(state, F_GETLK, request);
7384 return 0;
7385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386
7387 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7388 return -EINVAL;
7389
Trond Myklebustd9531262009-07-21 19:22:38 -04007390 if (request->fl_type == F_UNLCK) {
7391 if (state != NULL)
7392 return nfs4_proc_unlck(state, cmd, request);
7393 return 0;
7394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395
Trond Myklebustd9531262009-07-21 19:22:38 -04007396 if (state == NULL)
7397 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007398
7399 if ((request->fl_flags & FL_POSIX) &&
7400 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7401 return -ENOLCK;
7402
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007403 /*
7404 * Don't rely on the VFS having checked the file open mode,
7405 * since it won't do this for flock() locks.
7406 */
7407 switch (request->fl_type) {
7408 case F_RDLCK:
7409 if (!(filp->f_mode & FMODE_READ))
7410 return -EBADF;
7411 break;
7412 case F_WRLCK:
7413 if (!(filp->f_mode & FMODE_WRITE))
7414 return -EBADF;
7415 }
7416
Jeff Layton1ea67db2016-09-17 18:17:37 -04007417 status = nfs4_set_lock_state(state, request);
7418 if (status != 0)
7419 return status;
7420
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007421 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422}
7423
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007424int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007425{
7426 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007427 int err;
7428
7429 err = nfs4_set_lock_state(state, fl);
7430 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007431 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007432 do {
7433 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7434 if (err != -NFS4ERR_DELAY)
7435 break;
7436 ssleep(1);
7437 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007438 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007439}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007440
Trond Myklebustcf470c32012-03-07 13:49:12 -05007441struct nfs_release_lockowner_data {
7442 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007443 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007444 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007445 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007446 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007447};
7448
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007449static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7450{
7451 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007452 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007453 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7454 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007455 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007456 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007457}
7458
7459static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7460{
7461 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007462 struct nfs_server *server = data->server;
7463
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007464 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007465
7466 switch (task->tk_status) {
7467 case 0:
7468 renew_lease(server, data->timestamp);
7469 break;
7470 case -NFS4ERR_STALE_CLIENTID:
7471 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007472 nfs4_schedule_lease_recovery(server->nfs_client);
7473 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007474 case -NFS4ERR_LEASE_MOVED:
7475 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007476 if (nfs4_async_handle_error(task, server,
7477 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007478 rpc_restart_call_prepare(task);
7479 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007480}
7481
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007482static void nfs4_release_lockowner_release(void *calldata)
7483{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007484 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007485 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007486 kfree(calldata);
7487}
7488
Trond Myklebust17280172012-03-11 13:11:00 -04007489static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007490 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7491 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007492 .rpc_release = nfs4_release_lockowner_release,
7493};
7494
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007495static void
7496nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007497{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007498 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007499 struct rpc_message msg = {
7500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7501 };
7502
7503 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007504 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007505
Trond Myklebustcf470c32012-03-07 13:49:12 -05007506 data = kmalloc(sizeof(*data), GFP_NOFS);
7507 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007508 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007509 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007510 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007511 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7512 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7513 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007514
Trond Myklebustcf470c32012-03-07 13:49:12 -05007515 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007516 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007517 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007518 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007519}
7520
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007521#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7522
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007523static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007524 struct dentry *unused, struct inode *inode,
7525 const char *key, const void *buf,
7526 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007527{
Al Viro59301222016-05-27 10:19:30 -04007528 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007529}
7530
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007531static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007532 struct dentry *unused, struct inode *inode,
Mark Salyzyn3484eba2019-11-04 08:57:10 -08007533 const char *key, void *buf, size_t buflen,
7534 int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007535{
Al Virob2968212016-04-10 20:48:24 -04007536 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007537}
7538
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007539static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007540{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007541 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007542}
7543
David Quigleyc9bccef2013-05-22 12:50:45 -04007544#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007545
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007546static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007547 struct dentry *unused, struct inode *inode,
7548 const char *key, const void *buf,
7549 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007550{
7551 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007552 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007553
7554 return -EOPNOTSUPP;
7555}
7556
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007557static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007558 struct dentry *unused, struct inode *inode,
Mark Salyzyn3484eba2019-11-04 08:57:10 -08007559 const char *key, void *buf, size_t buflen,
7560 int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007561{
7562 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007563 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007564 return -EOPNOTSUPP;
7565}
7566
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007567static ssize_t
7568nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007569{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007570 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007571
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007572 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7573 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007574 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007575 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007576 }
7577 return len;
7578}
7579
7580static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7581 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007582 .get = nfs4_xattr_get_nfs4_label,
7583 .set = nfs4_xattr_set_nfs4_label,
7584};
David Quigleyc9bccef2013-05-22 12:50:45 -04007585
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007586#else
7587
7588static ssize_t
7589nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7590{
7591 return 0;
7592}
7593
7594#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007595
Frank van der Linden012a2112020-06-23 22:39:03 +00007596#ifdef CONFIG_NFS_V4_2
7597static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7598 struct dentry *unused, struct inode *inode,
7599 const char *key, const void *buf,
7600 size_t buflen, int flags)
7601{
7602 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007603 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007604
7605 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7606 return -EOPNOTSUPP;
7607
7608 /*
7609 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7610 * flags right now. Handling of xattr operations use the normal
7611 * file read/write permissions.
7612 *
7613 * Just in case the server has other ideas (which RFC 8276 allows),
7614 * do a cached access check for the XA* flags to possibly avoid
7615 * doing an RPC and getting EACCES back.
7616 */
7617 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7618 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7619 return -EACCES;
7620 }
7621
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007622 if (buf == NULL) {
7623 ret = nfs42_proc_removexattr(inode, key);
7624 if (!ret)
7625 nfs4_xattr_cache_remove(inode, key);
7626 } else {
7627 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7628 if (!ret)
7629 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7630 }
7631
7632 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007633}
7634
7635static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7636 struct dentry *unused, struct inode *inode,
Greg Kroah-Hartman4aeb6132020-08-17 09:24:44 +02007637 const char *key, void *buf, size_t buflen,
7638 int flags)
Frank van der Linden012a2112020-06-23 22:39:03 +00007639{
7640 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007641 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007642
7643 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7644 return -EOPNOTSUPP;
7645
7646 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7647 if (!(cache.mask & NFS_ACCESS_XAREAD))
7648 return -EACCES;
7649 }
7650
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007651 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7652 if (ret)
7653 return ret;
7654
7655 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7656 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7657 return ret;
7658
7659 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7660
7661 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007662}
7663
7664static ssize_t
7665nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7666{
7667 u64 cookie;
7668 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007669 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007670 char *buf;
7671 size_t buflen;
7672 struct nfs_access_entry cache;
7673
7674 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7675 return 0;
7676
7677 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7678 if (!(cache.mask & NFS_ACCESS_XALIST))
7679 return 0;
7680 }
7681
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007682 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7683 if (ret)
7684 return ret;
7685
7686 ret = nfs4_xattr_cache_list(inode, list, list_len);
7687 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7688 return ret;
7689
Frank van der Linden012a2112020-06-23 22:39:03 +00007690 cookie = 0;
7691 eof = false;
7692 buflen = list_len ? list_len : XATTR_LIST_MAX;
7693 buf = list_len ? list : NULL;
7694 size = 0;
7695
7696 while (!eof) {
7697 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7698 &cookie, &eof);
7699 if (ret < 0)
7700 return ret;
7701
7702 if (list_len) {
7703 buf += ret;
7704 buflen -= ret;
7705 }
7706 size += ret;
7707 }
7708
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007709 if (list_len)
7710 nfs4_xattr_cache_set_list(inode, list, size);
7711
Frank van der Linden012a2112020-06-23 22:39:03 +00007712 return size;
7713}
7714
7715#else
7716
7717static ssize_t
7718nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7719{
7720 return 0;
7721}
7722#endif /* CONFIG_NFS_V4_2 */
7723
Andy Adamson533eb462011-06-13 18:25:56 -04007724/*
7725 * nfs_fhget will use either the mounted_on_fileid or the fileid
7726 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007727static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7728{
Andy Adamson533eb462011-06-13 18:25:56 -04007729 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7730 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7731 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007732 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007733 return;
7734
7735 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007736 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007737 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7738 fattr->nlink = 2;
7739}
7740
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007741static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7742 const struct qstr *name,
7743 struct nfs4_fs_locations *fs_locations,
7744 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007745{
7746 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007747 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007748 struct nfs4_fs_locations_arg args = {
7749 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007750 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007751 .page = page,
7752 .bitmask = bitmask,
7753 };
Benny Halevy22958462009-04-01 09:22:02 -04007754 struct nfs4_fs_locations_res res = {
7755 .fs_locations = fs_locations,
7756 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007757 struct rpc_message msg = {
7758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7759 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007760 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007761 };
7762 int status;
7763
Harvey Harrison3110ff82008-05-02 13:42:44 -07007764 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007765
Chuck Leverc05cefc2017-11-05 15:45:22 -05007766 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7767 bitmask[1] = nfs4_fattr_bitmap[1];
7768
Andy Adamson533eb462011-06-13 18:25:56 -04007769 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7770 * is not supported */
7771 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007772 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007773 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007774 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007775
Trond Myklebustc228fd32007-01-13 02:28:11 -05007776 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007777 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007778 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007779 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007780 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007781 return status;
7782}
7783
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007784int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7785 const struct qstr *name,
7786 struct nfs4_fs_locations *fs_locations,
7787 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007788{
Trond Myklebust0688e642019-04-07 13:59:09 -04007789 struct nfs4_exception exception = {
7790 .interruptible = true,
7791 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007792 int err;
7793 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007794 err = _nfs4_proc_fs_locations(client, dir, name,
7795 fs_locations, page);
7796 trace_nfs4_get_fs_locations(dir, name, err);
7797 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007798 &exception);
7799 } while (exception.retry);
7800 return err;
7801}
7802
Chuck Leverb03d7352013-10-17 14:12:50 -04007803/*
7804 * This operation also signals the server that this client is
7805 * performing migration recovery. The server can stop returning
7806 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7807 * appended to this compound to identify the client ID which is
7808 * performing recovery.
7809 */
7810static int _nfs40_proc_get_locations(struct inode *inode,
7811 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007812 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007813{
7814 struct nfs_server *server = NFS_SERVER(inode);
7815 struct rpc_clnt *clnt = server->client;
7816 u32 bitmask[2] = {
7817 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7818 };
7819 struct nfs4_fs_locations_arg args = {
7820 .clientid = server->nfs_client->cl_clientid,
7821 .fh = NFS_FH(inode),
7822 .page = page,
7823 .bitmask = bitmask,
7824 .migration = 1, /* skip LOOKUP */
7825 .renew = 1, /* append RENEW */
7826 };
7827 struct nfs4_fs_locations_res res = {
7828 .fs_locations = locations,
7829 .migration = 1,
7830 .renew = 1,
7831 };
7832 struct rpc_message msg = {
7833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7834 .rpc_argp = &args,
7835 .rpc_resp = &res,
7836 .rpc_cred = cred,
7837 };
7838 unsigned long now = jiffies;
7839 int status;
7840
7841 nfs_fattr_init(&locations->fattr);
7842 locations->server = server;
7843 locations->nlocations = 0;
7844
Anna Schumakerfba83f32018-05-04 16:22:50 -04007845 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007846 status = nfs4_call_sync_sequence(clnt, server, &msg,
7847 &args.seq_args, &res.seq_res);
7848 if (status)
7849 return status;
7850
7851 renew_lease(server, now);
7852 return 0;
7853}
7854
7855#ifdef CONFIG_NFS_V4_1
7856
7857/*
7858 * This operation also signals the server that this client is
7859 * performing migration recovery. The server can stop asserting
7860 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7861 * performing this operation is identified in the SEQUENCE
7862 * operation in this compound.
7863 *
7864 * When the client supports GETATTR(fs_locations_info), it can
7865 * be plumbed in here.
7866 */
7867static int _nfs41_proc_get_locations(struct inode *inode,
7868 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007869 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007870{
7871 struct nfs_server *server = NFS_SERVER(inode);
7872 struct rpc_clnt *clnt = server->client;
7873 u32 bitmask[2] = {
7874 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7875 };
7876 struct nfs4_fs_locations_arg args = {
7877 .fh = NFS_FH(inode),
7878 .page = page,
7879 .bitmask = bitmask,
7880 .migration = 1, /* skip LOOKUP */
7881 };
7882 struct nfs4_fs_locations_res res = {
7883 .fs_locations = locations,
7884 .migration = 1,
7885 };
7886 struct rpc_message msg = {
7887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7888 .rpc_argp = &args,
7889 .rpc_resp = &res,
7890 .rpc_cred = cred,
7891 };
7892 int status;
7893
7894 nfs_fattr_init(&locations->fattr);
7895 locations->server = server;
7896 locations->nlocations = 0;
7897
Anna Schumakerfba83f32018-05-04 16:22:50 -04007898 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007899 status = nfs4_call_sync_sequence(clnt, server, &msg,
7900 &args.seq_args, &res.seq_res);
7901 if (status == NFS4_OK &&
7902 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7903 status = -NFS4ERR_LEASE_MOVED;
7904 return status;
7905}
7906
7907#endif /* CONFIG_NFS_V4_1 */
7908
7909/**
7910 * nfs4_proc_get_locations - discover locations for a migrated FSID
7911 * @inode: inode on FSID that is migrating
7912 * @locations: result of query
7913 * @page: buffer
7914 * @cred: credential to use for this operation
7915 *
7916 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7917 * operation failed, or a negative errno if a local error occurred.
7918 *
7919 * On success, "locations" is filled in, but if the server has
7920 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7921 * asserted.
7922 *
7923 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7924 * from this client that require migration recovery.
7925 */
7926int nfs4_proc_get_locations(struct inode *inode,
7927 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007928 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007929{
7930 struct nfs_server *server = NFS_SERVER(inode);
7931 struct nfs_client *clp = server->nfs_client;
7932 const struct nfs4_mig_recovery_ops *ops =
7933 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007934 struct nfs4_exception exception = {
7935 .interruptible = true,
7936 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007937 int status;
7938
7939 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7940 (unsigned long long)server->fsid.major,
7941 (unsigned long long)server->fsid.minor,
7942 clp->cl_hostname);
7943 nfs_display_fhandle(NFS_FH(inode), __func__);
7944
7945 do {
7946 status = ops->get_locations(inode, locations, page, cred);
7947 if (status != -NFS4ERR_DELAY)
7948 break;
7949 nfs4_handle_exception(server, status, &exception);
7950 } while (exception.retry);
7951 return status;
7952}
7953
Chuck Lever44c99932013-10-17 14:13:30 -04007954/*
7955 * This operation also signals the server that this client is
7956 * performing "lease moved" recovery. The server can stop
7957 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7958 * is appended to this compound to identify the client ID which is
7959 * performing recovery.
7960 */
NeilBrowna52458b2018-12-03 11:30:31 +11007961static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007962{
7963 struct nfs_server *server = NFS_SERVER(inode);
7964 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7965 struct rpc_clnt *clnt = server->client;
7966 struct nfs4_fsid_present_arg args = {
7967 .fh = NFS_FH(inode),
7968 .clientid = clp->cl_clientid,
7969 .renew = 1, /* append RENEW */
7970 };
7971 struct nfs4_fsid_present_res res = {
7972 .renew = 1,
7973 };
7974 struct rpc_message msg = {
7975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7976 .rpc_argp = &args,
7977 .rpc_resp = &res,
7978 .rpc_cred = cred,
7979 };
7980 unsigned long now = jiffies;
7981 int status;
7982
7983 res.fh = nfs_alloc_fhandle();
7984 if (res.fh == NULL)
7985 return -ENOMEM;
7986
Anna Schumakerfba83f32018-05-04 16:22:50 -04007987 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007988 status = nfs4_call_sync_sequence(clnt, server, &msg,
7989 &args.seq_args, &res.seq_res);
7990 nfs_free_fhandle(res.fh);
7991 if (status)
7992 return status;
7993
7994 do_renew_lease(clp, now);
7995 return 0;
7996}
7997
7998#ifdef CONFIG_NFS_V4_1
7999
8000/*
8001 * This operation also signals the server that this client is
8002 * performing "lease moved" recovery. The server can stop asserting
8003 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
8004 * this operation is identified in the SEQUENCE operation in this
8005 * compound.
8006 */
NeilBrowna52458b2018-12-03 11:30:31 +11008007static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008008{
8009 struct nfs_server *server = NFS_SERVER(inode);
8010 struct rpc_clnt *clnt = server->client;
8011 struct nfs4_fsid_present_arg args = {
8012 .fh = NFS_FH(inode),
8013 };
8014 struct nfs4_fsid_present_res res = {
8015 };
8016 struct rpc_message msg = {
8017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8018 .rpc_argp = &args,
8019 .rpc_resp = &res,
8020 .rpc_cred = cred,
8021 };
8022 int status;
8023
8024 res.fh = nfs_alloc_fhandle();
8025 if (res.fh == NULL)
8026 return -ENOMEM;
8027
Anna Schumakerfba83f32018-05-04 16:22:50 -04008028 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04008029 status = nfs4_call_sync_sequence(clnt, server, &msg,
8030 &args.seq_args, &res.seq_res);
8031 nfs_free_fhandle(res.fh);
8032 if (status == NFS4_OK &&
8033 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8034 status = -NFS4ERR_LEASE_MOVED;
8035 return status;
8036}
8037
8038#endif /* CONFIG_NFS_V4_1 */
8039
8040/**
8041 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8042 * @inode: inode on FSID to check
8043 * @cred: credential to use for this operation
8044 *
8045 * Server indicates whether the FSID is present, moved, or not
8046 * recognized. This operation is necessary to clear a LEASE_MOVED
8047 * condition for this client ID.
8048 *
8049 * Returns NFS4_OK if the FSID is present on this server,
8050 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8051 * NFS4ERR code if some error occurred on the server, or a
8052 * negative errno if a local failure occurred.
8053 */
NeilBrowna52458b2018-12-03 11:30:31 +11008054int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008055{
8056 struct nfs_server *server = NFS_SERVER(inode);
8057 struct nfs_client *clp = server->nfs_client;
8058 const struct nfs4_mig_recovery_ops *ops =
8059 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008060 struct nfs4_exception exception = {
8061 .interruptible = true,
8062 };
Chuck Lever44c99932013-10-17 14:13:30 -04008063 int status;
8064
8065 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8066 (unsigned long long)server->fsid.major,
8067 (unsigned long long)server->fsid.minor,
8068 clp->cl_hostname);
8069 nfs_display_fhandle(NFS_FH(inode), __func__);
8070
8071 do {
8072 status = ops->fsid_present(inode, cred);
8073 if (status != -NFS4ERR_DELAY)
8074 break;
8075 nfs4_handle_exception(server, status, &exception);
8076 } while (exception.retry);
8077 return status;
8078}
8079
Trond Myklebust302fad72019-02-18 13:32:38 -05008080/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008081 * If 'use_integrity' is true and the state managment nfs_client
8082 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8083 * and the machine credential as per RFC3530bis and RFC5661 Security
8084 * Considerations sections. Otherwise, just use the user cred with the
8085 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008086 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008087static 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 +00008088{
8089 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008090 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8091 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008092 struct nfs4_secinfo_arg args = {
8093 .dir_fh = NFS_FH(dir),
8094 .name = name,
8095 };
8096 struct nfs4_secinfo_res res = {
8097 .flavors = flavors,
8098 };
8099 struct rpc_message msg = {
8100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8101 .rpc_argp = &args,
8102 .rpc_resp = &res,
8103 };
Anna Schumaker50493362019-08-14 15:30:16 -04008104 struct nfs4_call_sync_data data = {
8105 .seq_server = NFS_SERVER(dir),
8106 .seq_args = &args.seq_args,
8107 .seq_res = &res.seq_res,
8108 };
8109 struct rpc_task_setup task_setup = {
8110 .rpc_client = clnt,
8111 .rpc_message = &msg,
8112 .callback_ops = clp->cl_mvops->call_sync_ops,
8113 .callback_data = &data,
8114 .flags = RPC_TASK_NO_ROUND_ROBIN,
8115 };
NeilBrowna52458b2018-12-03 11:30:31 +11008116 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008117
8118 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008119 clnt = clp->cl_rpcclient;
8120 task_setup.rpc_client = clnt;
8121
8122 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008123 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008124 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008125
8126 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008127
Anna Schumaker50493362019-08-14 15:30:16 -04008128 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8129 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8130 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008131
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008132 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008133
NeilBrowna52458b2018-12-03 11:30:31 +11008134 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008135 return status;
8136}
8137
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008138int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8139 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008140{
Trond Myklebust0688e642019-04-07 13:59:09 -04008141 struct nfs4_exception exception = {
8142 .interruptible = true,
8143 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008144 int err;
8145 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008146 err = -NFS4ERR_WRONGSEC;
8147
8148 /* try to use integrity protection with machine cred */
8149 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8150 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8151
8152 /*
8153 * if unable to use integrity protection, or SECINFO with
8154 * integrity protection returns NFS4ERR_WRONGSEC (which is
8155 * disallowed by spec, but exists in deployed servers) use
8156 * the current filesystem's rpc_client and the user cred.
8157 */
8158 if (err == -NFS4ERR_WRONGSEC)
8159 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8160
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008161 trace_nfs4_secinfo(dir, name, err);
8162 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008163 &exception);
8164 } while (exception.retry);
8165 return err;
8166}
8167
Andy Adamson557134a2009-04-01 09:21:53 -04008168#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008169/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008170 * Check the exchange flags returned by the server for invalid flags, having
8171 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8172 * DS flags set.
8173 */
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008174static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
Andy Adamson357f54d2010-12-14 10:11:57 -05008175{
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008176 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8177 goto out_inval;
8178 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
Andy Adamson357f54d2010-12-14 10:11:57 -05008179 goto out_inval;
8180 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8181 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8182 goto out_inval;
8183 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8184 goto out_inval;
8185 return NFS_OK;
8186out_inval:
8187 return -NFS4ERR_INVAL;
8188}
8189
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008190static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008191nfs41_same_server_scope(struct nfs41_server_scope *a,
8192 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008193{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008194 if (a->server_scope_sz != b->server_scope_sz)
8195 return false;
8196 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008197}
8198
Andy Adamson02a95de2016-02-05 16:08:37 -05008199static void
8200nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8201{
Trond Myklebust5c441542019-11-13 08:34:00 +01008202 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008203 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008204 struct nfs_client *clp = args->client;
8205
8206 switch (task->tk_status) {
8207 case -NFS4ERR_BADSESSION:
8208 case -NFS4ERR_DEADSESSION:
8209 nfs4_schedule_session_recovery(clp->cl_session,
8210 task->tk_status);
8211 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008212 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8213 res->dir != NFS4_CDFS4_BOTH) {
8214 rpc_task_close_connection(task);
8215 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8216 rpc_restart_call(task);
8217 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008218}
8219
8220static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008221 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008222};
8223
Andy Adamson357f54d2010-12-14 10:11:57 -05008224/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008225 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008226 *
8227 * The 4.1 client currently uses the same TCP connection for the
8228 * fore and backchannel.
8229 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008230static
8231int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8232 struct rpc_xprt *xprt,
8233 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008234 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008235{
8236 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008237 struct nfs41_bind_conn_to_session_args args = {
8238 .client = clp,
8239 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008240 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008241 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008242 struct nfs41_bind_conn_to_session_res res;
8243 struct rpc_message msg = {
8244 .rpc_proc =
8245 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008246 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008247 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008248 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008249 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008250 struct rpc_task_setup task_setup_data = {
8251 .rpc_client = clnt,
8252 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008253 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008254 .rpc_message = &msg,
8255 .flags = RPC_TASK_TIMEOUT,
8256 };
8257 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008258
Trond Myklebust71a097c2015-02-18 09:27:18 -08008259 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8260 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8261 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008262
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008263 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8264 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8265 args.dir = NFS4_CDFC4_FORE;
8266
8267 task = rpc_run_task(&task_setup_data);
8268 if (!IS_ERR(task)) {
8269 status = task->tk_status;
8270 rpc_put_task(task);
8271 } else
8272 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008273 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008274 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008275 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008276 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8277 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008278 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008279 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008280 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008281 dprintk("NFS: %s: Unexpected direction from server\n",
8282 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008283 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008284 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008285 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008286 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8287 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008288 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008289 }
8290 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008291
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008292 return status;
8293}
8294
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008295struct rpc_bind_conn_calldata {
8296 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008297 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008298};
8299
8300static int
8301nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8302 struct rpc_xprt *xprt,
8303 void *calldata)
8304{
8305 struct rpc_bind_conn_calldata *p = calldata;
8306
8307 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8308}
8309
NeilBrowna52458b2018-12-03 11:30:31 +11008310int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008311{
8312 struct rpc_bind_conn_calldata data = {
8313 .clp = clp,
8314 .cred = cred,
8315 };
8316 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8317 nfs4_proc_bind_conn_to_session_callback, &data);
8318}
8319
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008320/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008321 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8322 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008323 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008324static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8325 .how = SP4_MACH_CRED,
8326 .enforce.u.words = {
8327 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8328 1 << (OP_EXCHANGE_ID - 32) |
8329 1 << (OP_CREATE_SESSION - 32) |
8330 1 << (OP_DESTROY_SESSION - 32) |
8331 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008332 },
8333 .allow.u.words = {
8334 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008335 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008336 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008337 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008338 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008339 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008340 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008341 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008342 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008343 1 << (OP_FREE_STATEID - 32) |
8344 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008345 }
8346};
8347
8348/*
8349 * Select the state protection mode for client `clp' given the server results
8350 * from exchange_id in `sp'.
8351 *
8352 * Returns 0 on success, negative errno otherwise.
8353 */
8354static int nfs4_sp4_select_mode(struct nfs_client *clp,
8355 struct nfs41_state_protection *sp)
8356{
8357 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8358 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8359 1 << (OP_EXCHANGE_ID - 32) |
8360 1 << (OP_CREATE_SESSION - 32) |
8361 1 << (OP_DESTROY_SESSION - 32) |
8362 1 << (OP_DESTROY_CLIENTID - 32)
8363 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008364 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008365 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008366 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008367
8368 if (sp->how == SP4_MACH_CRED) {
8369 /* Print state protect result */
8370 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8371 for (i = 0; i <= LAST_NFS4_OP; i++) {
8372 if (test_bit(i, sp->enforce.u.longs))
8373 dfprintk(MOUNT, " enforce op %d\n", i);
8374 if (test_bit(i, sp->allow.u.longs))
8375 dfprintk(MOUNT, " allow op %d\n", i);
8376 }
8377
8378 /* make sure nothing is on enforce list that isn't supported */
8379 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8380 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8381 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008382 ret = -EINVAL;
8383 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008384 }
8385 }
8386
8387 /*
8388 * Minimal mode - state operations are allowed to use machine
8389 * credential. Note this already happens by default, so the
8390 * client doesn't have to do anything more than the negotiation.
8391 *
8392 * NOTE: we don't care if EXCHANGE_ID is in the list -
8393 * we're already using the machine cred for exchange_id
8394 * and will never use a different cred.
8395 */
8396 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8397 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8398 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8399 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8400 dfprintk(MOUNT, "sp4_mach_cred:\n");
8401 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008402 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008403 } else {
8404 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008405 ret = -EINVAL;
8406 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008407 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008408
8409 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008410 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8411 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008412 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8413 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008414 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008415 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008416
Andrew Elble99ade3c2015-12-02 09:39:51 -05008417 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8418 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008419 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008420 }
8421
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008422 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8423 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8424 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008425 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008426 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008427
8428 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8429 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8430 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008431 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008432 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008433
8434 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8435 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008436 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008437 }
8438
8439 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8440 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008441 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008442 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008443 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008444out:
8445 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008446 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008447}
8448
Andy Adamson8d89bd72016-09-09 09:22:18 -04008449struct nfs41_exchange_id_data {
8450 struct nfs41_exchange_id_res res;
8451 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008452};
8453
Andy Adamson8d89bd72016-09-09 09:22:18 -04008454static void nfs4_exchange_id_release(void *data)
8455{
8456 struct nfs41_exchange_id_data *cdata =
8457 (struct nfs41_exchange_id_data *)data;
8458
Olga Kornievskaia63513232017-03-13 10:36:19 -04008459 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008460 kfree(cdata->res.impl_id);
8461 kfree(cdata->res.server_scope);
8462 kfree(cdata->res.server_owner);
8463 kfree(cdata);
8464}
8465
8466static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008467 .rpc_release = nfs4_exchange_id_release,
8468};
8469
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008470/*
8471 * _nfs4_proc_exchange_id()
8472 *
8473 * Wrapper for EXCHANGE_ID operation.
8474 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008475static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008476nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008477 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008478{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008479 struct rpc_message msg = {
8480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008481 .rpc_cred = cred,
8482 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008483 struct rpc_task_setup task_setup_data = {
8484 .rpc_client = clp->cl_rpcclient,
8485 .callback_ops = &nfs4_exchange_id_call_ops,
8486 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008487 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008488 };
8489 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008490 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008491
Elena Reshetova212bf412017-10-20 12:53:38 +03008492 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008493 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008494
Trond Myklebust9c760d12017-07-31 18:38:50 -04008495 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008496 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008497 if (!calldata)
8498 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008499
Trond Myklebustfd405592017-08-01 16:02:47 -04008500 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008501
8502 status = nfs4_init_uniform_client_string(clp);
8503 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008504 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008505
Andy Adamson8d89bd72016-09-09 09:22:18 -04008506 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8507 GFP_NOFS);
8508 status = -ENOMEM;
8509 if (unlikely(calldata->res.server_owner == NULL))
8510 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008511
Andy Adamson8d89bd72016-09-09 09:22:18 -04008512 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008513 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008514 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008515 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008516
Andy Adamson8d89bd72016-09-09 09:22:18 -04008517 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8518 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008519 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008520
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008521 switch (sp4_how) {
8522 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008523 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008524 break;
8525
8526 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008527 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008528 break;
8529
8530 default:
8531 /* unsupported! */
8532 WARN_ON_ONCE(1);
8533 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008534 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008535 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008536 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008537 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008538 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008539 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8540 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008541 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008542 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008543 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8544 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008545#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008546 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008547#endif
8548 msg.rpc_argp = &calldata->args;
8549 msg.rpc_resp = &calldata->res;
8550 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008551
Trond Myklebust9c760d12017-07-31 18:38:50 -04008552 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008553
8554out_impl_id:
8555 kfree(calldata->res.impl_id);
8556out_server_scope:
8557 kfree(calldata->res.server_scope);
8558out_server_owner:
8559 kfree(calldata->res.server_owner);
8560out_calldata:
8561 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008562out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008563 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008564 return ERR_PTR(status);
8565}
8566
8567/*
8568 * _nfs4_proc_exchange_id()
8569 *
8570 * Wrapper for EXCHANGE_ID operation.
8571 */
NeilBrowna52458b2018-12-03 11:30:31 +11008572static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008573 u32 sp4_how)
8574{
8575 struct rpc_task *task;
8576 struct nfs41_exchange_id_args *argp;
8577 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008578 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008579 int status;
8580
8581 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8582 if (IS_ERR(task))
8583 return PTR_ERR(task);
8584
8585 argp = task->tk_msg.rpc_argp;
8586 resp = task->tk_msg.rpc_resp;
8587 status = task->tk_status;
8588 if (status != 0)
8589 goto out;
8590
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008591 status = nfs4_check_cl_exchange_flags(resp->flags,
8592 clp->cl_mvops->minor_version);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008593 if (status != 0)
8594 goto out;
8595
8596 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8597 if (status != 0)
8598 goto out;
8599
Robert Milkowski7dc29932020-01-30 09:43:25 +00008600 do_renew_lease(clp, now);
8601
Trond Myklebust9c760d12017-07-31 18:38:50 -04008602 clp->cl_clientid = resp->clientid;
8603 clp->cl_exchange_flags = resp->flags;
8604 clp->cl_seqid = resp->seqid;
8605 /* Client ID is not confirmed */
8606 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8607 clear_bit(NFS4_SESSION_ESTABLISHED,
8608 &clp->cl_session->session_state);
8609
8610 if (clp->cl_serverscope != NULL &&
8611 !nfs41_same_server_scope(clp->cl_serverscope,
8612 resp->server_scope)) {
8613 dprintk("%s: server_scope mismatch detected\n",
8614 __func__);
8615 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8616 }
8617
8618 swap(clp->cl_serverowner, resp->server_owner);
8619 swap(clp->cl_serverscope, resp->server_scope);
8620 swap(clp->cl_implid, resp->impl_id);
8621
8622 /* Save the EXCHANGE_ID verifier session trunk tests */
8623 memcpy(clp->cl_confirm.data, argp->verifier.data,
8624 sizeof(clp->cl_confirm.data));
8625out:
8626 trace_nfs4_exchange_id(clp, status);
8627 rpc_put_task(task);
8628 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008629}
8630
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008631/*
8632 * nfs4_proc_exchange_id()
8633 *
8634 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8635 *
8636 * Since the clientid has expired, all compounds using sessions
8637 * associated with the stale clientid will be returning
8638 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8639 * be in some phase of session reset.
8640 *
8641 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8642 */
NeilBrowna52458b2018-12-03 11:30:31 +11008643int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008644{
8645 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8646 int status;
8647
8648 /* try SP4_MACH_CRED if krb5i/p */
8649 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8650 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008651 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008652 if (!status)
8653 return 0;
8654 }
8655
8656 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008657 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008658}
8659
Andy Adamson04fa2c62016-09-09 09:22:29 -04008660/**
8661 * nfs4_test_session_trunk
8662 *
8663 * This is an add_xprt_test() test function called from
8664 * rpc_clnt_setup_test_and_add_xprt.
8665 *
8666 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8667 * and is dereferrenced in nfs4_exchange_id_release
8668 *
8669 * Upon success, add the new transport to the rpc_clnt
8670 *
8671 * @clnt: struct rpc_clnt to get new transport
8672 * @xprt: the rpc_xprt to test
8673 * @data: call data for _nfs4_proc_exchange_id.
8674 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308675void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008676 void *data)
8677{
8678 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008679 struct rpc_task *task;
8680 int status;
8681
Andy Adamson04fa2c62016-09-09 09:22:29 -04008682 u32 sp4_how;
8683
8684 dprintk("--> %s try %s\n", __func__,
8685 xprt->address_strings[RPC_DISPLAY_ADDR]);
8686
8687 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8688
8689 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008690 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8691 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308692 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008693
8694 status = task->tk_status;
8695 if (status == 0)
8696 status = nfs4_detect_session_trunking(adata->clp,
8697 task->tk_msg.rpc_resp, xprt);
8698
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308699 if (status == 0)
8700 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8701
Trond Myklebust9c760d12017-07-31 18:38:50 -04008702 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008703}
8704EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8705
Trond Myklebust66245532012-05-25 17:18:09 -04008706static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008707 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008708{
8709 struct rpc_message msg = {
8710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8711 .rpc_argp = clp,
8712 .rpc_cred = cred,
8713 };
8714 int status;
8715
NeilBrown5a0c2572019-05-30 10:41:28 +10008716 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8717 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008718 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008719 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008720 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008721 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8722 return status;
8723}
8724
8725static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008726 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008727{
8728 unsigned int loop;
8729 int ret;
8730
8731 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8732 ret = _nfs4_proc_destroy_clientid(clp, cred);
8733 switch (ret) {
8734 case -NFS4ERR_DELAY:
8735 case -NFS4ERR_CLIENTID_BUSY:
8736 ssleep(1);
8737 break;
8738 default:
8739 return ret;
8740 }
8741 }
8742 return 0;
8743}
8744
8745int nfs4_destroy_clientid(struct nfs_client *clp)
8746{
NeilBrowna52458b2018-12-03 11:30:31 +11008747 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008748 int ret = 0;
8749
8750 if (clp->cl_mvops->minor_version < 1)
8751 goto out;
8752 if (clp->cl_exchange_flags == 0)
8753 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008754 if (clp->cl_preserve_clid)
8755 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008756 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008757 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008758 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008759 switch (ret) {
8760 case 0:
8761 case -NFS4ERR_STALE_CLIENTID:
8762 clp->cl_exchange_flags = 0;
8763 }
8764out:
8765 return ret;
8766}
8767
Donald Buczek0efb01b2019-07-07 21:26:08 +02008768#endif /* CONFIG_NFS_V4_1 */
8769
Andy Adamson2050f0c2009-04-01 09:22:30 -04008770struct nfs4_get_lease_time_data {
8771 struct nfs4_get_lease_time_args *args;
8772 struct nfs4_get_lease_time_res *res;
8773 struct nfs_client *clp;
8774};
8775
8776static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8777 void *calldata)
8778{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008779 struct nfs4_get_lease_time_data *data =
8780 (struct nfs4_get_lease_time_data *)calldata;
8781
8782 dprintk("--> %s\n", __func__);
8783 /* just setup sequence, do not trigger session recovery
8784 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008785 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008786 &data->args->la_seq_args,
8787 &data->res->lr_seq_res,
8788 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008789 dprintk("<-- %s\n", __func__);
8790}
8791
8792/*
8793 * Called from nfs4_state_manager thread for session setup, so don't recover
8794 * from sequence operation or clientid errors.
8795 */
8796static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8797{
8798 struct nfs4_get_lease_time_data *data =
8799 (struct nfs4_get_lease_time_data *)calldata;
8800
8801 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008802 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008803 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008804 switch (task->tk_status) {
8805 case -NFS4ERR_DELAY:
8806 case -NFS4ERR_GRACE:
8807 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8808 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8809 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008810 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008811 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008812 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008813 return;
8814 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008815 dprintk("<-- %s\n", __func__);
8816}
8817
Trond Myklebust17280172012-03-11 13:11:00 -04008818static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008819 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8820 .rpc_call_done = nfs4_get_lease_time_done,
8821};
8822
8823int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8824{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008825 struct nfs4_get_lease_time_args args;
8826 struct nfs4_get_lease_time_res res = {
8827 .lr_fsinfo = fsinfo,
8828 };
8829 struct nfs4_get_lease_time_data data = {
8830 .args = &args,
8831 .res = &res,
8832 .clp = clp,
8833 };
8834 struct rpc_message msg = {
8835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8836 .rpc_argp = &args,
8837 .rpc_resp = &res,
8838 };
8839 struct rpc_task_setup task_setup = {
8840 .rpc_client = clp->cl_rpcclient,
8841 .rpc_message = &msg,
8842 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008843 .callback_data = &data,
8844 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008845 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008846
Anna Schumakerfba83f32018-05-04 16:22:50 -04008847 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008848 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008849}
8850
Donald Buczek0efb01b2019-07-07 21:26:08 +02008851#ifdef CONFIG_NFS_V4_1
8852
Andy Adamsonfc931582009-04-01 09:22:31 -04008853/*
8854 * Initialize the values to be used by the client in CREATE_SESSION
8855 * If nfs4_init_session set the fore channel request and response sizes,
8856 * use them.
8857 *
8858 * Set the back channel max_resp_sz_cached to zero to force the client to
8859 * always set csa_cachethis to FALSE because the current implementation
8860 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8861 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008862static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8863 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008864{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008865 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008866 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008867 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008868
Andy Adamson18aad3d2013-06-26 12:21:49 -04008869 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8870 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8871
Andy Adamsonfc931582009-04-01 09:22:31 -04008872 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008873 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8874 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008875 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008876 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008877
8878 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008879 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008880 __func__,
8881 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008882 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008883
8884 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008885 args->bc_attrs.max_rqst_sz = max_bc_payload;
8886 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008887 args->bc_attrs.max_resp_sz_cached = 0;
8888 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008889 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008890 if (args->bc_attrs.max_reqs > max_bc_slots)
8891 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008892
8893 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8894 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8895 __func__,
8896 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8897 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8898 args->bc_attrs.max_reqs);
8899}
8900
Trond Myklebust79969dd2015-02-18 11:30:18 -08008901static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8902 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008903{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008904 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008905 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008906
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008907 if (rcvd->max_resp_sz > sent->max_resp_sz)
8908 return -EINVAL;
8909 /*
8910 * Our requested max_ops is the minimum we need; we're not
8911 * prepared to break up compounds into smaller pieces than that.
8912 * So, no point even trying to continue if the server won't
8913 * cooperate:
8914 */
8915 if (rcvd->max_ops < sent->max_ops)
8916 return -EINVAL;
8917 if (rcvd->max_reqs == 0)
8918 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008919 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8920 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008921 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008922}
8923
Trond Myklebust79969dd2015-02-18 11:30:18 -08008924static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8925 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008926{
8927 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008928 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008929
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008930 if (!(res->flags & SESSION4_BACK_CHAN))
8931 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008932 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8933 return -EINVAL;
8934 if (rcvd->max_resp_sz < sent->max_resp_sz)
8935 return -EINVAL;
8936 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8937 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008938 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008939 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008940 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008941 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008942out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008943 return 0;
8944}
Andy Adamson8d353012009-04-01 09:22:32 -04008945
Andy Adamson8d353012009-04-01 09:22:32 -04008946static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008947 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008948{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008949 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008950
Trond Myklebust79969dd2015-02-18 11:30:18 -08008951 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008952 if (ret)
8953 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008954 return nfs4_verify_back_channel_attrs(args, res);
8955}
8956
8957static void nfs4_update_session(struct nfs4_session *session,
8958 struct nfs41_create_session_res *res)
8959{
8960 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008961 /* Mark client id and session as being confirmed */
8962 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8963 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008964 session->flags = res->flags;
8965 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008966 if (res->flags & SESSION4_BACK_CHAN)
8967 memcpy(&session->bc_attrs, &res->bc_attrs,
8968 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008969}
8970
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008971static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008972 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008973{
8974 struct nfs4_session *session = clp->cl_session;
8975 struct nfs41_create_session_args args = {
8976 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008977 .clientid = clp->cl_clientid,
8978 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008979 .cb_program = NFS4_CALLBACK,
8980 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008981 struct nfs41_create_session_res res;
8982
Andy Adamsonfc931582009-04-01 09:22:31 -04008983 struct rpc_message msg = {
8984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8985 .rpc_argp = &args,
8986 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008987 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008988 };
8989 int status;
8990
Chuck Lever6b26cc82016-05-02 14:40:40 -04008991 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008992 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008993
NeilBrown5a0c2572019-05-30 10:41:28 +10008994 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8995 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008996 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008997
Trond Myklebustb519d402016-09-11 14:50:01 -04008998 switch (status) {
8999 case -NFS4ERR_STALE_CLIENTID:
9000 case -NFS4ERR_DELAY:
9001 case -ETIMEDOUT:
9002 case -EACCES:
9003 case -EAGAIN:
9004 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08009005 }
Trond Myklebustb519d402016-09-11 14:50:01 -04009006
9007 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05009008 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04009009 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009010 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009011 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08009012 if (status)
9013 goto out;
9014 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04009015 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08009016out:
Andy Adamsonfc931582009-04-01 09:22:31 -04009017 return status;
9018}
9019
9020/*
9021 * Issues a CREATE_SESSION operation to the server.
9022 * It is the responsibility of the caller to verify the session is
9023 * expired before calling this routine.
9024 */
NeilBrowna52458b2018-12-03 11:30:31 +11009025int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04009026{
9027 int status;
9028 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04009029 struct nfs4_session *session = clp->cl_session;
9030
9031 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9032
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009033 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04009034 if (status)
9035 goto out;
9036
Andy Adamsonaacd5532011-11-09 13:58:21 -05009037 /* Init or reset the session slot tables */
9038 status = nfs4_setup_session_slot_tables(session);
9039 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009040 if (status)
9041 goto out;
9042
9043 ptr = (unsigned *)&session->sess_id.data[0];
9044 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9045 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04009046out:
9047 dprintk("<-- %s\n", __func__);
9048 return status;
9049}
9050
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009051/*
9052 * Issue the over-the-wire RPC DESTROY_SESSION.
9053 * The caller must serialize access to this routine.
9054 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009055int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11009056 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009057{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009058 struct rpc_message msg = {
9059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9060 .rpc_argp = session,
9061 .rpc_cred = cred,
9062 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009063 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009064
9065 dprintk("--> nfs4_proc_destroy_session\n");
9066
9067 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05009068 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9069 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009070
NeilBrown5a0c2572019-05-30 10:41:28 +10009071 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9072 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009073 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009074
9075 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04009076 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009077 "Session has been destroyed regardless...\n", status);
9078
9079 dprintk("<-- nfs4_proc_destroy_session\n");
9080 return status;
9081}
9082
Trond Myklebust7b38c362012-05-23 13:23:31 -04009083/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009084 * Renew the cl_session lease.
9085 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009086struct nfs4_sequence_data {
9087 struct nfs_client *clp;
9088 struct nfs4_sequence_args args;
9089 struct nfs4_sequence_res res;
9090};
9091
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009092static void nfs41_sequence_release(void *data)
9093{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009094 struct nfs4_sequence_data *calldata = data;
9095 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009096
Elena Reshetova212bf412017-10-20 12:53:38 +03009097 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009098 nfs4_schedule_state_renewal(clp);
9099 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009100 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009101}
9102
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009103static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9104{
9105 switch(task->tk_status) {
9106 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009107 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9108 return -EAGAIN;
9109 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009110 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009111 }
9112 return 0;
9113}
9114
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009115static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009116{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009117 struct nfs4_sequence_data *calldata = data;
9118 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009119
Trond Myklebust14516c32010-07-31 14:29:06 -04009120 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9121 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009122
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009123 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009124 if (task->tk_status < 0) {
9125 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009126 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009127 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009128
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009129 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9130 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009131 return;
9132 }
9133 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009134 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08009135out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009136 dprintk("<-- %s\n", __func__);
9137}
9138
9139static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9140{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009141 struct nfs4_sequence_data *calldata = data;
9142 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009143 struct nfs4_sequence_args *args;
9144 struct nfs4_sequence_res *res;
9145
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009146 args = task->tk_msg.rpc_argp;
9147 res = task->tk_msg.rpc_resp;
9148
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009149 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009150}
9151
9152static const struct rpc_call_ops nfs41_sequence_ops = {
9153 .rpc_call_done = nfs41_sequence_call_done,
9154 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009155 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009156};
9157
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009158static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009159 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009160 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009161 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009162{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009163 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009164 struct rpc_message msg = {
9165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9166 .rpc_cred = cred,
9167 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009168 struct rpc_task_setup task_setup_data = {
9169 .rpc_client = clp->cl_rpcclient,
9170 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009171 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04009172 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009173 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009174 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009175
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009176 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009177 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009178 goto out_err;
9179
9180 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009181 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009182 if (calldata == NULL)
9183 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009184 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009185 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009186 msg.rpc_argp = &calldata->args;
9187 msg.rpc_resp = &calldata->res;
9188 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009189 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009190
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009191 ret = rpc_run_task(&task_setup_data);
9192 if (IS_ERR(ret))
9193 goto out_err;
9194 return ret;
9195out_put_clp:
9196 nfs_put_client(clp);
9197out_err:
9198 nfs41_release_slot(slot);
9199 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009200}
9201
NeilBrowna52458b2018-12-03 11:30:31 +11009202static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009203{
9204 struct rpc_task *task;
9205 int ret = 0;
9206
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009207 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009208 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009209 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009210 if (IS_ERR(task))
9211 ret = PTR_ERR(task);
9212 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009213 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009214 dprintk("<-- %s status=%d\n", __func__, ret);
9215 return ret;
9216}
9217
NeilBrowna52458b2018-12-03 11:30:31 +11009218static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009219{
9220 struct rpc_task *task;
9221 int ret;
9222
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009223 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009224 if (IS_ERR(task)) {
9225 ret = PTR_ERR(task);
9226 goto out;
9227 }
9228 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009229 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009230 ret = task->tk_status;
9231 rpc_put_task(task);
9232out:
9233 dprintk("<-- %s status=%d\n", __func__, ret);
9234 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009235}
9236
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009237struct nfs4_reclaim_complete_data {
9238 struct nfs_client *clp;
9239 struct nfs41_reclaim_complete_args arg;
9240 struct nfs41_reclaim_complete_res res;
9241};
9242
9243static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9244{
9245 struct nfs4_reclaim_complete_data *calldata = data;
9246
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009247 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009248 &calldata->arg.seq_args,
9249 &calldata->res.seq_res,
9250 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009251}
9252
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009253static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9254{
9255 switch(task->tk_status) {
9256 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009257 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009258 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009259 case -NFS4ERR_COMPLETE_ALREADY:
9260 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9261 break;
9262 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009263 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009264 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009265 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009266 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009267 case -NFS4ERR_BADSESSION:
9268 case -NFS4ERR_DEADSESSION:
9269 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009270 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009271 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009272 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009273 }
9274 return 0;
9275}
9276
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009277static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9278{
9279 struct nfs4_reclaim_complete_data *calldata = data;
9280 struct nfs_client *clp = calldata->clp;
9281 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9282
9283 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04009284 if (!nfs41_sequence_done(task, res))
9285 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009286
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009287 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009288 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9289 rpc_restart_call_prepare(task);
9290 return;
9291 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009292 dprintk("<-- %s\n", __func__);
9293}
9294
9295static void nfs4_free_reclaim_complete_data(void *data)
9296{
9297 struct nfs4_reclaim_complete_data *calldata = data;
9298
9299 kfree(calldata);
9300}
9301
9302static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9303 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9304 .rpc_call_done = nfs4_reclaim_complete_done,
9305 .rpc_release = nfs4_free_reclaim_complete_data,
9306};
9307
9308/*
9309 * Issue a global reclaim complete.
9310 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009311static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009312 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009313{
9314 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009315 struct rpc_message msg = {
9316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009317 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009318 };
9319 struct rpc_task_setup task_setup_data = {
9320 .rpc_client = clp->cl_rpcclient,
9321 .rpc_message = &msg,
9322 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009323 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009324 };
9325 int status = -ENOMEM;
9326
9327 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009328 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009329 if (calldata == NULL)
9330 goto out;
9331 calldata->clp = clp;
9332 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009333
Anna Schumakerfba83f32018-05-04 16:22:50 -04009334 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009335 msg.rpc_argp = &calldata->arg;
9336 msg.rpc_resp = &calldata->res;
9337 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009338 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009339out:
9340 dprintk("<-- %s status=%d\n", __func__, status);
9341 return status;
9342}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009343
9344static void
9345nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9346{
9347 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009348 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009349
9350 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009351 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009352 &lgp->res.seq_res, task);
9353 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009354}
9355
9356static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9357{
9358 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009359
9360 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009361 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009362 dprintk("<-- %s\n", __func__);
9363}
9364
9365static int
9366nfs4_layoutget_handle_exception(struct rpc_task *task,
9367 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9368{
Trond Myklebustee314c22012-10-01 17:25:48 -07009369 struct inode *inode = lgp->args.inode;
9370 struct nfs_server *server = NFS_SERVER(inode);
9371 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009372 int nfs4err = task->tk_status;
9373 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009374 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009375
Boaz Harroshed7e5422014-01-22 20:34:54 +02009376 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009377
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009378 nfs4_sequence_free_slot(&lgp->res.seq_res);
9379
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009380 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009381 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009382 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009383
9384 /*
9385 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9386 * on the file. set tk_status to -ENODATA to tell upper layer to
9387 * retry go inband.
9388 */
9389 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009390 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009391 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009392 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009393 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9394 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9395 */
9396 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009397 status = -EOVERFLOW;
9398 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009399 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009400 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009401 * (or clients) writing to the same RAID stripe except when
9402 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009403 *
9404 * Treat it like we would RECALLCONFLICT -- we retry for a little
9405 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009406 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009407 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009408 if (lgp->args.minlength == 0) {
9409 status = -EOVERFLOW;
9410 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009411 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009412 status = -EBUSY;
9413 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009414 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009415 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009416 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009417 case -NFS4ERR_DELEG_REVOKED:
9418 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009419 case -NFS4ERR_EXPIRED:
9420 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009421 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009422 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009423 lo = NFS_I(inode)->layout;
9424 /* If the open stateid was bad, then recover it. */
9425 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009426 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009427 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009428 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009429 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009430 break;
9431 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009432
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009433 /*
9434 * Mark the bad layout state as invalid, then retry
9435 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009436 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009437 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009438 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009439 pnfs_free_lseg_list(&head);
9440 status = -EAGAIN;
9441 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009442 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009443
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009444 err = nfs4_handle_exception(server, nfs4err, exception);
9445 if (!status) {
9446 if (exception->retry)
9447 status = -EAGAIN;
9448 else
9449 status = err;
9450 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009451out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009452 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009453 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009454}
9455
Fred Isamandacb4522016-09-19 17:47:09 -04009456size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009457{
9458 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9459 return nfs_page_array_len(0, max_resp_sz);
9460}
9461
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009462static void nfs4_layoutget_release(void *calldata)
9463{
9464 struct nfs4_layoutget *lgp = calldata;
9465
9466 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009467 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009468 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009469 dprintk("<-- %s\n", __func__);
9470}
9471
9472static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9473 .rpc_call_prepare = nfs4_layoutget_prepare,
9474 .rpc_call_done = nfs4_layoutget_done,
9475 .rpc_release = nfs4_layoutget_release,
9476};
9477
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009478struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009479nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009480{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009481 struct inode *inode = lgp->args.inode;
9482 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009483 struct rpc_task *task;
9484 struct rpc_message msg = {
9485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9486 .rpc_argp = &lgp->args,
9487 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009488 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009489 };
9490 struct rpc_task_setup task_setup_data = {
9491 .rpc_client = server->client,
9492 .rpc_message = &msg,
9493 .callback_ops = &nfs4_layoutget_call_ops,
9494 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009495 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009496 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009497 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009498 struct nfs4_exception exception = {
9499 .inode = inode,
9500 .timeout = *timeout,
9501 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009502 int status = 0;
9503
9504 dprintk("--> %s\n", __func__);
9505
Peng Tao4bd5a982014-11-17 11:05:17 +08009506 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9507 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9508
Anna Schumakerfba83f32018-05-04 16:22:50 -04009509 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009510
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009511 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009512
Anna Schumaker820bf852017-01-11 15:01:43 -05009513 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009514 if (status != 0)
9515 goto out;
9516
Trond Myklebust18c07782019-02-13 07:55:31 -05009517 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009518 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9519 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009520 } else if (lgp->res.layoutp->len == 0) {
9521 status = -EAGAIN;
9522 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009523 } else
9524 lseg = pnfs_layout_process(lgp);
9525out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009526 trace_nfs4_layoutget(lgp->args.ctx,
9527 &lgp->args.range,
9528 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009529 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009530 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009531
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009532 rpc_put_task(task);
9533 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009534 if (status)
9535 return ERR_PTR(status);
9536 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009537}
9538
Benny Halevycbe82602011-05-22 19:52:37 +03009539static void
9540nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9541{
9542 struct nfs4_layoutreturn *lrp = calldata;
9543
9544 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009545 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009546 &lrp->args.seq_args,
9547 &lrp->res.seq_res,
9548 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009549 if (!pnfs_layout_is_valid(lrp->args.layout))
9550 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009551}
9552
9553static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9554{
9555 struct nfs4_layoutreturn *lrp = calldata;
9556 struct nfs_server *server;
9557
9558 dprintk("--> %s\n", __func__);
9559
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009560 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009561 return;
9562
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009563 /*
9564 * Was there an RPC level error? Assume the call succeeded,
9565 * and that we need to release the layout
9566 */
9567 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9568 lrp->res.lrs_present = 0;
9569 return;
9570 }
9571
Benny Halevycbe82602011-05-22 19:52:37 +03009572 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009573 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009574 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009575 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009576 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009577 lrp->args.inode))
9578 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009579 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009580 default:
9581 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009582 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009583 case 0:
9584 break;
9585 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009586 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009587 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009588 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009589 }
Benny Halevycbe82602011-05-22 19:52:37 +03009590 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009591 return;
9592out_restart:
9593 task->tk_status = 0;
9594 nfs4_sequence_free_slot(&lrp->res.seq_res);
9595 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009596}
9597
9598static void nfs4_layoutreturn_release(void *calldata)
9599{
9600 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009601 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009602
9603 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009604 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009605 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009606 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009607 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9608 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009609 pnfs_put_layout_hdr(lrp->args.layout);
9610 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009611 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009612 kfree(calldata);
9613 dprintk("<-- %s\n", __func__);
9614}
9615
9616static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9617 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9618 .rpc_call_done = nfs4_layoutreturn_done,
9619 .rpc_release = nfs4_layoutreturn_release,
9620};
9621
Peng Tao6c166052014-11-17 09:30:40 +08009622int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009623{
9624 struct rpc_task *task;
9625 struct rpc_message msg = {
9626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9627 .rpc_argp = &lrp->args,
9628 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009629 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009630 };
9631 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009632 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009633 .rpc_message = &msg,
9634 .callback_ops = &nfs4_layoutreturn_call_ops,
9635 .callback_data = lrp,
9636 };
Peng Tao6c166052014-11-17 09:30:40 +08009637 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009638
Andrew Elble99ade3c2015-12-02 09:39:51 -05009639 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9640 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9641 &task_setup_data.rpc_client, &msg);
9642
Benny Halevycbe82602011-05-22 19:52:37 +03009643 dprintk("--> %s\n", __func__);
Trond Myklebustd973bd02021-06-01 11:35:56 -04009644 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009645 if (!sync) {
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009646 if (!lrp->inode) {
9647 nfs4_layoutreturn_release(lrp);
9648 return -EAGAIN;
9649 }
9650 task_setup_data.flags |= RPC_TASK_ASYNC;
9651 }
Trond Myklebustd973bd02021-06-01 11:35:56 -04009652 if (!lrp->inode)
9653 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9654 1);
9655 else
9656 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
9657 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009658 task = rpc_run_task(&task_setup_data);
9659 if (IS_ERR(task))
9660 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009661 if (sync)
9662 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009663 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009664 dprintk("<-- %s status=%d\n", __func__, status);
9665 rpc_put_task(task);
9666 return status;
9667}
9668
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009669static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009670_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9671 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009672 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009673{
9674 struct nfs4_getdeviceinfo_args args = {
9675 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009676 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9677 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009678 };
9679 struct nfs4_getdeviceinfo_res res = {
9680 .pdev = pdev,
9681 };
9682 struct rpc_message msg = {
9683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9684 .rpc_argp = &args,
9685 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009686 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009687 };
9688 int status;
9689
9690 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009691 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009692 if (res.notification & ~args.notify_types)
9693 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009694 if (res.notification != args.notify_types)
9695 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009696
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009697 dprintk("<-- %s status=%d\n", __func__, status);
9698
9699 return status;
9700}
9701
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009702int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9703 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009704 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009705{
9706 struct nfs4_exception exception = { };
9707 int err;
9708
9709 do {
9710 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009711 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009712 &exception);
9713 } while (exception.retry);
9714 return err;
9715}
9716EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9717
Andy Adamson863a3c62011-03-23 13:27:54 +00009718static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9719{
9720 struct nfs4_layoutcommit_data *data = calldata;
9721 struct nfs_server *server = NFS_SERVER(data->args.inode);
9722
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009723 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009724 &data->args.seq_args,
9725 &data->res.seq_res,
9726 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009727}
9728
9729static void
9730nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9731{
9732 struct nfs4_layoutcommit_data *data = calldata;
9733 struct nfs_server *server = NFS_SERVER(data->args.inode);
9734
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009735 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009736 return;
9737
9738 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009739 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9740 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9741 case -NFS4ERR_BADLAYOUT: /* no layout */
9742 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009743 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009744 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009745 break;
9746 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009747 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009748 rpc_restart_call_prepare(task);
9749 return;
9750 }
9751 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009752}
9753
9754static void nfs4_layoutcommit_release(void *calldata)
9755{
9756 struct nfs4_layoutcommit_data *data = calldata;
9757
Andy Adamsondb29c082011-07-30 20:52:38 -04009758 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009759 nfs_post_op_update_inode_force_wcc(data->args.inode,
9760 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009761 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009762 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009763 kfree(data);
9764}
9765
9766static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9767 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9768 .rpc_call_done = nfs4_layoutcommit_done,
9769 .rpc_release = nfs4_layoutcommit_release,
9770};
9771
9772int
Andy Adamsonef311532011-03-12 02:58:10 -05009773nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009774{
9775 struct rpc_message msg = {
9776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9777 .rpc_argp = &data->args,
9778 .rpc_resp = &data->res,
9779 .rpc_cred = data->cred,
9780 };
9781 struct rpc_task_setup task_setup_data = {
9782 .task = &data->task,
9783 .rpc_client = NFS_CLIENT(data->args.inode),
9784 .rpc_message = &msg,
9785 .callback_ops = &nfs4_layoutcommit_ops,
9786 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009787 };
9788 struct rpc_task *task;
9789 int status = 0;
9790
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009791 dprintk("NFS: initiating layoutcommit call. sync %d "
9792 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009793 data->args.lastbytewritten,
9794 data->args.inode->i_ino);
9795
Trond Myklebust472e2592015-02-05 16:50:30 -05009796 if (!sync) {
9797 data->inode = nfs_igrab_and_active(data->args.inode);
9798 if (data->inode == NULL) {
9799 nfs4_layoutcommit_release(data);
9800 return -EAGAIN;
9801 }
9802 task_setup_data.flags = RPC_TASK_ASYNC;
9803 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009804 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009805 task = rpc_run_task(&task_setup_data);
9806 if (IS_ERR(task))
9807 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009808 if (sync)
9809 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009810 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009811 dprintk("%s: status %d\n", __func__, status);
9812 rpc_put_task(task);
9813 return status;
9814}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009815
Trond Myklebust302fad72019-02-18 13:32:38 -05009816/*
Andy Adamson97431202013-08-08 10:57:56 -04009817 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9818 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9819 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009820static int
9821_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009822 struct nfs_fsinfo *info,
9823 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009824{
9825 struct nfs41_secinfo_no_name_args args = {
9826 .style = SECINFO_STYLE_CURRENT_FH,
9827 };
9828 struct nfs4_secinfo_res res = {
9829 .flavors = flavors,
9830 };
9831 struct rpc_message msg = {
9832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9833 .rpc_argp = &args,
9834 .rpc_resp = &res,
9835 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009836 struct nfs4_call_sync_data data = {
9837 .seq_server = server,
9838 .seq_args = &args.seq_args,
9839 .seq_res = &res.seq_res,
9840 };
9841 struct rpc_task_setup task_setup = {
9842 .rpc_client = server->client,
9843 .rpc_message = &msg,
9844 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9845 .callback_data = &data,
9846 .flags = RPC_TASK_NO_ROUND_ROBIN,
9847 };
NeilBrowna52458b2018-12-03 11:30:31 +11009848 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009849 int status;
9850
9851 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009852 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009853
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009854 cred = nfs4_get_clid_cred(server->nfs_client);
9855 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009856 }
9857
9858 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009859 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9860 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009861 dprintk("<-- %s status=%d\n", __func__, status);
9862
NeilBrowna52458b2018-12-03 11:30:31 +11009863 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009864
9865 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009866}
9867
9868static int
9869nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9870 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9871{
Trond Myklebust0688e642019-04-07 13:59:09 -04009872 struct nfs4_exception exception = {
9873 .interruptible = true,
9874 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009875 int err;
9876 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009877 /* first try using integrity protection */
9878 err = -NFS4ERR_WRONGSEC;
9879
9880 /* try to use integrity protection with machine cred */
9881 if (_nfs4_is_integrity_protected(server->nfs_client))
9882 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9883 flavors, true);
9884
9885 /*
9886 * if unable to use integrity protection, or SECINFO with
9887 * integrity protection returns NFS4ERR_WRONGSEC (which is
9888 * disallowed by spec, but exists in deployed servers) use
9889 * the current filesystem's rpc_client and the user cred.
9890 */
9891 if (err == -NFS4ERR_WRONGSEC)
9892 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9893 flavors, false);
9894
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009895 switch (err) {
9896 case 0:
9897 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009898 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009899 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009900 default:
9901 err = nfs4_handle_exception(server, err, &exception);
9902 }
9903 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009904out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009905 return err;
9906}
9907
9908static int
9909nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9910 struct nfs_fsinfo *info)
9911{
9912 int err;
9913 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009914 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009915 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009916 struct nfs4_secinfo4 *secinfo;
9917 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009918
9919 page = alloc_page(GFP_KERNEL);
9920 if (!page) {
9921 err = -ENOMEM;
9922 goto out;
9923 }
9924
9925 flavors = page_address(page);
9926 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9927
9928 /*
9929 * Fall back on "guess and check" method if
9930 * the server doesn't support SECINFO_NO_NAME
9931 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009932 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009933 err = nfs4_find_root_sec(server, fhandle, info);
9934 goto out_freepage;
9935 }
9936 if (err)
9937 goto out_freepage;
9938
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009939 for (i = 0; i < flavors->num_flavors; i++) {
9940 secinfo = &flavors->flavors[i];
9941
9942 switch (secinfo->flavor) {
9943 case RPC_AUTH_NULL:
9944 case RPC_AUTH_UNIX:
9945 case RPC_AUTH_GSS:
9946 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9947 &secinfo->flavor_info);
9948 break;
9949 default:
9950 flavor = RPC_AUTH_MAXFLAVOR;
9951 break;
9952 }
9953
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009954 if (!nfs_auth_info_match(&server->auth_info, flavor))
9955 flavor = RPC_AUTH_MAXFLAVOR;
9956
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009957 if (flavor != RPC_AUTH_MAXFLAVOR) {
9958 err = nfs4_lookup_root_sec(server, fhandle,
9959 info, flavor);
9960 if (!err)
9961 break;
9962 }
9963 }
9964
9965 if (flavor == RPC_AUTH_MAXFLAVOR)
9966 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009967
9968out_freepage:
9969 put_page(page);
9970 if (err == -EACCES)
9971 return -EPERM;
9972out:
9973 return err;
9974}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009975
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009976static int _nfs41_test_stateid(struct nfs_server *server,
9977 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009978 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009979{
9980 int status;
9981 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009982 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009983 };
9984 struct nfs41_test_stateid_res res;
9985 struct rpc_message msg = {
9986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9987 .rpc_argp = &args,
9988 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009989 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009990 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009991 struct rpc_clnt *rpc_client = server->client;
9992
9993 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9994 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009995
Chuck Lever38527b12012-07-11 16:30:23 -04009996 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009997 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009998 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009999 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -040010000 if (status != NFS_OK) {
10001 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -040010002 return status;
Chuck Lever38527b12012-07-11 16:30:23 -040010003 }
10004 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -040010005 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -040010006}
10007
Trond Myklebust43912bb2016-09-22 13:38:56 -040010008static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10009 int err, struct nfs4_exception *exception)
10010{
10011 exception->retry = 0;
10012 switch(err) {
10013 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -040010014 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -040010015 nfs4_handle_exception(server, err, exception);
10016 break;
10017 case -NFS4ERR_BADSESSION:
10018 case -NFS4ERR_BADSLOT:
10019 case -NFS4ERR_BAD_HIGH_SLOT:
10020 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10021 case -NFS4ERR_DEADSESSION:
10022 nfs4_do_handle_exception(server, err, exception);
10023 }
10024}
10025
Chuck Lever38527b12012-07-11 16:30:23 -040010026/**
10027 * nfs41_test_stateid - perform a TEST_STATEID operation
10028 *
10029 * @server: server / transport on which to perform the operation
10030 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010031 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -040010032 *
10033 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10034 * Otherwise a negative NFS4ERR value is returned if the operation
10035 * failed or the state ID is not currently valid.
10036 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010037static int nfs41_test_stateid(struct nfs_server *server,
10038 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010039 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010040{
Trond Myklebust0688e642019-04-07 13:59:09 -040010041 struct nfs4_exception exception = {
10042 .interruptible = true,
10043 };
Bryan Schumaker7d974792011-06-02 14:59:08 -040010044 int err;
10045 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010046 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -040010047 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -040010048 } while (exception.retry);
10049 return err;
10050}
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010051
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010052struct nfs_free_stateid_data {
10053 struct nfs_server *server;
10054 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010055 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010056};
10057
10058static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10059{
10060 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -050010061 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010062 &data->args.seq_args,
10063 &data->res.seq_res,
10064 task);
10065}
10066
10067static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10068{
10069 struct nfs_free_stateid_data *data = calldata;
10070
10071 nfs41_sequence_done(task, &data->res.seq_res);
10072
10073 switch (task->tk_status) {
10074 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +100010075 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010076 rpc_restart_call_prepare(task);
10077 }
10078}
10079
10080static void nfs41_free_stateid_release(void *calldata)
10081{
10082 kfree(calldata);
10083}
10084
Trond Myklebust17f26b12013-08-21 15:48:42 -040010085static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010086 .rpc_call_prepare = nfs41_free_stateid_prepare,
10087 .rpc_call_done = nfs41_free_stateid_done,
10088 .rpc_release = nfs41_free_stateid_release,
10089};
10090
Anna Schumaker2f261022018-05-15 13:03:39 -040010091/**
10092 * nfs41_free_stateid - perform a FREE_STATEID operation
10093 *
10094 * @server: server / transport on which to perform the operation
10095 * @stateid: state ID to release
10096 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010097 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010098 *
10099 * Note: this function is always asynchronous.
10100 */
10101static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010102 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010103 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010104 bool privileged)
10105{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010106 struct rpc_message msg = {
10107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010108 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010109 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010110 struct rpc_task_setup task_setup = {
10111 .rpc_client = server->client,
10112 .rpc_message = &msg,
10113 .callback_ops = &nfs41_free_stateid_ops,
10114 .flags = RPC_TASK_ASYNC,
10115 };
10116 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010117 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010118
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010119 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10120 &task_setup.rpc_client, &msg);
10121
Chuck Lever38527b12012-07-11 16:30:23 -040010122 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010123 data = kmalloc(sizeof(*data), GFP_NOFS);
10124 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010125 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010126 data->server = server;
10127 nfs4_stateid_copy(&data->args.stateid, stateid);
10128
10129 task_setup.callback_data = data;
10130
10131 msg.rpc_argp = &data->args;
10132 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010133 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010134 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010135 if (IS_ERR(task))
10136 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010137 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010138 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010139}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010140
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010141static void
10142nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010143{
NeilBrowna52458b2018-12-03 11:30:31 +110010144 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010145
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010146 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010147 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010148}
10149
Trond Myklebust36281ca2012-03-04 18:13:56 -050010150static bool nfs41_match_stateid(const nfs4_stateid *s1,
10151 const nfs4_stateid *s2)
10152{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010153 if (s1->type != s2->type)
10154 return false;
10155
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010156 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010157 return false;
10158
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010159 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010160 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010161
Anna Schumaker045c5512017-01-11 16:59:48 -050010162 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010163}
10164
Andy Adamson557134a2009-04-01 09:21:53 -040010165#endif /* CONFIG_NFS_V4_1 */
10166
Trond Myklebust36281ca2012-03-04 18:13:56 -050010167static bool nfs4_match_stateid(const nfs4_stateid *s1,
10168 const nfs4_stateid *s2)
10169{
Trond Myklebustf597c532012-03-04 18:13:56 -050010170 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010171}
10172
10173
Trond Myklebust17280172012-03-11 13:11:00 -040010174static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010175 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010176 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 .recover_open = nfs4_open_reclaim,
10178 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010179 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010180 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010181};
10182
Andy Adamson591d71c2009-04-01 09:22:47 -040010183#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010184static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010185 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10186 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10187 .recover_open = nfs4_open_reclaim,
10188 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010189 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010190 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010191 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010192};
10193#endif /* CONFIG_NFS_V4_1 */
10194
Trond Myklebust17280172012-03-11 13:11:00 -040010195static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010196 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010197 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010198 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010199 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010200 .establish_clid = nfs4_init_clientid,
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_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010205 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10206 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010207 .recover_open = nfs41_open_expired,
10208 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010209 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010210};
10211#endif /* CONFIG_NFS_V4_1 */
10212
Trond Myklebust17280172012-03-11 13:11:00 -040010213static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010214 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010215 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010216 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010217};
10218
10219#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010220static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010221 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010222 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010223 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010224};
10225#endif
10226
Chuck Leverec011fe2013-10-17 14:12:39 -040010227static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010228 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010229 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010230};
10231
10232#if defined(CONFIG_NFS_V4_1)
10233static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010234 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010235 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010236};
10237#endif /* CONFIG_NFS_V4_1 */
10238
Trond Myklebust97dc1352010-06-16 09:52:26 -040010239static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10240 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010241 .init_caps = NFS_CAP_READDIRPLUS
10242 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010243 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010244 .init_client = nfs40_init_client,
10245 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010246 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010247 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010248 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010249 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010250 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010251 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010252 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10253 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10254 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010255 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010256};
10257
10258#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010259static struct nfs_seqid *
10260nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10261{
10262 return NULL;
10263}
10264
Trond Myklebust97dc1352010-06-16 09:52:26 -040010265static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10266 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010267 .init_caps = NFS_CAP_READDIRPLUS
10268 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010269 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010270 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010271 | NFS_CAP_ATOMIC_OPEN_V1
10272 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010273 .init_client = nfs41_init_client,
10274 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010275 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010276 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010277 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010278 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010279 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010280 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010281 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010282 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10283 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10284 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010285 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010286};
10287#endif
10288
Steve Dickson42c2c422013-05-22 12:50:38 -040010289#if defined(CONFIG_NFS_V4_2)
10290static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10291 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010292 .init_caps = NFS_CAP_READDIRPLUS
10293 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010294 | NFS_CAP_POSIX_LOCK
10295 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010296 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010297 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010298 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010299 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010300 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010301 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010302 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010303 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010304 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010305 | NFS_CAP_CLONE
Anna Schumakerc5675522014-05-28 13:41:22 -040010306 | NFS_CAP_LAYOUTERROR
10307 | NFS_CAP_READ_PLUS,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010308 .init_client = nfs41_init_client,
10309 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010310 .match_stateid = nfs41_match_stateid,
10311 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010312 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010313 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010314 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010315 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010316 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010317 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10318 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10319 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010320 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010321};
10322#endif
10323
Trond Myklebust97dc1352010-06-16 09:52:26 -040010324const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10325 [0] = &nfs_v4_0_minor_ops,
10326#if defined(CONFIG_NFS_V4_1)
10327 [1] = &nfs_v4_1_minor_ops,
10328#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010329#if defined(CONFIG_NFS_V4_2)
10330 [2] = &nfs_v4_2_minor_ops,
10331#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010332};
10333
Trond Myklebust13997822016-07-24 17:10:52 -040010334static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010335{
Frank van der Linden012a2112020-06-23 22:39:03 +000010336 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010337
10338 error = generic_listxattr(dentry, list, size);
10339 if (error < 0)
10340 return error;
10341 if (list) {
10342 list += error;
10343 size -= error;
10344 }
10345
10346 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10347 if (error2 < 0)
10348 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010349
10350 if (list) {
10351 list += error2;
10352 size -= error2;
10353 }
10354
10355 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10356 if (error3 < 0)
10357 return error3;
10358
10359 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010360}
10361
Trond Myklebust17f26b12013-08-21 15:48:42 -040010362static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010363 .create = nfs_create,
10364 .lookup = nfs_lookup,
10365 .atomic_open = nfs_atomic_open,
10366 .link = nfs_link,
10367 .unlink = nfs_unlink,
10368 .symlink = nfs_symlink,
10369 .mkdir = nfs_mkdir,
10370 .rmdir = nfs_rmdir,
10371 .mknod = nfs_mknod,
10372 .rename = nfs_rename,
10373 .permission = nfs_permission,
10374 .getattr = nfs_getattr,
10375 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010376 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010377};
10378
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010379static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010380 .permission = nfs_permission,
10381 .getattr = nfs_getattr,
10382 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010383 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010384};
10385
David Howells509de812006-08-22 20:06:11 -040010386const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010387 .version = 4, /* protocol version */
10388 .dentry_ops = &nfs4_dentry_operations,
10389 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010390 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010391 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010392 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010393 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010394 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010395 .getattr = nfs4_proc_getattr,
10396 .setattr = nfs4_proc_setattr,
10397 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010398 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399 .access = nfs4_proc_access,
10400 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010401 .create = nfs4_proc_create,
10402 .remove = nfs4_proc_remove,
10403 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010404 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010405 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010406 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010407 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010408 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010409 .link = nfs4_proc_link,
10410 .symlink = nfs4_proc_symlink,
10411 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010412 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010413 .readdir = nfs4_proc_readdir,
10414 .mknod = nfs4_proc_mknod,
10415 .statfs = nfs4_proc_statfs,
10416 .fsinfo = nfs4_proc_fsinfo,
10417 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010418 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010420 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010421 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010422 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010423 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010424 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010425 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010426 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010427 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010428 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010429 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010430 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010431 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010432 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010433 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010434 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -040010435 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010436 .create_server = nfs4_create_server,
10437 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010438};
10439
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010440static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010441 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010442 .list = nfs4_xattr_list_nfs4_acl,
10443 .get = nfs4_xattr_get_nfs4_acl,
10444 .set = nfs4_xattr_set_nfs4_acl,
10445};
10446
Frank van der Linden012a2112020-06-23 22:39:03 +000010447#ifdef CONFIG_NFS_V4_2
10448static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10449 .prefix = XATTR_USER_PREFIX,
10450 .get = nfs4_xattr_get_nfs4_user,
10451 .set = nfs4_xattr_set_nfs4_user,
10452};
10453#endif
10454
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010455const struct xattr_handler *nfs4_xattr_handlers[] = {
10456 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010457#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10458 &nfs4_xattr_nfs4_label_handler,
10459#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010460#ifdef CONFIG_NFS_V4_2
10461 &nfs4_xattr_nfs4_user_handler,
10462#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010463 NULL
10464};
10465
Linus Torvalds1da177e2005-04-16 15:20:36 -070010466/*
10467 * Local variables:
10468 * c-basic-offset: 8
10469 * End:
10470 */