blob: a213115204043bac4b8dda893f1056635461cf0d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust39d43d12020-10-07 18:24:18 -040066#include "sysfs.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040067#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050068#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000069#include "fscache.h"
Frank van der Linden012a2112020-06-23 22:39:03 +000070#include "nfs42.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Trond Myklebustc6d01c62013-08-09 11:51:26 -040072#include "nfs4trace.h"
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFSDBG_FACILITY NFSDBG_PROC
75
Trond Myklebust30846df2018-04-07 13:44:28 -040076#define NFS4_BITMASK_SZ 3
77
Trond Myklebust2066fe82006-09-15 08:30:46 -040078#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define NFS4_POLL_RETRY_MAX (15*HZ)
80
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020081/* file attributes which can be mapped to nfs attributes */
82#define NFS4_VALID_ATTRS (ATTR_MODE \
83 | ATTR_UID \
84 | ATTR_GID \
85 | ATTR_SIZE \
86 | ATTR_ATIME \
87 | ATTR_MTIME \
88 | ATTR_CTIME \
89 | ATTR_ATIME_SET \
90 | ATTR_MTIME_SET)
91
Trond Myklebustcdd4e682006-01-03 09:55:12 +010092struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080093static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050095static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040096static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110097static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040098 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110099 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -0400100 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400101#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400102static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100103 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400104 struct nfs4_slot *slot,
105 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400106static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100107 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400108static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100109 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400110#endif
Trond Myklebust332d1a02021-03-25 18:15:36 -0400111static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ],
112 const __u32 *src, struct inode *inode,
113 struct nfs_server *server,
114 struct nfs4_label *label);
David Quigleyaa9c2662013-05-22 12:50:44 -0400115
116#ifdef CONFIG_NFS_V4_SECURITY_LABEL
117static inline struct nfs4_label *
118nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
119 struct iattr *sattr, struct nfs4_label *label)
120{
121 int err;
122
123 if (label == NULL)
124 return NULL;
125
126 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
127 return NULL;
128
David Quigleyaa9c2662013-05-22 12:50:44 -0400129 err = security_dentry_init_security(dentry, sattr->ia_mode,
130 &dentry->d_name, (void **)&label->label, &label->len);
131 if (err == 0)
132 return label;
133
134 return NULL;
135}
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{
139 if (label)
140 security_release_secctx(label->label, label->len);
141}
142static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
143{
144 if (label)
145 return server->attr_bitmask;
146
147 return server->attr_bitmask_nl;
148}
149#else
150static inline struct nfs4_label *
151nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
152 struct iattr *sattr, struct nfs4_label *l)
153{ return NULL; }
154static inline void
155nfs4_label_release_security(struct nfs4_label *label)
156{ return; }
157static inline u32 *
158nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
159{ return server->attr_bitmask; }
160#endif
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500163static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 if (err >= -1000)
166 return err;
167 switch (err) {
168 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500169 case -NFS4ERR_LAYOUTTRYLATER:
170 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400171 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000172 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400173 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000174 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800175 case -NFS4ERR_BADOWNER:
176 case -NFS4ERR_BADNAME:
177 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400178 case -NFS4ERR_SHARE_DENIED:
179 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400180 case -NFS4ERR_MINOR_VERS_MISMATCH:
181 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400182 case -NFS4ERR_FILE_OPEN:
183 return -EBUSY;
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500184 case -NFS4ERR_NOT_SAME:
185 return -ENOTSYNC;
Trond Myklebust52567b02009-10-23 14:46:42 -0400186 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700188 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400189 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400191 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/*
195 * This is our standard bitmap for GETATTR requests.
196 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400197const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 FATTR4_WORD0_TYPE
199 | FATTR4_WORD0_CHANGE
200 | FATTR4_WORD0_SIZE
201 | FATTR4_WORD0_FSID
202 | FATTR4_WORD0_FILEID,
203 FATTR4_WORD1_MODE
204 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER
206 | FATTR4_WORD1_OWNER_GROUP
207 | FATTR4_WORD1_RAWDEV
208 | FATTR4_WORD1_SPACE_USED
209 | FATTR4_WORD1_TIME_ACCESS
210 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400211 | FATTR4_WORD1_TIME_MODIFY
212 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400213#ifdef CONFIG_NFS_V4_SECURITY_LABEL
214 FATTR4_WORD2_SECURITY_LABEL
215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
Trond Myklebust1549210f2012-06-05 09:16:47 -0400218static const u32 nfs4_pnfs_open_bitmap[3] = {
219 FATTR4_WORD0_TYPE
220 | FATTR4_WORD0_CHANGE
221 | FATTR4_WORD0_SIZE
222 | FATTR4_WORD0_FSID
223 | FATTR4_WORD0_FILEID,
224 FATTR4_WORD1_MODE
225 | FATTR4_WORD1_NUMLINKS
226 | FATTR4_WORD1_OWNER
227 | FATTR4_WORD1_OWNER_GROUP
228 | FATTR4_WORD1_RAWDEV
229 | FATTR4_WORD1_SPACE_USED
230 | FATTR4_WORD1_TIME_ACCESS
231 | FATTR4_WORD1_TIME_METADATA
232 | FATTR4_WORD1_TIME_MODIFY,
233 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500234#ifdef CONFIG_NFS_V4_SECURITY_LABEL
235 | FATTR4_WORD2_SECURITY_LABEL
236#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400237};
238
Andy Adamsone23008e2012-10-02 21:07:32 -0400239static const u32 nfs4_open_noattr_bitmap[3] = {
240 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400241 | FATTR4_WORD0_FILEID,
242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 FATTR4_WORD0_FILES_AVAIL
246 | FATTR4_WORD0_FILES_FREE
247 | FATTR4_WORD0_FILES_TOTAL,
248 FATTR4_WORD1_SPACE_AVAIL
249 | FATTR4_WORD1_SPACE_FREE
250 | FATTR4_WORD1_SPACE_TOTAL
251};
252
David Quigleya09df2c2013-05-22 12:50:41 -0400253const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 FATTR4_WORD0_MAXLINK
255 | FATTR4_WORD0_MAXNAME,
256 0
257};
258
Fred Isamandae100c2011-07-30 20:52:37 -0400259const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 | FATTR4_WORD0_MAXREAD
261 | FATTR4_WORD0_MAXWRITE
262 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700263 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400264 | FATTR4_WORD1_FS_LAYOUT_TYPES,
265 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800266 | FATTR4_WORD2_CLONE_BLKSIZE
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000267 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268};
269
David Quigleya09df2c2013-05-22 12:50:41 -0400270const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500271 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400272 | FATTR4_WORD0_SIZE
273 | FATTR4_WORD0_FSID
274 | FATTR4_WORD0_FILEID
275 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500276 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400277 | FATTR4_WORD1_OWNER_GROUP
278 | FATTR4_WORD1_RAWDEV
279 | FATTR4_WORD1_SPACE_USED
280 | FATTR4_WORD1_TIME_ACCESS
281 | FATTR4_WORD1_TIME_METADATA
282 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400283 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400284};
285
Trond Myklebust30846df2018-04-07 13:44:28 -0400286static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
287 struct inode *inode)
288{
289 unsigned long cache_validity;
290
291 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
292 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
293 return;
294
295 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
296 if (!(cache_validity & NFS_INO_REVAL_FORCED))
297 cache_validity &= ~(NFS_INO_INVALID_CHANGE
298 | NFS_INO_INVALID_SIZE);
299
300 if (!(cache_validity & NFS_INO_INVALID_SIZE))
301 dst[0] &= ~FATTR4_WORD0_SIZE;
302
303 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
304 dst[0] &= ~FATTR4_WORD0_CHANGE;
305}
306
307static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
308 const __u32 *src, struct inode *inode)
309{
310 nfs4_bitmap_copy_adjust(dst, src, inode);
311}
312
Al Virobc4785c2006-10-19 23:28:51 -0700313static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct nfs4_readdir_arg *readdir)
315{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400316 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700317 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000320 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
322 return;
323 }
324
325 readdir->cookie = 0;
326 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
327 if (cookie == 2)
328 return;
329
330 /*
331 * NFSv4 servers do not return entries for '.' and '..'
332 * Therefore, we fake these entries here. We let '.'
333 * have cookie 0 and '..' have cookie 1. Note that
334 * when talking to the server, we always send cookie 0
335 * instead of 1 or 2.
336 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800337 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (cookie == 0) {
340 *p++ = xdr_one; /* next */
341 *p++ = xdr_zero; /* cookie, first word */
342 *p++ = xdr_one; /* cookie, second word */
343 *p++ = xdr_one; /* entry len */
344 memcpy(p, ".\0\0\0", 4); /* entry */
345 p++;
346 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400347 *p++ = htonl(attrs); /* bitmap */
348 *p++ = htonl(12); /* attribute buffer length */
349 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000350 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
353 *p++ = xdr_one; /* next */
354 *p++ = xdr_zero; /* cookie, first word */
355 *p++ = xdr_two; /* cookie, second word */
356 *p++ = xdr_two; /* entry len */
357 memcpy(p, "..\0\0", 4); /* entry */
358 p++;
359 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400360 *p++ = htonl(attrs); /* bitmap */
361 *p++ = htonl(12); /* attribute buffer length */
362 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000363 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 readdir->pgbase = (char *)p - (char *)start;
366 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800367 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Trond Myklebust26d36302016-09-22 13:39:05 -0400370static void nfs4_test_and_free_stateid(struct nfs_server *server,
371 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100372 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400373{
374 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
375
376 ops->test_and_free_expired(server, stateid, cred);
377}
378
379static void __nfs4_free_revoked_stateid(struct nfs_server *server,
380 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100381 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400382{
383 stateid->type = NFS4_REVOKED_STATEID_TYPE;
384 nfs4_test_and_free_stateid(server, stateid, cred);
385}
386
387static void nfs4_free_revoked_stateid(struct nfs_server *server,
388 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100389 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400390{
391 nfs4_stateid tmp;
392
393 nfs4_stateid_copy(&tmp, stateid);
394 __nfs4_free_revoked_stateid(server, &tmp, cred);
395}
396
NeilBrown8478eaa2014-09-18 16:09:27 +1000397static long nfs4_update_delay(long *timeout)
398{
399 long ret;
400 if (!timeout)
401 return NFS4_POLL_RETRY_MAX;
402 if (*timeout <= 0)
403 *timeout = NFS4_POLL_RETRY_MIN;
404 if (*timeout > NFS4_POLL_RETRY_MAX)
405 *timeout = NFS4_POLL_RETRY_MAX;
406 ret = *timeout;
407 *timeout <<= 1;
408 return ret;
409}
410
Trond Myklebust0688e642019-04-07 13:59:09 -0400411static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500412{
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 might_sleep();
414
NeilBrown8478eaa2014-09-18 16:09:27 +1000415 freezable_schedule_timeout_killable_unsafe(
416 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400417 if (!__fatal_signal_pending(current))
418 return 0;
419 return -EINTR;
420}
421
422static int nfs4_delay_interruptible(long *timeout)
423{
424 might_sleep();
425
He Zhe59679d92020-07-06 17:52:24 +0800426 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400427 if (!signal_pending(current))
428 return 0;
429 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
430}
431
432static int nfs4_delay(long *timeout, bool interruptible)
433{
434 if (interruptible)
435 return nfs4_delay_interruptible(timeout);
436 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500437}
438
Trond Myklebust50c80002019-07-11 19:02:18 -0400439static const nfs4_stateid *
440nfs4_recoverable_stateid(const nfs4_stateid *stateid)
441{
442 if (!stateid)
443 return NULL;
444 switch (stateid->type) {
445 case NFS4_OPEN_STATEID_TYPE:
446 case NFS4_LOCK_STATEID_TYPE:
447 case NFS4_DELEGATION_STATEID_TYPE:
448 return stateid;
449 default:
450 break;
451 }
452 return NULL;
453}
454
Trond Myklebust65de8722008-12-23 15:21:44 -0500455/* This is the error handling routine for processes that are allowed
456 * to sleep.
457 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400458static int nfs4_do_handle_exception(struct nfs_server *server,
459 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500460{
461 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500462 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400463 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500464 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500465 int ret = errorcode;
466
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400467 exception->delay = 0;
468 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500469 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400470
Trond Myklebust50c80002019-07-11 19:02:18 -0400471 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400472 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400473 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400474
Trond Myklebust65de8722008-12-23 15:21:44 -0500475 switch(errorcode) {
476 case 0:
477 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400478 case -NFS4ERR_BADHANDLE:
479 case -ESTALE:
480 if (inode != NULL && S_ISREG(inode->i_mode))
481 pnfs_destroy_layout(NFS_I(inode));
482 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400483 case -NFS4ERR_DELEG_REVOKED:
484 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400485 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400486 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400487 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400488 if (inode != NULL && stateid != NULL) {
489 nfs_inode_find_state_and_recover(inode,
490 stateid);
491 goto wait_on_recovery;
492 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500493 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400494 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400495 if (inode) {
496 int err;
497
498 err = nfs_async_inode_return_delegation(inode,
499 stateid);
500 if (err == 0)
501 goto wait_on_recovery;
502 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
503 exception->retry = 1;
504 break;
505 }
506 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500507 if (state == NULL)
508 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400509 ret = nfs4_schedule_stateid_recovery(server, state);
510 if (ret < 0)
511 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500512 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500513 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500514 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500515 nfs4_schedule_lease_recovery(clp);
516 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400517 case -NFS4ERR_MOVED:
518 ret = nfs4_schedule_migration_recovery(server);
519 if (ret < 0)
520 break;
521 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400522 case -NFS4ERR_LEASE_MOVED:
523 nfs4_schedule_lease_moved_recovery(clp);
524 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500525#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400526 case -NFS4ERR_BADSESSION:
527 case -NFS4ERR_BADSLOT:
528 case -NFS4ERR_BAD_HIGH_SLOT:
529 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
530 case -NFS4ERR_DEADSESSION:
531 case -NFS4ERR_SEQ_FALSE_RETRY:
532 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100533 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400534 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500535#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500536 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500537 if (exception->timeout > HZ) {
538 /* We have retried a decent amount, time to
539 * fail
540 */
541 ret = -EBUSY;
542 break;
543 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500544 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500545 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400546 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500547 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400548 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400549 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400550 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400551 exception->delay = 1;
552 return 0;
553
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400554 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500555 case -NFS4ERR_OLD_STATEID:
556 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800557 break;
558 case -NFS4ERR_BADOWNER:
559 /* The following works around a Linux server bug! */
560 case -NFS4ERR_BADNAME:
561 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
562 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
563 exception->retry = 1;
564 printk(KERN_WARNING "NFS: v4 server %s "
565 "does not accept raw "
566 "uid/gids. "
567 "Reenabling the idmapper.\n",
568 server->nfs_client->cl_hostname);
569 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500570 }
571 /* We failed to handle the error */
572 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500573wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400574 exception->recovering = 1;
575 return 0;
576}
577
578/* This is the error handling routine for processes that are allowed
579 * to sleep.
580 */
581int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
582{
583 struct nfs_client *clp = server->nfs_client;
584 int ret;
585
586 ret = nfs4_do_handle_exception(server, errorcode, exception);
587 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400588 ret = nfs4_delay(&exception->timeout,
589 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400590 goto out_retry;
591 }
592 if (exception->recovering) {
593 ret = nfs4_wait_clnt_recover(clp);
594 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
595 return -EIO;
596 goto out_retry;
597 }
598 return ret;
599out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500600 if (ret == 0)
601 exception->retry = 1;
602 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500603}
604
Trond Myklebust037fc982015-09-20 15:51:00 -0400605static int
606nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
607 int errorcode, struct nfs4_exception *exception)
608{
609 struct nfs_client *clp = server->nfs_client;
610 int ret;
611
612 ret = nfs4_do_handle_exception(server, errorcode, exception);
613 if (exception->delay) {
614 rpc_delay(task, nfs4_update_delay(&exception->timeout));
615 goto out_retry;
616 }
617 if (exception->recovering) {
618 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
619 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
620 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
621 goto out_retry;
622 }
623 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
624 ret = -EIO;
625 return ret;
626out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500627 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400628 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500629 /*
630 * For NFS4ERR_MOVED, the client transport will need to
631 * be recomputed after migration recovery has completed.
632 */
633 if (errorcode == -NFS4ERR_MOVED)
634 rpc_task_release_transport(task);
635 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400636 return ret;
637}
638
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400639int
Trond Myklebust037fc982015-09-20 15:51:00 -0400640nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
641 struct nfs4_state *state, long *timeout)
642{
643 struct nfs4_exception exception = {
644 .state = state,
645 };
646
647 if (task->tk_status >= 0)
648 return 0;
649 if (timeout)
650 exception.timeout = *timeout;
651 task->tk_status = nfs4_async_handle_exception(task, server,
652 task->tk_status,
653 &exception);
654 if (exception.delay && timeout)
655 *timeout = exception.timeout;
656 if (exception.retry)
657 return -EAGAIN;
658 return 0;
659}
660
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400661/*
662 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
663 * or 'false' otherwise.
664 */
665static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
666{
667 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500668 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400669}
Trond Myklebust65de8722008-12-23 15:21:44 -0500670
Trond Myklebust452e9352010-07-31 14:29:06 -0400671static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 spin_lock(&clp->cl_lock);
674 if (time_before(clp->cl_last_renewal,timestamp))
675 clp->cl_last_renewal = timestamp;
676 spin_unlock(&clp->cl_lock);
677}
678
Trond Myklebust452e9352010-07-31 14:29:06 -0400679static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
680{
Trond Myklebustbe824162015-07-05 14:50:46 -0400681 struct nfs_client *clp = server->nfs_client;
682
683 if (!nfs4_has_session(clp))
684 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400685}
686
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400687struct nfs4_call_sync_data {
688 const struct nfs_server *seq_server;
689 struct nfs4_sequence_args *seq_args;
690 struct nfs4_sequence_res *seq_res;
691};
692
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800693void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400694 struct nfs4_sequence_res *res, int cache_reply,
695 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400696{
697 args->sa_slot = NULL;
698 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400699 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400700
701 res->sr_slot = NULL;
702}
703
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400704static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400705{
706 struct nfs4_slot *slot = res->sr_slot;
707 struct nfs4_slot_table *tbl;
708
Chuck Lever3bd23842013-08-09 12:49:19 -0400709 tbl = slot->table;
710 spin_lock(&tbl->slot_tbl_lock);
711 if (!nfs41_wake_and_assign_slot(tbl, slot))
712 nfs4_free_slot(tbl, slot);
713 spin_unlock(&tbl->slot_tbl_lock);
714
715 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400716}
717
718static int nfs40_sequence_done(struct rpc_task *task,
719 struct nfs4_sequence_res *res)
720{
721 if (res->sr_slot != NULL)
722 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400723 return 1;
724}
725
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400726#if defined(CONFIG_NFS_V4_1)
727
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400728static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400729{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500730 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400731 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500732 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400733
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400734 if (!slot)
735 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500736 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500737 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500738
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400739 /* Bump the slot sequence number */
740 if (slot->seq_done)
741 slot->seq_nr++;
742 slot->seq_done = 0;
743
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500744 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500745 /* Be nice to the server: try to ensure that the last transmitted
746 * value for highest_user_slotid <= target_highest_slotid
747 */
748 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
749 send_new_highest_used_slotid = true;
750
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500751 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500752 send_new_highest_used_slotid = false;
753 goto out_unlock;
754 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500755 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500756
757 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
758 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500759out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500760 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500761 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400762 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400763 if (waitqueue_active(&tbl->slot_waitq))
764 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400765}
766
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400767static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
768{
769 nfs41_release_slot(res->sr_slot);
770 res->sr_slot = NULL;
771}
772
Trond Myklebust3453d572018-06-20 17:53:34 -0400773static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
774 u32 seqnr)
775{
776 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
777 slot->seq_nr_highest_sent = seqnr;
778}
779static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
780 u32 seqnr)
781{
782 slot->seq_nr_highest_sent = seqnr;
783 slot->seq_nr_last_acked = seqnr;
784}
785
Anna Schumaker913fadc2020-07-08 10:33:40 -0400786static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
787 struct nfs4_slot *slot)
788{
789 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
790 if (!IS_ERR(task))
791 rpc_put_task_async(task);
792}
793
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400794static int nfs41_sequence_process(struct rpc_task *task,
795 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400796{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500797 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500798 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400799 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100800 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500801 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400802
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500803 if (slot == NULL)
804 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400805 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500806 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400807 goto out;
808
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500809 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400810 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500811
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400812 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100813
814 status = res->sr_status;
815 if (task->tk_status == -NFS4ERR_DEADSESSION)
816 status = -NFS4ERR_DEADSESSION;
817
Andy Adamson691daf32009-12-04 15:55:39 -0500818 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100819 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400820 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400821 /* Mark this sequence number as having been acked */
822 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400823 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400824 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500825 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500826 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400827 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
828 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500829 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400830 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500831 case 1:
832 /*
833 * sr_status remains 1 if an RPC level error occurred.
834 * The server may or may not have processed the sequence
835 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500836 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400837 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
838 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500839 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400840 case -NFS4ERR_DELAY:
841 /* The server detected a resend of the RPC call and
842 * returned NFS4ERR_DELAY as per Section 2.10.6.2
843 * of RFC5661.
844 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500845 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400846 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500847 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500848 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400849 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400850 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400851 case -NFS4ERR_RETRY_UNCACHED_REP:
852 case -NFS4ERR_SEQ_FALSE_RETRY:
853 /*
854 * The server thinks we tried to replay a request.
855 * Retry the call after bumping the sequence ID.
856 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400857 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400858 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500859 case -NFS4ERR_BADSLOT:
860 /*
861 * The slot id we used was probably retired. Try again
862 * using a different slot id.
863 */
Trond Myklebust99589102018-06-09 12:50:50 -0400864 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400865 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500866 goto retry_nowait;
867 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400868 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500869 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400870 * Were one or more calls using this slot interrupted?
871 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400872 * transmitted slot sequence number may be too high. However,
873 * if the server did receive the request then it might
874 * accidentally give us a reply with a mismatched operation.
875 * We can sort this out by sending a lone sequence operation
876 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500877 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400878 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
879 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400880 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
881 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
882 res->sr_slot = NULL;
883 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500884 goto retry_nowait;
885 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400886 /*
887 * RFC5661:
888 * A retry might be sent while the original request is
889 * still in progress on the replier. The replier SHOULD
890 * deal with the issue by returning NFS4ERR_DELAY as the
891 * reply to SEQUENCE or CB_SEQUENCE operation, but
892 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
893 *
894 * Restart the search after a delay.
895 */
896 slot->seq_nr = slot->seq_nr_highest_sent;
897 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100898 case -NFS4ERR_BADSESSION:
899 case -NFS4ERR_DEADSESSION:
900 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
901 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400902 default:
903 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400904 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400905 }
906out:
907 /* The session may be reset by one of the error handlers. */
908 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500909out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500910 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400911session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100912 nfs4_schedule_session_recovery(session, status);
913 dprintk("%s ERROR: %d Reset session\n", __func__, status);
914 nfs41_sequence_free_slot(res);
915 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400916retry_new_seq:
917 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500918retry_nowait:
919 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400920 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500921 task->tk_status = 0;
922 ret = 0;
923 }
924 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400925out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400926 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400927 goto out;
928 rpc_delay(task, NFS4_POLL_RETRY_MAX);
929 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400930}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400931
932int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
933{
934 if (!nfs41_sequence_process(task, res))
935 return 0;
936 if (res->sr_slot != NULL)
937 nfs41_sequence_free_slot(res);
938 return 1;
939
940}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500941EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400942
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400943static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
944{
945 if (res->sr_slot == NULL)
946 return 1;
947 if (res->sr_slot->table->session != NULL)
948 return nfs41_sequence_process(task, res);
949 return nfs40_sequence_done(task, res);
950}
951
952static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
953{
954 if (res->sr_slot != NULL) {
955 if (res->sr_slot->table->session != NULL)
956 nfs41_sequence_free_slot(res);
957 else
958 nfs40_sequence_free_slot(res);
959 }
960}
961
Peng Tao2c4b1312014-06-11 05:24:16 +0800962int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400963{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500964 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400965 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400966 if (!res->sr_slot->table->session)
967 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400968 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400969}
Peng Tao2c4b1312014-06-11 05:24:16 +0800970EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400971
Andy Adamsonce5039c2009-04-01 09:22:13 -0400972static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
973{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400974 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400975
Trond Myklebust035168ab2010-06-16 09:52:26 -0400976 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
977
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500978 nfs4_setup_sequence(data->seq_server->nfs_client,
979 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400980}
981
Andy Adamson69ab40c2009-04-01 09:22:19 -0400982static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
983{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400984 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400985
Trond Myklebust14516c32010-07-31 14:29:06 -0400986 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400987}
988
Trond Myklebust17280172012-03-11 13:11:00 -0400989static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400990 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400991 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400992};
993
Chuck Lever3bd23842013-08-09 12:49:19 -0400994#else /* !CONFIG_NFS_V4_1 */
995
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400996static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
997{
998 return nfs40_sequence_done(task, res);
999}
1000
1001static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1002{
1003 if (res->sr_slot != NULL)
1004 nfs40_sequence_free_slot(res);
1005}
1006
Peng Tao2c4b1312014-06-11 05:24:16 +08001007int nfs4_sequence_done(struct rpc_task *task,
1008 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001009{
Chuck Lever3bd23842013-08-09 12:49:19 -04001010 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001011}
Peng Tao2c4b1312014-06-11 05:24:16 +08001012EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001013
1014#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001015
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001016static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1017{
1018 res->sr_timestamp = jiffies;
1019 res->sr_status_flags = 0;
1020 res->sr_status = 1;
1021}
1022
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001023static
1024void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1025 struct nfs4_sequence_res *res,
1026 struct nfs4_slot *slot)
1027{
1028 if (!slot)
1029 return;
1030 slot->privileged = args->sa_privileged ? 1 : 0;
1031 args->sa_slot = slot;
1032
1033 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001034}
1035
1036int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001037 struct nfs4_sequence_args *args,
1038 struct nfs4_sequence_res *res,
1039 struct rpc_task *task)
1040{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001041 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001042 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001043 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001044
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001045 /* slot already allocated? */
1046 if (res->sr_slot != NULL)
1047 goto out_start;
1048
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001049 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001050 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001051
Trond Myklebust3453d572018-06-20 17:53:34 -04001052 spin_lock(&tbl->slot_tbl_lock);
1053 /* The state manager will wait until the slot table is empty */
1054 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1055 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001056
Trond Myklebust3453d572018-06-20 17:53:34 -04001057 slot = nfs4_alloc_slot(tbl);
1058 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001059 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001060 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001061 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001062 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001063 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001064
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001065 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001066
Anna Schumakerad05cc02017-01-11 13:37:06 -05001067 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001068out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001069 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001070 rpc_call_start(task);
1071 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001072out_sleep_timeout:
1073 /* Try again in 1/4 second */
1074 if (args->sa_privileged)
1075 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1076 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1077 else
1078 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1079 NULL, jiffies + (HZ >> 2));
1080 spin_unlock(&tbl->slot_tbl_lock);
1081 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001082out_sleep:
1083 if (args->sa_privileged)
1084 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001085 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001086 else
1087 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1088 spin_unlock(&tbl->slot_tbl_lock);
1089 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001090}
1091EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1092
Chuck Lever9915ea72013-08-09 12:48:27 -04001093static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1094{
1095 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001096 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001097 data->seq_args, data->seq_res, task);
1098}
1099
1100static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1101{
1102 struct nfs4_call_sync_data *data = calldata;
1103 nfs4_sequence_done(task, data->seq_res);
1104}
1105
1106static const struct rpc_call_ops nfs40_call_sync_ops = {
1107 .rpc_call_prepare = nfs40_call_sync_prepare,
1108 .rpc_call_done = nfs40_call_sync_done,
1109};
1110
Anna Schumaker48c05852019-08-14 15:27:00 -04001111static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1112{
1113 int ret;
1114 struct rpc_task *task;
1115
1116 task = rpc_run_task(task_setup);
1117 if (IS_ERR(task))
1118 return PTR_ERR(task);
1119
1120 ret = task->tk_status;
1121 rpc_put_task(task);
1122 return ret;
1123}
1124
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001125static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1126 struct nfs_server *server,
1127 struct rpc_message *msg,
1128 struct nfs4_sequence_args *args,
1129 struct nfs4_sequence_res *res,
1130 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001131{
Chuck Lever9915ea72013-08-09 12:48:27 -04001132 struct nfs_client *clp = server->nfs_client;
1133 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001134 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001135 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001136 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001137 };
1138 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001139 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001140 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001141 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001142 .callback_data = &data,
1143 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001144 };
1145
Anna Schumaker48c05852019-08-14 15:27:00 -04001146 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001147}
1148
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001149static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1150 struct nfs_server *server,
1151 struct rpc_message *msg,
1152 struct nfs4_sequence_args *args,
1153 struct nfs4_sequence_res *res)
1154{
1155 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1156}
1157
1158
Bryan Schumaker7c513052011-03-24 17:12:24 +00001159int nfs4_call_sync(struct rpc_clnt *clnt,
1160 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001161 struct rpc_message *msg,
1162 struct nfs4_sequence_args *args,
1163 struct nfs4_sequence_res *res,
1164 int cache_reply)
1165{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001166 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001167 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001168}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Trond Myklebust3c591172018-07-31 15:54:10 -04001170static void
1171nfs4_inc_nlink_locked(struct inode *inode)
1172{
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001173 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Trond Myklebust3c591172018-07-31 15:54:10 -04001174 inc_nlink(inode);
1175}
1176
1177static void
1178nfs4_dec_nlink_locked(struct inode *inode)
1179{
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001180 nfs_set_cache_invalid(inode, NFS_INO_INVALID_OTHER);
Trond Myklebust3c591172018-07-31 15:54:10 -04001181 drop_nlink(inode);
1182}
1183
1184static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001185nfs4_update_changeattr_locked(struct inode *inode,
1186 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001187 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001189 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001191 cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001192
1193 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001194 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1195 nfsi->attrtimeo_timestamp = jiffies;
1196 } else {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001197 if (S_ISDIR(inode->i_mode)) {
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001198 cache_validity |= NFS_INO_INVALID_DATA;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001199 nfs_force_lookup_revalidate(inode);
1200 } else {
1201 if (!NFS_PROTO(inode)->have_delegation(inode,
1202 FMODE_READ))
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001203 cache_validity |= NFS_INO_REVAL_PAGECACHE;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001204 }
1205
1206 if (cinfo->before != inode_peek_iversion_raw(inode))
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001207 cache_validity |= NFS_INO_INVALID_ACCESS |
1208 NFS_INO_INVALID_ACL |
1209 NFS_INO_INVALID_XATTR;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001210 }
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001211 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001212 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001213 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustb6f80a22021-03-08 14:42:55 -05001214 nfs_set_cache_invalid(inode, cache_validity);
Trond Myklebustc8d07152018-07-31 15:54:12 -04001215 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Trond Myklebust3c591172018-07-31 15:54:10 -04001216}
1217
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001218void
1219nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001220 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001221{
1222 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001223 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 spin_unlock(&dir->i_lock);
1225}
1226
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001227struct nfs4_open_createattrs {
1228 struct nfs4_label *label;
1229 struct iattr *sattr;
1230 const __u32 verf[2];
1231};
1232
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001233static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1234 int err, struct nfs4_exception *exception)
1235{
1236 if (err != -EINVAL)
1237 return false;
1238 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1239 return false;
1240 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1241 exception->retry = 1;
1242 return true;
1243}
1244
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001245static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1246{
1247 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1248}
1249
1250static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1251{
1252 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1253
1254 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1255}
1256
Trond Myklebust6ae37332015-01-30 14:21:14 -05001257static u32
1258nfs4_map_atomic_open_share(struct nfs_server *server,
1259 fmode_t fmode, int openflags)
1260{
1261 u32 res = 0;
1262
1263 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1264 case FMODE_READ:
1265 res = NFS4_SHARE_ACCESS_READ;
1266 break;
1267 case FMODE_WRITE:
1268 res = NFS4_SHARE_ACCESS_WRITE;
1269 break;
1270 case FMODE_READ|FMODE_WRITE:
1271 res = NFS4_SHARE_ACCESS_BOTH;
1272 }
1273 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1274 goto out;
1275 /* Want no delegation if we're using O_DIRECT */
1276 if (openflags & O_DIRECT)
1277 res |= NFS4_SHARE_WANT_NO_DELEG;
1278out:
1279 return res;
1280}
1281
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001282static enum open_claim_type4
1283nfs4_map_atomic_open_claim(struct nfs_server *server,
1284 enum open_claim_type4 claim)
1285{
1286 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1287 return claim;
1288 switch (claim) {
1289 default:
1290 return claim;
1291 case NFS4_OPEN_CLAIM_FH:
1292 return NFS4_OPEN_CLAIM_NULL;
1293 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1294 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1295 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1296 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1297 }
1298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301{
1302 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001303 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001304 p->o_res.seqid = p->o_arg.seqid;
1305 p->c_res.seqid = p->c_arg.seqid;
1306 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001307 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001308 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001309 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001310}
1311
Al Viro82a2c1b2011-06-22 18:30:55 -04001312static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001313 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001314 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001315 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001316 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001317{
Al Viro82a2c1b2011-06-22 18:30:55 -04001318 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001319 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001320 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001321 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001322 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001323 struct nfs4_opendata *p;
1324
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001325 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001326 if (p == NULL)
1327 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001328
1329 p->f_label = nfs4_label_alloc(server, gfp_mask);
1330 if (IS_ERR(p->f_label))
1331 goto err_free_p;
1332
Kinglong Meea49c2692015-07-27 15:31:38 +08001333 p->a_label = nfs4_label_alloc(server, gfp_mask);
1334 if (IS_ERR(p->a_label))
1335 goto err_free_f;
1336
Trond Myklebust63f5f792015-01-23 19:19:25 -05001337 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1338 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001339 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001340 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001341 nfs_sb_active(dentry->d_sb);
1342 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001343 p->dir = parent;
1344 p->owner = sp;
1345 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346 p->o_arg.open_flags = flags;
1347 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001348 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001349 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1350 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001351 if (flags & O_CREAT) {
1352 p->o_arg.umask = current_umask();
1353 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1354 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1355 p->o_arg.u.attrs = &p->attrs;
1356 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1357
1358 memcpy(p->o_arg.u.verifier.data, c->verf,
1359 sizeof(p->o_arg.u.verifier.data));
1360 }
1361 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001362 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1363 * will return permission denied for all bits until close */
1364 if (!(flags & O_EXCL)) {
1365 /* ask server to check for all possible rights as results
1366 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001367 switch (p->o_arg.claim) {
1368 default:
1369 break;
1370 case NFS4_OPEN_CLAIM_NULL:
1371 case NFS4_OPEN_CLAIM_FH:
1372 p->o_arg.access = NFS4_ACCESS_READ |
1373 NFS4_ACCESS_MODIFY |
1374 NFS4_ACCESS_EXTEND |
1375 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001376#ifdef CONFIG_NFS_V4_2
1377 if (server->caps & NFS_CAP_XATTR)
1378 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1379 NFS4_ACCESS_XAWRITE |
1380 NFS4_ACCESS_XALIST;
1381#endif
Trond Myklebust536585c2016-11-10 15:40:34 -05001382 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001383 }
David Howells7539bba2006-08-22 20:06:09 -04001384 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001385 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1386 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001387 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001388 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001389 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001390 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001391 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001392 case NFS4_OPEN_CLAIM_NULL:
1393 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1394 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1395 p->o_arg.fh = NFS_FH(dir);
1396 break;
1397 case NFS4_OPEN_CLAIM_PREVIOUS:
1398 case NFS4_OPEN_CLAIM_FH:
1399 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1400 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001401 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001402 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001403 p->c_arg.fh = &p->o_res.fh;
1404 p->c_arg.stateid = &p->o_res.stateid;
1405 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001406 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001407 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001408 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001409
1410err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001411 nfs4_label_free(p->a_label);
1412err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001413 nfs4_label_free(p->f_label);
1414err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001415 kfree(p);
1416err:
1417 dput(parent);
1418 return NULL;
1419}
1420
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001421static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001422{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001423 struct nfs4_opendata *p = container_of(kref,
1424 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001425 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001426
Fred Isaman30ae2412016-10-18 13:39:51 -04001427 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001428 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001429 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001430 if (p->state != NULL)
1431 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001432 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001433
Kinglong Meea49c2692015-07-27 15:31:38 +08001434 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001435 nfs4_label_free(p->f_label);
1436
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001437 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001438 dput(p->dentry);
1439 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001440 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001441 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001442 kfree(p);
1443}
1444
1445static void nfs4_opendata_put(struct nfs4_opendata *p)
1446{
1447 if (p != NULL)
1448 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001449}
1450
Trond Myklebust24311f82015-09-20 10:50:17 -04001451static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1452 fmode_t fmode)
1453{
1454 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1455 case FMODE_READ|FMODE_WRITE:
1456 return state->n_rdwr != 0;
1457 case FMODE_WRITE:
1458 return state->n_wronly != 0;
1459 case FMODE_READ:
1460 return state->n_rdonly != 0;
1461 }
1462 WARN_ON_ONCE(1);
1463 return false;
1464}
1465
Trond Myklebustbe189f72018-09-27 17:12:33 -04001466static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1467 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001468{
1469 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001470
Trond Myklebust536e43d2012-01-17 22:04:26 -05001471 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001472 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001473 switch (claim) {
1474 case NFS4_OPEN_CLAIM_NULL:
1475 case NFS4_OPEN_CLAIM_FH:
1476 goto out;
1477 default:
1478 break;
1479 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001480 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001481 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001482 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1483 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001484 break;
1485 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001486 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1487 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001488 break;
1489 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001490 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1491 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001492 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001493out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001494 return ret;
1495}
1496
Trond Myklebust2a606182015-08-19 22:30:00 -05001497static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1498 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001499{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001500 if (delegation == NULL)
1501 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001502 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001503 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001504 switch (claim) {
1505 case NFS4_OPEN_CLAIM_NULL:
1506 case NFS4_OPEN_CLAIM_FH:
1507 break;
1508 case NFS4_OPEN_CLAIM_PREVIOUS:
1509 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1510 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001511 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001512 default:
1513 return 0;
1514 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001515 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001516 return 1;
1517}
1518
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001519static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001520{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001521 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001522 case FMODE_WRITE:
1523 state->n_wronly++;
1524 break;
1525 case FMODE_READ:
1526 state->n_rdonly++;
1527 break;
1528 case FMODE_READ|FMODE_WRITE:
1529 state->n_rdwr++;
1530 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001531 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001532}
1533
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001534#ifdef CONFIG_NFS_V4_1
1535static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1536{
1537 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1538 return true;
1539 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1540 return true;
1541 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1542 return true;
1543 return false;
1544}
1545#endif /* CONFIG_NFS_V4_1 */
1546
Trond Myklebustc9399f22017-11-06 15:28:01 -05001547static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1548{
1549 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1550 wake_up_all(&state->waitq);
1551}
1552
Trond Myklebust4f14c192014-02-12 19:15:06 -05001553static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001554{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001555 struct nfs_client *clp = state->owner->so_server->nfs_client;
1556 bool need_recover = false;
1557
1558 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1559 need_recover = true;
1560 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1561 need_recover = true;
1562 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1563 need_recover = true;
1564 if (need_recover)
1565 nfs4_state_mark_reclaim_nograce(clp, state);
1566}
1567
Trond Myklebustc9399f22017-11-06 15:28:01 -05001568/*
1569 * Check for whether or not the caller may update the open stateid
1570 * to the value passed in by stateid.
1571 *
1572 * Note: This function relies heavily on the server implementing
1573 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1574 * correctly.
1575 * i.e. The stateid seqids have to be initialised to 1, and
1576 * are then incremented on every state transition.
1577 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001578static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001579 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001580{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001581 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1582 /* The common case - we're updating to a new sequence number */
1583 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1584 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1585 return true;
1586 }
1587 } else {
1588 /* This is the first OPEN in this generation */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001589 if (stateid->seqid == cpu_to_be32(1))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001590 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001591 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001592 return false;
1593}
1594
Trond Myklebustf95549c2015-01-23 18:06:09 -05001595static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1596{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001597 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1598 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001599 if (state->n_wronly)
1600 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1601 if (state->n_rdonly)
1602 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1603 if (state->n_rdwr)
1604 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001605 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001606}
1607
Trond Myklebust226056c2014-02-11 10:41:07 -05001608static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1609 nfs4_stateid *stateid, fmode_t fmode)
1610{
1611 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1612 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1613 case FMODE_WRITE:
1614 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1615 break;
1616 case FMODE_READ:
1617 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1618 break;
1619 case 0:
1620 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1621 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1622 clear_bit(NFS_OPEN_STATE, &state->flags);
1623 }
1624 if (stateid == NULL)
1625 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001626 /* Handle OPEN+OPEN_DOWNGRADE races */
1627 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1628 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001629 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001630 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001631 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001632 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001633 nfs4_stateid_copy(&state->stateid, stateid);
1634 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001635 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001636out:
1637 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001638}
1639
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001640static void nfs_clear_open_stateid(struct nfs4_state *state,
1641 nfs4_stateid *arg_stateid,
1642 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001643{
1644 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001645 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1646 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1647 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001648 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001649 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1650 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001651}
1652
Trond Myklebust1393d962016-09-22 13:39:13 -04001653static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001654 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001655 __must_hold(&state->owner->so_lock)
1656 __must_hold(&state->seqlock)
1657 __must_hold(RCU)
1658
Trond Myklebust003707c2007-07-05 18:07:55 -04001659{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001660 DEFINE_WAIT(wait);
1661 int status = 0;
1662 for (;;) {
1663
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001664 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001665 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001666
Trond Myklebustc9399f22017-11-06 15:28:01 -05001667 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001668 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001669 /* Rely on seqids for serialisation with NFSv4.0 */
1670 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1671 break;
1672
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001673 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001674 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1675 /*
1676 * Ensure we process the state changes in the same order
1677 * in which the server processed them by delaying the
1678 * update of the stateid until we are in sequence.
1679 */
1680 write_sequnlock(&state->seqlock);
1681 spin_unlock(&state->owner->so_lock);
1682 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001683 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001684
Trond Myklebustc9399f22017-11-06 15:28:01 -05001685 if (!signal_pending(current)) {
1686 if (schedule_timeout(5*HZ) == 0)
1687 status = -EAGAIN;
1688 else
1689 status = 0;
1690 } else
1691 status = -EINTR;
1692 finish_wait(&state->waitq, &wait);
1693 rcu_read_lock();
1694 spin_lock(&state->owner->so_lock);
1695 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001696 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001697
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001698 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1699 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001700 nfs4_stateid_copy(freeme, &state->open_stateid);
1701 nfs_test_and_clear_all_open_stateid(state);
1702 }
1703
Trond Myklebuste999e802014-02-10 18:20:47 -05001704 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1705 nfs4_stateid_copy(&state->stateid, stateid);
1706 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001707 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001708 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001709}
1710
Trond Myklebustc9399f22017-11-06 15:28:01 -05001711static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001712 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001713 fmode_t fmode,
1714 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001716 /*
1717 * Protect the call to nfs4_state_set_mode_locked and
1718 * serialise the stateid update
1719 */
1720 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001721 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1722 switch (fmode) {
1723 case FMODE_READ:
1724 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1725 break;
1726 case FMODE_WRITE:
1727 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1728 break;
1729 case FMODE_READ|FMODE_WRITE:
1730 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001731 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001732 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001733 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001734}
1735
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001736static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1737{
1738 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1739 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1740 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1741 clear_bit(NFS_OPEN_STATE, &state->flags);
1742}
1743
Trond Myklebustc9399f22017-11-06 15:28:01 -05001744static void nfs_state_set_delegation(struct nfs4_state *state,
1745 const nfs4_stateid *deleg_stateid,
1746 fmode_t fmode)
1747{
1748 /*
1749 * Protect the call to nfs4_state_set_mode_locked and
1750 * serialise the stateid update
1751 */
1752 write_seqlock(&state->seqlock);
1753 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1754 set_bit(NFS_DELEGATED_STATE, &state->flags);
1755 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001758static void nfs_state_clear_delegation(struct nfs4_state *state)
1759{
1760 write_seqlock(&state->seqlock);
1761 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1762 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1763 write_sequnlock(&state->seqlock);
1764}
1765
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001766int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001767 const nfs4_stateid *open_stateid,
1768 const nfs4_stateid *delegation,
1769 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001770{
Trond Myklebust1393d962016-09-22 13:39:13 -04001771 struct nfs_server *server = NFS_SERVER(state->inode);
1772 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001773 struct nfs_inode *nfsi = NFS_I(state->inode);
1774 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001775 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001776 int ret = 0;
1777
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001778 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001779
1780 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001781 spin_lock(&state->owner->so_lock);
1782 if (open_stateid != NULL) {
1783 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1784 ret = 1;
1785 }
1786
Trond Myklebust333ac782019-10-22 12:12:17 -04001787 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001788 if (deleg_cur == NULL)
1789 goto no_delegation;
1790
1791 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001792 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001793 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001794 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001795 goto no_delegation_unlock;
1796
1797 if (delegation == NULL)
1798 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001799 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001800 goto no_delegation_unlock;
1801
Trond Myklebustb7391f42008-12-23 15:21:52 -05001802 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001803 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001804 ret = 1;
1805no_delegation_unlock:
1806 spin_unlock(&deleg_cur->lock);
1807no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001808 if (ret)
1809 update_open_stateflags(state, fmode);
1810 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001811 rcu_read_unlock();
1812
Trond Myklebust4f14c192014-02-12 19:15:06 -05001813 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001814 nfs4_schedule_state_manager(clp);
1815 if (freeme.type != 0)
1816 nfs4_test_and_free_stateid(server, &freeme,
1817 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001818
1819 return ret;
1820}
1821
Trond Myklebust39071e62015-01-24 15:07:56 -05001822static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1823 const nfs4_stateid *stateid)
1824{
1825 struct nfs4_state *state = lsp->ls_state;
1826 bool ret = false;
1827
1828 spin_lock(&state->state_lock);
1829 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1830 goto out_noupdate;
1831 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1832 goto out_noupdate;
1833 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1834 ret = true;
1835out_noupdate:
1836 spin_unlock(&state->state_lock);
1837 return ret;
1838}
Trond Myklebust34310432008-12-23 15:21:38 -05001839
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001840static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001841{
1842 struct nfs_delegation *delegation;
1843
Trond Myklebustf5086242018-03-20 16:43:13 -04001844 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001845 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001846 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001847 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001848 rcu_read_unlock();
1849 return;
1850 }
1851 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001852 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001853}
1854
Trond Myklebust6ee41262007-07-08 14:11:36 -04001855static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856{
1857 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001858 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001859 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001860 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001861 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001862 nfs4_stateid stateid;
1863 int ret = -EAGAIN;
1864
Trond Myklebustaac00a82007-07-05 19:02:21 -04001865 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001866 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001867 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001868 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001869 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001870 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001871 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001872 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001873 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001874 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001875 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001876 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001877 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001878 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001879 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001880 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001881 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001882 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001883 if (!opendata->is_recover) {
1884 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1885 if (ret != 0)
1886 goto out;
1887 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001888 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001889
1890 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001891 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001892 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001893 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001894out:
1895 return ERR_PTR(ret);
1896out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001897 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001898 return state;
1899}
1900
Andy Adamsone23008e2012-10-02 21:07:32 -04001901static void
1902nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1903{
1904 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1905 struct nfs_delegation *delegation;
1906 int delegation_flags = 0;
1907
1908 rcu_read_lock();
1909 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1910 if (delegation)
1911 delegation_flags = delegation->flags;
1912 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001913 switch (data->o_arg.claim) {
1914 default:
1915 break;
1916 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1917 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001918 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1919 "returning a delegation for "
1920 "OPEN(CLAIM_DELEGATE_CUR)\n",
1921 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001922 return;
1923 }
1924 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001925 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001926 data->owner->so_cred,
1927 data->o_res.delegation_type,
1928 &data->o_res.delegation,
1929 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001930 else
1931 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001932 data->owner->so_cred,
1933 data->o_res.delegation_type,
1934 &data->o_res.delegation,
1935 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001936
1937 if (data->o_res.do_recall)
1938 nfs_async_inode_return_delegation(state->inode,
1939 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001940}
1941
1942/*
1943 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1944 * and update the nfs4_state.
1945 */
1946static struct nfs4_state *
1947_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1948{
1949 struct inode *inode = data->state->inode;
1950 struct nfs4_state *state = data->state;
1951 int ret;
1952
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001953 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001954 if (data->rpc_status)
1955 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001956 /* cached opens have already been processed */
1957 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001958 }
1959
Andy Adamsone23008e2012-10-02 21:07:32 -04001960 ret = nfs_refresh_inode(inode, &data->f_attr);
1961 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001962 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001963
1964 if (data->o_res.delegation_type != 0)
1965 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001966update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001967 if (!update_open_stateid(state, &data->o_res.stateid,
1968 NULL, data->o_arg.fmode))
1969 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001970 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001971
1972 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001973}
1974
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001975static struct inode *
1976nfs4_opendata_get_inode(struct nfs4_opendata *data)
1977{
1978 struct inode *inode;
1979
1980 switch (data->o_arg.claim) {
1981 case NFS4_OPEN_CLAIM_NULL:
1982 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1983 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1984 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1985 return ERR_PTR(-EAGAIN);
1986 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1987 &data->f_attr, data->f_label);
1988 break;
1989 default:
1990 inode = d_inode(data->dentry);
1991 ihold(inode);
1992 nfs_refresh_inode(inode, &data->f_attr);
1993 }
1994 return inode;
1995}
1996
Andy Adamsone23008e2012-10-02 21:07:32 -04001997static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001998nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1999{
2000 struct nfs4_state *state;
2001 struct inode *inode;
2002
2003 inode = nfs4_opendata_get_inode(data);
2004 if (IS_ERR(inode))
2005 return ERR_CAST(inode);
2006 if (data->state != NULL && data->state->inode == inode) {
2007 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002008 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002009 } else
2010 state = nfs4_get_open_state(inode, data->owner);
2011 iput(inode);
2012 if (state == NULL)
2013 state = ERR_PTR(-ENOMEM);
2014 return state;
2015}
2016
Andy Adamsone23008e2012-10-02 21:07:32 -04002017static struct nfs4_state *
2018_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002019{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002020 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002021
Trond Myklebustaac00a82007-07-05 19:02:21 -04002022 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002023 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002024 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002025 goto out;
2026 }
2027
Trond Myklebust75e8c482017-08-08 10:38:07 -04002028 state = nfs4_opendata_find_nfs4_state(data);
2029 if (IS_ERR(state))
2030 goto out;
2031
Andy Adamsone23008e2012-10-02 21:07:32 -04002032 if (data->o_res.delegation_type != 0)
2033 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002034 if (!update_open_stateid(state, &data->o_res.stateid,
2035 NULL, data->o_arg.fmode)) {
2036 nfs4_put_open_state(state);
2037 state = ERR_PTR(-EAGAIN);
2038 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002039out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002040 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002041 return state;
2042}
2043
Andy Adamsone23008e2012-10-02 21:07:32 -04002044static struct nfs4_state *
2045nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2046{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002047 struct nfs4_state *ret;
2048
Andy Adamsone23008e2012-10-02 21:07:32 -04002049 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002050 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2051 else
2052 ret = _nfs4_opendata_to_nfs4_state(data);
2053 nfs4_sequence_free_slot(&data->o_res.seq_res);
2054 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002055}
2056
Trond Myklebust0de43972018-09-02 15:57:01 -04002057static struct nfs_open_context *
2058nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002059{
2060 struct nfs_inode *nfsi = NFS_I(state->inode);
2061 struct nfs_open_context *ctx;
2062
Trond Myklebust0de43972018-09-02 15:57:01 -04002063 rcu_read_lock();
2064 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002065 if (ctx->state != state)
2066 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002067 if ((ctx->mode & mode) != mode)
2068 continue;
2069 if (!get_nfs_open_context(ctx))
2070 continue;
2071 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002072 return ctx;
2073 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002074 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002075 return ERR_PTR(-ENOENT);
2076}
2077
Trond Myklebust0de43972018-09-02 15:57:01 -04002078static struct nfs_open_context *
2079nfs4_state_find_open_context(struct nfs4_state *state)
2080{
2081 struct nfs_open_context *ctx;
2082
2083 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2084 if (!IS_ERR(ctx))
2085 return ctx;
2086 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2087 if (!IS_ERR(ctx))
2088 return ctx;
2089 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2090}
2091
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002092static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2093 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002094{
2095 struct nfs4_opendata *opendata;
2096
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002097 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002098 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002099 if (opendata == NULL)
2100 return ERR_PTR(-ENOMEM);
2101 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002102 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002103 return opendata;
2104}
2105
Trond Myklebust24311f82015-09-20 10:50:17 -04002106static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2107 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002108{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002109 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002110 int ret;
2111
Trond Myklebust24311f82015-09-20 10:50:17 -04002112 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002113 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002114 opendata->o_arg.open_flags = 0;
2115 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002116 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2117 NFS_SB(opendata->dentry->d_sb),
2118 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002119 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2120 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2121 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002122 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002123 if (ret != 0)
2124 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002125 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002126 if (IS_ERR(newstate))
2127 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002128 if (newstate != opendata->state)
2129 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002130 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002131 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002132}
2133
2134static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2135{
Trond Myklebust864472e2006-01-03 09:55:15 +01002136 int ret;
2137
2138 /* memory barrier prior to reading state->n_* */
2139 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002140 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2141 if (ret != 0)
2142 return ret;
2143 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2144 if (ret != 0)
2145 return ret;
2146 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2147 if (ret != 0)
2148 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002149 /*
2150 * We may have performed cached opens for all three recoveries.
2151 * Check if we need to update the current stateid.
2152 */
2153 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002154 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002155 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002156 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002157 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002158 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002159 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002160 return 0;
2161}
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163/*
2164 * OPEN_RECLAIM:
2165 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002167static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002169 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002170 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002171 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 int status;
2173
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002174 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2175 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002176 if (IS_ERR(opendata))
2177 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002178 rcu_read_lock();
2179 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002180 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002181 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002182 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002183 opendata->o_arg.u.delegation_type = delegation_type;
2184 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002185 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return status;
2187}
2188
Trond Myklebust539cd032007-06-05 11:46:42 -04002189static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 struct nfs_server *server = NFS_SERVER(state->inode);
2192 struct nfs4_exception exception = { };
2193 int err;
2194 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002195 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002196 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002197 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2198 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002199 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002200 break;
2201 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 } while (exception.retry);
2203 return err;
2204}
2205
Trond Myklebust864472e2006-01-03 09:55:15 +01002206static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2207{
2208 struct nfs_open_context *ctx;
2209 int ret;
2210
2211 ctx = nfs4_state_find_open_context(state);
2212 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002213 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002214 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2215 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002216 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002217 put_nfs_open_context(ctx);
2218 return ret;
2219}
2220
NeilBrowndce26302017-12-13 09:57:09 +11002221static 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 -07002222{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002223 switch (err) {
2224 default:
2225 printk(KERN_ERR "NFS: %s: unhandled error "
2226 "%d.\n", __func__, err);
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06002227 fallthrough;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002228 case 0:
2229 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002230 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002231 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002232 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002233 break;
2234 case -NFS4ERR_BADSESSION:
2235 case -NFS4ERR_BADSLOT:
2236 case -NFS4ERR_BAD_HIGH_SLOT:
2237 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2238 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002239 return -EAGAIN;
2240 case -NFS4ERR_STALE_CLIENTID:
2241 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002242 /* Don't recall a delegation if it was lost */
2243 nfs4_schedule_lease_recovery(server->nfs_client);
2244 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002245 case -NFS4ERR_MOVED:
2246 nfs4_schedule_migration_recovery(server);
2247 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002248 case -NFS4ERR_LEASE_MOVED:
2249 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2250 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002251 case -NFS4ERR_DELEG_REVOKED:
2252 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002253 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002254 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002255 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002256 nfs_inode_find_state_and_recover(state->inode,
2257 stateid);
2258 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002259 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002260 case -NFS4ERR_DELAY:
2261 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002262 ssleep(1);
2263 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002264 case -ENOMEM:
2265 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002266 if (fl) {
2267 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2268 if (lsp)
2269 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2270 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002271 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return err;
2274}
2275
Trond Myklebust24311f82015-09-20 10:50:17 -04002276int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002277 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002278{
2279 struct nfs_server *server = NFS_SERVER(state->inode);
2280 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002281 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002282
2283 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2284 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2285 if (IS_ERR(opendata))
2286 return PTR_ERR(opendata);
2287 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002288 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002289 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2290 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002291 goto out;
2292 }
2293 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002294 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2295 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002296 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002297 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002298 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2299 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2300 if (err)
2301 goto out;
2302 }
2303 nfs_state_clear_delegation(state);
2304out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002305 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002306 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002307}
2308
Chuck Leverbe05c862013-08-09 12:49:47 -04002309static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2310{
2311 struct nfs4_opendata *data = calldata;
2312
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002313 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2314 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002315}
2316
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002317static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2318{
2319 struct nfs4_opendata *data = calldata;
2320
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002321 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002322
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002323 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002324 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002325 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002326 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002327 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002328 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002329 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002330}
2331
2332static void nfs4_open_confirm_release(void *calldata)
2333{
2334 struct nfs4_opendata *data = calldata;
2335 struct nfs4_state *state = NULL;
2336
2337 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002338 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002339 goto out_free;
2340 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002341 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002342 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002343 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002344 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002345 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002346out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002347 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002348}
2349
2350static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002351 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002352 .rpc_call_done = nfs4_open_confirm_done,
2353 .rpc_release = nfs4_open_confirm_release,
2354};
2355
2356/*
2357 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2358 */
2359static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2360{
David Howells2b0143b2015-03-17 22:25:59 +00002361 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002362 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002363 struct rpc_message msg = {
2364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2365 .rpc_argp = &data->c_arg,
2366 .rpc_resp = &data->c_res,
2367 .rpc_cred = data->owner->so_cred,
2368 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002369 struct rpc_task_setup task_setup_data = {
2370 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002371 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002372 .callback_ops = &nfs4_open_confirm_ops,
2373 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002374 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002375 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002376 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 int status;
2378
Anna Schumakerfba83f32018-05-04 16:22:50 -04002379 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2380 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002381 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002382 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002383 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002384 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002385 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002386 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002387 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002388 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002389 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002390 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002391 smp_wmb();
2392 } else
2393 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002394 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return status;
2396}
2397
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002398static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002400 struct nfs4_opendata *data = calldata;
2401 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002402 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002403 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002404
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002405 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002406 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002407 /*
2408 * Check if we still need to send an OPEN call, or if we can use
2409 * a delegation instead.
2410 */
2411 if (data->state != NULL) {
2412 struct nfs_delegation *delegation;
2413
Trond Myklebustbe189f72018-09-27 17:12:33 -04002414 if (can_open_cached(data->state, data->o_arg.fmode,
2415 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002416 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002417 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002418 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002419 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002420 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002421 rcu_read_unlock();
2422 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002423 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002424 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002425 switch (claim) {
2426 default:
2427 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002428 case NFS4_OPEN_CLAIM_PREVIOUS:
2429 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2430 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002431 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002432 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002433 case NFS4_OPEN_CLAIM_FH:
2434 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002435 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002436 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002437 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002438 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002439 &data->o_res.seq_res,
2440 task) != 0)
2441 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002442
2443 /* Set the create mode (note dependency on the session type) */
2444 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2445 if (data->o_arg.open_flags & O_EXCL) {
2446 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2447 if (nfs4_has_persistent_session(clp))
2448 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2449 else if (clp->cl_mvops->minor_version > 0)
2450 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2451 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002452 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002453unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002454 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002455 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002456out_no_action:
2457 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002458out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002459 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002460}
2461
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002462static void nfs4_open_done(struct rpc_task *task, void *calldata)
2463{
2464 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002466 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002467
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002468 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002469 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002470
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002471 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002472 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2473 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002474 case S_IFREG:
2475 break;
2476 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002477 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002478 break;
2479 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002480 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002481 break;
2482 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002483 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002484 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002485 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002486 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002487 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2488 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002489 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002490 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002491}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002492
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002493static void nfs4_open_release(void *calldata)
2494{
2495 struct nfs4_opendata *data = calldata;
2496 struct nfs4_state *state = NULL;
2497
2498 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002499 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002500 goto out_free;
2501 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002502 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002503 goto out_free;
2504 /* In case we need an open_confirm, no cleanup! */
2505 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2506 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002507 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002508 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002509 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002510out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002511 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002512}
2513
2514static const struct rpc_call_ops nfs4_open_ops = {
2515 .rpc_call_prepare = nfs4_open_prepare,
2516 .rpc_call_done = nfs4_open_done,
2517 .rpc_release = nfs4_open_release,
2518};
2519
Fred Isaman3b65a302016-09-19 10:06:49 -04002520static int nfs4_run_open_task(struct nfs4_opendata *data,
2521 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002522{
David Howells2b0143b2015-03-17 22:25:59 +00002523 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002524 struct nfs_server *server = NFS_SERVER(dir);
2525 struct nfs_openargs *o_arg = &data->o_arg;
2526 struct nfs_openres *o_res = &data->o_res;
2527 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002528 struct rpc_message msg = {
2529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2530 .rpc_argp = o_arg,
2531 .rpc_resp = o_res,
2532 .rpc_cred = data->owner->so_cred,
2533 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002534 struct rpc_task_setup task_setup_data = {
2535 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002536 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002537 .callback_ops = &nfs4_open_ops,
2538 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002539 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002540 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002541 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002542 int status;
2543
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002544 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002545 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002546 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002547 data->cancelled = false;
2548 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002549 if (!ctx) {
2550 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002551 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002552 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002553 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002554 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002555 pnfs_lgopen_prepare(data, ctx);
2556 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002557 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002558 if (IS_ERR(task))
2559 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002560 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002561 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002562 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002563 smp_wmb();
2564 } else
2565 status = data->rpc_status;
2566 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002567
2568 return status;
2569}
2570
2571static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2572{
David Howells2b0143b2015-03-17 22:25:59 +00002573 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002574 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002575 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002576
Fred Isaman3b65a302016-09-19 10:06:49 -04002577 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002578 if (status != 0 || !data->rpc_done)
2579 return status;
2580
Trond Myklebust6926afd12012-01-07 13:22:46 -05002581 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2582
Anna Schumakerd7e98252017-01-11 16:13:29 -05002583 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002584 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002585
2586 return status;
2587}
2588
Trond Myklebustf3792d62014-07-10 08:54:32 -04002589/*
2590 * Additional permission checks in order to distinguish between an
2591 * open for read, and an open for execute. This works around the
2592 * fact that NFSv4 OPEN treats read and execute permissions as being
2593 * the same.
2594 * Note that in the non-execute case, we want to turn off permission
2595 * checking if we just created a new file (POSIX open() semantics).
2596 */
NeilBrowna52458b2018-12-03 11:30:31 +11002597static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002598 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002599 struct nfs4_state *state, fmode_t fmode,
2600 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002601{
2602 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002603 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002604
2605 /* access call failed or for some reason the server doesn't
2606 * support any access modes -- defer access call until later */
2607 if (opendata->o_res.access_supported == 0)
2608 return 0;
2609
2610 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002611 /*
2612 * Use openflags to check for exec, because fmode won't
2613 * always have FMODE_EXEC set when file open for exec.
2614 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002615 if (openflags & __FMODE_EXEC) {
2616 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002617 if (S_ISDIR(state->inode->i_mode))
2618 mask = NFS4_ACCESS_LOOKUP;
2619 else
2620 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002621 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002622 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002623
2624 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002625 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2626 nfs_access_add_cache(state->inode, &cache);
2627
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002628 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2629 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002630 return 0;
2631
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002632 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002633}
2634
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002635/*
2636 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2637 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002638static int _nfs4_proc_open(struct nfs4_opendata *data,
2639 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002640{
David Howells2b0143b2015-03-17 22:25:59 +00002641 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002642 struct nfs_server *server = NFS_SERVER(dir);
2643 struct nfs_openargs *o_arg = &data->o_arg;
2644 struct nfs_openres *o_res = &data->o_res;
2645 int status;
2646
Fred Isaman3b65a302016-09-19 10:06:49 -04002647 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002648 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002649 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002650 if (status != 0) {
2651 if (status == -NFS4ERR_BADNAME &&
2652 !(o_arg->open_flags & O_CREAT))
2653 return -ENOENT;
2654 return status;
2655 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002656
Trond Myklebust6926afd12012-01-07 13:22:46 -05002657 nfs_fattr_map_and_free_names(server, &data->f_attr);
2658
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002659 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002660 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002661 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002662 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002663 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002664 if (data->file_created ||
2665 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002666 nfs4_update_changeattr(dir, &o_res->cinfo,
2667 o_res->f_attr->time_start,
2668 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002669 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002670 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2671 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002673 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002675 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002677 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2678 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002679 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2680 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002681 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685/*
2686 * OPEN_EXPIRED:
2687 * reclaim state on the server after a network partition.
2688 * Assumes caller holds the appropriate lock
2689 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002690static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002692 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002693 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002695 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002696 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002697 if (IS_ERR(opendata))
2698 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002699 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002700 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002701 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002702 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002703 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
2705
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002706static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002707{
Trond Myklebust539cd032007-06-05 11:46:42 -04002708 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002709 struct nfs4_exception exception = { };
2710 int err;
2711
2712 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002713 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002714 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002715 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2716 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002717 switch (err) {
2718 default:
2719 goto out;
2720 case -NFS4ERR_GRACE:
2721 case -NFS4ERR_DELAY:
2722 nfs4_handle_exception(server, err, &exception);
2723 err = 0;
2724 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002725 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002726out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002727 return err;
2728}
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2731{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002733 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Trond Myklebust864472e2006-01-03 09:55:15 +01002735 ctx = nfs4_state_find_open_context(state);
2736 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002737 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002738 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002739 put_nfs_open_context(ctx);
2740 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
Trond Myklebust41020b62016-09-22 13:38:58 -04002743static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2744 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002745{
Trond Myklebust41020b62016-09-22 13:38:58 -04002746 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002747 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002748}
2749
2750static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2751{
2752 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002753 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002754}
2755
2756static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2757{
2758 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2759 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002760 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002761 return nfs4_open_expired(sp, state);
2762}
2763
Trond Myklebust45870d62016-09-22 13:38:59 -04002764static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2765 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002766 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002767{
2768 return -NFS4ERR_BAD_STATEID;
2769}
2770
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002771#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002772static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2773 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002774 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002775{
2776 int status;
2777
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002778 switch (stateid->type) {
2779 default:
2780 break;
2781 case NFS4_INVALID_STATEID_TYPE:
2782 case NFS4_SPECIAL_STATEID_TYPE:
2783 return -NFS4ERR_BAD_STATEID;
2784 case NFS4_REVOKED_STATEID_TYPE:
2785 goto out_free;
2786 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002787
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002788 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002789 switch (status) {
2790 case -NFS4ERR_EXPIRED:
2791 case -NFS4ERR_ADMIN_REVOKED:
2792 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002793 break;
2794 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002795 return status;
2796 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002797out_free:
2798 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002799 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002800 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002801}
2802
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002803static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002804{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002805 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002806 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002807 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002808 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002809 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002810
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002811 /* Get the delegation credential for use by test/free_stateid */
2812 rcu_read_lock();
2813 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002814 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002815 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002816 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002817 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002818 }
2819
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002820 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002821 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002822
Trond Myklebust994b15b2018-09-05 14:07:14 -04002823 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2824 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002825 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002826 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002827 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002828 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002829
NeilBrowna52458b2018-12-03 11:30:31 +11002830 if (delegation->cred)
2831 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002832 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002833 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002834 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002835 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002836 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002837 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002838 else
2839 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002840
Trond Myklebust8c39a392019-07-19 13:48:44 -04002841 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002842 return ret;
2843}
2844
2845static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2846{
2847 nfs4_stateid tmp;
2848
2849 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2850 nfs4_copy_delegation_stateid(state->inode, state->state,
2851 &tmp, NULL) &&
2852 nfs4_stateid_match_other(&state->stateid, &tmp))
2853 nfs_state_set_delegation(state, &tmp, state->state);
2854 else
2855 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002856}
2857
2858/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002859 * nfs41_check_expired_locks - possibly free a lock stateid
2860 *
2861 * @state: NFSv4 state for an inode
2862 *
2863 * Returns NFS_OK if recovery for this stateid is now finished.
2864 * Otherwise a negative NFS4ERR value is returned.
2865 */
2866static int nfs41_check_expired_locks(struct nfs4_state *state)
2867{
2868 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002869 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002870 struct nfs_server *server = NFS_SERVER(state->inode);
2871
2872 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2873 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002874
2875 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002876 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2877 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002878 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002879
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002880 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002881 spin_unlock(&state->state_lock);
2882
2883 nfs4_put_lock_state(prev);
2884 prev = lsp;
2885
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002886 status = nfs41_test_and_free_expired_stateid(server,
2887 &lsp->ls_stateid,
2888 cred);
2889 trace_nfs4_test_lock_stateid(state, lsp, status);
2890 if (status == -NFS4ERR_EXPIRED ||
2891 status == -NFS4ERR_BAD_STATEID) {
2892 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002893 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002894 if (!recover_lost_locks)
2895 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2896 } else if (status != NFS_OK) {
2897 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002898 nfs4_put_lock_state(prev);
2899 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002900 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002901 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002902 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002903 }
2904 spin_unlock(&state->state_lock);
2905 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002906out:
2907 return ret;
2908}
2909
2910/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002911 * nfs41_check_open_stateid - possibly free an open stateid
2912 *
2913 * @state: NFSv4 state for an inode
2914 *
2915 * Returns NFS_OK if recovery for this stateid is now finished.
2916 * Otherwise a negative NFS4ERR value is returned.
2917 */
2918static int nfs41_check_open_stateid(struct nfs4_state *state)
2919{
2920 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002921 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002922 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002923 int status;
2924
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002925 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002926 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002927 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002928 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002929 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002930 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002931 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002932 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002933 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002934 if (nfs_open_stateid_recover_openmode(state))
2935 return -NFS4ERR_OPENMODE;
2936 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002937}
2938
2939static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2940{
Chuck Levereb64cf92012-07-11 16:30:05 -04002941 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002942
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002943 status = nfs41_check_delegation_stateid(state);
2944 if (status != NFS_OK)
2945 return status;
2946 nfs41_delegation_recover_stateid(state);
2947
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002948 status = nfs41_check_expired_locks(state);
2949 if (status != NFS_OK)
2950 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002951 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002952 if (status != NFS_OK)
2953 status = nfs4_open_expired(sp, state);
2954 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002955}
2956#endif
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002959 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2960 * fields corresponding to attributes that were used to store the verifier.
2961 * Make sure we clobber those fields in the later setattr call
2962 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002963static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002964 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002965{
Trond Myklebust609339c2018-03-28 16:18:17 -04002966 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2967 __u32 attrset[3];
2968 unsigned ret;
2969 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002970
Trond Myklebust609339c2018-03-28 16:18:17 -04002971 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2972 attrset[i] = opendata->o_res.attrset[i];
2973 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2974 attrset[i] &= ~bitmask[i];
2975 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002976
Trond Myklebust609339c2018-03-28 16:18:17 -04002977 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2978 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002979
Trond Myklebust609339c2018-03-28 16:18:17 -04002980 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2981 if (sattr->ia_valid & ATTR_ATIME_SET)
2982 ret |= ATTR_ATIME_SET;
2983 else
2984 ret |= ATTR_ATIME;
2985 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002986
Trond Myklebust609339c2018-03-28 16:18:17 -04002987 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2988 if (sattr->ia_valid & ATTR_MTIME_SET)
2989 ret |= ATTR_MTIME_SET;
2990 else
2991 ret |= ATTR_MTIME;
2992 }
2993
2994 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002995 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002996 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002997}
2998
Trond Myklebustc21443c2013-02-07 14:26:21 -05002999static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003000 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003001{
3002 struct nfs4_state_owner *sp = opendata->owner;
3003 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003004 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003005 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003006 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003007 struct inode *dir = d_inode(opendata->dir);
3008 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003009 unsigned int seq;
3010 int ret;
3011
3012 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003013 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003014
Fred Isaman3b65a302016-09-19 10:06:49 -04003015 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003016 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003017 goto out;
3018
Trond Myklebustae55e592018-05-22 11:17:16 -04003019 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003020 ret = PTR_ERR(state);
3021 if (IS_ERR(state))
3022 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003023 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003024 if (server->caps & NFS_CAP_POSIX_LOCK)
3025 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003026 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3027 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003028
Trond Myklebust275bb302013-05-29 13:11:28 -04003029 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003030 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003031 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003032 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003033 alias = d_exact_alias(dentry, state->inode);
3034 if (!alias)
3035 alias = d_splice_alias(igrab(state->inode), dentry);
3036 /* d_splice_alias() can't fail here - it's a non-directory */
3037 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003038 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003039 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003040 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003041 }
3042
3043 switch(opendata->o_arg.claim) {
3044 default:
3045 break;
3046 case NFS4_OPEN_CLAIM_NULL:
3047 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3048 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3049 if (!opendata->rpc_done)
3050 break;
3051 if (opendata->o_res.delegation_type != 0)
3052 dir_verifier = nfs_save_change_attribute(dir);
3053 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003054 }
3055
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003056 /* Parse layoutget results before we check for access */
3057 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3058
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003059 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3060 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003061 if (ret != 0)
3062 goto out;
3063
David Howells2b0143b2015-03-17 22:25:59 +00003064 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003065 nfs_inode_attach_open_context(ctx);
3066 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3067 nfs4_schedule_stateid_recovery(server, state);
3068 }
Fred Isaman2409a972016-10-06 12:11:21 -04003069
Trond Myklebustc21443c2013-02-07 14:26:21 -05003070out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003071 if (!opendata->cancelled)
3072 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003073 return ret;
3074}
3075
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003076/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003077 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 */
Andy Adamson82be4172012-05-23 05:02:35 -04003079static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003080 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003081 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003082 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003083 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
3085 struct nfs4_state_owner *sp;
3086 struct nfs4_state *state = NULL;
3087 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003088 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003089 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003090 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003091 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003092 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003093 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003094 struct iattr *sattr = c->sattr;
3095 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003096 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003097 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
3099 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003101 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3102 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3104 goto out_err;
3105 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003106 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003107 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003108 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003109 if (d_really_is_positive(dentry))
3110 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003111 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003112 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003113 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003114 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3115 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003116 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003117 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
David Quigley14c43f72013-05-22 12:50:43 -04003119 if (label) {
3120 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3121 if (IS_ERR(olabel)) {
3122 status = PTR_ERR(olabel);
3123 goto err_opendata_put;
3124 }
3125 }
3126
Trond Myklebuste911b812014-03-26 13:24:37 -07003127 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3128 if (!opendata->f_attr.mdsthreshold) {
3129 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3130 if (!opendata->f_attr.mdsthreshold)
3131 goto err_free_label;
3132 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003133 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003134 }
David Howells2b0143b2015-03-17 22:25:59 +00003135 if (d_really_is_positive(dentry))
3136 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003137
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003138 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003139 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003140 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003141 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003142
NeilBrownefcbc042015-07-30 13:00:56 +10003143 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003144 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003145 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003146 /*
3147 * send create attributes which was not set by open
3148 * with an extra setattr.
3149 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003150 if (attrs || label) {
3151 unsigned ia_old = sattr->ia_valid;
3152
3153 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003154 nfs_fattr_init(opendata->o_res.f_attr);
3155 status = nfs4_do_setattr(state->inode, cred,
3156 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003157 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003158 if (status == 0) {
3159 nfs_setattr_update_inode(state->inode, sattr,
3160 opendata->o_res.f_attr);
3161 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3162 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003163 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003164 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003165 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003166 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003167 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003168
Trond Myklebuste911b812014-03-26 13:24:37 -07003169 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003170 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003171 opendata->f_attr.mdsthreshold = NULL;
3172 }
Andy Adamson82be4172012-05-23 05:02:35 -04003173
David Quigley14c43f72013-05-22 12:50:43 -04003174 nfs4_label_free(olabel);
3175
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003176 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003179err_free_label:
3180 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003181err_opendata_put:
3182 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003183err_put_state_owner:
3184 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 return status;
3187}
3188
3189
Andy Adamson82be4172012-05-23 05:02:35 -04003190static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003191 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003192 int flags,
3193 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003194 struct nfs4_label *label,
3195 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003197 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003198 struct nfs4_exception exception = {
3199 .interruptible = true,
3200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003202 struct nfs4_open_createattrs c = {
3203 .label = label,
3204 .sattr = sattr,
3205 .verf = {
3206 [0] = (__u32)jiffies,
3207 [1] = (__u32)current->pid,
3208 },
3209 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 int status;
3211
3212 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003213 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003214 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003215 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 if (status == 0)
3217 break;
3218 /* NOTE: BAD_SEQID means the server and client disagree about the
3219 * book-keeping w.r.t. state-changing operations
3220 * (OPEN/CLOSE/LOCK/LOCKU...)
3221 * It is actually a sign of a bug on the client or on the server.
3222 *
3223 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003224 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 * have unhashed the old state_owner for us, and that we can
3226 * therefore safely retry using a new one. We should still warn
3227 * the user though...
3228 */
3229 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003230 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003231 " returned a bad sequence-id error!\n",
3232 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 exception.retry = 1;
3234 continue;
3235 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003236 /*
3237 * BAD_STATEID on OPEN means that the server cancelled our
3238 * state before it received the OPEN_CONFIRM.
3239 * Recover by retrying the request as per the discussion
3240 * on Page 181 of RFC3530.
3241 */
3242 if (status == -NFS4ERR_BAD_STATEID) {
3243 exception.retry = 1;
3244 continue;
3245 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003246 if (status == -NFS4ERR_EXPIRED) {
3247 nfs4_schedule_lease_recovery(server->nfs_client);
3248 exception.retry = 1;
3249 continue;
3250 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003251 if (status == -EAGAIN) {
3252 /* We must have found a delegation */
3253 exception.retry = 1;
3254 continue;
3255 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003256 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3257 continue;
3258 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 status, &exception));
3260 } while (exception.retry);
3261 return res;
3262}
3263
Trond Myklebust8487c472016-06-26 08:44:35 -04003264static int _nfs4_do_setattr(struct inode *inode,
3265 struct nfs_setattrargs *arg,
3266 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003267 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003268 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003269{
3270 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003271 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3273 .rpc_argp = arg,
3274 .rpc_resp = res,
3275 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003276 };
NeilBrowna52458b2018-12-03 11:30:31 +11003277 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003278 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003279 bool truncate;
3280 int status;
3281
3282 nfs_fattr_init(res->fattr);
3283
3284 /* Servers should only apply open mode checks for file size changes */
3285 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003286 if (!truncate) {
3287 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003288 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003289 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003290
Trond Myklebust991eedb2018-04-09 11:15:30 -04003291 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003292 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003293 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003294 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003295 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003296 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003297 l_ctx = nfs_get_lock_context(ctx);
3298 if (IS_ERR(l_ctx))
3299 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003300 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3301 &arg->stateid, &delegation_cred);
3302 nfs_put_lock_context(l_ctx);
3303 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003304 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003305 else if (status == -EAGAIN)
3306 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003307 } else {
3308zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003309 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003310 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003311 if (delegation_cred)
3312 msg.rpc_cred = delegation_cred;
3313
3314 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3315
NeilBrowna52458b2018-12-03 11:30:31 +11003316 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003317 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003318 renew_lease(server, timestamp);
3319 trace_nfs4_setattr(inode, &arg->stateid, status);
3320 return status;
3321}
3322
NeilBrowna52458b2018-12-03 11:30:31 +11003323static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003324 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003325 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003326 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003328 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003329 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003330 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003331 struct nfs_setattrargs arg = {
3332 .fh = NFS_FH(inode),
3333 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003335 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003336 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003337 };
3338 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003340 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003342 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003343 struct nfs4_exception exception = {
3344 .state = state,
3345 .inode = inode,
3346 .stateid = &arg.stateid,
3347 };
3348 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003351 nfs4_bitmap_copy_adjust_setattr(bitmask,
3352 nfs4_bitmask(server, olabel),
3353 inode);
3354
NeilBrown29b59f92016-10-13 15:26:47 +11003355 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003356 switch (err) {
3357 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003358 if (!(sattr->ia_valid & ATTR_SIZE)) {
3359 pr_warn_once("NFSv4: server %s is incorrectly "
3360 "applying open mode checks to "
3361 "a SETATTR that is not "
3362 "changing file size.\n",
3363 server->nfs_client->cl_hostname);
3364 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003365 if (state && !(state->state & FMODE_WRITE)) {
3366 err = -EBADF;
3367 if (sattr->ia_valid & ATTR_OPEN)
3368 err = -EACCES;
3369 goto out;
3370 }
3371 }
3372 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003374out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 return err;
3376}
3377
Peng Tao500d7012015-09-22 11:35:22 +08003378static bool
3379nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3380{
3381 if (inode == NULL || !nfs_have_layout(inode))
3382 return false;
3383
3384 return pnfs_wait_on_layoutreturn(inode, task);
3385}
3386
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003387/*
3388 * Update the seqid of an open stateid
3389 */
3390static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3391 struct nfs4_state *state)
3392{
3393 __be32 seqid_open;
3394 u32 dst_seqid;
3395 int seq;
3396
3397 for (;;) {
3398 if (!nfs4_valid_open_stateid(state))
3399 break;
3400 seq = read_seqbegin(&state->seqlock);
3401 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3402 nfs4_stateid_copy(dst, &state->open_stateid);
3403 if (read_seqretry(&state->seqlock, seq))
3404 continue;
3405 break;
3406 }
3407 seqid_open = state->open_stateid.seqid;
3408 if (read_seqretry(&state->seqlock, seq))
3409 continue;
3410
3411 dst_seqid = be32_to_cpu(dst->seqid);
3412 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3413 dst->seqid = seqid_open;
3414 break;
3415 }
3416}
3417
3418/*
3419 * Update the seqid of an open stateid after receiving
3420 * NFS4ERR_OLD_STATEID
3421 */
3422static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3423 struct nfs4_state *state)
3424{
3425 __be32 seqid_open;
3426 u32 dst_seqid;
3427 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003428 int seq, status = -EAGAIN;
3429 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003430
3431 for (;;) {
3432 ret = false;
3433 if (!nfs4_valid_open_stateid(state))
3434 break;
3435 seq = read_seqbegin(&state->seqlock);
3436 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3437 if (read_seqretry(&state->seqlock, seq))
3438 continue;
3439 break;
3440 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003441
3442 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003443 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003444
3445 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003446
3447 /* Did another OPEN bump the state's seqid? try again: */
3448 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003449 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003450 write_sequnlock(&state->seqlock);
3451 ret = true;
3452 break;
3453 }
3454
3455 /* server says we're behind but we haven't seen the update yet */
3456 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3457 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3458 write_sequnlock(&state->seqlock);
3459 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3460
3461 if (signal_pending(current))
3462 status = -EINTR;
3463 else
3464 if (schedule_timeout(5*HZ) != 0)
3465 status = 0;
3466
3467 finish_wait(&state->waitq, &wait);
3468
3469 if (!status)
3470 continue;
3471 if (status == -EINTR)
3472 break;
3473
3474 /* we slept the whole 5 seconds, we must have lost a seqid */
3475 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003476 ret = true;
3477 break;
3478 }
3479
3480 return ret;
3481}
3482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483struct nfs4_closedata {
3484 struct inode *inode;
3485 struct nfs4_state *state;
3486 struct nfs_closeargs arg;
3487 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003488 struct {
3489 struct nfs4_layoutreturn_args arg;
3490 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003491 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003492 u32 roc_barrier;
3493 bool roc;
3494 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003495 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003496 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497};
3498
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003499static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003500{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003501 struct nfs4_closedata *calldata = data;
3502 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003503 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003504
Trond Myklebustcf805162016-11-15 14:56:07 -05003505 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003506 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3507 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003508 nfs4_put_open_state(calldata->state);
3509 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003510 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003511 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003512 kfree(calldata);
3513}
3514
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003515static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003517 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003520 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003521 struct nfs4_exception exception = {
3522 .state = state,
3523 .inode = calldata->inode,
3524 .stateid = &calldata->arg.stateid,
3525 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Chuck Levera3ca5652012-03-01 17:00:40 -05003527 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003528 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3529 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003530 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003531
3532 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05003533 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3534 &calldata->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04003535 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003536
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003537 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 * the state_owner. we keep this around to process errors
3539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 switch (task->tk_status) {
3541 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003542 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003543 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003544 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003545 case -NFS4ERR_ACCESS:
3546 if (calldata->arg.bitmask != NULL) {
3547 calldata->arg.bitmask = NULL;
3548 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003549 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003550
3551 }
3552 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003553 case -NFS4ERR_OLD_STATEID:
3554 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003555 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003556 state))
3557 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003558 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003559 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003561 case -NFS4ERR_EXPIRED:
3562 nfs4_free_revoked_stateid(server,
3563 &calldata->arg.stateid,
3564 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003565 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003566 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003567 if (calldata->arg.fmode == 0)
3568 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003569 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003571 task->tk_status = nfs4_async_handle_exception(task,
3572 server, task->tk_status, &exception);
3573 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003574 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003576 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3577 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003578out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003579 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003580 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003581 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003582 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003583 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003584out_restart:
3585 task->tk_status = 0;
3586 rpc_restart_call_prepare(task);
3587 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588}
3589
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003590static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003592 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003593 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003594 struct inode *inode = calldata->inode;
Trond Myklebust332d1a02021-03-25 18:15:36 -04003595 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003596 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003597 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003598 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003599
Chuck Levera3ca5652012-03-01 17:00:40 -05003600 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003601 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003602 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003603
Trond Myklebust88069f72009-12-08 08:33:16 -05003604 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003605 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003606 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3607 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3608 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003609 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003610 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003611 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003612 if (state->n_rdonly == 0)
3613 call_close |= is_rdonly;
3614 else if (is_rdonly)
3615 calldata->arg.fmode |= FMODE_READ;
3616 if (state->n_wronly == 0)
3617 call_close |= is_wronly;
3618 else if (is_wronly)
3619 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003620 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3621 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003622 } else if (is_rdwr)
3623 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3624
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003625 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3626 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003627 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003628 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003629
3630 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003631 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003632 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003633 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003634
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003635 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003636 nfs_release_seqid(calldata->arg.seqid);
3637 goto out_wait;
3638 }
3639
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003640 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3641 if (lo && !pnfs_layout_is_valid(lo)) {
3642 calldata->arg.lr_args = NULL;
3643 calldata->res.lr_res = NULL;
3644 }
3645
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003646 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003647 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003648
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003649 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003650 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003651 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust332d1a02021-03-25 18:15:36 -04003652 nfs4_bitmask_set(calldata->arg.bitmask_store,
3653 server->cache_consistency_bitmask,
3654 inode, server, NULL);
3655 calldata->arg.bitmask = calldata->arg.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003656 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003657 calldata->arg.bitmask = NULL;
3658 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003659
Trond Myklebust6ae37332015-01-30 14:21:14 -05003660 calldata->arg.share_access =
3661 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3662 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003663
Trond Myklebustd8d84982016-12-19 12:14:44 -05003664 if (calldata->res.fattr == NULL)
3665 calldata->arg.bitmask = NULL;
3666 else if (calldata->arg.bitmask == NULL)
3667 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003668 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003669 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003670 &calldata->arg.seq_args,
3671 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003672 task) != 0)
3673 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003674 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003675 return;
3676out_no_action:
3677 task->tk_action = NULL;
3678out_wait:
3679 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680}
3681
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003682static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003683 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003684 .rpc_call_done = nfs4_close_done,
3685 .rpc_release = nfs4_free_closedata,
3686};
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688/*
3689 * It is possible for data to be read/written from a mem-mapped file
3690 * after the sys_close call (which hits the vfs layer as a flush).
3691 * This means that we can't safely call nfsv4 close on a file until
3692 * the inode is cleared. This in turn means that we are not good
3693 * NFSv4 citizens - we do not indicate to the server to update the file's
3694 * share state even when we are done with one of the three share
3695 * stateid's in the inode.
3696 *
3697 * NOTE: Caller must be holding the sp->so_owner semaphore!
3698 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003699int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003701 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003702 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003704 struct nfs4_state_owner *sp = state->owner;
3705 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003706 struct rpc_message msg = {
3707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3708 .rpc_cred = state->owner->so_cred,
3709 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003710 struct rpc_task_setup task_setup_data = {
3711 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003712 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003713 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003714 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003715 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003716 };
Trond Myklebust95121352005-10-18 14:20:12 -07003717 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003719 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3720 &task_setup_data.rpc_client, &msg);
3721
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003722 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003724 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003725 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003726 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003728 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003729 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3730 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003732 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3733 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003734 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003735 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003736 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003737 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003738 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003739 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003740 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003741 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003742 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003743 calldata->lr.roc = pnfs_roc(state->inode,
3744 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3745 if (calldata->lr.roc) {
3746 calldata->arg.lr_args = &calldata->lr.arg;
3747 calldata->res.lr_res = &calldata->lr.res;
3748 }
Al Viro643168c2011-06-22 18:20:23 -04003749 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003750
Trond Myklebust1174dd12010-12-21 10:52:24 -05003751 msg.rpc_argp = &calldata->arg;
3752 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003753 task_setup_data.callback_data = calldata;
3754 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003755 if (IS_ERR(task))
3756 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003757 status = 0;
3758 if (wait)
3759 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003760 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003761 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003762out_free_calldata:
3763 kfree(calldata);
3764out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003765 nfs4_put_open_state(state);
3766 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003767 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768}
3769
Trond Myklebust2b484292010-09-17 10:56:51 -04003770static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003771nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3772 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003775 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3776
3777 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Trond Myklebust565277f2007-10-15 18:17:53 -04003779 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003780 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003781
3782 nfs4_label_release_security(label);
3783
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003784 if (IS_ERR(state))
3785 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003786 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
Trond Myklebust1185a552009-12-03 15:54:02 -05003789static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003790{
3791 if (ctx->state == NULL)
3792 return;
3793 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003794 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003795 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003796 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003797}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Trond Myklebustb944dba2013-11-04 15:20:20 -05003799#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3800#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003801#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3804{
Kinglong Mee8c612822015-08-26 21:12:58 +08003805 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003806 struct nfs4_server_caps_arg args = {
3807 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003808 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003809 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 struct nfs4_server_caps_res res = {};
3811 struct rpc_message msg = {
3812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003813 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 .rpc_resp = &res,
3815 };
3816 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003817 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Kinglong Mee8c612822015-08-26 21:12:58 +08003819 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3820 FATTR4_WORD0_FH_EXPIRE_TYPE |
3821 FATTR4_WORD0_LINK_SUPPORT |
3822 FATTR4_WORD0_SYMLINK_SUPPORT |
3823 FATTR4_WORD0_ACLSUPPORT;
3824 if (minorversion)
3825 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3826
Bryan Schumaker7c513052011-03-24 17:12:24 +00003827 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003829 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003830 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003831 case 0:
3832 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3833 res.attr_bitmask[2] = 0;
3834 break;
3835 case 1:
3836 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3837 break;
3838 case 2:
3839 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003842 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3843 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3844 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3845 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003846 NFS_CAP_CTIME|NFS_CAP_MTIME|
3847 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003848 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3849 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 server->caps |= NFS_CAP_ACLS;
3851 if (res.has_links != 0)
3852 server->caps |= NFS_CAP_HARDLINKS;
3853 if (res.has_symlinks != 0)
3854 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003855 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3856 server->caps |= NFS_CAP_FILEID;
3857 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3858 server->caps |= NFS_CAP_MODE;
3859 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3860 server->caps |= NFS_CAP_NLINK;
3861 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3862 server->caps |= NFS_CAP_OWNER;
3863 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3864 server->caps |= NFS_CAP_OWNER_GROUP;
3865 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3866 server->caps |= NFS_CAP_ATIME;
3867 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3868 server->caps |= NFS_CAP_CTIME;
3869 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3870 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003871#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3872 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3873 server->caps |= NFS_CAP_SECURITY_LABEL;
3874#endif
3875 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3876 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003877 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003878
Trond Myklebusta65318b2009-03-11 14:10:28 -04003879 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3880 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3881 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003882 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003883
3884 /* Avoid a regression due to buggy server */
3885 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3886 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003887 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3888 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003891 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 return status;
3895}
3896
Trond Myklebust55a97592006-06-09 09:34:19 -04003897int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898{
Trond Myklebust0688e642019-04-07 13:59:09 -04003899 struct nfs4_exception exception = {
3900 .interruptible = true,
3901 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 int err;
3903 do {
3904 err = nfs4_handle_exception(server,
3905 _nfs4_server_capabilities(server, fhandle),
3906 &exception);
3907 } while (exception.retry);
3908 return err;
3909}
3910
3911static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3912 struct nfs_fsinfo *info)
3913{
David Quigleyaa9c2662013-05-22 12:50:44 -04003914 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003916 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 };
3918 struct nfs4_lookup_res res = {
3919 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003920 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 .fh = fhandle,
3922 };
3923 struct rpc_message msg = {
3924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3925 .rpc_argp = &args,
3926 .rpc_resp = &res,
3927 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003928
David Quigleyaa9c2662013-05-22 12:50:44 -04003929 bitmask[0] = nfs4_fattr_bitmap[0];
3930 bitmask[1] = nfs4_fattr_bitmap[1];
3931 /*
3932 * Process the label in the upcoming getfattr
3933 */
3934 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3935
Trond Myklebust0e574af2005-10-27 22:12:38 -04003936 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003937 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938}
3939
3940static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3941 struct nfs_fsinfo *info)
3942{
Trond Myklebust0688e642019-04-07 13:59:09 -04003943 struct nfs4_exception exception = {
3944 .interruptible = true,
3945 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 int err;
3947 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003948 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003949 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003950 switch (err) {
3951 case 0:
3952 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003953 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003954 default:
3955 err = nfs4_handle_exception(server, err, &exception);
3956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003958out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 return err;
3960}
3961
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003962static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3963 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3964{
Trond Myklebustc2190662013-08-26 19:23:04 -04003965 struct rpc_auth_create_args auth_args = {
3966 .pseudoflavor = flavor,
3967 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003968 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003969
Trond Myklebustc2190662013-08-26 19:23:04 -04003970 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003971 if (IS_ERR(auth))
3972 return -EACCES;
3973 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003974}
3975
Chuck Lever9a744ba2013-03-16 15:56:02 -04003976/*
3977 * Retry pseudoroot lookup with various security flavors. We do this when:
3978 *
3979 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3980 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3981 *
3982 * Returns zero on success, or a negative NFS4ERR value, or a
3983 * negative errno value.
3984 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003985static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003986 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003988 /* Per 3530bis 15.33.5 */
3989 static const rpc_authflavor_t flav_array[] = {
3990 RPC_AUTH_GSS_KRB5P,
3991 RPC_AUTH_GSS_KRB5I,
3992 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003993 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003994 RPC_AUTH_NULL,
3995 };
3996 int status = -EPERM;
3997 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003999 if (server->auth_info.flavor_len > 0) {
4000 /* try each flavor specified by user */
4001 for (i = 0; i < server->auth_info.flavor_len; i++) {
4002 status = nfs4_lookup_root_sec(server, fhandle, info,
4003 server->auth_info.flavors[i]);
4004 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4005 continue;
4006 break;
4007 }
4008 } else {
4009 /* no flavors specified by user, try default list */
4010 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4011 status = nfs4_lookup_root_sec(server, fhandle, info,
4012 flav_array[i]);
4013 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4014 continue;
4015 break;
4016 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004017 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004018
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004019 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004020 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004021 * to access the mount. It could also mean that we tried to mount
4022 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4023 * existing mount programs don't handle -EACCES very well so it should
4024 * be mapped to -EPERM instead.
4025 */
4026 if (status == -EACCES)
4027 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004028 return status;
4029}
4030
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004031/**
4032 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4033 * @server: initialized nfs_server handle
4034 * @fhandle: we fill in the pseudo-fs root file handle
4035 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004036 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004037 *
4038 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004039 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004040int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004041 struct nfs_fsinfo *info,
4042 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004043{
Andre Przywarac7757072015-04-23 17:17:40 +01004044 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004045
Andre Przywarac7757072015-04-23 17:17:40 +01004046 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004047 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004048
4049 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004050 status = server->nfs_client->cl_mvops->find_root_sec(server,
4051 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004052
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 if (status == 0)
4054 status = nfs4_server_capabilities(server, fhandle);
4055 if (status == 0)
4056 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004057
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004058 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059}
4060
Bryan Schumakerbae36242012-05-10 15:07:31 -04004061static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4062 struct nfs_fsinfo *info)
4063{
4064 int error;
4065 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004066 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004067
4068 error = nfs4_server_capabilities(server, mntfh);
4069 if (error < 0) {
4070 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4071 return error;
4072 }
4073
Trond Myklebusta841b542018-04-07 13:50:59 -04004074 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004075 if (error < 0) {
4076 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004077 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004078 }
4079
4080 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4081 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4082 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4083
Scott Mayhew779df6a2020-03-03 17:58:37 -05004084out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004085 return error;
4086}
4087
Manoj Naik6b97fd32006-06-09 09:34:29 -04004088/*
4089 * Get locations and (maybe) other attributes of a referral.
4090 * Note that we'll actually follow the referral later when
4091 * we detect fsid mismatch in inode revalidation
4092 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004093static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4094 const struct qstr *name, struct nfs_fattr *fattr,
4095 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004096{
4097 int status = -ENOMEM;
4098 struct page *page = NULL;
4099 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004100
4101 page = alloc_page(GFP_KERNEL);
4102 if (page == NULL)
4103 goto out;
4104 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4105 if (locations == NULL)
4106 goto out;
4107
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004108 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004109 if (status != 0)
4110 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004111
4112 /*
4113 * If the fsid didn't change, this is a migration event, not a
4114 * referral. Cause us to drop into the exception handler, which
4115 * will kick off migration recovery.
4116 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004117 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004118 dprintk("%s: server did not return a different fsid for"
4119 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004120 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004121 goto out;
4122 }
Andy Adamson533eb462011-06-13 18:25:56 -04004123 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4124 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004125
Andy Adamson533eb462011-06-13 18:25:56 -04004126 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004127 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004128 memset(fhandle, 0, sizeof(struct nfs_fh));
4129out:
4130 if (page)
4131 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004132 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004133 return status;
4134}
4135
David Quigley1775fd32013-05-22 12:50:42 -04004136static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004137 struct nfs_fattr *fattr, struct nfs4_label *label,
4138 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
Trond Myklebust771734f2018-04-07 13:54:23 -04004140 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 struct nfs4_getattr_arg args = {
4142 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004143 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 };
4145 struct nfs4_getattr_res res = {
4146 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004147 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 .server = server,
4149 };
4150 struct rpc_message msg = {
4151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4152 .rpc_argp = &args,
4153 .rpc_resp = &res,
4154 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004155 unsigned short task_flags = 0;
4156
4157 /* Is this is an attribute revalidation, subject to softreval? */
4158 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4159 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004160
Trond Myklebust771734f2018-04-07 13:54:23 -04004161 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004162
Trond Myklebust0e574af2005-10-27 22:12:38 -04004163 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004164 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4165 return nfs4_do_call_sync(server->client, server, &msg,
4166 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167}
4168
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004169int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004170 struct nfs_fattr *fattr, struct nfs4_label *label,
4171 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172{
Trond Myklebust0688e642019-04-07 13:59:09 -04004173 struct nfs4_exception exception = {
4174 .interruptible = true,
4175 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 int err;
4177 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004178 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004179 trace_nfs4_getattr(server, fhandle, fattr, err);
4180 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 &exception);
4182 } while (exception.retry);
4183 return err;
4184}
4185
4186/*
4187 * The file is not closed if it is opened due to the a request to change
4188 * the size of the file. The open call will not be needed once the
4189 * VFS layer lookup-intents are implemented.
4190 *
4191 * Close is called when the inode is destroyed.
4192 * If we haven't opened the file for O_WRONLY, we
4193 * need to in the size_change case to obtain a stateid.
4194 *
4195 * Got race?
4196 * Because OPEN is always done by name in nfsv4, it is
4197 * possible that we opened a different file by the same
4198 * name. We can recognize this race condition, but we
4199 * can't do anything about it besides returning an error.
4200 *
4201 * This will be fixed with VFS changes (lookup-intent).
4202 */
4203static int
4204nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4205 struct iattr *sattr)
4206{
David Howells2b0143b2015-03-17 22:25:59 +00004207 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004208 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004209 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004210 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 int status;
4212
Peng Tao88ac8152014-09-12 11:04:10 +08004213 if (pnfs_ld_layoutret_on_setattr(inode) &&
4214 sattr->ia_valid & ATTR_SIZE &&
4215 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004216 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004217
Trond Myklebust0e574af2005-10-27 22:12:38 -04004218 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
Andy Adamson26699402012-05-30 16:12:24 -04004220 /* Deal with open(O_TRUNC) */
4221 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004222 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004223
4224 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004225 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004226 return 0;
4227
Trond Myklebustd5308382005-11-04 15:33:38 -05004228 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004229 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004230
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004231 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004232 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004233 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004234 }
4235
David Quigley14c43f72013-05-22 12:50:43 -04004236 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4237 if (IS_ERR(label))
4238 return PTR_ERR(label);
4239
Trond Myklebust199366f2018-03-20 16:43:18 -04004240 /* Return any delegations if we're going to change ACLs */
4241 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004242 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004243
NeilBrown29b59f92016-10-13 15:26:47 +11004244 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004245 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004246 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004247 nfs_setsecurity(inode, fattr, label);
4248 }
David Quigley14c43f72013-05-22 12:50:43 -04004249 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return status;
4251}
4252
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004253static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004254 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004255 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004256{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004257 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004258 int status;
4259 struct nfs4_lookup_arg args = {
4260 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004261 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004262 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004263 };
4264 struct nfs4_lookup_res res = {
4265 .server = server,
4266 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004267 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004268 .fh = fhandle,
4269 };
4270 struct rpc_message msg = {
4271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4272 .rpc_argp = &args,
4273 .rpc_resp = &res,
4274 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004275 unsigned short task_flags = 0;
4276
4277 /* Is this is an attribute revalidation, subject to softreval? */
4278 if (nfs_lookup_is_soft_revalidate(dentry))
4279 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004280
David Quigleyaa9c2662013-05-22 12:50:44 -04004281 args.bitmask = nfs4_bitmask(server, label);
4282
David Howells2b3de442006-08-22 20:06:09 -04004283 nfs_fattr_init(fattr);
4284
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004285 dprintk("NFS call lookup %pd2\n", dentry);
4286 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4287 status = nfs4_do_call_sync(clnt, server, &msg,
4288 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 dprintk("NFS reply lookup: %d\n", status);
4290 return status;
4291}
4292
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004293static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004294{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004295 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004296 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004297 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4298 fattr->nlink = 2;
4299}
4300
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004301static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004302 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004303 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004304{
Trond Myklebust0688e642019-04-07 13:59:09 -04004305 struct nfs4_exception exception = {
4306 .interruptible = true,
4307 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004308 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004309 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004310 int err;
4311 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004312 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004313 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004314 switch (err) {
4315 case -NFS4ERR_BADNAME:
4316 err = -ENOENT;
4317 goto out;
4318 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004319 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004320 if (err == -NFS4ERR_MOVED)
4321 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004322 goto out;
4323 case -NFS4ERR_WRONGSEC:
4324 err = -EPERM;
4325 if (client != *clnt)
4326 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004327 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004328 if (IS_ERR(client))
4329 return PTR_ERR(client);
4330
4331 exception.retry = 1;
4332 break;
4333 default:
4334 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4335 }
4336 } while (exception.retry);
4337
4338out:
4339 if (err == 0)
4340 *clnt = client;
4341 else if (client != *clnt)
4342 rpc_shutdown_client(client);
4343
4344 return err;
4345}
4346
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004347static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004348 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4349 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004351 int status;
4352 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004353
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004354 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004355 if (client != NFS_CLIENT(dir)) {
4356 rpc_shutdown_client(client);
4357 nfs_fixup_secinfo_attributes(fattr);
4358 }
4359 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004362struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004363nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004364 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4365{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004366 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004367 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004368
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004369 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004370 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004371 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004372 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004373}
4374
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004375static int _nfs4_proc_lookupp(struct inode *inode,
4376 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4377 struct nfs4_label *label)
4378{
4379 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4380 struct nfs_server *server = NFS_SERVER(inode);
4381 int status;
4382 struct nfs4_lookupp_arg args = {
4383 .bitmask = server->attr_bitmask,
4384 .fh = NFS_FH(inode),
4385 };
4386 struct nfs4_lookupp_res res = {
4387 .server = server,
4388 .fattr = fattr,
4389 .label = label,
4390 .fh = fhandle,
4391 };
4392 struct rpc_message msg = {
4393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4394 .rpc_argp = &args,
4395 .rpc_resp = &res,
4396 };
Trond Myklebust76998eb2020-10-20 14:30:35 -04004397 unsigned short task_flags = 0;
4398
4399 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4400 task_flags |= RPC_TASK_TIMEOUT;
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004401
4402 args.bitmask = nfs4_bitmask(server, label);
4403
4404 nfs_fattr_init(fattr);
4405
4406 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4407 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
Trond Myklebust76998eb2020-10-20 14:30:35 -04004408 &res.seq_res, task_flags);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004409 dprintk("NFS reply lookupp: %d\n", status);
4410 return status;
4411}
4412
4413static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4414 struct nfs_fattr *fattr, struct nfs4_label *label)
4415{
Trond Myklebust0688e642019-04-07 13:59:09 -04004416 struct nfs4_exception exception = {
4417 .interruptible = true,
4418 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004419 int err;
4420 do {
4421 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4422 trace_nfs4_lookupp(inode, err);
4423 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4424 &exception);
4425 } while (exception.retry);
4426 return err;
4427}
4428
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4430{
Trond Myklebust76b32992007-08-10 17:45:11 -04004431 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 struct nfs4_accessargs args = {
4433 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004434 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004436 struct nfs4_accessres res = {
4437 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004438 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 struct rpc_message msg = {
4440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4441 .rpc_argp = &args,
4442 .rpc_resp = &res,
4443 .rpc_cred = entry->cred,
4444 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004445 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Trond Myklebust7c672652018-06-04 15:00:53 -04004447 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004448 res.fattr = nfs_alloc_fattr();
4449 if (res.fattr == NULL)
4450 return -ENOMEM;
4451 args.bitmask = server->cache_consistency_bitmask;
4452 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004453 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004455 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004456 if (res.fattr)
4457 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004459 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 return status;
4461}
4462
4463static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4464{
Trond Myklebust0688e642019-04-07 13:59:09 -04004465 struct nfs4_exception exception = {
4466 .interruptible = true,
4467 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 int err;
4469 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004470 err = _nfs4_proc_access(inode, entry);
4471 trace_nfs4_access(inode, err);
4472 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 &exception);
4474 } while (exception.retry);
4475 return err;
4476}
4477
4478/*
4479 * TODO: For the time being, we don't try to get any attributes
4480 * along with any of the zero-copy operations READ, READDIR,
4481 * READLINK, WRITE.
4482 *
4483 * In the case of the first three, we want to put the GETATTR
4484 * after the read-type operation -- this is because it is hard
4485 * to predict the length of a GETATTR response in v4, and thus
4486 * align the READ data correctly. This means that the GETATTR
4487 * may end up partially falling into the page cache, and we should
4488 * shift it into the 'tail' of the xdr_buf before processing.
4489 * To do this efficiently, we need to know the total length
4490 * of data received, which doesn't seem to be available outside
4491 * of the RPC layer.
4492 *
4493 * In the case of WRITE, we also want to put the GETATTR after
4494 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004495 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 *
4497 * Both of these changes to the XDR layer would in fact be quite
4498 * minor, but I decided to leave them for a subsequent patch.
4499 */
4500static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4501 unsigned int pgbase, unsigned int pglen)
4502{
4503 struct nfs4_readlink args = {
4504 .fh = NFS_FH(inode),
4505 .pgbase = pgbase,
4506 .pglen = pglen,
4507 .pages = &page,
4508 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004509 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 struct rpc_message msg = {
4511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4512 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004513 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 };
4515
Bryan Schumaker7c513052011-03-24 17:12:24 +00004516 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 -07004517}
4518
4519static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4520 unsigned int pgbase, unsigned int pglen)
4521{
Trond Myklebust0688e642019-04-07 13:59:09 -04004522 struct nfs4_exception exception = {
4523 .interruptible = true,
4524 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 int err;
4526 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004527 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4528 trace_nfs4_readlink(inode, err);
4529 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 &exception);
4531 } while (exception.retry);
4532 return err;
4533}
4534
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004536 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538static int
4539nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004540 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004542 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004543 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004544 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 int status = 0;
4547
NeilBrown532d4de2016-10-13 15:26:47 +11004548 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004549 if (IS_ERR(ctx))
4550 return PTR_ERR(ctx);
4551
David Quigleyaa9c2662013-05-22 12:50:44 -04004552 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4553
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004554 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4555 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004556 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 if (IS_ERR(state)) {
4558 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004562 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004563 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 return status;
4565}
4566
Trond Myklebust3c591172018-07-31 15:54:10 -04004567static int
4568_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
Trond Myklebust16e42952005-10-27 22:12:44 -04004570 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004571 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004573 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004575 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004576 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004577 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4580 .rpc_argp = &args,
4581 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004583 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004584 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004585
Bryan Schumaker7c513052011-03-24 17:12:24 +00004586 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004587 if (status == 0) {
4588 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004589 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4590 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004591 /* Removing a directory decrements nlink in the parent */
4592 if (ftype == NF4DIR && dir->i_nlink > 2)
4593 nfs4_dec_nlink_locked(dir);
4594 spin_unlock(&dir->i_lock);
4595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 return status;
4597}
4598
Trond Myklebust912678d2018-03-20 16:43:15 -04004599static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4600{
Trond Myklebust0688e642019-04-07 13:59:09 -04004601 struct nfs4_exception exception = {
4602 .interruptible = true,
4603 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004604 struct inode *inode = d_inode(dentry);
4605 int err;
4606
Trond Myklebustc01d3642018-03-20 16:43:20 -04004607 if (inode) {
4608 if (inode->i_nlink == 1)
4609 nfs4_inode_return_delegation(inode);
4610 else
4611 nfs4_inode_make_writeable(inode);
4612 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004613 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004614 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004615 trace_nfs4_remove(dir, &dentry->d_name, err);
4616 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4617 &exception);
4618 } while (exception.retry);
4619 return err;
4620}
4621
4622static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623{
Trond Myklebust0688e642019-04-07 13:59:09 -04004624 struct nfs4_exception exception = {
4625 .interruptible = true,
4626 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004630 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004631 trace_nfs4_remove(dir, name, err);
4632 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 &exception);
4634 } while (exception.retry);
4635 return err;
4636}
4637
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004638static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4639 struct dentry *dentry,
4640 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004642 struct nfs_removeargs *args = msg->rpc_argp;
4643 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Trond Myklebust977fcc22018-03-20 16:43:17 -04004645 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004647 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004648
4649 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004650
4651 if (inode)
4652 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004655static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4656{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004657 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004658 &data->args.seq_args,
4659 &data->res.seq_res,
4660 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661}
4662
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004663static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004665 struct nfs_unlinkdata *data = task->tk_calldata;
4666 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004667
Trond Myklebust14516c32010-07-31 14:29:06 -04004668 if (!nfs4_sequence_done(task, &res->seq_res))
4669 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004670 if (nfs4_async_handle_error(task, res->server, NULL,
4671 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004672 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004673 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004674 nfs4_update_changeattr(dir, &res->cinfo,
4675 res->dir_attr->time_start,
4676 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004677 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678}
4679
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004680static void nfs4_proc_rename_setup(struct rpc_message *msg,
4681 struct dentry *old_dentry,
4682 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004683{
Jeff Laytond3d41522010-09-17 17:31:57 -04004684 struct nfs_renameargs *arg = msg->rpc_argp;
4685 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004686 struct inode *old_inode = d_inode(old_dentry);
4687 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004688
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004689 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004690 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004691 if (new_inode)
4692 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004693 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004694 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004695 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004696}
4697
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004698static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4699{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004700 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004701 &data->args.seq_args,
4702 &data->res.seq_res,
4703 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004704}
4705
4706static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4707 struct inode *new_dir)
4708{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004709 struct nfs_renamedata *data = task->tk_calldata;
4710 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004711
4712 if (!nfs4_sequence_done(task, &res->seq_res))
4713 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004714 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004715 return 0;
4716
Trond Myklebustc733c492017-01-11 12:32:26 -05004717 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004718 if (new_dir != old_dir) {
4719 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004720 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004721 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004722 NFS_INO_INVALID_OTHER |
4723 NFS_INO_INVALID_DATA);
4724 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004725 res->new_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004726 NFS_INO_INVALID_OTHER |
4727 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004728 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004729 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004730 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004731 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004732 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004733 return 1;
4734}
4735
Al Virobeffb8f2016-07-20 16:34:42 -04004736static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004738 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004739 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 struct nfs4_link_arg arg = {
4741 .fh = NFS_FH(inode),
4742 .dir_fh = NFS_FH(dir),
4743 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004744 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004746 struct nfs4_link_res res = {
4747 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004748 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004749 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 struct rpc_message msg = {
4751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4752 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004753 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004755 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Trond Myklebust136f2622010-04-16 16:22:49 -04004757 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004758 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004759 goto out;
4760
David Quigley14c43f72013-05-22 12:50:43 -04004761 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4762 if (IS_ERR(res.label)) {
4763 status = PTR_ERR(res.label);
4764 goto out;
4765 }
4766
Trond Myklebustc01d3642018-03-20 16:43:20 -04004767 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004768 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004769
Bryan Schumaker7c513052011-03-24 17:12:24 +00004770 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004771 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004772 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4773 NFS_INO_INVALID_DATA);
David Quigleyaa9c2662013-05-22 12:50:44 -04004774 status = nfs_post_op_update_inode(inode, res.fattr);
4775 if (!status)
4776 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004777 }
David Quigley14c43f72013-05-22 12:50:43 -04004778
4779
4780 nfs4_label_free(res.label);
4781
Trond Myklebust136f2622010-04-16 16:22:49 -04004782out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004783 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 return status;
4785}
4786
Al Virobeffb8f2016-07-20 16:34:42 -04004787static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788{
Trond Myklebust0688e642019-04-07 13:59:09 -04004789 struct nfs4_exception exception = {
4790 .interruptible = true,
4791 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 int err;
4793 do {
4794 err = nfs4_handle_exception(NFS_SERVER(inode),
4795 _nfs4_proc_link(inode, dir, name),
4796 &exception);
4797 } while (exception.retry);
4798 return err;
4799}
4800
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004801struct nfs4_createdata {
4802 struct rpc_message msg;
4803 struct nfs4_create_arg arg;
4804 struct nfs4_create_res res;
4805 struct nfs_fh fh;
4806 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004807 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004808};
4809
4810static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004811 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004812{
4813 struct nfs4_createdata *data;
4814
4815 data = kzalloc(sizeof(*data), GFP_KERNEL);
4816 if (data != NULL) {
4817 struct nfs_server *server = NFS_SERVER(dir);
4818
David Quigley14c43f72013-05-22 12:50:43 -04004819 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4820 if (IS_ERR(data->label))
4821 goto out_free;
4822
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004823 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4824 data->msg.rpc_argp = &data->arg;
4825 data->msg.rpc_resp = &data->res;
4826 data->arg.dir_fh = NFS_FH(dir);
4827 data->arg.server = server;
4828 data->arg.name = name;
4829 data->arg.attrs = sattr;
4830 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004831 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004832 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004833 data->res.server = server;
4834 data->res.fh = &data->fh;
4835 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004836 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004837 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004838 }
4839 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004840out_free:
4841 kfree(data);
4842 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004843}
4844
4845static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4846{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004847 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004848 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004849 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004850 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004851 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4852 data->res.fattr->time_start,
4853 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004854 /* Creating a directory bumps nlink in the parent */
4855 if (data->arg.ftype == NF4DIR)
4856 nfs4_inc_nlink_locked(dir);
4857 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004858 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004859 }
4860 return status;
4861}
4862
4863static void nfs4_free_createdata(struct nfs4_createdata *data)
4864{
David Quigley14c43f72013-05-22 12:50:43 -04004865 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004866 kfree(data);
4867}
4868
Chuck Lever4f390c12006-08-22 20:06:22 -04004869static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004870 struct page *page, unsigned int len, struct iattr *sattr,
4871 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004873 struct nfs4_createdata *data;
4874 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
Chuck Lever94a6d752006-08-22 20:06:23 -04004876 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004877 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004878
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004879 status = -ENOMEM;
4880 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4881 if (data == NULL)
4882 goto out;
4883
4884 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4885 data->arg.u.symlink.pages = &page;
4886 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004887 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004889 status = nfs4_do_create(dir, dentry, data);
4890
4891 nfs4_free_createdata(data);
4892out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 return status;
4894}
4895
Chuck Lever4f390c12006-08-22 20:06:22 -04004896static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004897 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898{
Trond Myklebust0688e642019-04-07 13:59:09 -04004899 struct nfs4_exception exception = {
4900 .interruptible = true,
4901 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004902 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004904
4905 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004908 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4909 trace_nfs4_symlink(dir, &dentry->d_name, err);
4910 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 &exception);
4912 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004913
4914 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 return err;
4916}
4917
4918static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004919 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004921 struct nfs4_createdata *data;
4922 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004924 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4925 if (data == NULL)
4926 goto out;
4927
David Quigley1775fd32013-05-22 12:50:42 -04004928 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004929 status = nfs4_do_create(dir, dentry, data);
4930
4931 nfs4_free_createdata(data);
4932out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 return status;
4934}
4935
4936static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4937 struct iattr *sattr)
4938{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004939 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004940 struct nfs4_exception exception = {
4941 .interruptible = true,
4942 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004943 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004945
David Quigleyaa9c2662013-05-22 12:50:44 -04004946 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4947
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004948 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4949 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004951 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4952 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4953 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 &exception);
4955 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004956 nfs4_label_release_security(label);
4957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 return err;
4959}
4960
Trond Myklebust82e22a52020-11-02 17:34:23 -05004961static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4962 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963{
Trond Myklebust82e22a52020-11-02 17:34:23 -05004964 struct inode *dir = d_inode(nr_arg->dentry);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004965 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 struct nfs4_readdir_arg args = {
4967 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -05004968 .pages = nr_arg->pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 .pgbase = 0,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004970 .count = nr_arg->page_len,
4971 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 };
4973 struct nfs4_readdir_res res;
4974 struct rpc_message msg = {
4975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4976 .rpc_argp = &args,
4977 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004978 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 };
4980 int status;
4981
Trond Myklebust82e22a52020-11-02 17:34:23 -05004982 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
4983 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004984 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4985 args.bitmask = server->attr_bitmask_nl;
4986 else
4987 args.bitmask = server->attr_bitmask;
4988
Trond Myklebust82e22a52020-11-02 17:34:23 -05004989 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 res.pgbase = args.pgbase;
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004991 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4992 &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05004993 if (status >= 0) {
Trond Myklebust82e22a52020-11-02 17:34:23 -05004994 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004995 status += args.pgbase;
4996 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004997
4998 nfs_invalidate_atime(dir);
4999
Harvey Harrison3110ff82008-05-02 13:42:44 -07005000 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 return status;
5002}
5003
Trond Myklebust82e22a52020-11-02 17:34:23 -05005004static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5005 struct nfs_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
Trond Myklebust0688e642019-04-07 13:59:09 -04005007 struct nfs4_exception exception = {
5008 .interruptible = true,
5009 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 int err;
5011 do {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005012 err = _nfs4_proc_readdir(arg, res);
5013 trace_nfs4_readdir(d_inode(arg->dentry), err);
5014 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5015 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 } while (exception.retry);
5017 return err;
5018}
5019
5020static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005021 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005023 struct nfs4_createdata *data;
5024 int mode = sattr->ia_mode;
5025 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005027 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5028 if (data == NULL)
5029 goto out;
5030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005032 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005034 data->arg.ftype = NF4BLK;
5035 data->arg.u.device.specdata1 = MAJOR(rdev);
5036 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 }
5038 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005039 data->arg.ftype = NF4CHR;
5040 data->arg.u.device.specdata1 = MAJOR(rdev);
5041 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005042 } else if (!S_ISSOCK(mode)) {
5043 status = -EINVAL;
5044 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 }
David Quigley1775fd32013-05-22 12:50:42 -04005046
David Quigleyaa9c2662013-05-22 12:50:44 -04005047 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005048 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005049out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005050 nfs4_free_createdata(data);
5051out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 return status;
5053}
5054
5055static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5056 struct iattr *sattr, dev_t rdev)
5057{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005058 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005059 struct nfs4_exception exception = {
5060 .interruptible = true,
5061 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005062 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005064
David Quigleyaa9c2662013-05-22 12:50:44 -04005065 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5066
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005067 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5068 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005070 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5071 trace_nfs4_mknod(dir, &dentry->d_name, err);
5072 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 &exception);
5074 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005075
5076 nfs4_label_release_security(label);
5077
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return err;
5079}
5080
5081static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5082 struct nfs_fsstat *fsstat)
5083{
5084 struct nfs4_statfs_arg args = {
5085 .fh = fhandle,
5086 .bitmask = server->attr_bitmask,
5087 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005088 struct nfs4_statfs_res res = {
5089 .fsstat = fsstat,
5090 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 struct rpc_message msg = {
5092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5093 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005094 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 };
5096
Trond Myklebust0e574af2005-10-27 22:12:38 -04005097 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005098 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099}
5100
5101static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5102{
Trond Myklebust0688e642019-04-07 13:59:09 -04005103 struct nfs4_exception exception = {
5104 .interruptible = true,
5105 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 int err;
5107 do {
5108 err = nfs4_handle_exception(server,
5109 _nfs4_proc_statfs(server, fhandle, fsstat),
5110 &exception);
5111 } while (exception.retry);
5112 return err;
5113}
5114
5115static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5116 struct nfs_fsinfo *fsinfo)
5117{
5118 struct nfs4_fsinfo_arg args = {
5119 .fh = fhandle,
5120 .bitmask = server->attr_bitmask,
5121 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005122 struct nfs4_fsinfo_res res = {
5123 .fsinfo = fsinfo,
5124 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 struct rpc_message msg = {
5126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5127 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005128 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 };
5130
Bryan Schumaker7c513052011-03-24 17:12:24 +00005131 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132}
5133
5134static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5135{
Trond Myklebust0688e642019-04-07 13:59:09 -04005136 struct nfs4_exception exception = {
5137 .interruptible = true,
5138 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 int err;
5140
5141 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005142 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005143 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005144 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005145 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005146 break;
5147 }
5148 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 } while (exception.retry);
5150 return err;
5151}
5152
5153static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5154{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005155 int error;
5156
Trond Myklebust0e574af2005-10-27 22:12:38 -04005157 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005158 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005159 if (error == 0) {
5160 /* block layout checks this! */
5161 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005162 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005163 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005164
5165 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166}
5167
5168static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5169 struct nfs_pathconf *pathconf)
5170{
5171 struct nfs4_pathconf_arg args = {
5172 .fh = fhandle,
5173 .bitmask = server->attr_bitmask,
5174 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005175 struct nfs4_pathconf_res res = {
5176 .pathconf = pathconf,
5177 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 struct rpc_message msg = {
5179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5180 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005181 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 };
5183
5184 /* None of the pathconf attributes are mandatory to implement */
5185 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5186 memset(pathconf, 0, sizeof(*pathconf));
5187 return 0;
5188 }
5189
Trond Myklebust0e574af2005-10-27 22:12:38 -04005190 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005191 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192}
5193
5194static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5195 struct nfs_pathconf *pathconf)
5196{
Trond Myklebust0688e642019-04-07 13:59:09 -04005197 struct nfs4_exception exception = {
5198 .interruptible = true,
5199 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 int err;
5201
5202 do {
5203 err = nfs4_handle_exception(server,
5204 _nfs4_proc_pathconf(server, fhandle, pathconf),
5205 &exception);
5206 } while (exception.retry);
5207 return err;
5208}
5209
Trond Myklebust5521abf2013-03-16 20:54:34 -04005210int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005211 const struct nfs_open_context *ctx,
5212 const struct nfs_lock_context *l_ctx,
5213 fmode_t fmode)
5214{
NeilBrown17393472016-10-13 15:26:47 +11005215 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005216}
5217EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5218
Trond Myklebust5521abf2013-03-16 20:54:34 -04005219static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5220 const struct nfs_open_context *ctx,
5221 const struct nfs_lock_context *l_ctx,
5222 fmode_t fmode)
5223{
Ben Dooksd49dd112019-10-16 17:28:21 +01005224 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005225
Trond Myklebuste1253be2014-03-05 08:44:23 -05005226 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005227 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005228 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005229 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005230}
5231
5232static bool nfs4_error_stateid_expired(int err)
5233{
5234 switch (err) {
5235 case -NFS4ERR_DELEG_REVOKED:
5236 case -NFS4ERR_ADMIN_REVOKED:
5237 case -NFS4ERR_BAD_STATEID:
5238 case -NFS4ERR_STALE_STATEID:
5239 case -NFS4ERR_OLD_STATEID:
5240 case -NFS4ERR_OPENMODE:
5241 case -NFS4ERR_EXPIRED:
5242 return true;
5243 }
5244 return false;
5245}
5246
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005247static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005249 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005251 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005252 if (task->tk_status < 0) {
5253 struct nfs4_exception exception = {
5254 .inode = hdr->inode,
5255 .state = hdr->args.context->state,
5256 .stateid = &hdr->args.stateid,
5257 };
5258 task->tk_status = nfs4_async_handle_exception(task,
5259 server, task->tk_status, &exception);
5260 if (exception.retry) {
5261 rpc_restart_call_prepare(task);
5262 return -EAGAIN;
5263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005265
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005267 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269}
5270
Trond Myklebust5521abf2013-03-16 20:54:34 -04005271static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005272 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005273{
5274
5275 if (!nfs4_error_stateid_expired(task->tk_status) ||
5276 nfs4_stateid_is_current(&args->stateid,
5277 args->context,
5278 args->lock_context,
5279 FMODE_READ))
5280 return false;
5281 rpc_restart_call_prepare(task);
5282 return true;
5283}
5284
Anna Schumakerc5675522014-05-28 13:41:22 -04005285static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5286 struct nfs_pgio_header *hdr)
5287{
5288 struct nfs_server *server = NFS_SERVER(hdr->inode);
5289 struct rpc_message *msg = &task->tk_msg;
5290
5291 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5292 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5293 server->caps &= ~NFS_CAP_READ_PLUS;
5294 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5295 rpc_restart_call_prepare(task);
5296 return true;
5297 }
5298 return false;
5299}
5300
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005301static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005302{
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005303 dprintk("--> %s\n", __func__);
5304
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005305 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005306 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005307 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005308 return -EAGAIN;
Anna Schumakerc5675522014-05-28 13:41:22 -04005309 if (nfs4_read_plus_not_supported(task, hdr))
5310 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005311 if (task->tk_status > 0)
5312 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005313 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5314 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005315}
5316
Anna Schumaker21e31402020-12-03 15:18:39 -05005317#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005318static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5319 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005320{
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005321 /* Note: We don't use READ_PLUS with pNFS yet */
5322 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
Anna Schumakerc5675522014-05-28 13:41:22 -04005323 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
Anna Schumakerc5675522014-05-28 13:41:22 -04005324}
5325#else
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005326static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5327 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005328{
Anna Schumakerc5675522014-05-28 13:41:22 -04005329}
5330#endif /* CONFIG_NFS_V4_2 */
5331
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005332static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5333 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005335 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005336 if (!hdr->pgio_done_cb)
5337 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005338 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5339 nfs42_read_plus_support(hdr, msg);
Anna Schumakerfba83f32018-05-04 16:22:50 -04005340 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341}
5342
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005343static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5344 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005345{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005346 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005347 &hdr->args.seq_args,
5348 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005349 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005350 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005351 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5352 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005353 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005354 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005355 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005356 return -EIO;
5357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358}
5359
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005360static int nfs4_write_done_cb(struct rpc_task *task,
5361 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005363 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005364
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005365 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005366 if (task->tk_status < 0) {
5367 struct nfs4_exception exception = {
5368 .inode = hdr->inode,
5369 .state = hdr->args.context->state,
5370 .stateid = &hdr->args.stateid,
5371 };
5372 task->tk_status = nfs4_async_handle_exception(task,
5373 NFS_SERVER(inode), task->tk_status,
5374 &exception);
5375 if (exception.retry) {
5376 rpc_restart_call_prepare(task);
5377 return -EAGAIN;
5378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005380 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005381 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005382 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005383 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385}
5386
Trond Myklebust5521abf2013-03-16 20:54:34 -04005387static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005388 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005389{
5390
5391 if (!nfs4_error_stateid_expired(task->tk_status) ||
5392 nfs4_stateid_is_current(&args->stateid,
5393 args->context,
5394 args->lock_context,
5395 FMODE_WRITE))
5396 return false;
5397 rpc_restart_call_prepare(task);
5398 return true;
5399}
5400
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005401static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005402{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005403 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005404 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005405 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005406 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005407 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5408 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005409}
5410
Trond Myklebust5a37f852012-04-28 14:55:16 -04005411static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005412bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005413{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005414 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005415 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005416 return false;
5417 /* Otherwise, request attributes if and only if we don't hold
5418 * a delegation
5419 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005420 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005421}
Fred Isamana69aef12011-03-03 15:13:47 +00005422
Trond Myklebust332d1a02021-03-25 18:15:36 -04005423static void nfs4_bitmask_set(__u32 bitmask[NFS4_BITMASK_SZ], const __u32 *src,
5424 struct inode *inode, struct nfs_server *server,
5425 struct nfs4_label *label)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005426{
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005427 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
Trond Myklebust332d1a02021-03-25 18:15:36 -04005428 unsigned int i;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005429
Trond Myklebust332d1a02021-03-25 18:15:36 -04005430 memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005431
Trond Myklebust13c0b082021-03-25 21:07:21 -04005432 if (cache_validity & NFS_INO_INVALID_CHANGE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005433 bitmask[0] |= FATTR4_WORD0_CHANGE;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005434 if (cache_validity & NFS_INO_INVALID_ATIME)
5435 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
Trond Myklebust45901a22021-02-08 08:49:32 -05005436 if (cache_validity & NFS_INO_INVALID_OTHER)
5437 bitmask[1] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
5438 FATTR4_WORD1_OWNER_GROUP |
5439 FATTR4_WORD1_NUMLINKS;
5440 if (label && label->len && cache_validity & NFS_INO_INVALID_LABEL)
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005441 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust45901a22021-02-08 08:49:32 -05005442 if (cache_validity & NFS_INO_INVALID_CTIME)
5443 bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005444 if (cache_validity & NFS_INO_INVALID_MTIME)
5445 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005446 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5447 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
Trond Myklebust332d1a02021-03-25 18:15:36 -04005448
5449 if (nfs4_have_delegation(inode, FMODE_READ) &&
5450 !(cache_validity & NFS_INO_REVAL_FORCED))
5451 bitmask[0] &= ~FATTR4_WORD0_SIZE;
Trond Myklebust13c0b082021-03-25 21:07:21 -04005452 else if (cache_validity & NFS_INO_INVALID_SIZE)
Trond Myklebust332d1a02021-03-25 18:15:36 -04005453 bitmask[0] |= FATTR4_WORD0_SIZE;
5454
5455 for (i = 0; i < NFS4_BITMASK_SZ; i++)
5456 bitmask[i] &= server->attr_bitmask[i];
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005457}
5458
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005459static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005460 struct rpc_message *msg,
5461 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005463 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005464
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005465 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5466 hdr->args.bitmask = NULL;
5467 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005468 } else {
Trond Myklebust332d1a02021-03-25 18:15:36 -04005469 nfs4_bitmask_set(hdr->args.bitmask_store,
5470 server->cache_consistency_bitmask,
5471 hdr->inode, server, NULL);
5472 hdr->args.bitmask = hdr->args.bitmask_store;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005473 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005474
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005475 if (!hdr->pgio_done_cb)
5476 hdr->pgio_done_cb = nfs4_write_done_cb;
5477 hdr->res.server = server;
5478 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005480 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005481 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005482 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483}
5484
Fred Isaman0b7c0152012-04-20 14:47:39 -04005485static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5486{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005487 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005488 &data->args.seq_args,
5489 &data->res.seq_res,
5490 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491}
5492
Fred Isaman0b7c0152012-04-20 14:47:39 -04005493static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005496
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005497 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005498 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5499 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005500 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005501 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504}
5505
Fred Isaman0b7c0152012-04-20 14:47:39 -04005506static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005507{
5508 if (!nfs4_sequence_done(task, &data->res.seq_res))
5509 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005510 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005511}
5512
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005513static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5514 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005516 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005517
Fred Isaman0b7c0152012-04-20 14:47:39 -04005518 if (data->commit_done_cb == NULL)
5519 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005520 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005521 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005522 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005523 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524}
5525
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005526static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5527 struct nfs_commitres *res)
5528{
5529 struct inode *dst_inode = file_inode(dst);
5530 struct nfs_server *server = NFS_SERVER(dst_inode);
5531 struct rpc_message msg = {
5532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5533 .rpc_argp = args,
5534 .rpc_resp = res,
5535 };
5536
5537 args->fh = NFS_FH(dst_inode);
5538 return nfs4_call_sync(server->client, server, &msg,
5539 &args->seq_args, &res->seq_res, 1);
5540}
5541
5542int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5543{
5544 struct nfs_commitargs args = {
5545 .offset = offset,
5546 .count = count,
5547 };
5548 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5549 struct nfs4_exception exception = { };
5550 int status;
5551
5552 do {
5553 status = _nfs4_proc_commit(dst, &args, res);
5554 status = nfs4_handle_exception(dst_server, status, &exception);
5555 } while (exception.retry);
5556
5557 return status;
5558}
5559
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005560struct nfs4_renewdata {
5561 struct nfs_client *client;
5562 unsigned long timestamp;
5563};
5564
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565/*
5566 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5567 * standalone procedure for queueing an asynchronous RENEW.
5568 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005569static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005570{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005571 struct nfs4_renewdata *data = calldata;
5572 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005573
Elena Reshetova212bf412017-10-20 12:53:38 +03005574 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005575 nfs4_schedule_state_renewal(clp);
5576 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005577 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005578}
5579
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005580static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005582 struct nfs4_renewdata *data = calldata;
5583 struct nfs_client *clp = data->client;
5584 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005586 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005587 switch (task->tk_status) {
5588 case 0:
5589 break;
5590 case -NFS4ERR_LEASE_MOVED:
5591 nfs4_schedule_lease_moved_recovery(clp);
5592 break;
5593 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005594 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005595 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5596 return;
5597 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005598 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005599 return;
5600 }
5601 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005603 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604}
5605
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005606static const struct rpc_call_ops nfs4_renew_ops = {
5607 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005608 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005609};
5610
NeilBrowna52458b2018-12-03 11:30:31 +11005611static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612{
5613 struct rpc_message msg = {
5614 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5615 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005616 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005618 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005620 if (renew_flags == 0)
5621 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005622 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005623 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005624 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005625 if (data == NULL) {
5626 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005627 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005628 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005629 data->client = clp;
5630 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005631 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005632 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633}
5634
NeilBrowna52458b2018-12-03 11:30:31 +11005635static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636{
5637 struct rpc_message msg = {
5638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5639 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005640 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 };
5642 unsigned long now = jiffies;
5643 int status;
5644
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005645 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 if (status < 0)
5647 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005648 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 return 0;
5650}
5651
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005652static inline int nfs4_server_supports_acls(struct nfs_server *server)
5653{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005654 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005655}
5656
Trond Myklebust21f498c2012-08-24 10:59:25 -04005657/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5658 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005659 * the stack.
5660 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005661#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005662
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005663int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005664 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005665{
5666 struct page *newpage, **spages;
5667 int rc = 0;
5668 size_t len;
5669 spages = pages;
5670
5671 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005672 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005673 newpage = alloc_page(GFP_KERNEL);
5674
5675 if (newpage == NULL)
5676 goto unwind;
5677 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005678 buf += len;
5679 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005680 *pages++ = newpage;
5681 rc++;
5682 } while (buflen != 0);
5683
5684 return rc;
5685
5686unwind:
5687 for(; rc > 0; rc--)
5688 __free_page(spages[rc-1]);
5689 return -ENOMEM;
5690}
5691
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005692struct nfs4_cached_acl {
5693 int cached;
5694 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005695 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005696};
5697
5698static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005699{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005700 struct nfs_inode *nfsi = NFS_I(inode);
5701
5702 spin_lock(&inode->i_lock);
5703 kfree(nfsi->nfs4_acl);
5704 nfsi->nfs4_acl = acl;
5705 spin_unlock(&inode->i_lock);
5706}
5707
5708static void nfs4_zap_acl_attr(struct inode *inode)
5709{
5710 nfs4_set_cached_acl(inode, NULL);
5711}
5712
5713static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5714{
5715 struct nfs_inode *nfsi = NFS_I(inode);
5716 struct nfs4_cached_acl *acl;
5717 int ret = -ENOENT;
5718
5719 spin_lock(&inode->i_lock);
5720 acl = nfsi->nfs4_acl;
5721 if (acl == NULL)
5722 goto out;
5723 if (buf == NULL) /* user is just asking for length */
5724 goto out_len;
5725 if (acl->cached == 0)
5726 goto out;
5727 ret = -ERANGE; /* see getxattr(2) man page */
5728 if (acl->len > buflen)
5729 goto out;
5730 memcpy(buf, acl->data, acl->len);
5731out_len:
5732 ret = acl->len;
5733out:
5734 spin_unlock(&inode->i_lock);
5735 return ret;
5736}
5737
Sachin Prabhu5794d212012-04-17 14:36:40 +01005738static 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 +00005739{
5740 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005741 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005742
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005743 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005744 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005745 if (acl == NULL)
5746 goto out;
5747 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005748 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005749 } else {
5750 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5751 if (acl == NULL)
5752 goto out;
5753 acl->cached = 0;
5754 }
5755 acl->len = acl_len;
5756out:
5757 nfs4_set_cached_acl(inode, acl);
5758}
5759
Andy Adamsonbf118a32011-12-07 11:55:27 -05005760/*
5761 * The getxattr API returns the required buffer length when called with a
5762 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5763 * the required buf. On a NULL buf, we send a page of data to the server
5764 * guessing that the ACL request can be serviced by a page. If so, we cache
5765 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5766 * the cache. If not so, we throw away the page, and cache the required
5767 * length. The next getxattr call will then produce another round trip to
5768 * the server, this time with the input buf of the required size.
5769 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005770static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005771{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005772 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005773 struct nfs_getaclargs args = {
5774 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005775 .acl_len = buflen,
5776 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005777 struct nfs_getaclres res = {
5778 .acl_len = buflen,
5779 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005780 struct rpc_message msg = {
5781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5782 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005783 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005784 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005785 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005786 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005787 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005788
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005789 if (buflen == 0)
5790 buflen = server->rsize;
5791
5792 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5793 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5794 if (!pages)
5795 return -ENOMEM;
5796
5797 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005798
Andy Adamsonbf118a32011-12-07 11:55:27 -05005799 for (i = 0; i < npages; i++) {
5800 pages[i] = alloc_page(GFP_KERNEL);
5801 if (!pages[i])
5802 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005803 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005804
5805 /* for decoding across pages */
5806 res.acl_scratch = alloc_page(GFP_KERNEL);
5807 if (!res.acl_scratch)
5808 goto out_free;
5809
Andy Adamsonbf118a32011-12-07 11:55:27 -05005810 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005811
Peng Taode040be2012-01-10 22:42:47 +08005812 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005813 __func__, buf, buflen, npages, args.acl_len);
5814 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5815 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005816 if (ret)
5817 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005818
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005819 /* Handle the case where the passed-in buffer is too short */
5820 if (res.acl_flags & NFS4_ACL_TRUNC) {
5821 /* Did the user only issue a request for the acl length? */
5822 if (buf == NULL)
5823 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005824 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005825 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005826 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005827 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005828 if (buf) {
5829 if (res.acl_len > buflen) {
5830 ret = -ERANGE;
5831 goto out_free;
5832 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005833 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005834 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005835out_ok:
5836 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005837out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005838 for (i = 0; i < npages; i++)
5839 if (pages[i])
5840 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005841 if (res.acl_scratch)
5842 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005843 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005844 return ret;
5845}
5846
Trond Myklebust16b42892006-08-24 12:27:15 -04005847static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5848{
Trond Myklebust0688e642019-04-07 13:59:09 -04005849 struct nfs4_exception exception = {
5850 .interruptible = true,
5851 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005852 ssize_t ret;
5853 do {
5854 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005855 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005856 if (ret >= 0)
5857 break;
5858 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5859 } while (exception.retry);
5860 return ret;
5861}
5862
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005863static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5864{
5865 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005866 int ret;
5867
5868 if (!nfs4_server_supports_acls(server))
5869 return -EOPNOTSUPP;
Trond Myklebust1f3208b2021-03-25 11:04:34 -04005870 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005871 if (ret < 0)
5872 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005873 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5874 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005875 ret = nfs4_read_cached_acl(inode, buf, buflen);
5876 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005877 /* -ENOENT is returned if there is no ACL or if there is an ACL
5878 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005879 return ret;
5880 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005881}
5882
Trond Myklebust16b42892006-08-24 12:27:15 -04005883static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005884{
5885 struct nfs_server *server = NFS_SERVER(inode);
5886 struct page *pages[NFS4ACL_MAXPAGES];
5887 struct nfs_setaclargs arg = {
5888 .fh = NFS_FH(inode),
5889 .acl_pages = pages,
5890 .acl_len = buflen,
5891 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005892 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005893 struct rpc_message msg = {
5894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5895 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005896 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005897 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005898 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005899 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005900
J. Bruce Fields4f8be1f2021-01-28 17:36:38 -05005901 /* You can't remove system.nfs4_acl: */
5902 if (buflen == 0)
5903 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005904 if (!nfs4_server_supports_acls(server))
5905 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005906 if (npages > ARRAY_SIZE(pages))
5907 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005908 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005909 if (i < 0)
5910 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005911 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005912 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005913
5914 /*
5915 * Free each page after tx, so the only ref left is
5916 * held by the network stack
5917 */
5918 for (; i > 0; i--)
5919 put_page(pages[i-1]);
5920
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005921 /*
5922 * Acl update can result in inode attribute update.
5923 * so mark the attribute cache invalid.
5924 */
5925 spin_lock(&inode->i_lock);
Trond Myklebustb6f80a22021-03-08 14:42:55 -05005926 nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
5927 NFS_INO_INVALID_CTIME |
5928 NFS_INO_REVAL_FORCED);
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005929 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005930 nfs_access_zap_cache(inode);
5931 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005932 return ret;
5933}
5934
Trond Myklebust16b42892006-08-24 12:27:15 -04005935static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5936{
5937 struct nfs4_exception exception = { };
5938 int err;
5939 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005940 err = __nfs4_proc_set_acl(inode, buf, buflen);
5941 trace_nfs4_set_acl(inode, err);
5942 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005943 &exception);
5944 } while (exception.retry);
5945 return err;
5946}
5947
David Quigleyaa9c2662013-05-22 12:50:44 -04005948#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5949static int _nfs4_get_security_label(struct inode *inode, void *buf,
5950 size_t buflen)
5951{
5952 struct nfs_server *server = NFS_SERVER(inode);
5953 struct nfs_fattr fattr;
5954 struct nfs4_label label = {0, 0, buflen, buf};
5955
5956 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005957 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005958 .fh = NFS_FH(inode),
5959 .bitmask = bitmask,
5960 };
5961 struct nfs4_getattr_res res = {
5962 .fattr = &fattr,
5963 .label = &label,
5964 .server = server,
5965 };
5966 struct rpc_message msg = {
5967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005968 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005969 .rpc_resp = &res,
5970 };
5971 int ret;
5972
5973 nfs_fattr_init(&fattr);
5974
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005975 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005976 if (ret)
5977 return ret;
5978 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5979 return -ENOENT;
Ondrej Mosnacek53cb2452021-01-15 18:43:56 +01005980 return label.len;
David Quigleyaa9c2662013-05-22 12:50:44 -04005981}
5982
5983static int nfs4_get_security_label(struct inode *inode, void *buf,
5984 size_t buflen)
5985{
Trond Myklebust0688e642019-04-07 13:59:09 -04005986 struct nfs4_exception exception = {
5987 .interruptible = true,
5988 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005989 int err;
5990
5991 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5992 return -EOPNOTSUPP;
5993
5994 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005995 err = _nfs4_get_security_label(inode, buf, buflen);
5996 trace_nfs4_get_security_label(inode, err);
5997 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005998 &exception);
5999 } while (exception.retry);
6000 return err;
6001}
6002
6003static int _nfs4_do_set_security_label(struct inode *inode,
6004 struct nfs4_label *ilabel,
6005 struct nfs_fattr *fattr,
6006 struct nfs4_label *olabel)
6007{
6008
6009 struct iattr sattr = {0};
6010 struct nfs_server *server = NFS_SERVER(inode);
6011 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04006012 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006013 .fh = NFS_FH(inode),
6014 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006015 .server = server,
6016 .bitmask = bitmask,
6017 .label = ilabel,
6018 };
6019 struct nfs_setattrres res = {
6020 .fattr = fattr,
6021 .label = olabel,
6022 .server = server,
6023 };
6024 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6026 .rpc_argp = &arg,
6027 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04006028 };
6029 int status;
6030
Jeff Layton12207f62013-11-01 10:49:32 -04006031 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04006032
Jeff Layton12207f62013-11-01 10:49:32 -04006033 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04006034 if (status)
6035 dprintk("%s failed: %d\n", __func__, status);
6036
6037 return status;
6038}
6039
6040static int nfs4_do_set_security_label(struct inode *inode,
6041 struct nfs4_label *ilabel,
6042 struct nfs_fattr *fattr,
6043 struct nfs4_label *olabel)
6044{
6045 struct nfs4_exception exception = { };
6046 int err;
6047
6048 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006049 err = _nfs4_do_set_security_label(inode, ilabel,
6050 fattr, olabel);
6051 trace_nfs4_set_security_label(inode, err);
6052 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006053 &exception);
6054 } while (exception.retry);
6055 return err;
6056}
6057
6058static int
Al Viro59301222016-05-27 10:19:30 -04006059nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006060{
6061 struct nfs4_label ilabel, *olabel = NULL;
6062 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006063 int status;
6064
6065 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6066 return -EOPNOTSUPP;
6067
6068 nfs_fattr_init(&fattr);
6069
6070 ilabel.pi = 0;
6071 ilabel.lfs = 0;
6072 ilabel.label = (char *)buf;
6073 ilabel.len = buflen;
6074
David Quigleyaa9c2662013-05-22 12:50:44 -04006075 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6076 if (IS_ERR(olabel)) {
6077 status = -PTR_ERR(olabel);
6078 goto out;
6079 }
6080
6081 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6082 if (status == 0)
6083 nfs_setsecurity(inode, &fattr, olabel);
6084
6085 nfs4_label_free(olabel);
6086out:
David Quigleyaa9c2662013-05-22 12:50:44 -04006087 return status;
6088}
6089#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6090
6091
Chuck Leverf0920752012-05-21 22:45:41 -04006092static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6093 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006094{
6095 __be32 verf[2];
6096
Chuck Lever2c820d92012-05-21 22:45:33 -04006097 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6098 /* An impossible timestamp guarantees this value
6099 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006100 verf[0] = cpu_to_be32(U32_MAX);
6101 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006102 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006103 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006104 u64 ns = ktime_to_ns(nn->boot_time);
6105
6106 verf[0] = cpu_to_be32(ns >> 32);
6107 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006108 }
Chuck Levercd937102012-03-02 17:14:31 -05006109 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6110}
6111
Trond Myklebust1aee5512020-10-07 18:24:17 -04006112static size_t
Trond Myklebust39d43d12020-10-07 18:24:18 -04006113nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
Trond Myklebust1aee5512020-10-07 18:24:17 -04006114{
Trond Myklebust39d43d12020-10-07 18:24:18 -04006115 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6116 struct nfs_netns_client *nn_clp = nn->nfs_client;
6117 const char *id;
6118
Trond Myklebust1aee5512020-10-07 18:24:17 -04006119 buf[0] = '\0';
6120
Trond Myklebust39d43d12020-10-07 18:24:18 -04006121 if (nn_clp) {
6122 rcu_read_lock();
6123 id = rcu_dereference(nn_clp->identifier);
6124 if (id)
6125 strscpy(buf, id, buflen);
6126 rcu_read_unlock();
6127 }
6128
6129 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
Trond Myklebust1aee5512020-10-07 18:24:17 -04006130 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6131
6132 return strlen(buf);
6133}
6134
Jeff Laytona3192682015-06-09 19:43:59 -04006135static int
6136nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006137{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006138 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6139 size_t buflen;
Jeff Laytona3192682015-06-09 19:43:59 -04006140 size_t len;
6141 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006142
Trond Myklebustceb3a162015-01-03 15:16:04 -05006143 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006144 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006145
Jeff Laytona3192682015-06-09 19:43:59 -04006146 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006147 len = 14 +
6148 strlen(clp->cl_rpcclient->cl_nodename) +
6149 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006150 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006151 1;
6152 rcu_read_unlock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006153
Trond Myklebust39d43d12020-10-07 18:24:18 -04006154 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006155 if (buflen)
6156 len += buflen + 1;
6157
Jeff Laytona3192682015-06-09 19:43:59 -04006158 if (len > NFS4_OPAQUE_LIMIT + 1)
6159 return -EINVAL;
6160
6161 /*
6162 * Since this string is allocated at mount time, and held until the
6163 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6164 * about a memory-reclaim deadlock.
6165 */
6166 str = kmalloc(len, GFP_KERNEL);
6167 if (!str)
6168 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006169
Chuck Levere984a552012-09-14 17:24:21 -04006170 rcu_read_lock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006171 if (buflen)
Chuck Lever025bb9f2018-06-04 10:53:34 -04006172 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Trond Myklebust1aee5512020-10-07 18:24:17 -04006173 clp->cl_rpcclient->cl_nodename, buf,
Chuck Lever848a4eb2018-06-04 10:53:29 -04006174 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006175 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006176 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006177 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006178 clp->cl_rpcclient->cl_nodename,
6179 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006180 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006181 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006182
Jeff Laytona3192682015-06-09 19:43:59 -04006183 clp->cl_owner_id = str;
6184 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006185}
6186
Jeff Layton873e3852015-06-09 19:44:00 -04006187static int
Jeff Layton873e3852015-06-09 19:44:00 -04006188nfs4_init_uniform_client_string(struct nfs_client *clp)
6189{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006190 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6191 size_t buflen;
Jeff Layton873e3852015-06-09 19:44:00 -04006192 size_t len;
6193 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006194
6195 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006196 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006197
Jeff Layton873e3852015-06-09 19:44:00 -04006198 len = 10 + 10 + 1 + 10 + 1 +
6199 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6200
Trond Myklebust39d43d12020-10-07 18:24:18 -04006201 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006202 if (buflen)
6203 len += buflen + 1;
6204
Jeff Layton873e3852015-06-09 19:44:00 -04006205 if (len > NFS4_OPAQUE_LIMIT + 1)
6206 return -EINVAL;
6207
6208 /*
6209 * Since this string is allocated at mount time, and held until the
6210 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6211 * about a memory-reclaim deadlock.
6212 */
6213 str = kmalloc(len, GFP_KERNEL);
6214 if (!str)
6215 return -ENOMEM;
6216
Trond Myklebust1aee5512020-10-07 18:24:17 -04006217 if (buflen)
6218 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6219 clp->rpc_ops->version, clp->cl_minorversion,
6220 buf, clp->cl_rpcclient->cl_nodename);
6221 else
6222 scnprintf(str, len, "Linux NFSv%u.%u %s",
6223 clp->rpc_ops->version, clp->cl_minorversion,
6224 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006225 clp->cl_owner_id = str;
6226 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006227}
6228
Chuck Lever706cb8d2014-03-12 12:51:47 -04006229/*
6230 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6231 * services. Advertise one based on the address family of the
6232 * clientaddr.
6233 */
6234static unsigned int
6235nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6236{
6237 if (strchr(clp->cl_ipaddr, ':') != NULL)
6238 return scnprintf(buf, len, "tcp6");
6239 else
6240 return scnprintf(buf, len, "tcp");
6241}
6242
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006243static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6244{
6245 struct nfs4_setclientid *sc = calldata;
6246
6247 if (task->tk_status == 0)
6248 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6249}
6250
6251static const struct rpc_call_ops nfs4_setclientid_ops = {
6252 .rpc_call_done = nfs4_setclientid_done,
6253};
6254
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006255/**
6256 * nfs4_proc_setclientid - Negotiate client ID
6257 * @clp: state data structure
6258 * @program: RPC program for NFSv4 callback service
6259 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006260 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006261 * @res: where to place the result
6262 *
6263 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6264 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006265int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006266 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006267 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268{
6269 nfs4_verifier sc_verifier;
6270 struct nfs4_setclientid setclientid = {
6271 .sc_verifier = &sc_verifier,
6272 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006273 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 };
6275 struct rpc_message msg = {
6276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6277 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006278 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006279 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006281 struct rpc_task_setup task_setup_data = {
6282 .rpc_client = clp->cl_rpcclient,
6283 .rpc_message = &msg,
6284 .callback_ops = &nfs4_setclientid_ops,
6285 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006286 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006287 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006288 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006289 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
Chuck Leverde734832012-07-11 16:30:50 -04006291 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006292 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006293
6294 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6295 status = nfs4_init_uniform_client_string(clp);
6296 else
Jeff Laytona3192682015-06-09 19:43:59 -04006297 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006298
6299 if (status)
6300 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006301
Chuck Leverde734832012-07-11 16:30:50 -04006302 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006303 setclientid.sc_netid_len =
6304 nfs4_init_callback_netid(clp,
6305 setclientid.sc_netid,
6306 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006307 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006308 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 clp->cl_ipaddr, port >> 8, port & 255);
6310
Jeff Layton3a6bb732015-06-09 19:43:57 -04006311 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006312 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006313 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006314
6315 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006316 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006317 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006318 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6319 put_rpccred(setclientid.sc_cred);
6320 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006321
6322 if (status == 0)
6323 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006324out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006325 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006326 dprintk("NFS reply setclientid: %d\n", status);
6327 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328}
6329
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006330/**
6331 * nfs4_proc_setclientid_confirm - Confirm client ID
6332 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006333 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006334 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006335 *
6336 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6337 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006338int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006339 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006340 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 struct rpc_message msg = {
6343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006344 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006345 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 int status;
6348
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006349 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6350 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6351 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006352 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6353 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006354 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006355 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 return status;
6357}
6358
Trond Myklebustfe650402006-01-03 09:55:18 +01006359struct nfs4_delegreturndata {
6360 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006361 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006362 struct nfs_fh fh;
6363 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006364 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006365 struct {
6366 struct nfs4_layoutreturn_args arg;
6367 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006368 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006369 u32 roc_barrier;
6370 bool roc;
6371 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006372 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006373 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006374 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006375};
6376
Trond Myklebustfe650402006-01-03 09:55:18 +01006377static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6378{
6379 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006380 struct nfs4_exception exception = {
6381 .inode = data->inode,
6382 .stateid = &data->stateid,
6383 };
Andy Adamson938e1012009-04-01 09:22:28 -04006384
Trond Myklebust14516c32010-07-31 14:29:06 -04006385 if (!nfs4_sequence_done(task, &data->res.seq_res))
6386 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006387
Trond Myklebustca8acf82013-08-13 10:36:56 -04006388 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006389
6390 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05006391 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6392 &data->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04006393 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006394
Ricardo Labiaga79708862009-12-07 09:23:21 -05006395 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006396 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006397 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006398 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006399 case -NFS4ERR_ADMIN_REVOKED:
6400 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006401 case -NFS4ERR_EXPIRED:
6402 nfs4_free_revoked_stateid(data->res.server,
6403 data->args.stateid,
6404 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006405 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006406 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006407 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006408 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006409 task->tk_status = 0;
6410 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006411 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006412 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6413 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006414 if (data->args.bitmask) {
6415 data->args.bitmask = NULL;
6416 data->res.fattr = NULL;
6417 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006418 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006419 case -NFS4ERR_ACCESS:
6420 if (data->args.bitmask) {
6421 data->args.bitmask = NULL;
6422 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006423 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006424 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006425 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006426 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006427 task->tk_status = nfs4_async_handle_exception(task,
6428 data->res.server, task->tk_status,
6429 &exception);
6430 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006431 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006432 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006433 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006434 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006435 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006436out_restart:
6437 task->tk_status = 0;
6438 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006439}
6440
6441static void nfs4_delegreturn_release(void *calldata)
6442{
Peng Tao039b7562014-07-03 13:05:02 +08006443 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006444 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006445
Trond Myklebust078000d2021-01-04 13:18:03 -05006446 if (data->lr.roc)
6447 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6448 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006449 if (inode) {
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006450 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006451 nfs_iput_and_deactive(inode);
6452 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006453 kfree(calldata);
6454}
6455
Andy Adamson938e1012009-04-01 09:22:28 -04006456static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6457{
6458 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006459 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006460
6461 d_data = (struct nfs4_delegreturndata *)data;
6462
Trond Myklebust5326de92019-11-13 09:39:36 +01006463 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6464 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006465 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006466 }
Peng Tao500d7012015-09-22 11:35:22 +08006467
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006468 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6469 if (lo && !pnfs_layout_is_valid(lo)) {
6470 d_data->args.lr_args = NULL;
6471 d_data->res.lr_res = NULL;
6472 }
6473
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006474 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006475 &d_data->args.seq_args,
6476 &d_data->res.seq_res,
6477 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006478}
Andy Adamson938e1012009-04-01 09:22:28 -04006479
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006480static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006481 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006482 .rpc_call_done = nfs4_delegreturn_done,
6483 .rpc_release = nfs4_delegreturn_release,
6484};
6485
NeilBrowna52458b2018-12-03 11:30:31 +11006486static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006487{
6488 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006489 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006490 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006491 struct rpc_message msg = {
6492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6493 .rpc_cred = cred,
6494 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006495 struct rpc_task_setup task_setup_data = {
6496 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006497 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006498 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebustf304a802020-05-11 10:42:04 -04006499 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006500 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006501 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006502
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006503 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006504 if (data == NULL)
6505 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006506 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006507
6508 nfs4_state_protect(server->nfs_client,
6509 NFS_SP4_MACH_CRED_CLEANUP,
6510 &task_setup_data.rpc_client, &msg);
6511
Trond Myklebustfe650402006-01-03 09:55:18 +01006512 data->args.fhandle = &data->fh;
6513 data->args.stateid = &data->stateid;
Trond Myklebust332d1a02021-03-25 18:15:36 -04006514 nfs4_bitmask_set(data->args.bitmask_store,
6515 server->cache_consistency_bitmask, inode, server,
6516 NULL);
6517 data->args.bitmask = data->args.bitmask_store;
Trond Myklebustfe650402006-01-03 09:55:18 +01006518 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006519 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006520 data->res.fattr = &data->fattr;
6521 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006522 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006523 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006524 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006525 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006526 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006527 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust078000d2021-01-04 13:18:03 -05006528 if (data->inode || issync) {
6529 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6530 cred);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006531 if (data->lr.roc) {
6532 data->args.lr_args = &data->lr.arg;
6533 data->res.lr_res = &data->lr.res;
6534 }
6535 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006536
Trond Myklebustc970aa82007-07-14 15:39:59 -04006537 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006538 msg.rpc_argp = &data->args;
6539 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006540 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006541 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006542 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006543 if (!issync)
6544 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006545 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006546 if (status != 0)
6547 goto out;
6548 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006549out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006550 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006551 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552}
6553
NeilBrowna52458b2018-12-03 11:30:31 +11006554int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555{
6556 struct nfs_server *server = NFS_SERVER(inode);
6557 struct nfs4_exception exception = { };
6558 int err;
6559 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006560 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006561 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 switch (err) {
6563 case -NFS4ERR_STALE_STATEID:
6564 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565 case 0:
6566 return 0;
6567 }
6568 err = nfs4_handle_exception(server, err, &exception);
6569 } while (exception.retry);
6570 return err;
6571}
6572
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6574{
6575 struct inode *inode = state->inode;
6576 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006577 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006578 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006580 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006582 struct nfs_lockt_res res = {
6583 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 };
6585 struct rpc_message msg = {
6586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006587 .rpc_argp = &arg,
6588 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 .rpc_cred = state->owner->so_cred,
6590 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 struct nfs4_lock_state *lsp;
6592 int status;
6593
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006594 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006595 status = nfs4_set_lock_state(state, request);
6596 if (status != 0)
6597 goto out;
6598 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006599 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006600 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006601 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006602 switch (status) {
6603 case 0:
6604 request->fl_type = F_UNLCK;
6605 break;
6606 case -NFS4ERR_DENIED:
6607 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006609 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006610 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006611out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 return status;
6613}
6614
6615static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6616{
Trond Myklebust0688e642019-04-07 13:59:09 -04006617 struct nfs4_exception exception = {
6618 .interruptible = true,
6619 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 int err;
6621
6622 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006623 err = _nfs4_proc_getlk(state, cmd, request);
6624 trace_nfs4_get_lock(request, state, cmd, err);
6625 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 &exception);
6627 } while (exception.retry);
6628 return err;
6629}
6630
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006631/*
6632 * Update the seqid of a lock stateid after receiving
6633 * NFS4ERR_OLD_STATEID
6634 */
6635static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6636 struct nfs4_lock_state *lsp)
6637{
6638 struct nfs4_state *state = lsp->ls_state;
6639 bool ret = false;
6640
6641 spin_lock(&state->state_lock);
6642 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6643 goto out;
6644 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6645 nfs4_stateid_seqid_inc(dst);
6646 else
6647 dst->seqid = lsp->ls_stateid.seqid;
6648 ret = true;
6649out:
6650 spin_unlock(&state->state_lock);
6651 return ret;
6652}
6653
6654static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6655 struct nfs4_lock_state *lsp)
6656{
6657 struct nfs4_state *state = lsp->ls_state;
6658 bool ret;
6659
6660 spin_lock(&state->state_lock);
6661 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6662 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6663 spin_unlock(&state->state_lock);
6664 return ret;
6665}
6666
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006667struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006668 struct nfs_locku_args arg;
6669 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006670 struct nfs4_lock_state *lsp;
6671 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006672 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006673 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006674 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006675 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006676};
6677
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006678static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6679 struct nfs_open_context *ctx,
6680 struct nfs4_lock_state *lsp,
6681 struct nfs_seqid *seqid)
6682{
6683 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006684 struct nfs4_state *state = lsp->ls_state;
6685 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006686
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006687 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006688 if (p == NULL)
6689 return NULL;
6690 p->arg.fh = NFS_FH(inode);
6691 p->arg.fl = &p->fl;
6692 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006693 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006694 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006695 /* Ensure we don't close file until we're done freeing locks! */
6696 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006697 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006698 locks_init_lock(&p->fl);
6699 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006700 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006701 spin_lock(&state->state_lock);
6702 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6703 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006704 return p;
6705}
6706
Trond Myklebust06f814a2006-01-03 09:55:07 +01006707static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006708{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006709 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006710 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006711 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006712 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006713 put_nfs_open_context(calldata->ctx);
6714 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006715}
6716
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006717static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006718{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006719 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006720 struct nfs4_exception exception = {
6721 .inode = calldata->lsp->ls_state->inode,
6722 .stateid = &calldata->arg.stateid,
6723 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006724
Trond Myklebust14516c32010-07-31 14:29:06 -04006725 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6726 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006727 switch (task->tk_status) {
6728 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006729 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006730 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006731 if (nfs4_update_lock_stateid(calldata->lsp,
6732 &calldata->res.stateid))
6733 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006734 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006735 case -NFS4ERR_ADMIN_REVOKED:
6736 case -NFS4ERR_EXPIRED:
6737 nfs4_free_revoked_stateid(calldata->server,
6738 &calldata->arg.stateid,
6739 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006740 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006741 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006742 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006743 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6744 calldata->lsp))
6745 rpc_restart_call_prepare(task);
6746 break;
6747 case -NFS4ERR_OLD_STATEID:
6748 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6749 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006750 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006751 break;
6752 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006753 task->tk_status = nfs4_async_handle_exception(task,
6754 calldata->server, task->tk_status,
6755 &exception);
6756 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006757 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006758 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006759 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006760}
6761
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006762static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006763{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006764 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006766 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6767 nfs_async_iocounter_wait(task, calldata->l_ctx))
6768 return;
6769
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006770 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006771 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006772 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006773 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006774 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006775 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006776 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006777 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006778 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006779 &calldata->res.seq_res,
6780 task) != 0)
6781 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006782 return;
6783out_no_action:
6784 task->tk_action = NULL;
6785out_wait:
6786 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787}
6788
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006789static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006790 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006791 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006792 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006793};
6794
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006795static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6796 struct nfs_open_context *ctx,
6797 struct nfs4_lock_state *lsp,
6798 struct nfs_seqid *seqid)
6799{
6800 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006801 struct rpc_message msg = {
6802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6803 .rpc_cred = ctx->cred,
6804 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006805 struct rpc_task_setup task_setup_data = {
6806 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006807 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006808 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006809 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006810 .flags = RPC_TASK_ASYNC,
6811 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006812
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006813 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6814 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6815
Frank Filz137d6ac2007-07-09 15:32:29 -07006816 /* Ensure this is an unlock - when canceling a lock, the
6817 * canceled lock is passed in, and it won't be an unlock.
6818 */
6819 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006820 if (fl->fl_flags & FL_CLOSE)
6821 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006822
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006823 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6824 if (data == NULL) {
6825 nfs_free_seqid(seqid);
6826 return ERR_PTR(-ENOMEM);
6827 }
6828
Anna Schumakerfba83f32018-05-04 16:22:50 -04006829 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006830 msg.rpc_argp = &data->arg;
6831 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006832 task_setup_data.callback_data = data;
6833 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006834}
6835
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6837{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006838 struct inode *inode = state->inode;
6839 struct nfs4_state_owner *sp = state->owner;
6840 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006841 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006842 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006843 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006844 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006845 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006846 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847
Trond Myklebust9b073572006-06-29 16:38:34 -04006848 status = nfs4_set_lock_state(state, request);
6849 /* Unlock _before_ we do the RPC call */
6850 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006851 /* Exclude nfs_delegation_claim_locks() */
6852 mutex_lock(&sp->so_delegreturn_mutex);
6853 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006854 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006855 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006856 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006857 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006858 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006859 }
6860 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006861 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006862 if (status != 0)
6863 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006864 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006865 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006866 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6867 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006868 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6869 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006870 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006871 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006872 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006873 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006874 status = PTR_ERR(task);
6875 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006876 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006877 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006878 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006879out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006880 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006881 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006882 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883}
6884
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006885struct nfs4_lockdata {
6886 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006887 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006888 struct nfs4_lock_state *lsp;
6889 struct nfs_open_context *ctx;
6890 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006891 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006892 int rpc_status;
6893 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006894 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006895};
6896
6897static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006898 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6899 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006900{
6901 struct nfs4_lockdata *p;
6902 struct inode *inode = lsp->ls_state->inode;
6903 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006904 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006905
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006906 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006907 if (p == NULL)
6908 return NULL;
6909
6910 p->arg.fh = NFS_FH(inode);
6911 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006912 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006913 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006914 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006915 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6916 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006917 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006918 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006919 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006920 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006921 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006922 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006923 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006924 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006925 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006926 locks_init_lock(&p->fl);
6927 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006928 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006929out_free_seqid:
6930 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006931out_free:
6932 kfree(p);
6933 return NULL;
6934}
6935
6936static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6937{
6938 struct nfs4_lockdata *data = calldata;
6939 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940
Harvey Harrison3110ff82008-05-02 13:42:44 -07006941 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006942 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006943 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006944 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006945 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006946 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006947 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006948 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006949 nfs4_stateid_copy(&data->arg.open_stateid,
6950 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006951 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006952 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006953 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006954 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006955 nfs4_stateid_copy(&data->arg.lock_stateid,
6956 &data->lsp->ls_stateid);
6957 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006958 if (!nfs4_valid_open_stateid(state)) {
6959 data->rpc_status = -EBADF;
6960 task->tk_action = NULL;
6961 goto out_release_open_seqid;
6962 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006963 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006964 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006965 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006966 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006967 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006968 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006969out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006970 nfs_release_seqid(data->arg.open_seqid);
6971out_release_lock_seqid:
6972 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006973out_wait:
6974 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006975 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006976}
6977
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006978static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6979{
6980 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006981 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006982
Harvey Harrison3110ff82008-05-02 13:42:44 -07006983 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006984
Trond Myklebust14516c32010-07-31 14:29:06 -04006985 if (!nfs4_sequence_done(task, &data->res.seq_res))
6986 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006987
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006988 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006989 switch (task->tk_status) {
6990 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006991 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006992 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006993 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006994 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006995 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006996 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006997 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006998 if (data->arg.new_lock_owner != 0) {
6999 nfs_confirm_seqid(&lsp->ls_seqid, 0);
7000 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7001 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007002 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7003 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007004 break;
7005 case -NFS4ERR_BAD_STATEID:
7006 case -NFS4ERR_OLD_STATEID:
7007 case -NFS4ERR_STALE_STATEID:
7008 case -NFS4ERR_EXPIRED:
7009 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007010 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007011 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007012 goto out_restart;
7013 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007014 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007015 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007016 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007017out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07007018 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007019 return;
7020out_restart:
7021 if (!data->cancelled)
7022 rpc_restart_call_prepare(task);
7023 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007024}
7025
7026static void nfs4_lock_release(void *calldata)
7027{
7028 struct nfs4_lockdata *data = calldata;
7029
Harvey Harrison3110ff82008-05-02 13:42:44 -07007030 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007031 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007032 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007033 struct rpc_task *task;
7034 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7035 data->arg.lock_seqid);
7036 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08007037 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007038 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007039 } else
7040 nfs_free_seqid(data->arg.lock_seqid);
7041 nfs4_put_lock_state(data->lsp);
7042 put_nfs_open_context(data->ctx);
7043 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007044 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007045}
7046
7047static const struct rpc_call_ops nfs4_lock_ops = {
7048 .rpc_call_prepare = nfs4_lock_prepare,
7049 .rpc_call_done = nfs4_lock_done,
7050 .rpc_release = nfs4_lock_release,
7051};
7052
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007053static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7054{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007055 switch (error) {
7056 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007057 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007058 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007059 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007060 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007061 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007062 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007063 break;
7064 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007065 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007066 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007067 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007068}
7069
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007070static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007071{
7072 struct nfs4_lockdata *data;
7073 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007074 struct rpc_message msg = {
7075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7076 .rpc_cred = state->owner->so_cred,
7077 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007078 struct rpc_task_setup task_setup_data = {
7079 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007080 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007081 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007082 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007083 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007084 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007085 int ret;
7086
Harvey Harrison3110ff82008-05-02 13:42:44 -07007087 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007088 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007089 fl->fl_u.nfs4_fl.owner,
7090 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007091 if (data == NULL)
7092 return -ENOMEM;
7093 if (IS_SETLKW(cmd))
7094 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007095 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7096 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007097 msg.rpc_argp = &data->arg;
7098 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007099 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007100 if (recovery_type > NFS_LOCK_NEW) {
7101 if (recovery_type == NFS_LOCK_RECLAIM)
7102 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007103 } else
7104 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007105 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007106 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007107 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007108 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007109 if (ret == 0) {
7110 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007111 if (ret)
7112 nfs4_handle_setlk_error(data->server, data->lsp,
7113 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007114 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007115 data->cancelled = true;
Dave Wysochanski3d1a90a2020-12-11 05:12:51 -05007116 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007117 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007118 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007119 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120}
7121
7122static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7123{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007124 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007125 struct nfs4_exception exception = {
7126 .inode = state->inode,
7127 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007128 int err;
7129
7130 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007131 /* Cache the lock if possible... */
7132 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7133 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007134 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007135 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007136 break;
7137 nfs4_handle_exception(server, err, &exception);
7138 } while (exception.retry);
7139 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140}
7141
7142static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7143{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007144 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007145 struct nfs4_exception exception = {
7146 .inode = state->inode,
7147 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007148 int err;
7149
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007150 err = nfs4_set_lock_state(state, request);
7151 if (err != 0)
7152 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007153 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007154 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7155 return 0;
7156 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007157 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007158 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7159 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007160 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007161 switch (err) {
7162 default:
7163 goto out;
7164 case -NFS4ERR_GRACE:
7165 case -NFS4ERR_DELAY:
7166 nfs4_handle_exception(server, err, &exception);
7167 err = 0;
7168 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007169 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007170out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007171 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172}
7173
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007174#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007175static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7176{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007177 struct nfs4_lock_state *lsp;
7178 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007179
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007180 status = nfs4_set_lock_state(state, request);
7181 if (status != 0)
7182 return status;
7183 lsp = request->fl_u.nfs4_fl.owner;
7184 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7185 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7186 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007187 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007188}
7189#endif
7190
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7192{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007193 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007194 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007195 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007196 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197
Trond Myklebust01c3b862006-06-29 16:38:39 -04007198 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007199 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007200 if (status < 0)
7201 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007202 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007203 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007204 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007205 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007206 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007207 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007208 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007209 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007210 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007211 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007212 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007213 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007214 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007215 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007216out:
7217 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 return status;
7219}
7220
7221static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7222{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007223 struct nfs4_exception exception = {
7224 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007225 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007226 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007227 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 int err;
7229
7230 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007231 err = _nfs4_proc_setlk(state, cmd, request);
7232 if (err == -NFS4ERR_DENIED)
7233 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007235 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 } while (exception.retry);
7237 return err;
7238}
7239
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007240#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7241#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7242
7243static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007244nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7245 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007246{
7247 int status = -ERESTARTSYS;
7248 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7249
7250 while(!signalled()) {
7251 status = nfs4_proc_setlk(state, cmd, request);
7252 if ((status != -EAGAIN) || IS_SETLK(cmd))
7253 break;
7254 freezable_schedule_timeout_interruptible(timeout);
7255 timeout *= 2;
7256 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7257 status = -ERESTARTSYS;
7258 }
7259 return status;
7260}
7261
Jeff Laytona1d617d82016-09-17 18:17:39 -04007262#ifdef CONFIG_NFS_V4_1
7263struct nfs4_lock_waiter {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007264 struct inode *inode;
Trond Myklebust86438182021-02-02 14:22:51 -05007265 struct nfs_lowner owner;
7266 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007267};
7268
7269static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007270nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007271{
Trond Myklebust86438182021-02-02 14:22:51 -05007272 struct nfs4_lock_waiter *waiter =
7273 container_of(wait, struct nfs4_lock_waiter, wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007274
Jeff Layton57174592018-03-18 08:37:03 -04007275 /* NULL key means to wake up everyone */
7276 if (key) {
7277 struct cb_notify_lock_args *cbnl = key;
7278 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
Trond Myklebust86438182021-02-02 14:22:51 -05007279 *wowner = &waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007280
Jeff Layton57174592018-03-18 08:37:03 -04007281 /* Only wake if the callback was for the same owner. */
7282 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7283 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007284
Jeff Layton57174592018-03-18 08:37:03 -04007285 /* Make sure it's for the right inode */
7286 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7287 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007288 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007289
Trond Myklebust86438182021-02-02 14:22:51 -05007290 return woken_wake_function(wait, mode, flags, key);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007291}
7292
7293static int
7294nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7295{
Jeff Laytona1d617d82016-09-17 18:17:39 -04007296 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7297 struct nfs_server *server = NFS_SERVER(state->inode);
7298 struct nfs_client *clp = server->nfs_client;
7299 wait_queue_head_t *q = &clp->cl_lock_waitq;
Trond Myklebust86438182021-02-02 14:22:51 -05007300 struct nfs4_lock_waiter waiter = {
7301 .inode = state->inode,
7302 .owner = { .clientid = clp->cl_clientid,
7303 .id = lsp->ls_seqid.owner_id,
7304 .s_dev = server->s_dev },
7305 };
7306 int status;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007307
7308 /* Don't bother with waitqueue if we don't expect a callback */
7309 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7310 return nfs4_retry_setlk_simple(state, cmd, request);
7311
Trond Myklebust86438182021-02-02 14:22:51 -05007312 init_wait(&waiter.wait);
7313 waiter.wait.func = nfs4_wake_lock_waiter;
7314 add_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007315
Trond Myklebust86438182021-02-02 14:22:51 -05007316 do {
Jeff Laytona1d617d82016-09-17 18:17:39 -04007317 status = nfs4_proc_setlk(state, cmd, request);
Trond Myklebust86438182021-02-02 14:22:51 -05007318 if (status != -EAGAIN || IS_SETLK(cmd))
Jeff Laytona1d617d82016-09-17 18:17:39 -04007319 break;
7320
7321 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007322 freezer_do_not_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007323 wait_woken(&waiter.wait, TASK_INTERRUPTIBLE,
7324 NFS4_LOCK_MAXTIMEOUT);
Yihao Wu52b042a2019-05-22 01:57:10 +08007325 freezer_count();
Trond Myklebust86438182021-02-02 14:22:51 -05007326 } while (!signalled());
7327
7328 remove_wait_queue(q, &waiter.wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007329
Jeff Laytona1d617d82016-09-17 18:17:39 -04007330 return status;
7331}
7332#else /* !CONFIG_NFS_V4_1 */
7333static inline int
7334nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7335{
7336 return nfs4_retry_setlk_simple(state, cmd, request);
7337}
7338#endif
7339
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340static int
7341nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7342{
7343 struct nfs_open_context *ctx;
7344 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 int status;
7346
7347 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007348 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 state = ctx->state;
7350
Trond Myklebustd9531262009-07-21 19:22:38 -04007351 if (IS_GETLK(cmd)) {
7352 if (state != NULL)
7353 return nfs4_proc_getlk(state, F_GETLK, request);
7354 return 0;
7355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356
7357 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7358 return -EINVAL;
7359
Trond Myklebustd9531262009-07-21 19:22:38 -04007360 if (request->fl_type == F_UNLCK) {
7361 if (state != NULL)
7362 return nfs4_proc_unlck(state, cmd, request);
7363 return 0;
7364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365
Trond Myklebustd9531262009-07-21 19:22:38 -04007366 if (state == NULL)
7367 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007368
7369 if ((request->fl_flags & FL_POSIX) &&
7370 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7371 return -ENOLCK;
7372
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007373 /*
7374 * Don't rely on the VFS having checked the file open mode,
7375 * since it won't do this for flock() locks.
7376 */
7377 switch (request->fl_type) {
7378 case F_RDLCK:
7379 if (!(filp->f_mode & FMODE_READ))
7380 return -EBADF;
7381 break;
7382 case F_WRLCK:
7383 if (!(filp->f_mode & FMODE_WRITE))
7384 return -EBADF;
7385 }
7386
Jeff Layton1ea67db2016-09-17 18:17:37 -04007387 status = nfs4_set_lock_state(state, request);
7388 if (status != 0)
7389 return status;
7390
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007391 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392}
7393
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007394int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007395{
7396 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007397 int err;
7398
7399 err = nfs4_set_lock_state(state, fl);
7400 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007401 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007402 do {
7403 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7404 if (err != -NFS4ERR_DELAY)
7405 break;
7406 ssleep(1);
7407 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007408 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007409}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007410
Trond Myklebustcf470c32012-03-07 13:49:12 -05007411struct nfs_release_lockowner_data {
7412 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007413 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007414 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007415 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007416 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007417};
7418
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007419static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7420{
7421 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007422 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007423 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7424 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007425 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007426 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007427}
7428
7429static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7430{
7431 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007432 struct nfs_server *server = data->server;
7433
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007434 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007435
7436 switch (task->tk_status) {
7437 case 0:
7438 renew_lease(server, data->timestamp);
7439 break;
7440 case -NFS4ERR_STALE_CLIENTID:
7441 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007442 nfs4_schedule_lease_recovery(server->nfs_client);
7443 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007444 case -NFS4ERR_LEASE_MOVED:
7445 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007446 if (nfs4_async_handle_error(task, server,
7447 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007448 rpc_restart_call_prepare(task);
7449 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007450}
7451
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007452static void nfs4_release_lockowner_release(void *calldata)
7453{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007454 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007455 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007456 kfree(calldata);
7457}
7458
Trond Myklebust17280172012-03-11 13:11:00 -04007459static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007460 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7461 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007462 .rpc_release = nfs4_release_lockowner_release,
7463};
7464
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007465static void
7466nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007467{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007468 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007469 struct rpc_message msg = {
7470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7471 };
7472
7473 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007474 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007475
Trond Myklebustcf470c32012-03-07 13:49:12 -05007476 data = kmalloc(sizeof(*data), GFP_NOFS);
7477 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007478 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007479 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007480 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007481 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7482 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7483 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007484
Trond Myklebustcf470c32012-03-07 13:49:12 -05007485 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007486 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007487 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007488 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007489}
7490
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007491#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7492
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007493static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007494 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007495 struct dentry *unused, struct inode *inode,
7496 const char *key, const void *buf,
7497 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007498{
Al Viro59301222016-05-27 10:19:30 -04007499 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007500}
7501
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007502static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007503 struct dentry *unused, struct inode *inode,
7504 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007505{
Al Virob2968212016-04-10 20:48:24 -04007506 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007507}
7508
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007509static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007510{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007511 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007512}
7513
David Quigleyc9bccef2013-05-22 12:50:45 -04007514#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007515
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007516static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007517 struct user_namespace *mnt_userns,
Al Viro59301222016-05-27 10:19:30 -04007518 struct dentry *unused, struct inode *inode,
7519 const char *key, const void *buf,
7520 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007521{
7522 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007523 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007524
7525 return -EOPNOTSUPP;
7526}
7527
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007528static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007529 struct dentry *unused, struct inode *inode,
7530 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007531{
7532 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007533 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007534 return -EOPNOTSUPP;
7535}
7536
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007537static ssize_t
7538nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007539{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007540 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007541
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007542 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7543 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007544 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007545 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007546 }
7547 return len;
7548}
7549
7550static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7551 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007552 .get = nfs4_xattr_get_nfs4_label,
7553 .set = nfs4_xattr_set_nfs4_label,
7554};
David Quigleyc9bccef2013-05-22 12:50:45 -04007555
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007556#else
7557
7558static ssize_t
7559nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7560{
7561 return 0;
7562}
7563
7564#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007565
Frank van der Linden012a2112020-06-23 22:39:03 +00007566#ifdef CONFIG_NFS_V4_2
7567static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +01007568 struct user_namespace *mnt_userns,
Frank van der Linden012a2112020-06-23 22:39:03 +00007569 struct dentry *unused, struct inode *inode,
7570 const char *key, const void *buf,
7571 size_t buflen, int flags)
7572{
7573 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007574 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007575
7576 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7577 return -EOPNOTSUPP;
7578
7579 /*
7580 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7581 * flags right now. Handling of xattr operations use the normal
7582 * file read/write permissions.
7583 *
7584 * Just in case the server has other ideas (which RFC 8276 allows),
7585 * do a cached access check for the XA* flags to possibly avoid
7586 * doing an RPC and getting EACCES back.
7587 */
7588 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7589 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7590 return -EACCES;
7591 }
7592
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007593 if (buf == NULL) {
7594 ret = nfs42_proc_removexattr(inode, key);
7595 if (!ret)
7596 nfs4_xattr_cache_remove(inode, key);
7597 } else {
7598 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7599 if (!ret)
7600 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7601 }
7602
7603 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007604}
7605
7606static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7607 struct dentry *unused, struct inode *inode,
7608 const char *key, void *buf, size_t buflen)
7609{
7610 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007611 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007612
7613 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7614 return -EOPNOTSUPP;
7615
7616 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7617 if (!(cache.mask & NFS_ACCESS_XAREAD))
7618 return -EACCES;
7619 }
7620
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007621 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007622 if (ret)
7623 return ret;
7624
7625 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7626 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7627 return ret;
7628
7629 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7630
7631 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007632}
7633
7634static ssize_t
7635nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7636{
7637 u64 cookie;
7638 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007639 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007640 char *buf;
7641 size_t buflen;
7642 struct nfs_access_entry cache;
7643
7644 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7645 return 0;
7646
7647 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7648 if (!(cache.mask & NFS_ACCESS_XALIST))
7649 return 0;
7650 }
7651
Trond Myklebust1f3208b2021-03-25 11:04:34 -04007652 ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007653 if (ret)
7654 return ret;
7655
7656 ret = nfs4_xattr_cache_list(inode, list, list_len);
7657 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7658 return ret;
7659
Frank van der Linden012a2112020-06-23 22:39:03 +00007660 cookie = 0;
7661 eof = false;
7662 buflen = list_len ? list_len : XATTR_LIST_MAX;
7663 buf = list_len ? list : NULL;
7664 size = 0;
7665
7666 while (!eof) {
7667 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7668 &cookie, &eof);
7669 if (ret < 0)
7670 return ret;
7671
7672 if (list_len) {
7673 buf += ret;
7674 buflen -= ret;
7675 }
7676 size += ret;
7677 }
7678
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007679 if (list_len)
7680 nfs4_xattr_cache_set_list(inode, list, size);
7681
Frank van der Linden012a2112020-06-23 22:39:03 +00007682 return size;
7683}
7684
7685#else
7686
7687static ssize_t
7688nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7689{
7690 return 0;
7691}
7692#endif /* CONFIG_NFS_V4_2 */
7693
Andy Adamson533eb462011-06-13 18:25:56 -04007694/*
7695 * nfs_fhget will use either the mounted_on_fileid or the fileid
7696 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007697static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7698{
Andy Adamson533eb462011-06-13 18:25:56 -04007699 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7700 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7701 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007702 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007703 return;
7704
7705 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007706 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007707 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7708 fattr->nlink = 2;
7709}
7710
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007711static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7712 const struct qstr *name,
7713 struct nfs4_fs_locations *fs_locations,
7714 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007715{
7716 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007717 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007718 struct nfs4_fs_locations_arg args = {
7719 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007720 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007721 .page = page,
7722 .bitmask = bitmask,
7723 };
Benny Halevy22958462009-04-01 09:22:02 -04007724 struct nfs4_fs_locations_res res = {
7725 .fs_locations = fs_locations,
7726 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007727 struct rpc_message msg = {
7728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7729 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007730 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007731 };
7732 int status;
7733
Harvey Harrison3110ff82008-05-02 13:42:44 -07007734 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007735
Chuck Leverc05cefc2017-11-05 15:45:22 -05007736 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7737 bitmask[1] = nfs4_fattr_bitmap[1];
7738
Andy Adamson533eb462011-06-13 18:25:56 -04007739 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7740 * is not supported */
7741 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007742 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007743 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007744 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007745
Trond Myklebustc228fd32007-01-13 02:28:11 -05007746 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007747 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007748 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007749 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007750 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007751 return status;
7752}
7753
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007754int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7755 const struct qstr *name,
7756 struct nfs4_fs_locations *fs_locations,
7757 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007758{
Trond Myklebust0688e642019-04-07 13:59:09 -04007759 struct nfs4_exception exception = {
7760 .interruptible = true,
7761 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007762 int err;
7763 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007764 err = _nfs4_proc_fs_locations(client, dir, name,
7765 fs_locations, page);
7766 trace_nfs4_get_fs_locations(dir, name, err);
7767 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007768 &exception);
7769 } while (exception.retry);
7770 return err;
7771}
7772
Chuck Leverb03d7352013-10-17 14:12:50 -04007773/*
7774 * This operation also signals the server that this client is
7775 * performing migration recovery. The server can stop returning
7776 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7777 * appended to this compound to identify the client ID which is
7778 * performing recovery.
7779 */
7780static int _nfs40_proc_get_locations(struct inode *inode,
7781 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007782 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007783{
7784 struct nfs_server *server = NFS_SERVER(inode);
7785 struct rpc_clnt *clnt = server->client;
7786 u32 bitmask[2] = {
7787 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7788 };
7789 struct nfs4_fs_locations_arg args = {
7790 .clientid = server->nfs_client->cl_clientid,
7791 .fh = NFS_FH(inode),
7792 .page = page,
7793 .bitmask = bitmask,
7794 .migration = 1, /* skip LOOKUP */
7795 .renew = 1, /* append RENEW */
7796 };
7797 struct nfs4_fs_locations_res res = {
7798 .fs_locations = locations,
7799 .migration = 1,
7800 .renew = 1,
7801 };
7802 struct rpc_message msg = {
7803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7804 .rpc_argp = &args,
7805 .rpc_resp = &res,
7806 .rpc_cred = cred,
7807 };
7808 unsigned long now = jiffies;
7809 int status;
7810
7811 nfs_fattr_init(&locations->fattr);
7812 locations->server = server;
7813 locations->nlocations = 0;
7814
Anna Schumakerfba83f32018-05-04 16:22:50 -04007815 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007816 status = nfs4_call_sync_sequence(clnt, server, &msg,
7817 &args.seq_args, &res.seq_res);
7818 if (status)
7819 return status;
7820
7821 renew_lease(server, now);
7822 return 0;
7823}
7824
7825#ifdef CONFIG_NFS_V4_1
7826
7827/*
7828 * This operation also signals the server that this client is
7829 * performing migration recovery. The server can stop asserting
7830 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7831 * performing this operation is identified in the SEQUENCE
7832 * operation in this compound.
7833 *
7834 * When the client supports GETATTR(fs_locations_info), it can
7835 * be plumbed in here.
7836 */
7837static int _nfs41_proc_get_locations(struct inode *inode,
7838 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007839 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007840{
7841 struct nfs_server *server = NFS_SERVER(inode);
7842 struct rpc_clnt *clnt = server->client;
7843 u32 bitmask[2] = {
7844 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7845 };
7846 struct nfs4_fs_locations_arg args = {
7847 .fh = NFS_FH(inode),
7848 .page = page,
7849 .bitmask = bitmask,
7850 .migration = 1, /* skip LOOKUP */
7851 };
7852 struct nfs4_fs_locations_res res = {
7853 .fs_locations = locations,
7854 .migration = 1,
7855 };
7856 struct rpc_message msg = {
7857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7858 .rpc_argp = &args,
7859 .rpc_resp = &res,
7860 .rpc_cred = cred,
7861 };
7862 int status;
7863
7864 nfs_fattr_init(&locations->fattr);
7865 locations->server = server;
7866 locations->nlocations = 0;
7867
Anna Schumakerfba83f32018-05-04 16:22:50 -04007868 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007869 status = nfs4_call_sync_sequence(clnt, server, &msg,
7870 &args.seq_args, &res.seq_res);
7871 if (status == NFS4_OK &&
7872 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7873 status = -NFS4ERR_LEASE_MOVED;
7874 return status;
7875}
7876
7877#endif /* CONFIG_NFS_V4_1 */
7878
7879/**
7880 * nfs4_proc_get_locations - discover locations for a migrated FSID
7881 * @inode: inode on FSID that is migrating
7882 * @locations: result of query
7883 * @page: buffer
7884 * @cred: credential to use for this operation
7885 *
7886 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7887 * operation failed, or a negative errno if a local error occurred.
7888 *
7889 * On success, "locations" is filled in, but if the server has
7890 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7891 * asserted.
7892 *
7893 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7894 * from this client that require migration recovery.
7895 */
7896int nfs4_proc_get_locations(struct inode *inode,
7897 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007898 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007899{
7900 struct nfs_server *server = NFS_SERVER(inode);
7901 struct nfs_client *clp = server->nfs_client;
7902 const struct nfs4_mig_recovery_ops *ops =
7903 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007904 struct nfs4_exception exception = {
7905 .interruptible = true,
7906 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007907 int status;
7908
7909 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7910 (unsigned long long)server->fsid.major,
7911 (unsigned long long)server->fsid.minor,
7912 clp->cl_hostname);
7913 nfs_display_fhandle(NFS_FH(inode), __func__);
7914
7915 do {
7916 status = ops->get_locations(inode, locations, page, cred);
7917 if (status != -NFS4ERR_DELAY)
7918 break;
7919 nfs4_handle_exception(server, status, &exception);
7920 } while (exception.retry);
7921 return status;
7922}
7923
Chuck Lever44c99932013-10-17 14:13:30 -04007924/*
7925 * This operation also signals the server that this client is
7926 * performing "lease moved" recovery. The server can stop
7927 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7928 * is appended to this compound to identify the client ID which is
7929 * performing recovery.
7930 */
NeilBrowna52458b2018-12-03 11:30:31 +11007931static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007932{
7933 struct nfs_server *server = NFS_SERVER(inode);
7934 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7935 struct rpc_clnt *clnt = server->client;
7936 struct nfs4_fsid_present_arg args = {
7937 .fh = NFS_FH(inode),
7938 .clientid = clp->cl_clientid,
7939 .renew = 1, /* append RENEW */
7940 };
7941 struct nfs4_fsid_present_res res = {
7942 .renew = 1,
7943 };
7944 struct rpc_message msg = {
7945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7946 .rpc_argp = &args,
7947 .rpc_resp = &res,
7948 .rpc_cred = cred,
7949 };
7950 unsigned long now = jiffies;
7951 int status;
7952
7953 res.fh = nfs_alloc_fhandle();
7954 if (res.fh == NULL)
7955 return -ENOMEM;
7956
Anna Schumakerfba83f32018-05-04 16:22:50 -04007957 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007958 status = nfs4_call_sync_sequence(clnt, server, &msg,
7959 &args.seq_args, &res.seq_res);
7960 nfs_free_fhandle(res.fh);
7961 if (status)
7962 return status;
7963
7964 do_renew_lease(clp, now);
7965 return 0;
7966}
7967
7968#ifdef CONFIG_NFS_V4_1
7969
7970/*
7971 * This operation also signals the server that this client is
7972 * performing "lease moved" recovery. The server can stop asserting
7973 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7974 * this operation is identified in the SEQUENCE operation in this
7975 * compound.
7976 */
NeilBrowna52458b2018-12-03 11:30:31 +11007977static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007978{
7979 struct nfs_server *server = NFS_SERVER(inode);
7980 struct rpc_clnt *clnt = server->client;
7981 struct nfs4_fsid_present_arg args = {
7982 .fh = NFS_FH(inode),
7983 };
7984 struct nfs4_fsid_present_res res = {
7985 };
7986 struct rpc_message msg = {
7987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7988 .rpc_argp = &args,
7989 .rpc_resp = &res,
7990 .rpc_cred = cred,
7991 };
7992 int status;
7993
7994 res.fh = nfs_alloc_fhandle();
7995 if (res.fh == NULL)
7996 return -ENOMEM;
7997
Anna Schumakerfba83f32018-05-04 16:22:50 -04007998 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007999 status = nfs4_call_sync_sequence(clnt, server, &msg,
8000 &args.seq_args, &res.seq_res);
8001 nfs_free_fhandle(res.fh);
8002 if (status == NFS4_OK &&
8003 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8004 status = -NFS4ERR_LEASE_MOVED;
8005 return status;
8006}
8007
8008#endif /* CONFIG_NFS_V4_1 */
8009
8010/**
8011 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8012 * @inode: inode on FSID to check
8013 * @cred: credential to use for this operation
8014 *
8015 * Server indicates whether the FSID is present, moved, or not
8016 * recognized. This operation is necessary to clear a LEASE_MOVED
8017 * condition for this client ID.
8018 *
8019 * Returns NFS4_OK if the FSID is present on this server,
8020 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8021 * NFS4ERR code if some error occurred on the server, or a
8022 * negative errno if a local failure occurred.
8023 */
NeilBrowna52458b2018-12-03 11:30:31 +11008024int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008025{
8026 struct nfs_server *server = NFS_SERVER(inode);
8027 struct nfs_client *clp = server->nfs_client;
8028 const struct nfs4_mig_recovery_ops *ops =
8029 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008030 struct nfs4_exception exception = {
8031 .interruptible = true,
8032 };
Chuck Lever44c99932013-10-17 14:13:30 -04008033 int status;
8034
8035 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8036 (unsigned long long)server->fsid.major,
8037 (unsigned long long)server->fsid.minor,
8038 clp->cl_hostname);
8039 nfs_display_fhandle(NFS_FH(inode), __func__);
8040
8041 do {
8042 status = ops->fsid_present(inode, cred);
8043 if (status != -NFS4ERR_DELAY)
8044 break;
8045 nfs4_handle_exception(server, status, &exception);
8046 } while (exception.retry);
8047 return status;
8048}
8049
Trond Myklebust302fad72019-02-18 13:32:38 -05008050/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008051 * If 'use_integrity' is true and the state managment nfs_client
8052 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8053 * and the machine credential as per RFC3530bis and RFC5661 Security
8054 * Considerations sections. Otherwise, just use the user cred with the
8055 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008056 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008057static 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 +00008058{
8059 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008060 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8061 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008062 struct nfs4_secinfo_arg args = {
8063 .dir_fh = NFS_FH(dir),
8064 .name = name,
8065 };
8066 struct nfs4_secinfo_res res = {
8067 .flavors = flavors,
8068 };
8069 struct rpc_message msg = {
8070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8071 .rpc_argp = &args,
8072 .rpc_resp = &res,
8073 };
Anna Schumaker50493362019-08-14 15:30:16 -04008074 struct nfs4_call_sync_data data = {
8075 .seq_server = NFS_SERVER(dir),
8076 .seq_args = &args.seq_args,
8077 .seq_res = &res.seq_res,
8078 };
8079 struct rpc_task_setup task_setup = {
8080 .rpc_client = clnt,
8081 .rpc_message = &msg,
8082 .callback_ops = clp->cl_mvops->call_sync_ops,
8083 .callback_data = &data,
8084 .flags = RPC_TASK_NO_ROUND_ROBIN,
8085 };
NeilBrowna52458b2018-12-03 11:30:31 +11008086 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008087
8088 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008089 clnt = clp->cl_rpcclient;
8090 task_setup.rpc_client = clnt;
8091
8092 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008093 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008094 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008095
8096 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008097
Anna Schumaker50493362019-08-14 15:30:16 -04008098 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8099 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8100 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008101
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008102 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008103
NeilBrowna52458b2018-12-03 11:30:31 +11008104 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008105 return status;
8106}
8107
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008108int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8109 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008110{
Trond Myklebust0688e642019-04-07 13:59:09 -04008111 struct nfs4_exception exception = {
8112 .interruptible = true,
8113 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008114 int err;
8115 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008116 err = -NFS4ERR_WRONGSEC;
8117
8118 /* try to use integrity protection with machine cred */
8119 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8120 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8121
8122 /*
8123 * if unable to use integrity protection, or SECINFO with
8124 * integrity protection returns NFS4ERR_WRONGSEC (which is
8125 * disallowed by spec, but exists in deployed servers) use
8126 * the current filesystem's rpc_client and the user cred.
8127 */
8128 if (err == -NFS4ERR_WRONGSEC)
8129 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8130
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008131 trace_nfs4_secinfo(dir, name, err);
8132 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008133 &exception);
8134 } while (exception.retry);
8135 return err;
8136}
8137
Andy Adamson557134a2009-04-01 09:21:53 -04008138#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008139/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008140 * Check the exchange flags returned by the server for invalid flags, having
8141 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8142 * DS flags set.
8143 */
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008144static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
Andy Adamson357f54d2010-12-14 10:11:57 -05008145{
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008146 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8147 goto out_inval;
8148 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
Andy Adamson357f54d2010-12-14 10:11:57 -05008149 goto out_inval;
8150 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8151 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8152 goto out_inval;
8153 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8154 goto out_inval;
8155 return NFS_OK;
8156out_inval:
8157 return -NFS4ERR_INVAL;
8158}
8159
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008160static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008161nfs41_same_server_scope(struct nfs41_server_scope *a,
8162 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008163{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008164 if (a->server_scope_sz != b->server_scope_sz)
8165 return false;
8166 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008167}
8168
Andy Adamson02a95de2016-02-05 16:08:37 -05008169static void
8170nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8171{
Trond Myklebust5c441542019-11-13 08:34:00 +01008172 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008173 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008174 struct nfs_client *clp = args->client;
8175
8176 switch (task->tk_status) {
8177 case -NFS4ERR_BADSESSION:
8178 case -NFS4ERR_DEADSESSION:
8179 nfs4_schedule_session_recovery(clp->cl_session,
8180 task->tk_status);
8181 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008182 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8183 res->dir != NFS4_CDFS4_BOTH) {
8184 rpc_task_close_connection(task);
8185 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8186 rpc_restart_call(task);
8187 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008188}
8189
8190static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008191 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008192};
8193
Andy Adamson357f54d2010-12-14 10:11:57 -05008194/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008195 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008196 *
8197 * The 4.1 client currently uses the same TCP connection for the
8198 * fore and backchannel.
8199 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008200static
8201int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8202 struct rpc_xprt *xprt,
8203 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008204 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008205{
8206 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008207 struct nfs41_bind_conn_to_session_args args = {
8208 .client = clp,
8209 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008210 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008211 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008212 struct nfs41_bind_conn_to_session_res res;
8213 struct rpc_message msg = {
8214 .rpc_proc =
8215 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008216 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008217 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008218 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008219 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008220 struct rpc_task_setup task_setup_data = {
8221 .rpc_client = clnt,
8222 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008223 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008224 .rpc_message = &msg,
8225 .flags = RPC_TASK_TIMEOUT,
8226 };
8227 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008228
Trond Myklebust71a097c2015-02-18 09:27:18 -08008229 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8230 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8231 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008232
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008233 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8234 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8235 args.dir = NFS4_CDFC4_FORE;
8236
8237 task = rpc_run_task(&task_setup_data);
8238 if (!IS_ERR(task)) {
8239 status = task->tk_status;
8240 rpc_put_task(task);
8241 } else
8242 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008243 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008244 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008245 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008246 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8247 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008248 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008249 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008250 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008251 dprintk("NFS: %s: Unexpected direction from server\n",
8252 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008253 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008254 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008255 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008256 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8257 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008258 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008259 }
8260 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008261
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008262 return status;
8263}
8264
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008265struct rpc_bind_conn_calldata {
8266 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008267 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008268};
8269
8270static int
8271nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8272 struct rpc_xprt *xprt,
8273 void *calldata)
8274{
8275 struct rpc_bind_conn_calldata *p = calldata;
8276
8277 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8278}
8279
NeilBrowna52458b2018-12-03 11:30:31 +11008280int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008281{
8282 struct rpc_bind_conn_calldata data = {
8283 .clp = clp,
8284 .cred = cred,
8285 };
8286 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8287 nfs4_proc_bind_conn_to_session_callback, &data);
8288}
8289
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008290/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008291 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8292 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008293 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008294static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8295 .how = SP4_MACH_CRED,
8296 .enforce.u.words = {
8297 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8298 1 << (OP_EXCHANGE_ID - 32) |
8299 1 << (OP_CREATE_SESSION - 32) |
8300 1 << (OP_DESTROY_SESSION - 32) |
8301 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008302 },
8303 .allow.u.words = {
8304 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008305 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008306 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008307 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008308 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008309 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008310 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008311 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008312 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008313 1 << (OP_FREE_STATEID - 32) |
8314 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008315 }
8316};
8317
8318/*
8319 * Select the state protection mode for client `clp' given the server results
8320 * from exchange_id in `sp'.
8321 *
8322 * Returns 0 on success, negative errno otherwise.
8323 */
8324static int nfs4_sp4_select_mode(struct nfs_client *clp,
8325 struct nfs41_state_protection *sp)
8326{
8327 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8328 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8329 1 << (OP_EXCHANGE_ID - 32) |
8330 1 << (OP_CREATE_SESSION - 32) |
8331 1 << (OP_DESTROY_SESSION - 32) |
8332 1 << (OP_DESTROY_CLIENTID - 32)
8333 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008334 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008335 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008336 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008337
8338 if (sp->how == SP4_MACH_CRED) {
8339 /* Print state protect result */
8340 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8341 for (i = 0; i <= LAST_NFS4_OP; i++) {
8342 if (test_bit(i, sp->enforce.u.longs))
8343 dfprintk(MOUNT, " enforce op %d\n", i);
8344 if (test_bit(i, sp->allow.u.longs))
8345 dfprintk(MOUNT, " allow op %d\n", i);
8346 }
8347
8348 /* make sure nothing is on enforce list that isn't supported */
8349 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8350 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8351 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008352 ret = -EINVAL;
8353 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008354 }
8355 }
8356
8357 /*
8358 * Minimal mode - state operations are allowed to use machine
8359 * credential. Note this already happens by default, so the
8360 * client doesn't have to do anything more than the negotiation.
8361 *
8362 * NOTE: we don't care if EXCHANGE_ID is in the list -
8363 * we're already using the machine cred for exchange_id
8364 * and will never use a different cred.
8365 */
8366 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8367 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8368 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8369 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8370 dfprintk(MOUNT, "sp4_mach_cred:\n");
8371 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008372 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008373 } else {
8374 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008375 ret = -EINVAL;
8376 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008377 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008378
8379 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008380 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8381 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008382 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8383 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008384 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008385 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008386
Andrew Elble99ade3c2015-12-02 09:39:51 -05008387 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8388 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008389 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008390 }
8391
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008392 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8393 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8394 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008395 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008396 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008397
8398 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8399 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8400 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008401 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008402 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008403
8404 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8405 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008406 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008407 }
8408
8409 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8410 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008411 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008412 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008413 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008414out:
8415 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008416 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008417}
8418
Andy Adamson8d89bd72016-09-09 09:22:18 -04008419struct nfs41_exchange_id_data {
8420 struct nfs41_exchange_id_res res;
8421 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008422};
8423
Andy Adamson8d89bd72016-09-09 09:22:18 -04008424static void nfs4_exchange_id_release(void *data)
8425{
8426 struct nfs41_exchange_id_data *cdata =
8427 (struct nfs41_exchange_id_data *)data;
8428
Olga Kornievskaia63513232017-03-13 10:36:19 -04008429 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008430 kfree(cdata->res.impl_id);
8431 kfree(cdata->res.server_scope);
8432 kfree(cdata->res.server_owner);
8433 kfree(cdata);
8434}
8435
8436static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008437 .rpc_release = nfs4_exchange_id_release,
8438};
8439
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008440/*
8441 * _nfs4_proc_exchange_id()
8442 *
8443 * Wrapper for EXCHANGE_ID operation.
8444 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008445static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008446nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008447 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008448{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008449 struct rpc_message msg = {
8450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008451 .rpc_cred = cred,
8452 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008453 struct rpc_task_setup task_setup_data = {
8454 .rpc_client = clp->cl_rpcclient,
8455 .callback_ops = &nfs4_exchange_id_call_ops,
8456 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008457 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008458 };
8459 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008460 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008461
Elena Reshetova212bf412017-10-20 12:53:38 +03008462 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008463 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008464
Trond Myklebust9c760d12017-07-31 18:38:50 -04008465 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008466 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008467 if (!calldata)
8468 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008469
Trond Myklebustfd405592017-08-01 16:02:47 -04008470 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008471
8472 status = nfs4_init_uniform_client_string(clp);
8473 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008474 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008475
Andy Adamson8d89bd72016-09-09 09:22:18 -04008476 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8477 GFP_NOFS);
8478 status = -ENOMEM;
8479 if (unlikely(calldata->res.server_owner == NULL))
8480 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008481
Andy Adamson8d89bd72016-09-09 09:22:18 -04008482 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008483 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008484 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008485 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008486
Andy Adamson8d89bd72016-09-09 09:22:18 -04008487 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8488 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008489 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008490
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008491 switch (sp4_how) {
8492 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008493 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008494 break;
8495
8496 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008497 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008498 break;
8499
8500 default:
8501 /* unsupported! */
8502 WARN_ON_ONCE(1);
8503 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008504 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008505 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008506 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008507 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008508 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008509 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8510 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008511 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008512 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008513 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8514 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008515#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008516 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008517#endif
8518 msg.rpc_argp = &calldata->args;
8519 msg.rpc_resp = &calldata->res;
8520 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008521
Trond Myklebust9c760d12017-07-31 18:38:50 -04008522 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008523
8524out_impl_id:
8525 kfree(calldata->res.impl_id);
8526out_server_scope:
8527 kfree(calldata->res.server_scope);
8528out_server_owner:
8529 kfree(calldata->res.server_owner);
8530out_calldata:
8531 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008532out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008533 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008534 return ERR_PTR(status);
8535}
8536
8537/*
8538 * _nfs4_proc_exchange_id()
8539 *
8540 * Wrapper for EXCHANGE_ID operation.
8541 */
NeilBrowna52458b2018-12-03 11:30:31 +11008542static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008543 u32 sp4_how)
8544{
8545 struct rpc_task *task;
8546 struct nfs41_exchange_id_args *argp;
8547 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008548 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008549 int status;
8550
8551 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8552 if (IS_ERR(task))
8553 return PTR_ERR(task);
8554
8555 argp = task->tk_msg.rpc_argp;
8556 resp = task->tk_msg.rpc_resp;
8557 status = task->tk_status;
8558 if (status != 0)
8559 goto out;
8560
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008561 status = nfs4_check_cl_exchange_flags(resp->flags,
8562 clp->cl_mvops->minor_version);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008563 if (status != 0)
8564 goto out;
8565
8566 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8567 if (status != 0)
8568 goto out;
8569
Robert Milkowski7dc29932020-01-30 09:43:25 +00008570 do_renew_lease(clp, now);
8571
Trond Myklebust9c760d12017-07-31 18:38:50 -04008572 clp->cl_clientid = resp->clientid;
8573 clp->cl_exchange_flags = resp->flags;
8574 clp->cl_seqid = resp->seqid;
8575 /* Client ID is not confirmed */
8576 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8577 clear_bit(NFS4_SESSION_ESTABLISHED,
8578 &clp->cl_session->session_state);
8579
8580 if (clp->cl_serverscope != NULL &&
8581 !nfs41_same_server_scope(clp->cl_serverscope,
8582 resp->server_scope)) {
8583 dprintk("%s: server_scope mismatch detected\n",
8584 __func__);
8585 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8586 }
8587
8588 swap(clp->cl_serverowner, resp->server_owner);
8589 swap(clp->cl_serverscope, resp->server_scope);
8590 swap(clp->cl_implid, resp->impl_id);
8591
8592 /* Save the EXCHANGE_ID verifier session trunk tests */
8593 memcpy(clp->cl_confirm.data, argp->verifier.data,
8594 sizeof(clp->cl_confirm.data));
8595out:
8596 trace_nfs4_exchange_id(clp, status);
8597 rpc_put_task(task);
8598 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008599}
8600
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008601/*
8602 * nfs4_proc_exchange_id()
8603 *
8604 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8605 *
8606 * Since the clientid has expired, all compounds using sessions
8607 * associated with the stale clientid will be returning
8608 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8609 * be in some phase of session reset.
8610 *
8611 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8612 */
NeilBrowna52458b2018-12-03 11:30:31 +11008613int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008614{
8615 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8616 int status;
8617
8618 /* try SP4_MACH_CRED if krb5i/p */
8619 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8620 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008621 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008622 if (!status)
8623 return 0;
8624 }
8625
8626 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008627 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008628}
8629
Andy Adamson04fa2c62016-09-09 09:22:29 -04008630/**
8631 * nfs4_test_session_trunk
8632 *
8633 * This is an add_xprt_test() test function called from
8634 * rpc_clnt_setup_test_and_add_xprt.
8635 *
8636 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8637 * and is dereferrenced in nfs4_exchange_id_release
8638 *
8639 * Upon success, add the new transport to the rpc_clnt
8640 *
8641 * @clnt: struct rpc_clnt to get new transport
8642 * @xprt: the rpc_xprt to test
8643 * @data: call data for _nfs4_proc_exchange_id.
8644 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308645void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008646 void *data)
8647{
8648 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008649 struct rpc_task *task;
8650 int status;
8651
Andy Adamson04fa2c62016-09-09 09:22:29 -04008652 u32 sp4_how;
8653
8654 dprintk("--> %s try %s\n", __func__,
8655 xprt->address_strings[RPC_DISPLAY_ADDR]);
8656
8657 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8658
8659 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008660 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8661 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308662 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008663
8664 status = task->tk_status;
8665 if (status == 0)
8666 status = nfs4_detect_session_trunking(adata->clp,
8667 task->tk_msg.rpc_resp, xprt);
8668
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308669 if (status == 0)
8670 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8671
Trond Myklebust9c760d12017-07-31 18:38:50 -04008672 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008673}
8674EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8675
Trond Myklebust66245532012-05-25 17:18:09 -04008676static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008677 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008678{
8679 struct rpc_message msg = {
8680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8681 .rpc_argp = clp,
8682 .rpc_cred = cred,
8683 };
8684 int status;
8685
NeilBrown5a0c2572019-05-30 10:41:28 +10008686 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8687 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008688 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008689 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008690 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008691 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8692 return status;
8693}
8694
8695static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008696 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008697{
8698 unsigned int loop;
8699 int ret;
8700
8701 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8702 ret = _nfs4_proc_destroy_clientid(clp, cred);
8703 switch (ret) {
8704 case -NFS4ERR_DELAY:
8705 case -NFS4ERR_CLIENTID_BUSY:
8706 ssleep(1);
8707 break;
8708 default:
8709 return ret;
8710 }
8711 }
8712 return 0;
8713}
8714
8715int nfs4_destroy_clientid(struct nfs_client *clp)
8716{
NeilBrowna52458b2018-12-03 11:30:31 +11008717 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008718 int ret = 0;
8719
8720 if (clp->cl_mvops->minor_version < 1)
8721 goto out;
8722 if (clp->cl_exchange_flags == 0)
8723 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008724 if (clp->cl_preserve_clid)
8725 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008726 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008727 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008728 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008729 switch (ret) {
8730 case 0:
8731 case -NFS4ERR_STALE_CLIENTID:
8732 clp->cl_exchange_flags = 0;
8733 }
8734out:
8735 return ret;
8736}
8737
Donald Buczek0efb01b2019-07-07 21:26:08 +02008738#endif /* CONFIG_NFS_V4_1 */
8739
Andy Adamson2050f0c2009-04-01 09:22:30 -04008740struct nfs4_get_lease_time_data {
8741 struct nfs4_get_lease_time_args *args;
8742 struct nfs4_get_lease_time_res *res;
8743 struct nfs_client *clp;
8744};
8745
8746static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8747 void *calldata)
8748{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008749 struct nfs4_get_lease_time_data *data =
8750 (struct nfs4_get_lease_time_data *)calldata;
8751
8752 dprintk("--> %s\n", __func__);
8753 /* just setup sequence, do not trigger session recovery
8754 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008755 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008756 &data->args->la_seq_args,
8757 &data->res->lr_seq_res,
8758 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008759 dprintk("<-- %s\n", __func__);
8760}
8761
8762/*
8763 * Called from nfs4_state_manager thread for session setup, so don't recover
8764 * from sequence operation or clientid errors.
8765 */
8766static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8767{
8768 struct nfs4_get_lease_time_data *data =
8769 (struct nfs4_get_lease_time_data *)calldata;
8770
8771 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008772 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008773 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008774 switch (task->tk_status) {
8775 case -NFS4ERR_DELAY:
8776 case -NFS4ERR_GRACE:
8777 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8778 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8779 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008780 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008781 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008782 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008783 return;
8784 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008785 dprintk("<-- %s\n", __func__);
8786}
8787
Trond Myklebust17280172012-03-11 13:11:00 -04008788static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008789 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8790 .rpc_call_done = nfs4_get_lease_time_done,
8791};
8792
8793int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8794{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008795 struct nfs4_get_lease_time_args args;
8796 struct nfs4_get_lease_time_res res = {
8797 .lr_fsinfo = fsinfo,
8798 };
8799 struct nfs4_get_lease_time_data data = {
8800 .args = &args,
8801 .res = &res,
8802 .clp = clp,
8803 };
8804 struct rpc_message msg = {
8805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8806 .rpc_argp = &args,
8807 .rpc_resp = &res,
8808 };
8809 struct rpc_task_setup task_setup = {
8810 .rpc_client = clp->cl_rpcclient,
8811 .rpc_message = &msg,
8812 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008813 .callback_data = &data,
8814 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008815 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008816
Anna Schumakerfba83f32018-05-04 16:22:50 -04008817 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008818 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008819}
8820
Donald Buczek0efb01b2019-07-07 21:26:08 +02008821#ifdef CONFIG_NFS_V4_1
8822
Andy Adamsonfc931582009-04-01 09:22:31 -04008823/*
8824 * Initialize the values to be used by the client in CREATE_SESSION
8825 * If nfs4_init_session set the fore channel request and response sizes,
8826 * use them.
8827 *
8828 * Set the back channel max_resp_sz_cached to zero to force the client to
8829 * always set csa_cachethis to FALSE because the current implementation
8830 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8831 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008832static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8833 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008834{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008835 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008836 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008837 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008838
Andy Adamson18aad3d2013-06-26 12:21:49 -04008839 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8840 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8841
Andy Adamsonfc931582009-04-01 09:22:31 -04008842 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008843 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8844 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008845 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008846 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008847
8848 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008849 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008850 __func__,
8851 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008852 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008853
8854 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008855 args->bc_attrs.max_rqst_sz = max_bc_payload;
8856 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008857 args->bc_attrs.max_resp_sz_cached = 0;
8858 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008859 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008860 if (args->bc_attrs.max_reqs > max_bc_slots)
8861 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008862
8863 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8864 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8865 __func__,
8866 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8867 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8868 args->bc_attrs.max_reqs);
8869}
8870
Trond Myklebust79969dd2015-02-18 11:30:18 -08008871static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8872 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008873{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008874 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008875 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008876
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008877 if (rcvd->max_resp_sz > sent->max_resp_sz)
8878 return -EINVAL;
8879 /*
8880 * Our requested max_ops is the minimum we need; we're not
8881 * prepared to break up compounds into smaller pieces than that.
8882 * So, no point even trying to continue if the server won't
8883 * cooperate:
8884 */
8885 if (rcvd->max_ops < sent->max_ops)
8886 return -EINVAL;
8887 if (rcvd->max_reqs == 0)
8888 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008889 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8890 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008891 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008892}
8893
Trond Myklebust79969dd2015-02-18 11:30:18 -08008894static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8895 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008896{
8897 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008898 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008899
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008900 if (!(res->flags & SESSION4_BACK_CHAN))
8901 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008902 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8903 return -EINVAL;
8904 if (rcvd->max_resp_sz < sent->max_resp_sz)
8905 return -EINVAL;
8906 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8907 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008908 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008909 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008910 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008911 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008912out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008913 return 0;
8914}
Andy Adamson8d353012009-04-01 09:22:32 -04008915
Andy Adamson8d353012009-04-01 09:22:32 -04008916static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008917 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008918{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008919 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008920
Trond Myklebust79969dd2015-02-18 11:30:18 -08008921 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008922 if (ret)
8923 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008924 return nfs4_verify_back_channel_attrs(args, res);
8925}
8926
8927static void nfs4_update_session(struct nfs4_session *session,
8928 struct nfs41_create_session_res *res)
8929{
8930 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008931 /* Mark client id and session as being confirmed */
8932 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8933 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008934 session->flags = res->flags;
8935 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008936 if (res->flags & SESSION4_BACK_CHAN)
8937 memcpy(&session->bc_attrs, &res->bc_attrs,
8938 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008939}
8940
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008941static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008942 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008943{
8944 struct nfs4_session *session = clp->cl_session;
8945 struct nfs41_create_session_args args = {
8946 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008947 .clientid = clp->cl_clientid,
8948 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008949 .cb_program = NFS4_CALLBACK,
8950 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008951 struct nfs41_create_session_res res;
8952
Andy Adamsonfc931582009-04-01 09:22:31 -04008953 struct rpc_message msg = {
8954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8955 .rpc_argp = &args,
8956 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008957 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008958 };
8959 int status;
8960
Chuck Lever6b26cc82016-05-02 14:40:40 -04008961 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008962 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008963
NeilBrown5a0c2572019-05-30 10:41:28 +10008964 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8965 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008966 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008967
Trond Myklebustb519d402016-09-11 14:50:01 -04008968 switch (status) {
8969 case -NFS4ERR_STALE_CLIENTID:
8970 case -NFS4ERR_DELAY:
8971 case -ETIMEDOUT:
8972 case -EACCES:
8973 case -EAGAIN:
8974 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08008975 }
Trond Myklebustb519d402016-09-11 14:50:01 -04008976
8977 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008978 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008979 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008980 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008981 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008982 if (status)
8983 goto out;
8984 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008985 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008986out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008987 return status;
8988}
8989
8990/*
8991 * Issues a CREATE_SESSION operation to the server.
8992 * It is the responsibility of the caller to verify the session is
8993 * expired before calling this routine.
8994 */
NeilBrowna52458b2018-12-03 11:30:31 +11008995int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008996{
8997 int status;
8998 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008999 struct nfs4_session *session = clp->cl_session;
9000
9001 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9002
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009003 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04009004 if (status)
9005 goto out;
9006
Andy Adamsonaacd5532011-11-09 13:58:21 -05009007 /* Init or reset the session slot tables */
9008 status = nfs4_setup_session_slot_tables(session);
9009 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009010 if (status)
9011 goto out;
9012
9013 ptr = (unsigned *)&session->sess_id.data[0];
9014 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9015 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04009016out:
9017 dprintk("<-- %s\n", __func__);
9018 return status;
9019}
9020
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009021/*
9022 * Issue the over-the-wire RPC DESTROY_SESSION.
9023 * The caller must serialize access to this routine.
9024 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009025int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11009026 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009027{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009028 struct rpc_message msg = {
9029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9030 .rpc_argp = session,
9031 .rpc_cred = cred,
9032 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009033 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009034
9035 dprintk("--> nfs4_proc_destroy_session\n");
9036
9037 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05009038 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9039 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009040
NeilBrown5a0c2572019-05-30 10:41:28 +10009041 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9042 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009043 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009044
9045 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04009046 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009047 "Session has been destroyed regardless...\n", status);
9048
9049 dprintk("<-- nfs4_proc_destroy_session\n");
9050 return status;
9051}
9052
Trond Myklebust7b38c362012-05-23 13:23:31 -04009053/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009054 * Renew the cl_session lease.
9055 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009056struct nfs4_sequence_data {
9057 struct nfs_client *clp;
9058 struct nfs4_sequence_args args;
9059 struct nfs4_sequence_res res;
9060};
9061
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009062static void nfs41_sequence_release(void *data)
9063{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009064 struct nfs4_sequence_data *calldata = data;
9065 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009066
Elena Reshetova212bf412017-10-20 12:53:38 +03009067 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009068 nfs4_schedule_state_renewal(clp);
9069 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009070 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009071}
9072
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009073static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9074{
9075 switch(task->tk_status) {
9076 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009077 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9078 return -EAGAIN;
9079 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009080 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009081 }
9082 return 0;
9083}
9084
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009085static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009086{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009087 struct nfs4_sequence_data *calldata = data;
9088 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009089
Trond Myklebust14516c32010-07-31 14:29:06 -04009090 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9091 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009092
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009093 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009094 if (task->tk_status < 0) {
9095 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009096 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009097 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009098
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009099 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9100 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009101 return;
9102 }
9103 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009104 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08009105out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009106 dprintk("<-- %s\n", __func__);
9107}
9108
9109static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9110{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009111 struct nfs4_sequence_data *calldata = data;
9112 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009113 struct nfs4_sequence_args *args;
9114 struct nfs4_sequence_res *res;
9115
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009116 args = task->tk_msg.rpc_argp;
9117 res = task->tk_msg.rpc_resp;
9118
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009119 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009120}
9121
9122static const struct rpc_call_ops nfs41_sequence_ops = {
9123 .rpc_call_done = nfs41_sequence_call_done,
9124 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009125 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009126};
9127
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009128static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009129 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009130 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009131 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009132{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009133 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009134 struct rpc_message msg = {
9135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9136 .rpc_cred = cred,
9137 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009138 struct rpc_task_setup task_setup_data = {
9139 .rpc_client = clp->cl_rpcclient,
9140 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009141 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04009142 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009143 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009144 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009145
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009146 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009147 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009148 goto out_err;
9149
9150 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009151 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009152 if (calldata == NULL)
9153 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009154 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009155 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009156 msg.rpc_argp = &calldata->args;
9157 msg.rpc_resp = &calldata->res;
9158 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009159 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009160
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009161 ret = rpc_run_task(&task_setup_data);
9162 if (IS_ERR(ret))
9163 goto out_err;
9164 return ret;
9165out_put_clp:
9166 nfs_put_client(clp);
9167out_err:
9168 nfs41_release_slot(slot);
9169 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009170}
9171
NeilBrowna52458b2018-12-03 11:30:31 +11009172static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009173{
9174 struct rpc_task *task;
9175 int ret = 0;
9176
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009177 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009178 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009179 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009180 if (IS_ERR(task))
9181 ret = PTR_ERR(task);
9182 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009183 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009184 dprintk("<-- %s status=%d\n", __func__, ret);
9185 return ret;
9186}
9187
NeilBrowna52458b2018-12-03 11:30:31 +11009188static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009189{
9190 struct rpc_task *task;
9191 int ret;
9192
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009193 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009194 if (IS_ERR(task)) {
9195 ret = PTR_ERR(task);
9196 goto out;
9197 }
9198 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009199 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009200 ret = task->tk_status;
9201 rpc_put_task(task);
9202out:
9203 dprintk("<-- %s status=%d\n", __func__, ret);
9204 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009205}
9206
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009207struct nfs4_reclaim_complete_data {
9208 struct nfs_client *clp;
9209 struct nfs41_reclaim_complete_args arg;
9210 struct nfs41_reclaim_complete_res res;
9211};
9212
9213static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9214{
9215 struct nfs4_reclaim_complete_data *calldata = data;
9216
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009217 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009218 &calldata->arg.seq_args,
9219 &calldata->res.seq_res,
9220 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009221}
9222
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009223static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9224{
9225 switch(task->tk_status) {
9226 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009227 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009228 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009229 case -NFS4ERR_COMPLETE_ALREADY:
9230 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9231 break;
9232 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009233 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009234 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009235 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009236 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009237 case -NFS4ERR_BADSESSION:
9238 case -NFS4ERR_DEADSESSION:
9239 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009240 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009241 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009242 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009243 }
9244 return 0;
9245}
9246
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009247static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9248{
9249 struct nfs4_reclaim_complete_data *calldata = data;
9250 struct nfs_client *clp = calldata->clp;
9251 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9252
9253 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04009254 if (!nfs41_sequence_done(task, res))
9255 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009256
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009257 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009258 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9259 rpc_restart_call_prepare(task);
9260 return;
9261 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009262 dprintk("<-- %s\n", __func__);
9263}
9264
9265static void nfs4_free_reclaim_complete_data(void *data)
9266{
9267 struct nfs4_reclaim_complete_data *calldata = data;
9268
9269 kfree(calldata);
9270}
9271
9272static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9273 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9274 .rpc_call_done = nfs4_reclaim_complete_done,
9275 .rpc_release = nfs4_free_reclaim_complete_data,
9276};
9277
9278/*
9279 * Issue a global reclaim complete.
9280 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009281static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009282 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009283{
9284 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009285 struct rpc_message msg = {
9286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009287 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009288 };
9289 struct rpc_task_setup task_setup_data = {
9290 .rpc_client = clp->cl_rpcclient,
9291 .rpc_message = &msg,
9292 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009293 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009294 };
9295 int status = -ENOMEM;
9296
9297 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009298 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009299 if (calldata == NULL)
9300 goto out;
9301 calldata->clp = clp;
9302 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009303
Anna Schumakerfba83f32018-05-04 16:22:50 -04009304 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009305 msg.rpc_argp = &calldata->arg;
9306 msg.rpc_resp = &calldata->res;
9307 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009308 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009309out:
9310 dprintk("<-- %s status=%d\n", __func__, status);
9311 return status;
9312}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009313
9314static void
9315nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9316{
9317 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009318 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009319
9320 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009321 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009322 &lgp->res.seq_res, task);
9323 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009324}
9325
9326static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9327{
9328 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009329
9330 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009331 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009332 dprintk("<-- %s\n", __func__);
9333}
9334
9335static int
9336nfs4_layoutget_handle_exception(struct rpc_task *task,
9337 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9338{
Trond Myklebustee314c22012-10-01 17:25:48 -07009339 struct inode *inode = lgp->args.inode;
9340 struct nfs_server *server = NFS_SERVER(inode);
9341 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009342 int nfs4err = task->tk_status;
9343 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009344 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009345
Boaz Harroshed7e5422014-01-22 20:34:54 +02009346 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009347
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009348 nfs4_sequence_free_slot(&lgp->res.seq_res);
9349
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009350 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009351 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009352 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009353
9354 /*
9355 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9356 * on the file. set tk_status to -ENODATA to tell upper layer to
9357 * retry go inband.
9358 */
9359 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009360 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009361 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009362 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009363 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9364 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9365 */
9366 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009367 status = -EOVERFLOW;
9368 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009369 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009370 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009371 * (or clients) writing to the same RAID stripe except when
9372 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009373 *
9374 * Treat it like we would RECALLCONFLICT -- we retry for a little
9375 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009376 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009377 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009378 if (lgp->args.minlength == 0) {
9379 status = -EOVERFLOW;
9380 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009381 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009382 status = -EBUSY;
9383 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009384 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009385 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009386 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009387 case -NFS4ERR_DELEG_REVOKED:
9388 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009389 case -NFS4ERR_EXPIRED:
9390 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009391 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009392 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009393 lo = NFS_I(inode)->layout;
9394 /* If the open stateid was bad, then recover it. */
9395 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009396 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009397 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009398 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009399 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009400 break;
9401 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009402
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009403 /*
9404 * Mark the bad layout state as invalid, then retry
9405 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009406 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009407 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009408 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009409 pnfs_free_lseg_list(&head);
9410 status = -EAGAIN;
9411 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009412 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009413
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009414 err = nfs4_handle_exception(server, nfs4err, exception);
9415 if (!status) {
9416 if (exception->retry)
9417 status = -EAGAIN;
9418 else
9419 status = err;
9420 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009421out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009422 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009423 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009424}
9425
Fred Isamandacb4522016-09-19 17:47:09 -04009426size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009427{
9428 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9429 return nfs_page_array_len(0, max_resp_sz);
9430}
9431
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009432static void nfs4_layoutget_release(void *calldata)
9433{
9434 struct nfs4_layoutget *lgp = calldata;
9435
9436 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009437 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009438 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009439 dprintk("<-- %s\n", __func__);
9440}
9441
9442static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9443 .rpc_call_prepare = nfs4_layoutget_prepare,
9444 .rpc_call_done = nfs4_layoutget_done,
9445 .rpc_release = nfs4_layoutget_release,
9446};
9447
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009448struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009449nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009450{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009451 struct inode *inode = lgp->args.inode;
9452 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009453 struct rpc_task *task;
9454 struct rpc_message msg = {
9455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9456 .rpc_argp = &lgp->args,
9457 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009458 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009459 };
9460 struct rpc_task_setup task_setup_data = {
9461 .rpc_client = server->client,
9462 .rpc_message = &msg,
9463 .callback_ops = &nfs4_layoutget_call_ops,
9464 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009465 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009466 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009467 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009468 struct nfs4_exception exception = {
9469 .inode = inode,
9470 .timeout = *timeout,
9471 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009472 int status = 0;
9473
9474 dprintk("--> %s\n", __func__);
9475
Peng Tao4bd5a982014-11-17 11:05:17 +08009476 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9477 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9478
Anna Schumakerfba83f32018-05-04 16:22:50 -04009479 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009480
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009481 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009482
Anna Schumaker820bf852017-01-11 15:01:43 -05009483 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009484 if (status != 0)
9485 goto out;
9486
Trond Myklebust18c07782019-02-13 07:55:31 -05009487 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009488 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9489 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009490 } else if (lgp->res.layoutp->len == 0) {
9491 status = -EAGAIN;
9492 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009493 } else
9494 lseg = pnfs_layout_process(lgp);
9495out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009496 trace_nfs4_layoutget(lgp->args.ctx,
9497 &lgp->args.range,
9498 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009499 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009500 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009501
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009502 rpc_put_task(task);
9503 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009504 if (status)
9505 return ERR_PTR(status);
9506 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009507}
9508
Benny Halevycbe82602011-05-22 19:52:37 +03009509static void
9510nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9511{
9512 struct nfs4_layoutreturn *lrp = calldata;
9513
9514 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009515 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009516 &lrp->args.seq_args,
9517 &lrp->res.seq_res,
9518 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009519 if (!pnfs_layout_is_valid(lrp->args.layout))
9520 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009521}
9522
9523static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9524{
9525 struct nfs4_layoutreturn *lrp = calldata;
9526 struct nfs_server *server;
9527
9528 dprintk("--> %s\n", __func__);
9529
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009530 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009531 return;
9532
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009533 /*
9534 * Was there an RPC level error? Assume the call succeeded,
9535 * and that we need to release the layout
9536 */
9537 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9538 lrp->res.lrs_present = 0;
9539 return;
9540 }
9541
Benny Halevycbe82602011-05-22 19:52:37 +03009542 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009543 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009544 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009545 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009546 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009547 lrp->args.inode))
9548 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009549 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009550 default:
9551 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009552 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009553 case 0:
9554 break;
9555 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009556 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009557 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009558 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009559 }
Benny Halevycbe82602011-05-22 19:52:37 +03009560 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009561 return;
9562out_restart:
9563 task->tk_status = 0;
9564 nfs4_sequence_free_slot(&lrp->res.seq_res);
9565 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009566}
9567
9568static void nfs4_layoutreturn_release(void *calldata)
9569{
9570 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009571 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009572
9573 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009574 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009575 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009576 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009577 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9578 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009579 pnfs_put_layout_hdr(lrp->args.layout);
9580 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009581 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009582 kfree(calldata);
9583 dprintk("<-- %s\n", __func__);
9584}
9585
9586static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9587 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9588 .rpc_call_done = nfs4_layoutreturn_done,
9589 .rpc_release = nfs4_layoutreturn_release,
9590};
9591
Peng Tao6c166052014-11-17 09:30:40 +08009592int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009593{
9594 struct rpc_task *task;
9595 struct rpc_message msg = {
9596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9597 .rpc_argp = &lrp->args,
9598 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009599 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009600 };
9601 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009602 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009603 .rpc_message = &msg,
9604 .callback_ops = &nfs4_layoutreturn_call_ops,
9605 .callback_data = lrp,
9606 };
Peng Tao6c166052014-11-17 09:30:40 +08009607 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009608
Andrew Elble99ade3c2015-12-02 09:39:51 -05009609 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9610 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9611 &task_setup_data.rpc_client, &msg);
9612
Benny Halevycbe82602011-05-22 19:52:37 +03009613 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009614 if (!sync) {
9615 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9616 if (!lrp->inode) {
9617 nfs4_layoutreturn_release(lrp);
9618 return -EAGAIN;
9619 }
9620 task_setup_data.flags |= RPC_TASK_ASYNC;
9621 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009622 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009623 task = rpc_run_task(&task_setup_data);
9624 if (IS_ERR(task))
9625 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009626 if (sync)
9627 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009628 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009629 dprintk("<-- %s status=%d\n", __func__, status);
9630 rpc_put_task(task);
9631 return status;
9632}
9633
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009634static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009635_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9636 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009637 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009638{
9639 struct nfs4_getdeviceinfo_args args = {
9640 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009641 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9642 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009643 };
9644 struct nfs4_getdeviceinfo_res res = {
9645 .pdev = pdev,
9646 };
9647 struct rpc_message msg = {
9648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9649 .rpc_argp = &args,
9650 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009651 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009652 };
9653 int status;
9654
9655 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009656 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009657 if (res.notification & ~args.notify_types)
9658 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009659 if (res.notification != args.notify_types)
9660 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009661
Trond Myklebustcac1d3a2020-12-16 16:31:26 -05009662 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9663
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009664 dprintk("<-- %s status=%d\n", __func__, status);
9665
9666 return status;
9667}
9668
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009669int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9670 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009671 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009672{
9673 struct nfs4_exception exception = { };
9674 int err;
9675
9676 do {
9677 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009678 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009679 &exception);
9680 } while (exception.retry);
9681 return err;
9682}
9683EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9684
Andy Adamson863a3c62011-03-23 13:27:54 +00009685static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9686{
9687 struct nfs4_layoutcommit_data *data = calldata;
9688 struct nfs_server *server = NFS_SERVER(data->args.inode);
9689
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009690 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009691 &data->args.seq_args,
9692 &data->res.seq_res,
9693 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009694}
9695
9696static void
9697nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9698{
9699 struct nfs4_layoutcommit_data *data = calldata;
9700 struct nfs_server *server = NFS_SERVER(data->args.inode);
9701
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009702 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009703 return;
9704
9705 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009706 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9707 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9708 case -NFS4ERR_BADLAYOUT: /* no layout */
9709 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009710 task->tk_status = 0;
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06009711 break;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009712 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009713 break;
9714 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009715 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009716 rpc_restart_call_prepare(task);
9717 return;
9718 }
9719 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009720}
9721
9722static void nfs4_layoutcommit_release(void *calldata)
9723{
9724 struct nfs4_layoutcommit_data *data = calldata;
9725
Andy Adamsondb29c082011-07-30 20:52:38 -04009726 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009727 nfs_post_op_update_inode_force_wcc(data->args.inode,
9728 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009729 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009730 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009731 kfree(data);
9732}
9733
9734static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9735 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9736 .rpc_call_done = nfs4_layoutcommit_done,
9737 .rpc_release = nfs4_layoutcommit_release,
9738};
9739
9740int
Andy Adamsonef311532011-03-12 02:58:10 -05009741nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009742{
9743 struct rpc_message msg = {
9744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9745 .rpc_argp = &data->args,
9746 .rpc_resp = &data->res,
9747 .rpc_cred = data->cred,
9748 };
9749 struct rpc_task_setup task_setup_data = {
9750 .task = &data->task,
9751 .rpc_client = NFS_CLIENT(data->args.inode),
9752 .rpc_message = &msg,
9753 .callback_ops = &nfs4_layoutcommit_ops,
9754 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009755 };
9756 struct rpc_task *task;
9757 int status = 0;
9758
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009759 dprintk("NFS: initiating layoutcommit call. sync %d "
9760 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009761 data->args.lastbytewritten,
9762 data->args.inode->i_ino);
9763
Trond Myklebust472e2592015-02-05 16:50:30 -05009764 if (!sync) {
9765 data->inode = nfs_igrab_and_active(data->args.inode);
9766 if (data->inode == NULL) {
9767 nfs4_layoutcommit_release(data);
9768 return -EAGAIN;
9769 }
9770 task_setup_data.flags = RPC_TASK_ASYNC;
9771 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009772 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009773 task = rpc_run_task(&task_setup_data);
9774 if (IS_ERR(task))
9775 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009776 if (sync)
9777 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009778 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009779 dprintk("%s: status %d\n", __func__, status);
9780 rpc_put_task(task);
9781 return status;
9782}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009783
Trond Myklebust302fad72019-02-18 13:32:38 -05009784/*
Andy Adamson97431202013-08-08 10:57:56 -04009785 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9786 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9787 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009788static int
9789_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009790 struct nfs_fsinfo *info,
9791 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009792{
9793 struct nfs41_secinfo_no_name_args args = {
9794 .style = SECINFO_STYLE_CURRENT_FH,
9795 };
9796 struct nfs4_secinfo_res res = {
9797 .flavors = flavors,
9798 };
9799 struct rpc_message msg = {
9800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9801 .rpc_argp = &args,
9802 .rpc_resp = &res,
9803 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009804 struct nfs4_call_sync_data data = {
9805 .seq_server = server,
9806 .seq_args = &args.seq_args,
9807 .seq_res = &res.seq_res,
9808 };
9809 struct rpc_task_setup task_setup = {
9810 .rpc_client = server->client,
9811 .rpc_message = &msg,
9812 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9813 .callback_data = &data,
9814 .flags = RPC_TASK_NO_ROUND_ROBIN,
9815 };
NeilBrowna52458b2018-12-03 11:30:31 +11009816 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009817 int status;
9818
9819 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009820 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009821
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009822 cred = nfs4_get_clid_cred(server->nfs_client);
9823 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009824 }
9825
9826 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009827 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9828 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009829 dprintk("<-- %s status=%d\n", __func__, status);
9830
NeilBrowna52458b2018-12-03 11:30:31 +11009831 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009832
9833 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009834}
9835
9836static int
9837nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9838 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9839{
Trond Myklebust0688e642019-04-07 13:59:09 -04009840 struct nfs4_exception exception = {
9841 .interruptible = true,
9842 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009843 int err;
9844 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009845 /* first try using integrity protection */
9846 err = -NFS4ERR_WRONGSEC;
9847
9848 /* try to use integrity protection with machine cred */
9849 if (_nfs4_is_integrity_protected(server->nfs_client))
9850 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9851 flavors, true);
9852
9853 /*
9854 * if unable to use integrity protection, or SECINFO with
9855 * integrity protection returns NFS4ERR_WRONGSEC (which is
9856 * disallowed by spec, but exists in deployed servers) use
9857 * the current filesystem's rpc_client and the user cred.
9858 */
9859 if (err == -NFS4ERR_WRONGSEC)
9860 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9861 flavors, false);
9862
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009863 switch (err) {
9864 case 0:
9865 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009866 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009867 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009868 default:
9869 err = nfs4_handle_exception(server, err, &exception);
9870 }
9871 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009872out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009873 return err;
9874}
9875
9876static int
9877nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9878 struct nfs_fsinfo *info)
9879{
9880 int err;
9881 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009882 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009883 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009884 struct nfs4_secinfo4 *secinfo;
9885 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009886
9887 page = alloc_page(GFP_KERNEL);
9888 if (!page) {
9889 err = -ENOMEM;
9890 goto out;
9891 }
9892
9893 flavors = page_address(page);
9894 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9895
9896 /*
9897 * Fall back on "guess and check" method if
9898 * the server doesn't support SECINFO_NO_NAME
9899 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009900 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009901 err = nfs4_find_root_sec(server, fhandle, info);
9902 goto out_freepage;
9903 }
9904 if (err)
9905 goto out_freepage;
9906
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009907 for (i = 0; i < flavors->num_flavors; i++) {
9908 secinfo = &flavors->flavors[i];
9909
9910 switch (secinfo->flavor) {
9911 case RPC_AUTH_NULL:
9912 case RPC_AUTH_UNIX:
9913 case RPC_AUTH_GSS:
9914 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9915 &secinfo->flavor_info);
9916 break;
9917 default:
9918 flavor = RPC_AUTH_MAXFLAVOR;
9919 break;
9920 }
9921
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009922 if (!nfs_auth_info_match(&server->auth_info, flavor))
9923 flavor = RPC_AUTH_MAXFLAVOR;
9924
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009925 if (flavor != RPC_AUTH_MAXFLAVOR) {
9926 err = nfs4_lookup_root_sec(server, fhandle,
9927 info, flavor);
9928 if (!err)
9929 break;
9930 }
9931 }
9932
9933 if (flavor == RPC_AUTH_MAXFLAVOR)
9934 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009935
9936out_freepage:
9937 put_page(page);
9938 if (err == -EACCES)
9939 return -EPERM;
9940out:
9941 return err;
9942}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009943
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009944static int _nfs41_test_stateid(struct nfs_server *server,
9945 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009946 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009947{
9948 int status;
9949 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009950 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009951 };
9952 struct nfs41_test_stateid_res res;
9953 struct rpc_message msg = {
9954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9955 .rpc_argp = &args,
9956 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009957 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009958 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009959 struct rpc_clnt *rpc_client = server->client;
9960
9961 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9962 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009963
Chuck Lever38527b12012-07-11 16:30:23 -04009964 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009965 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009966 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009967 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009968 if (status != NFS_OK) {
9969 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009970 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009971 }
9972 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009973 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009974}
9975
Trond Myklebust43912bb2016-09-22 13:38:56 -04009976static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9977 int err, struct nfs4_exception *exception)
9978{
9979 exception->retry = 0;
9980 switch(err) {
9981 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009982 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009983 nfs4_handle_exception(server, err, exception);
9984 break;
9985 case -NFS4ERR_BADSESSION:
9986 case -NFS4ERR_BADSLOT:
9987 case -NFS4ERR_BAD_HIGH_SLOT:
9988 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9989 case -NFS4ERR_DEADSESSION:
9990 nfs4_do_handle_exception(server, err, exception);
9991 }
9992}
9993
Chuck Lever38527b12012-07-11 16:30:23 -04009994/**
9995 * nfs41_test_stateid - perform a TEST_STATEID operation
9996 *
9997 * @server: server / transport on which to perform the operation
9998 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009999 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -040010000 *
10001 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10002 * Otherwise a negative NFS4ERR value is returned if the operation
10003 * failed or the state ID is not currently valid.
10004 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010005static int nfs41_test_stateid(struct nfs_server *server,
10006 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010007 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010008{
Trond Myklebust0688e642019-04-07 13:59:09 -040010009 struct nfs4_exception exception = {
10010 .interruptible = true,
10011 };
Bryan Schumaker7d974792011-06-02 14:59:08 -040010012 int err;
10013 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010014 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -040010015 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -040010016 } while (exception.retry);
10017 return err;
10018}
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010019
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010020struct nfs_free_stateid_data {
10021 struct nfs_server *server;
10022 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010023 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010024};
10025
10026static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10027{
10028 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -050010029 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010030 &data->args.seq_args,
10031 &data->res.seq_res,
10032 task);
10033}
10034
10035static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10036{
10037 struct nfs_free_stateid_data *data = calldata;
10038
10039 nfs41_sequence_done(task, &data->res.seq_res);
10040
10041 switch (task->tk_status) {
10042 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +100010043 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010044 rpc_restart_call_prepare(task);
10045 }
10046}
10047
10048static void nfs41_free_stateid_release(void *calldata)
10049{
10050 kfree(calldata);
10051}
10052
Trond Myklebust17f26b12013-08-21 15:48:42 -040010053static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010054 .rpc_call_prepare = nfs41_free_stateid_prepare,
10055 .rpc_call_done = nfs41_free_stateid_done,
10056 .rpc_release = nfs41_free_stateid_release,
10057};
10058
Anna Schumaker2f261022018-05-15 13:03:39 -040010059/**
10060 * nfs41_free_stateid - perform a FREE_STATEID operation
10061 *
10062 * @server: server / transport on which to perform the operation
10063 * @stateid: state ID to release
10064 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010065 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010066 *
10067 * Note: this function is always asynchronous.
10068 */
10069static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010070 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010071 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010072 bool privileged)
10073{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010074 struct rpc_message msg = {
10075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010076 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010077 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010078 struct rpc_task_setup task_setup = {
10079 .rpc_client = server->client,
10080 .rpc_message = &msg,
10081 .callback_ops = &nfs41_free_stateid_ops,
10082 .flags = RPC_TASK_ASYNC,
10083 };
10084 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010085 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010086
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010087 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10088 &task_setup.rpc_client, &msg);
10089
Chuck Lever38527b12012-07-11 16:30:23 -040010090 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010091 data = kmalloc(sizeof(*data), GFP_NOFS);
10092 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010093 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010094 data->server = server;
10095 nfs4_stateid_copy(&data->args.stateid, stateid);
10096
10097 task_setup.callback_data = data;
10098
10099 msg.rpc_argp = &data->args;
10100 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010101 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010102 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010103 if (IS_ERR(task))
10104 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010105 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010106 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010107}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010108
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010109static void
10110nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010111{
NeilBrowna52458b2018-12-03 11:30:31 +110010112 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010113
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010114 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010115 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010116}
10117
Trond Myklebust36281ca2012-03-04 18:13:56 -050010118static bool nfs41_match_stateid(const nfs4_stateid *s1,
10119 const nfs4_stateid *s2)
10120{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010121 if (s1->type != s2->type)
10122 return false;
10123
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010124 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010125 return false;
10126
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010127 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010128 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010129
Anna Schumaker045c5512017-01-11 16:59:48 -050010130 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010131}
10132
Andy Adamson557134a2009-04-01 09:21:53 -040010133#endif /* CONFIG_NFS_V4_1 */
10134
Trond Myklebust36281ca2012-03-04 18:13:56 -050010135static bool nfs4_match_stateid(const nfs4_stateid *s1,
10136 const nfs4_stateid *s2)
10137{
Trond Myklebustf597c532012-03-04 18:13:56 -050010138 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010139}
10140
10141
Trond Myklebust17280172012-03-11 13:11:00 -040010142static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010143 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010144 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145 .recover_open = nfs4_open_reclaim,
10146 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010147 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010148 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010149};
10150
Andy Adamson591d71c2009-04-01 09:22:47 -040010151#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010152static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010153 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10154 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10155 .recover_open = nfs4_open_reclaim,
10156 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010157 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010158 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010159 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010160};
10161#endif /* CONFIG_NFS_V4_1 */
10162
Trond Myklebust17280172012-03-11 13:11:00 -040010163static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010164 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010165 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010166 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010168 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169};
10170
Andy Adamson591d71c2009-04-01 09:22:47 -040010171#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010172static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010173 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10174 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010175 .recover_open = nfs41_open_expired,
10176 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010177 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010178};
10179#endif /* CONFIG_NFS_V4_1 */
10180
Trond Myklebust17280172012-03-11 13:11:00 -040010181static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010182 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010183 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010184 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010185};
10186
10187#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010188static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010189 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010190 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010191 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010192};
10193#endif
10194
Chuck Leverec011fe2013-10-17 14:12:39 -040010195static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010196 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010197 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010198};
10199
10200#if defined(CONFIG_NFS_V4_1)
10201static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010202 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010203 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010204};
10205#endif /* CONFIG_NFS_V4_1 */
10206
Trond Myklebust97dc1352010-06-16 09:52:26 -040010207static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10208 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010209 .init_caps = NFS_CAP_READDIRPLUS
10210 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010211 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010212 .init_client = nfs40_init_client,
10213 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010214 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010215 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010216 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010217 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010218 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010219 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010220 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10221 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10222 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010223 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010224};
10225
10226#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010227static struct nfs_seqid *
10228nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10229{
10230 return NULL;
10231}
10232
Trond Myklebust97dc1352010-06-16 09:52:26 -040010233static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10234 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010235 .init_caps = NFS_CAP_READDIRPLUS
10236 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010237 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010238 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010239 | NFS_CAP_ATOMIC_OPEN_V1
10240 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010241 .init_client = nfs41_init_client,
10242 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010243 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010244 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010245 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010246 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010247 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010248 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010249 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010250 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10251 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10252 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010253 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010254};
10255#endif
10256
Steve Dickson42c2c422013-05-22 12:50:38 -040010257#if defined(CONFIG_NFS_V4_2)
10258static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10259 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010260 .init_caps = NFS_CAP_READDIRPLUS
10261 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010262 | NFS_CAP_POSIX_LOCK
10263 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010264 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010265 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010266 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010267 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010268 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010269 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010270 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010271 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010272 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010273 | NFS_CAP_CLONE
Anna Schumakerc5675522014-05-28 13:41:22 -040010274 | NFS_CAP_LAYOUTERROR
10275 | NFS_CAP_READ_PLUS,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010276 .init_client = nfs41_init_client,
10277 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010278 .match_stateid = nfs41_match_stateid,
10279 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010280 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010281 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010282 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010283 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010284 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010285 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10286 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10287 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010288 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010289};
10290#endif
10291
Trond Myklebust97dc1352010-06-16 09:52:26 -040010292const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10293 [0] = &nfs_v4_0_minor_ops,
10294#if defined(CONFIG_NFS_V4_1)
10295 [1] = &nfs_v4_1_minor_ops,
10296#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010297#if defined(CONFIG_NFS_V4_2)
10298 [2] = &nfs_v4_2_minor_ops,
10299#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010300};
10301
Trond Myklebust13997822016-07-24 17:10:52 -040010302static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010303{
Frank van der Linden012a2112020-06-23 22:39:03 +000010304 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010305
10306 error = generic_listxattr(dentry, list, size);
10307 if (error < 0)
10308 return error;
10309 if (list) {
10310 list += error;
10311 size -= error;
10312 }
10313
10314 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10315 if (error2 < 0)
10316 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010317
10318 if (list) {
10319 list += error2;
10320 size -= error2;
10321 }
10322
10323 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10324 if (error3 < 0)
10325 return error3;
10326
10327 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010328}
10329
Trond Myklebust17f26b12013-08-21 15:48:42 -040010330static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010331 .create = nfs_create,
10332 .lookup = nfs_lookup,
10333 .atomic_open = nfs_atomic_open,
10334 .link = nfs_link,
10335 .unlink = nfs_unlink,
10336 .symlink = nfs_symlink,
10337 .mkdir = nfs_mkdir,
10338 .rmdir = nfs_rmdir,
10339 .mknod = nfs_mknod,
10340 .rename = nfs_rename,
10341 .permission = nfs_permission,
10342 .getattr = nfs_getattr,
10343 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010344 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010345};
10346
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010347static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010348 .permission = nfs_permission,
10349 .getattr = nfs_getattr,
10350 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010351 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010352};
10353
David Howells509de812006-08-22 20:06:11 -040010354const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010355 .version = 4, /* protocol version */
10356 .dentry_ops = &nfs4_dentry_operations,
10357 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010358 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010359 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010360 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010361 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010362 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010363 .getattr = nfs4_proc_getattr,
10364 .setattr = nfs4_proc_setattr,
10365 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010366 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010367 .access = nfs4_proc_access,
10368 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010369 .create = nfs4_proc_create,
10370 .remove = nfs4_proc_remove,
10371 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010372 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010373 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010374 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010375 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010376 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010377 .link = nfs4_proc_link,
10378 .symlink = nfs4_proc_symlink,
10379 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010380 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010381 .readdir = nfs4_proc_readdir,
10382 .mknod = nfs4_proc_mknod,
10383 .statfs = nfs4_proc_statfs,
10384 .fsinfo = nfs4_proc_fsinfo,
10385 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010386 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010387 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010388 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010389 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010390 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010391 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010392 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010393 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010394 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010395 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010396 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010397 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010398 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010399 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010400 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010401 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010402 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010403 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010404 .create_server = nfs4_create_server,
10405 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010406};
10407
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010408static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010409 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010410 .list = nfs4_xattr_list_nfs4_acl,
10411 .get = nfs4_xattr_get_nfs4_acl,
10412 .set = nfs4_xattr_set_nfs4_acl,
10413};
10414
Frank van der Linden012a2112020-06-23 22:39:03 +000010415#ifdef CONFIG_NFS_V4_2
10416static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10417 .prefix = XATTR_USER_PREFIX,
10418 .get = nfs4_xattr_get_nfs4_user,
10419 .set = nfs4_xattr_set_nfs4_user,
10420};
10421#endif
10422
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010423const struct xattr_handler *nfs4_xattr_handlers[] = {
10424 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010425#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10426 &nfs4_xattr_nfs4_label_handler,
10427#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010428#ifdef CONFIG_NFS_V4_2
10429 &nfs4_xattr_nfs4_user_handler,
10430#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010431 NULL
10432};
10433
Linus Torvalds1da177e2005-04-16 15:20:36 -070010434/*
10435 * Local variables:
10436 * c-basic-offset: 8
10437 * End:
10438 */