blob: 8e03a647c61aa50d0092f2ad4f0817b6ae4b427d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust39d43d12020-10-07 18:24:18 -040066#include "sysfs.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040067#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050068#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000069#include "fscache.h"
Frank van der Linden012a2112020-06-23 22:39:03 +000070#include "nfs42.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Trond Myklebustc6d01c62013-08-09 11:51:26 -040072#include "nfs4trace.h"
73
Anna Schumakerc5675522014-05-28 13:41:22 -040074#ifdef CONFIG_NFS_V4_2
75#include "nfs42.h"
76#endif /* CONFIG_NFS_V4_2 */
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define NFSDBG_FACILITY NFSDBG_PROC
79
Trond Myklebust30846df2018-04-07 13:44:28 -040080#define NFS4_BITMASK_SZ 3
81
Trond Myklebust2066fe82006-09-15 08:30:46 -040082#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define NFS4_POLL_RETRY_MAX (15*HZ)
84
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020085/* file attributes which can be mapped to nfs attributes */
86#define NFS4_VALID_ATTRS (ATTR_MODE \
87 | ATTR_UID \
88 | ATTR_GID \
89 | ATTR_SIZE \
90 | ATTR_ATIME \
91 | ATTR_MTIME \
92 | ATTR_CTIME \
93 | ATTR_ATIME_SET \
94 | ATTR_MTIME_SET)
95
Trond Myklebustcdd4e682006-01-03 09:55:12 +010096struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080097static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050099static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -0400100static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +1100101static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -0400102 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +1100103 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -0400104 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400105#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400106static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100107 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400108 struct nfs4_slot *slot,
109 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400110static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100111 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400112static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100113 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400114#endif
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -0400115static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
116 struct nfs_server *server,
117 struct nfs4_label *label);
David Quigleyaa9c2662013-05-22 12:50:44 -0400118
119#ifdef CONFIG_NFS_V4_SECURITY_LABEL
120static inline struct nfs4_label *
121nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
122 struct iattr *sattr, struct nfs4_label *label)
123{
124 int err;
125
126 if (label == NULL)
127 return NULL;
128
129 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
130 return NULL;
131
David Quigleyaa9c2662013-05-22 12:50:44 -0400132 err = security_dentry_init_security(dentry, sattr->ia_mode,
133 &dentry->d_name, (void **)&label->label, &label->len);
134 if (err == 0)
135 return label;
136
137 return NULL;
138}
139static inline void
140nfs4_label_release_security(struct nfs4_label *label)
141{
142 if (label)
143 security_release_secctx(label->label, label->len);
144}
145static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
146{
147 if (label)
148 return server->attr_bitmask;
149
150 return server->attr_bitmask_nl;
151}
152#else
153static inline struct nfs4_label *
154nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
155 struct iattr *sattr, struct nfs4_label *l)
156{ return NULL; }
157static inline void
158nfs4_label_release_security(struct nfs4_label *label)
159{ return; }
160static inline u32 *
161nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
162{ return server->attr_bitmask; }
163#endif
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500166static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 if (err >= -1000)
169 return err;
170 switch (err) {
171 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500172 case -NFS4ERR_LAYOUTTRYLATER:
173 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000175 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400176 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000177 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800178 case -NFS4ERR_BADOWNER:
179 case -NFS4ERR_BADNAME:
180 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400181 case -NFS4ERR_SHARE_DENIED:
182 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400183 case -NFS4ERR_MINOR_VERS_MISMATCH:
184 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400185 case -NFS4ERR_FILE_OPEN:
186 return -EBUSY;
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500187 case -NFS4ERR_NOT_SAME:
188 return -ENOTSYNC;
Trond Myklebust52567b02009-10-23 14:46:42 -0400189 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700191 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400194 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/*
198 * This is our standard bitmap for GETATTR requests.
199 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400200const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 FATTR4_WORD0_TYPE
202 | FATTR4_WORD0_CHANGE
203 | FATTR4_WORD0_SIZE
204 | FATTR4_WORD0_FSID
205 | FATTR4_WORD0_FILEID,
206 FATTR4_WORD1_MODE
207 | FATTR4_WORD1_NUMLINKS
208 | FATTR4_WORD1_OWNER
209 | FATTR4_WORD1_OWNER_GROUP
210 | FATTR4_WORD1_RAWDEV
211 | FATTR4_WORD1_SPACE_USED
212 | FATTR4_WORD1_TIME_ACCESS
213 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400214 | FATTR4_WORD1_TIME_MODIFY
215 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400216#ifdef CONFIG_NFS_V4_SECURITY_LABEL
217 FATTR4_WORD2_SECURITY_LABEL
218#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
Trond Myklebust1549210f2012-06-05 09:16:47 -0400221static const u32 nfs4_pnfs_open_bitmap[3] = {
222 FATTR4_WORD0_TYPE
223 | FATTR4_WORD0_CHANGE
224 | FATTR4_WORD0_SIZE
225 | FATTR4_WORD0_FSID
226 | FATTR4_WORD0_FILEID,
227 FATTR4_WORD1_MODE
228 | FATTR4_WORD1_NUMLINKS
229 | FATTR4_WORD1_OWNER
230 | FATTR4_WORD1_OWNER_GROUP
231 | FATTR4_WORD1_RAWDEV
232 | FATTR4_WORD1_SPACE_USED
233 | FATTR4_WORD1_TIME_ACCESS
234 | FATTR4_WORD1_TIME_METADATA
235 | FATTR4_WORD1_TIME_MODIFY,
236 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500237#ifdef CONFIG_NFS_V4_SECURITY_LABEL
238 | FATTR4_WORD2_SECURITY_LABEL
239#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400240};
241
Andy Adamsone23008e2012-10-02 21:07:32 -0400242static const u32 nfs4_open_noattr_bitmap[3] = {
243 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400244 | FATTR4_WORD0_FILEID,
245};
246
David Quigleya09df2c2013-05-22 12:50:41 -0400247const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 FATTR4_WORD0_FILES_AVAIL
249 | FATTR4_WORD0_FILES_FREE
250 | FATTR4_WORD0_FILES_TOTAL,
251 FATTR4_WORD1_SPACE_AVAIL
252 | FATTR4_WORD1_SPACE_FREE
253 | FATTR4_WORD1_SPACE_TOTAL
254};
255
David Quigleya09df2c2013-05-22 12:50:41 -0400256const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 FATTR4_WORD0_MAXLINK
258 | FATTR4_WORD0_MAXNAME,
259 0
260};
261
Fred Isamandae100c2011-07-30 20:52:37 -0400262const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 | FATTR4_WORD0_MAXREAD
264 | FATTR4_WORD0_MAXWRITE
265 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700266 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400267 | FATTR4_WORD1_FS_LAYOUT_TYPES,
268 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800269 | FATTR4_WORD2_CLONE_BLKSIZE
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000270 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
David Quigleya09df2c2013-05-22 12:50:41 -0400273const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500274 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400275 | FATTR4_WORD0_SIZE
276 | FATTR4_WORD0_FSID
277 | FATTR4_WORD0_FILEID
278 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500279 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400280 | FATTR4_WORD1_OWNER_GROUP
281 | FATTR4_WORD1_RAWDEV
282 | FATTR4_WORD1_SPACE_USED
283 | FATTR4_WORD1_TIME_ACCESS
284 | FATTR4_WORD1_TIME_METADATA
285 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400286 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400287};
288
Trond Myklebust30846df2018-04-07 13:44:28 -0400289static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
290 struct inode *inode)
291{
292 unsigned long cache_validity;
293
294 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
295 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
296 return;
297
298 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
299 if (!(cache_validity & NFS_INO_REVAL_FORCED))
300 cache_validity &= ~(NFS_INO_INVALID_CHANGE
301 | NFS_INO_INVALID_SIZE);
302
303 if (!(cache_validity & NFS_INO_INVALID_SIZE))
304 dst[0] &= ~FATTR4_WORD0_SIZE;
305
306 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
307 dst[0] &= ~FATTR4_WORD0_CHANGE;
308}
309
310static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
311 const __u32 *src, struct inode *inode)
312{
313 nfs4_bitmap_copy_adjust(dst, src, inode);
314}
315
Al Virobc4785c2006-10-19 23:28:51 -0700316static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct nfs4_readdir_arg *readdir)
318{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400319 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700320 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000323 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
325 return;
326 }
327
328 readdir->cookie = 0;
329 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
330 if (cookie == 2)
331 return;
332
333 /*
334 * NFSv4 servers do not return entries for '.' and '..'
335 * Therefore, we fake these entries here. We let '.'
336 * have cookie 0 and '..' have cookie 1. Note that
337 * when talking to the server, we always send cookie 0
338 * instead of 1 or 2.
339 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800340 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (cookie == 0) {
343 *p++ = xdr_one; /* next */
344 *p++ = xdr_zero; /* cookie, first word */
345 *p++ = xdr_one; /* cookie, second word */
346 *p++ = xdr_one; /* entry len */
347 memcpy(p, ".\0\0\0", 4); /* entry */
348 p++;
349 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400350 *p++ = htonl(attrs); /* bitmap */
351 *p++ = htonl(12); /* attribute buffer length */
352 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000353 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
356 *p++ = xdr_one; /* next */
357 *p++ = xdr_zero; /* cookie, first word */
358 *p++ = xdr_two; /* cookie, second word */
359 *p++ = xdr_two; /* entry len */
360 memcpy(p, "..\0\0", 4); /* entry */
361 p++;
362 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400363 *p++ = htonl(attrs); /* bitmap */
364 *p++ = htonl(12); /* attribute buffer length */
365 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000366 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 readdir->pgbase = (char *)p - (char *)start;
369 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800370 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Trond Myklebust26d36302016-09-22 13:39:05 -0400373static void nfs4_test_and_free_stateid(struct nfs_server *server,
374 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100375 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400376{
377 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
378
379 ops->test_and_free_expired(server, stateid, cred);
380}
381
382static void __nfs4_free_revoked_stateid(struct nfs_server *server,
383 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100384 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400385{
386 stateid->type = NFS4_REVOKED_STATEID_TYPE;
387 nfs4_test_and_free_stateid(server, stateid, cred);
388}
389
390static void nfs4_free_revoked_stateid(struct nfs_server *server,
391 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100392 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400393{
394 nfs4_stateid tmp;
395
396 nfs4_stateid_copy(&tmp, stateid);
397 __nfs4_free_revoked_stateid(server, &tmp, cred);
398}
399
NeilBrown8478eaa2014-09-18 16:09:27 +1000400static long nfs4_update_delay(long *timeout)
401{
402 long ret;
403 if (!timeout)
404 return NFS4_POLL_RETRY_MAX;
405 if (*timeout <= 0)
406 *timeout = NFS4_POLL_RETRY_MIN;
407 if (*timeout > NFS4_POLL_RETRY_MAX)
408 *timeout = NFS4_POLL_RETRY_MAX;
409 ret = *timeout;
410 *timeout <<= 1;
411 return ret;
412}
413
Trond Myklebust0688e642019-04-07 13:59:09 -0400414static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500415{
Trond Myklebust65de8722008-12-23 15:21:44 -0500416 might_sleep();
417
NeilBrown8478eaa2014-09-18 16:09:27 +1000418 freezable_schedule_timeout_killable_unsafe(
419 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400420 if (!__fatal_signal_pending(current))
421 return 0;
422 return -EINTR;
423}
424
425static int nfs4_delay_interruptible(long *timeout)
426{
427 might_sleep();
428
He Zhe59679d92020-07-06 17:52:24 +0800429 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400430 if (!signal_pending(current))
431 return 0;
432 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
433}
434
435static int nfs4_delay(long *timeout, bool interruptible)
436{
437 if (interruptible)
438 return nfs4_delay_interruptible(timeout);
439 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500440}
441
Trond Myklebust50c80002019-07-11 19:02:18 -0400442static const nfs4_stateid *
443nfs4_recoverable_stateid(const nfs4_stateid *stateid)
444{
445 if (!stateid)
446 return NULL;
447 switch (stateid->type) {
448 case NFS4_OPEN_STATEID_TYPE:
449 case NFS4_LOCK_STATEID_TYPE:
450 case NFS4_DELEGATION_STATEID_TYPE:
451 return stateid;
452 default:
453 break;
454 }
455 return NULL;
456}
457
Trond Myklebust65de8722008-12-23 15:21:44 -0500458/* This is the error handling routine for processes that are allowed
459 * to sleep.
460 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400461static int nfs4_do_handle_exception(struct nfs_server *server,
462 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500463{
464 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500465 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400466 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500467 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 int ret = errorcode;
469
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400470 exception->delay = 0;
471 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500472 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400473
Trond Myklebust50c80002019-07-11 19:02:18 -0400474 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400475 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400476 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400477
Trond Myklebust65de8722008-12-23 15:21:44 -0500478 switch(errorcode) {
479 case 0:
480 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400481 case -NFS4ERR_BADHANDLE:
482 case -ESTALE:
483 if (inode != NULL && S_ISREG(inode->i_mode))
484 pnfs_destroy_layout(NFS_I(inode));
485 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400486 case -NFS4ERR_DELEG_REVOKED:
487 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400488 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400489 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400490 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400491 if (inode != NULL && stateid != NULL) {
492 nfs_inode_find_state_and_recover(inode,
493 stateid);
494 goto wait_on_recovery;
495 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500496 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400497 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400498 if (inode) {
499 int err;
500
501 err = nfs_async_inode_return_delegation(inode,
502 stateid);
503 if (err == 0)
504 goto wait_on_recovery;
505 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
506 exception->retry = 1;
507 break;
508 }
509 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500510 if (state == NULL)
511 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400512 ret = nfs4_schedule_stateid_recovery(server, state);
513 if (ret < 0)
514 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500515 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500516 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500517 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500518 nfs4_schedule_lease_recovery(clp);
519 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400520 case -NFS4ERR_MOVED:
521 ret = nfs4_schedule_migration_recovery(server);
522 if (ret < 0)
523 break;
524 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400525 case -NFS4ERR_LEASE_MOVED:
526 nfs4_schedule_lease_moved_recovery(clp);
527 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500528#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400529 case -NFS4ERR_BADSESSION:
530 case -NFS4ERR_BADSLOT:
531 case -NFS4ERR_BAD_HIGH_SLOT:
532 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
533 case -NFS4ERR_DEADSESSION:
534 case -NFS4ERR_SEQ_FALSE_RETRY:
535 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100536 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400537 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500538#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500539 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500540 if (exception->timeout > HZ) {
541 /* We have retried a decent amount, time to
542 * fail
543 */
544 ret = -EBUSY;
545 break;
546 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500547 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500548 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400549 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500550 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400551 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400552 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400553 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400554 exception->delay = 1;
555 return 0;
556
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400557 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500558 case -NFS4ERR_OLD_STATEID:
559 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800560 break;
561 case -NFS4ERR_BADOWNER:
562 /* The following works around a Linux server bug! */
563 case -NFS4ERR_BADNAME:
564 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
565 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
566 exception->retry = 1;
567 printk(KERN_WARNING "NFS: v4 server %s "
568 "does not accept raw "
569 "uid/gids. "
570 "Reenabling the idmapper.\n",
571 server->nfs_client->cl_hostname);
572 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500573 }
574 /* We failed to handle the error */
575 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500576wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400577 exception->recovering = 1;
578 return 0;
579}
580
581/* This is the error handling routine for processes that are allowed
582 * to sleep.
583 */
584int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
585{
586 struct nfs_client *clp = server->nfs_client;
587 int ret;
588
589 ret = nfs4_do_handle_exception(server, errorcode, exception);
590 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400591 ret = nfs4_delay(&exception->timeout,
592 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400593 goto out_retry;
594 }
595 if (exception->recovering) {
596 ret = nfs4_wait_clnt_recover(clp);
597 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
598 return -EIO;
599 goto out_retry;
600 }
601 return ret;
602out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500603 if (ret == 0)
604 exception->retry = 1;
605 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500606}
607
Trond Myklebust037fc982015-09-20 15:51:00 -0400608static int
609nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
610 int errorcode, struct nfs4_exception *exception)
611{
612 struct nfs_client *clp = server->nfs_client;
613 int ret;
614
615 ret = nfs4_do_handle_exception(server, errorcode, exception);
616 if (exception->delay) {
617 rpc_delay(task, nfs4_update_delay(&exception->timeout));
618 goto out_retry;
619 }
620 if (exception->recovering) {
621 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
622 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
623 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
624 goto out_retry;
625 }
626 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
627 ret = -EIO;
628 return ret;
629out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500630 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400631 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500632 /*
633 * For NFS4ERR_MOVED, the client transport will need to
634 * be recomputed after migration recovery has completed.
635 */
636 if (errorcode == -NFS4ERR_MOVED)
637 rpc_task_release_transport(task);
638 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400639 return ret;
640}
641
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400642int
Trond Myklebust037fc982015-09-20 15:51:00 -0400643nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
644 struct nfs4_state *state, long *timeout)
645{
646 struct nfs4_exception exception = {
647 .state = state,
648 };
649
650 if (task->tk_status >= 0)
651 return 0;
652 if (timeout)
653 exception.timeout = *timeout;
654 task->tk_status = nfs4_async_handle_exception(task, server,
655 task->tk_status,
656 &exception);
657 if (exception.delay && timeout)
658 *timeout = exception.timeout;
659 if (exception.retry)
660 return -EAGAIN;
661 return 0;
662}
663
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400664/*
665 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
666 * or 'false' otherwise.
667 */
668static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
669{
670 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500671 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400672}
Trond Myklebust65de8722008-12-23 15:21:44 -0500673
Trond Myklebust452e9352010-07-31 14:29:06 -0400674static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 spin_lock(&clp->cl_lock);
677 if (time_before(clp->cl_last_renewal,timestamp))
678 clp->cl_last_renewal = timestamp;
679 spin_unlock(&clp->cl_lock);
680}
681
Trond Myklebust452e9352010-07-31 14:29:06 -0400682static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
683{
Trond Myklebustbe824162015-07-05 14:50:46 -0400684 struct nfs_client *clp = server->nfs_client;
685
686 if (!nfs4_has_session(clp))
687 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400688}
689
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400690struct nfs4_call_sync_data {
691 const struct nfs_server *seq_server;
692 struct nfs4_sequence_args *seq_args;
693 struct nfs4_sequence_res *seq_res;
694};
695
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800696void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400697 struct nfs4_sequence_res *res, int cache_reply,
698 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400699{
700 args->sa_slot = NULL;
701 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400702 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400703
704 res->sr_slot = NULL;
705}
706
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400707static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400708{
709 struct nfs4_slot *slot = res->sr_slot;
710 struct nfs4_slot_table *tbl;
711
Chuck Lever3bd23842013-08-09 12:49:19 -0400712 tbl = slot->table;
713 spin_lock(&tbl->slot_tbl_lock);
714 if (!nfs41_wake_and_assign_slot(tbl, slot))
715 nfs4_free_slot(tbl, slot);
716 spin_unlock(&tbl->slot_tbl_lock);
717
718 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400719}
720
721static int nfs40_sequence_done(struct rpc_task *task,
722 struct nfs4_sequence_res *res)
723{
724 if (res->sr_slot != NULL)
725 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400726 return 1;
727}
728
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400729#if defined(CONFIG_NFS_V4_1)
730
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400731static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400732{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500733 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400734 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500735 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400736
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400737 if (!slot)
738 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500739 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500740 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500741
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400742 /* Bump the slot sequence number */
743 if (slot->seq_done)
744 slot->seq_nr++;
745 slot->seq_done = 0;
746
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500747 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500748 /* Be nice to the server: try to ensure that the last transmitted
749 * value for highest_user_slotid <= target_highest_slotid
750 */
751 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
752 send_new_highest_used_slotid = true;
753
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500754 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500755 send_new_highest_used_slotid = false;
756 goto out_unlock;
757 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500758 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500759
760 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
761 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500762out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500763 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500764 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400765 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400766 if (waitqueue_active(&tbl->slot_waitq))
767 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400768}
769
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400770static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
771{
772 nfs41_release_slot(res->sr_slot);
773 res->sr_slot = NULL;
774}
775
Trond Myklebust3453d572018-06-20 17:53:34 -0400776static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
777 u32 seqnr)
778{
779 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
780 slot->seq_nr_highest_sent = seqnr;
781}
782static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
783 u32 seqnr)
784{
785 slot->seq_nr_highest_sent = seqnr;
786 slot->seq_nr_last_acked = seqnr;
787}
788
Anna Schumaker913fadc2020-07-08 10:33:40 -0400789static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
790 struct nfs4_slot *slot)
791{
792 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
793 if (!IS_ERR(task))
794 rpc_put_task_async(task);
795}
796
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400797static int nfs41_sequence_process(struct rpc_task *task,
798 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400799{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500800 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500801 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400802 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100803 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500804 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400805
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500806 if (slot == NULL)
807 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400808 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500809 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400810 goto out;
811
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500812 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400813 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500814
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400815 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100816
817 status = res->sr_status;
818 if (task->tk_status == -NFS4ERR_DEADSESSION)
819 status = -NFS4ERR_DEADSESSION;
820
Andy Adamson691daf32009-12-04 15:55:39 -0500821 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100822 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400824 /* Mark this sequence number as having been acked */
825 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400826 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400827 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500828 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500829 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400830 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
831 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500832 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400833 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500834 case 1:
835 /*
836 * sr_status remains 1 if an RPC level error occurred.
837 * The server may or may not have processed the sequence
838 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500839 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400840 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
841 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500842 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400843 case -NFS4ERR_DELAY:
844 /* The server detected a resend of the RPC call and
845 * returned NFS4ERR_DELAY as per Section 2.10.6.2
846 * of RFC5661.
847 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500848 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400849 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500850 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500851 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400852 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400853 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400854 case -NFS4ERR_RETRY_UNCACHED_REP:
855 case -NFS4ERR_SEQ_FALSE_RETRY:
856 /*
857 * The server thinks we tried to replay a request.
858 * Retry the call after bumping the sequence ID.
859 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400860 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400861 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500862 case -NFS4ERR_BADSLOT:
863 /*
864 * The slot id we used was probably retired. Try again
865 * using a different slot id.
866 */
Trond Myklebust99589102018-06-09 12:50:50 -0400867 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400868 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500869 goto retry_nowait;
870 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400871 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500872 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400873 * Were one or more calls using this slot interrupted?
874 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400875 * transmitted slot sequence number may be too high. However,
876 * if the server did receive the request then it might
877 * accidentally give us a reply with a mismatched operation.
878 * We can sort this out by sending a lone sequence operation
879 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500880 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400881 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
882 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400883 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
884 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
885 res->sr_slot = NULL;
886 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500887 goto retry_nowait;
888 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400889 /*
890 * RFC5661:
891 * A retry might be sent while the original request is
892 * still in progress on the replier. The replier SHOULD
893 * deal with the issue by returning NFS4ERR_DELAY as the
894 * reply to SEQUENCE or CB_SEQUENCE operation, but
895 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
896 *
897 * Restart the search after a delay.
898 */
899 slot->seq_nr = slot->seq_nr_highest_sent;
900 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100901 case -NFS4ERR_BADSESSION:
902 case -NFS4ERR_DEADSESSION:
903 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
904 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400905 default:
906 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400907 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400908 }
909out:
910 /* The session may be reset by one of the error handlers. */
911 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500912out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500913 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400914session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100915 nfs4_schedule_session_recovery(session, status);
916 dprintk("%s ERROR: %d Reset session\n", __func__, status);
917 nfs41_sequence_free_slot(res);
918 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400919retry_new_seq:
920 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500921retry_nowait:
922 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400923 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500924 task->tk_status = 0;
925 ret = 0;
926 }
927 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400928out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400929 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400930 goto out;
931 rpc_delay(task, NFS4_POLL_RETRY_MAX);
932 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400933}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400934
935int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
936{
937 if (!nfs41_sequence_process(task, res))
938 return 0;
939 if (res->sr_slot != NULL)
940 nfs41_sequence_free_slot(res);
941 return 1;
942
943}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500944EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400945
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400946static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
947{
948 if (res->sr_slot == NULL)
949 return 1;
950 if (res->sr_slot->table->session != NULL)
951 return nfs41_sequence_process(task, res);
952 return nfs40_sequence_done(task, res);
953}
954
955static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
956{
957 if (res->sr_slot != NULL) {
958 if (res->sr_slot->table->session != NULL)
959 nfs41_sequence_free_slot(res);
960 else
961 nfs40_sequence_free_slot(res);
962 }
963}
964
Peng Tao2c4b1312014-06-11 05:24:16 +0800965int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400966{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500967 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400968 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400969 if (!res->sr_slot->table->session)
970 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400971 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400972}
Peng Tao2c4b1312014-06-11 05:24:16 +0800973EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400974
Andy Adamsonce5039c2009-04-01 09:22:13 -0400975static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
976{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400977 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400978
Trond Myklebust035168ab2010-06-16 09:52:26 -0400979 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
980
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500981 nfs4_setup_sequence(data->seq_server->nfs_client,
982 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400983}
984
Andy Adamson69ab40c2009-04-01 09:22:19 -0400985static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
986{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400987 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988
Trond Myklebust14516c32010-07-31 14:29:06 -0400989 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400990}
991
Trond Myklebust17280172012-03-11 13:11:00 -0400992static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400993 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400994 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400995};
996
Chuck Lever3bd23842013-08-09 12:49:19 -0400997#else /* !CONFIG_NFS_V4_1 */
998
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400999static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1000{
1001 return nfs40_sequence_done(task, res);
1002}
1003
1004static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1005{
1006 if (res->sr_slot != NULL)
1007 nfs40_sequence_free_slot(res);
1008}
1009
Peng Tao2c4b1312014-06-11 05:24:16 +08001010int nfs4_sequence_done(struct rpc_task *task,
1011 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001012{
Chuck Lever3bd23842013-08-09 12:49:19 -04001013 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001014}
Peng Tao2c4b1312014-06-11 05:24:16 +08001015EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001016
1017#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001018
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001019static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1020{
1021 res->sr_timestamp = jiffies;
1022 res->sr_status_flags = 0;
1023 res->sr_status = 1;
1024}
1025
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001026static
1027void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1028 struct nfs4_sequence_res *res,
1029 struct nfs4_slot *slot)
1030{
1031 if (!slot)
1032 return;
1033 slot->privileged = args->sa_privileged ? 1 : 0;
1034 args->sa_slot = slot;
1035
1036 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001037}
1038
1039int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001040 struct nfs4_sequence_args *args,
1041 struct nfs4_sequence_res *res,
1042 struct rpc_task *task)
1043{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001044 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001045 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001046 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001047
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001048 /* slot already allocated? */
1049 if (res->sr_slot != NULL)
1050 goto out_start;
1051
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001052 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001053 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001054
Trond Myklebust3453d572018-06-20 17:53:34 -04001055 spin_lock(&tbl->slot_tbl_lock);
1056 /* The state manager will wait until the slot table is empty */
1057 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1058 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001059
Trond Myklebust3453d572018-06-20 17:53:34 -04001060 slot = nfs4_alloc_slot(tbl);
1061 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001062 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001063 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001064 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001065 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001066 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001067
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001068 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001069
Anna Schumakerad05cc02017-01-11 13:37:06 -05001070 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001071out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001072 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001073 rpc_call_start(task);
1074 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001075out_sleep_timeout:
1076 /* Try again in 1/4 second */
1077 if (args->sa_privileged)
1078 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1079 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1080 else
1081 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1082 NULL, jiffies + (HZ >> 2));
1083 spin_unlock(&tbl->slot_tbl_lock);
1084 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001085out_sleep:
1086 if (args->sa_privileged)
1087 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001088 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001089 else
1090 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1091 spin_unlock(&tbl->slot_tbl_lock);
1092 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001093}
1094EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1095
Chuck Lever9915ea72013-08-09 12:48:27 -04001096static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1097{
1098 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001099 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001100 data->seq_args, data->seq_res, task);
1101}
1102
1103static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1104{
1105 struct nfs4_call_sync_data *data = calldata;
1106 nfs4_sequence_done(task, data->seq_res);
1107}
1108
1109static const struct rpc_call_ops nfs40_call_sync_ops = {
1110 .rpc_call_prepare = nfs40_call_sync_prepare,
1111 .rpc_call_done = nfs40_call_sync_done,
1112};
1113
Anna Schumaker48c05852019-08-14 15:27:00 -04001114static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1115{
1116 int ret;
1117 struct rpc_task *task;
1118
1119 task = rpc_run_task(task_setup);
1120 if (IS_ERR(task))
1121 return PTR_ERR(task);
1122
1123 ret = task->tk_status;
1124 rpc_put_task(task);
1125 return ret;
1126}
1127
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001128static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1129 struct nfs_server *server,
1130 struct rpc_message *msg,
1131 struct nfs4_sequence_args *args,
1132 struct nfs4_sequence_res *res,
1133 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001134{
Chuck Lever9915ea72013-08-09 12:48:27 -04001135 struct nfs_client *clp = server->nfs_client;
1136 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001137 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001138 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001140 };
1141 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001142 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001143 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001144 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001145 .callback_data = &data,
1146 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001147 };
1148
Anna Schumaker48c05852019-08-14 15:27:00 -04001149 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001150}
1151
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001152static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1153 struct nfs_server *server,
1154 struct rpc_message *msg,
1155 struct nfs4_sequence_args *args,
1156 struct nfs4_sequence_res *res)
1157{
1158 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1159}
1160
1161
Bryan Schumaker7c513052011-03-24 17:12:24 +00001162int nfs4_call_sync(struct rpc_clnt *clnt,
1163 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001164 struct rpc_message *msg,
1165 struct nfs4_sequence_args *args,
1166 struct nfs4_sequence_res *res,
1167 int cache_reply)
1168{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001169 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001170 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001171}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Trond Myklebust3c591172018-07-31 15:54:10 -04001173static void
1174nfs4_inc_nlink_locked(struct inode *inode)
1175{
1176 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1177 inc_nlink(inode);
1178}
1179
1180static void
1181nfs4_dec_nlink_locked(struct inode *inode)
1182{
1183 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1184 drop_nlink(inode);
1185}
1186
1187static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001188nfs4_update_changeattr_locked(struct inode *inode,
1189 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001190 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001192 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Trond Myklebust16e14372018-03-20 16:53:31 -04001194 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1195 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001196 | cache_validity;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001197
1198 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001199 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1200 nfsi->attrtimeo_timestamp = jiffies;
1201 } else {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001202 if (S_ISDIR(inode->i_mode)) {
1203 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1204 nfs_force_lookup_revalidate(inode);
1205 } else {
1206 if (!NFS_PROTO(inode)->have_delegation(inode,
1207 FMODE_READ))
1208 nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1209 }
1210
1211 if (cinfo->before != inode_peek_iversion_raw(inode))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001212 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
Frank van der Linden0f44da52020-06-23 22:39:00 +00001213 NFS_INO_INVALID_ACL |
1214 NFS_INO_INVALID_XATTR;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001215 }
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001216 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001217 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001218 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001219 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001220
1221 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1222 nfs_fscache_invalidate(inode);
Trond Myklebust3c591172018-07-31 15:54:10 -04001223}
1224
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001225void
1226nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001227 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001228{
1229 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001230 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 spin_unlock(&dir->i_lock);
1232}
1233
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001234struct nfs4_open_createattrs {
1235 struct nfs4_label *label;
1236 struct iattr *sattr;
1237 const __u32 verf[2];
1238};
1239
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001240static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1241 int err, struct nfs4_exception *exception)
1242{
1243 if (err != -EINVAL)
1244 return false;
1245 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1246 return false;
1247 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1248 exception->retry = 1;
1249 return true;
1250}
1251
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001252static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1253{
1254 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1255}
1256
1257static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1258{
1259 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1260
1261 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1262}
1263
Trond Myklebust6ae37332015-01-30 14:21:14 -05001264static u32
1265nfs4_map_atomic_open_share(struct nfs_server *server,
1266 fmode_t fmode, int openflags)
1267{
1268 u32 res = 0;
1269
1270 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1271 case FMODE_READ:
1272 res = NFS4_SHARE_ACCESS_READ;
1273 break;
1274 case FMODE_WRITE:
1275 res = NFS4_SHARE_ACCESS_WRITE;
1276 break;
1277 case FMODE_READ|FMODE_WRITE:
1278 res = NFS4_SHARE_ACCESS_BOTH;
1279 }
1280 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1281 goto out;
1282 /* Want no delegation if we're using O_DIRECT */
1283 if (openflags & O_DIRECT)
1284 res |= NFS4_SHARE_WANT_NO_DELEG;
1285out:
1286 return res;
1287}
1288
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001289static enum open_claim_type4
1290nfs4_map_atomic_open_claim(struct nfs_server *server,
1291 enum open_claim_type4 claim)
1292{
1293 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1294 return claim;
1295 switch (claim) {
1296 default:
1297 return claim;
1298 case NFS4_OPEN_CLAIM_FH:
1299 return NFS4_OPEN_CLAIM_NULL;
1300 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1301 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1302 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1303 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1304 }
1305}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001308{
1309 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001310 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001311 p->o_res.seqid = p->o_arg.seqid;
1312 p->c_res.seqid = p->c_arg.seqid;
1313 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001314 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001315 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001316 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001317}
1318
Al Viro82a2c1b2011-06-22 18:30:55 -04001319static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001320 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001321 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001322 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001323 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001324{
Al Viro82a2c1b2011-06-22 18:30:55 -04001325 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001326 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001327 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001328 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001329 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001330 struct nfs4_opendata *p;
1331
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001332 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001333 if (p == NULL)
1334 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001335
1336 p->f_label = nfs4_label_alloc(server, gfp_mask);
1337 if (IS_ERR(p->f_label))
1338 goto err_free_p;
1339
Kinglong Meea49c2692015-07-27 15:31:38 +08001340 p->a_label = nfs4_label_alloc(server, gfp_mask);
1341 if (IS_ERR(p->a_label))
1342 goto err_free_f;
1343
Trond Myklebust63f5f792015-01-23 19:19:25 -05001344 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1345 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001346 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001347 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001348 nfs_sb_active(dentry->d_sb);
1349 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001350 p->dir = parent;
1351 p->owner = sp;
1352 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353 p->o_arg.open_flags = flags;
1354 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001355 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001356 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1357 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001358 if (flags & O_CREAT) {
1359 p->o_arg.umask = current_umask();
1360 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1361 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1362 p->o_arg.u.attrs = &p->attrs;
1363 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1364
1365 memcpy(p->o_arg.u.verifier.data, c->verf,
1366 sizeof(p->o_arg.u.verifier.data));
1367 }
1368 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001369 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1370 * will return permission denied for all bits until close */
1371 if (!(flags & O_EXCL)) {
1372 /* ask server to check for all possible rights as results
1373 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001374 switch (p->o_arg.claim) {
1375 default:
1376 break;
1377 case NFS4_OPEN_CLAIM_NULL:
1378 case NFS4_OPEN_CLAIM_FH:
1379 p->o_arg.access = NFS4_ACCESS_READ |
1380 NFS4_ACCESS_MODIFY |
1381 NFS4_ACCESS_EXTEND |
1382 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001383#ifdef CONFIG_NFS_V4_2
1384 if (server->caps & NFS_CAP_XATTR)
1385 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1386 NFS4_ACCESS_XAWRITE |
1387 NFS4_ACCESS_XALIST;
1388#endif
Trond Myklebust536585c2016-11-10 15:40:34 -05001389 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001390 }
David Howells7539bba2006-08-22 20:06:09 -04001391 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001392 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1393 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001394 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001395 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001396 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001397 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001398 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001399 case NFS4_OPEN_CLAIM_NULL:
1400 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1401 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1402 p->o_arg.fh = NFS_FH(dir);
1403 break;
1404 case NFS4_OPEN_CLAIM_PREVIOUS:
1405 case NFS4_OPEN_CLAIM_FH:
1406 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1407 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001408 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001409 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001410 p->c_arg.fh = &p->o_res.fh;
1411 p->c_arg.stateid = &p->o_res.stateid;
1412 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001413 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001414 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001415 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001416
1417err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001418 nfs4_label_free(p->a_label);
1419err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001420 nfs4_label_free(p->f_label);
1421err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001422 kfree(p);
1423err:
1424 dput(parent);
1425 return NULL;
1426}
1427
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001428static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001429{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001430 struct nfs4_opendata *p = container_of(kref,
1431 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001432 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001433
Fred Isaman30ae2412016-10-18 13:39:51 -04001434 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001435 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001436 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001437 if (p->state != NULL)
1438 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001439 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001440
Kinglong Meea49c2692015-07-27 15:31:38 +08001441 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001442 nfs4_label_free(p->f_label);
1443
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001444 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001445 dput(p->dentry);
1446 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001447 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001448 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001449 kfree(p);
1450}
1451
1452static void nfs4_opendata_put(struct nfs4_opendata *p)
1453{
1454 if (p != NULL)
1455 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001456}
1457
Trond Myklebust24311f82015-09-20 10:50:17 -04001458static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1459 fmode_t fmode)
1460{
1461 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1462 case FMODE_READ|FMODE_WRITE:
1463 return state->n_rdwr != 0;
1464 case FMODE_WRITE:
1465 return state->n_wronly != 0;
1466 case FMODE_READ:
1467 return state->n_rdonly != 0;
1468 }
1469 WARN_ON_ONCE(1);
1470 return false;
1471}
1472
Trond Myklebustbe189f72018-09-27 17:12:33 -04001473static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1474 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001475{
1476 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001477
Trond Myklebust536e43d2012-01-17 22:04:26 -05001478 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001479 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001480 switch (claim) {
1481 case NFS4_OPEN_CLAIM_NULL:
1482 case NFS4_OPEN_CLAIM_FH:
1483 goto out;
1484 default:
1485 break;
1486 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001487 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001488 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001489 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1490 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001491 break;
1492 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001493 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1494 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001495 break;
1496 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001497 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1498 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001499 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001500out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001501 return ret;
1502}
1503
Trond Myklebust2a606182015-08-19 22:30:00 -05001504static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1505 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001506{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001507 if (delegation == NULL)
1508 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001509 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001510 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001511 switch (claim) {
1512 case NFS4_OPEN_CLAIM_NULL:
1513 case NFS4_OPEN_CLAIM_FH:
1514 break;
1515 case NFS4_OPEN_CLAIM_PREVIOUS:
1516 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1517 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001518 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001519 default:
1520 return 0;
1521 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001522 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001523 return 1;
1524}
1525
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001526static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001527{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001528 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001529 case FMODE_WRITE:
1530 state->n_wronly++;
1531 break;
1532 case FMODE_READ:
1533 state->n_rdonly++;
1534 break;
1535 case FMODE_READ|FMODE_WRITE:
1536 state->n_rdwr++;
1537 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001538 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001539}
1540
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001541#ifdef CONFIG_NFS_V4_1
1542static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1543{
1544 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1545 return true;
1546 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1547 return true;
1548 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1549 return true;
1550 return false;
1551}
1552#endif /* CONFIG_NFS_V4_1 */
1553
Trond Myklebustc9399f22017-11-06 15:28:01 -05001554static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1555{
1556 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1557 wake_up_all(&state->waitq);
1558}
1559
Trond Myklebust4f14c192014-02-12 19:15:06 -05001560static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001561{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001562 struct nfs_client *clp = state->owner->so_server->nfs_client;
1563 bool need_recover = false;
1564
1565 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1566 need_recover = true;
1567 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1568 need_recover = true;
1569 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1570 need_recover = true;
1571 if (need_recover)
1572 nfs4_state_mark_reclaim_nograce(clp, state);
1573}
1574
Trond Myklebustc9399f22017-11-06 15:28:01 -05001575/*
1576 * Check for whether or not the caller may update the open stateid
1577 * to the value passed in by stateid.
1578 *
1579 * Note: This function relies heavily on the server implementing
1580 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1581 * correctly.
1582 * i.e. The stateid seqids have to be initialised to 1, and
1583 * are then incremented on every state transition.
1584 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001585static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001586 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001587{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001588 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1589 /* The common case - we're updating to a new sequence number */
1590 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1591 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1592 return true;
1593 }
1594 } else {
1595 /* This is the first OPEN in this generation */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001596 if (stateid->seqid == cpu_to_be32(1))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001597 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001598 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001599 return false;
1600}
1601
Trond Myklebustf95549c2015-01-23 18:06:09 -05001602static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1603{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001604 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1605 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001606 if (state->n_wronly)
1607 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1608 if (state->n_rdonly)
1609 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1610 if (state->n_rdwr)
1611 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001612 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001613}
1614
Trond Myklebust226056c2014-02-11 10:41:07 -05001615static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1616 nfs4_stateid *stateid, fmode_t fmode)
1617{
1618 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1619 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1620 case FMODE_WRITE:
1621 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1622 break;
1623 case FMODE_READ:
1624 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1625 break;
1626 case 0:
1627 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1628 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1629 clear_bit(NFS_OPEN_STATE, &state->flags);
1630 }
1631 if (stateid == NULL)
1632 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001633 /* Handle OPEN+OPEN_DOWNGRADE races */
1634 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1635 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001636 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001637 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001638 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001639 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001640 nfs4_stateid_copy(&state->stateid, stateid);
1641 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001642 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001643out:
1644 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001645}
1646
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001647static void nfs_clear_open_stateid(struct nfs4_state *state,
1648 nfs4_stateid *arg_stateid,
1649 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001650{
1651 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001652 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1653 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1654 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001655 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001656 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1657 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001658}
1659
Trond Myklebust1393d962016-09-22 13:39:13 -04001660static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001661 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001662 __must_hold(&state->owner->so_lock)
1663 __must_hold(&state->seqlock)
1664 __must_hold(RCU)
1665
Trond Myklebust003707c2007-07-05 18:07:55 -04001666{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001667 DEFINE_WAIT(wait);
1668 int status = 0;
1669 for (;;) {
1670
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001671 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001672 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001673
Trond Myklebustc9399f22017-11-06 15:28:01 -05001674 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001675 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001676 /* Rely on seqids for serialisation with NFSv4.0 */
1677 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1678 break;
1679
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001680 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001681 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1682 /*
1683 * Ensure we process the state changes in the same order
1684 * in which the server processed them by delaying the
1685 * update of the stateid until we are in sequence.
1686 */
1687 write_sequnlock(&state->seqlock);
1688 spin_unlock(&state->owner->so_lock);
1689 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001690 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001691
Trond Myklebustc9399f22017-11-06 15:28:01 -05001692 if (!signal_pending(current)) {
1693 if (schedule_timeout(5*HZ) == 0)
1694 status = -EAGAIN;
1695 else
1696 status = 0;
1697 } else
1698 status = -EINTR;
1699 finish_wait(&state->waitq, &wait);
1700 rcu_read_lock();
1701 spin_lock(&state->owner->so_lock);
1702 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001703 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001704
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001705 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1706 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001707 nfs4_stateid_copy(freeme, &state->open_stateid);
1708 nfs_test_and_clear_all_open_stateid(state);
1709 }
1710
Trond Myklebuste999e802014-02-10 18:20:47 -05001711 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1712 nfs4_stateid_copy(&state->stateid, stateid);
1713 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001714 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001715 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001716}
1717
Trond Myklebustc9399f22017-11-06 15:28:01 -05001718static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001719 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001720 fmode_t fmode,
1721 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001723 /*
1724 * Protect the call to nfs4_state_set_mode_locked and
1725 * serialise the stateid update
1726 */
1727 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001728 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1729 switch (fmode) {
1730 case FMODE_READ:
1731 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1732 break;
1733 case FMODE_WRITE:
1734 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1735 break;
1736 case FMODE_READ|FMODE_WRITE:
1737 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001738 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001739 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001740 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001741}
1742
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001743static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1744{
1745 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1746 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1747 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1748 clear_bit(NFS_OPEN_STATE, &state->flags);
1749}
1750
Trond Myklebustc9399f22017-11-06 15:28:01 -05001751static void nfs_state_set_delegation(struct nfs4_state *state,
1752 const nfs4_stateid *deleg_stateid,
1753 fmode_t fmode)
1754{
1755 /*
1756 * Protect the call to nfs4_state_set_mode_locked and
1757 * serialise the stateid update
1758 */
1759 write_seqlock(&state->seqlock);
1760 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1761 set_bit(NFS_DELEGATED_STATE, &state->flags);
1762 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001765static void nfs_state_clear_delegation(struct nfs4_state *state)
1766{
1767 write_seqlock(&state->seqlock);
1768 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1769 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1770 write_sequnlock(&state->seqlock);
1771}
1772
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001773int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001774 const nfs4_stateid *open_stateid,
1775 const nfs4_stateid *delegation,
1776 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001777{
Trond Myklebust1393d962016-09-22 13:39:13 -04001778 struct nfs_server *server = NFS_SERVER(state->inode);
1779 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001780 struct nfs_inode *nfsi = NFS_I(state->inode);
1781 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001782 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001783 int ret = 0;
1784
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001785 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001786
1787 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001788 spin_lock(&state->owner->so_lock);
1789 if (open_stateid != NULL) {
1790 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1791 ret = 1;
1792 }
1793
Trond Myklebust333ac782019-10-22 12:12:17 -04001794 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001795 if (deleg_cur == NULL)
1796 goto no_delegation;
1797
1798 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001799 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001800 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001801 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001802 goto no_delegation_unlock;
1803
1804 if (delegation == NULL)
1805 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001806 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001807 goto no_delegation_unlock;
1808
Trond Myklebustb7391f42008-12-23 15:21:52 -05001809 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001810 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001811 ret = 1;
1812no_delegation_unlock:
1813 spin_unlock(&deleg_cur->lock);
1814no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001815 if (ret)
1816 update_open_stateflags(state, fmode);
1817 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001818 rcu_read_unlock();
1819
Trond Myklebust4f14c192014-02-12 19:15:06 -05001820 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001821 nfs4_schedule_state_manager(clp);
1822 if (freeme.type != 0)
1823 nfs4_test_and_free_stateid(server, &freeme,
1824 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001825
1826 return ret;
1827}
1828
Trond Myklebust39071e62015-01-24 15:07:56 -05001829static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1830 const nfs4_stateid *stateid)
1831{
1832 struct nfs4_state *state = lsp->ls_state;
1833 bool ret = false;
1834
1835 spin_lock(&state->state_lock);
1836 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1837 goto out_noupdate;
1838 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1839 goto out_noupdate;
1840 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1841 ret = true;
1842out_noupdate:
1843 spin_unlock(&state->state_lock);
1844 return ret;
1845}
Trond Myklebust34310432008-12-23 15:21:38 -05001846
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001847static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001848{
1849 struct nfs_delegation *delegation;
1850
Trond Myklebustf5086242018-03-20 16:43:13 -04001851 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001852 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001853 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001854 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001855 rcu_read_unlock();
1856 return;
1857 }
1858 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001859 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001860}
1861
Trond Myklebust6ee41262007-07-08 14:11:36 -04001862static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001863{
1864 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001865 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001866 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001867 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001868 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001869 nfs4_stateid stateid;
1870 int ret = -EAGAIN;
1871
Trond Myklebustaac00a82007-07-05 19:02:21 -04001872 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001873 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001874 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001875 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001876 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001877 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001878 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001879 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001880 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001881 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001882 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001883 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001884 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001885 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001886 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001887 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001888 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001889 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001890 if (!opendata->is_recover) {
1891 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1892 if (ret != 0)
1893 goto out;
1894 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001895 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001896
1897 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001898 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001899 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001900 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001901out:
1902 return ERR_PTR(ret);
1903out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001904 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001905 return state;
1906}
1907
Andy Adamsone23008e2012-10-02 21:07:32 -04001908static void
1909nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1910{
1911 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1912 struct nfs_delegation *delegation;
1913 int delegation_flags = 0;
1914
1915 rcu_read_lock();
1916 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1917 if (delegation)
1918 delegation_flags = delegation->flags;
1919 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001920 switch (data->o_arg.claim) {
1921 default:
1922 break;
1923 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1924 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001925 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1926 "returning a delegation for "
1927 "OPEN(CLAIM_DELEGATE_CUR)\n",
1928 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001929 return;
1930 }
1931 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001932 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001933 data->owner->so_cred,
1934 data->o_res.delegation_type,
1935 &data->o_res.delegation,
1936 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001937 else
1938 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001939 data->owner->so_cred,
1940 data->o_res.delegation_type,
1941 &data->o_res.delegation,
1942 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001943
1944 if (data->o_res.do_recall)
1945 nfs_async_inode_return_delegation(state->inode,
1946 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001947}
1948
1949/*
1950 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1951 * and update the nfs4_state.
1952 */
1953static struct nfs4_state *
1954_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1955{
1956 struct inode *inode = data->state->inode;
1957 struct nfs4_state *state = data->state;
1958 int ret;
1959
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001960 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001961 if (data->rpc_status)
1962 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001963 /* cached opens have already been processed */
1964 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001965 }
1966
Andy Adamsone23008e2012-10-02 21:07:32 -04001967 ret = nfs_refresh_inode(inode, &data->f_attr);
1968 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001969 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001970
1971 if (data->o_res.delegation_type != 0)
1972 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001973update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001974 if (!update_open_stateid(state, &data->o_res.stateid,
1975 NULL, data->o_arg.fmode))
1976 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001977 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001978
1979 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001980}
1981
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001982static struct inode *
1983nfs4_opendata_get_inode(struct nfs4_opendata *data)
1984{
1985 struct inode *inode;
1986
1987 switch (data->o_arg.claim) {
1988 case NFS4_OPEN_CLAIM_NULL:
1989 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1990 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1991 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1992 return ERR_PTR(-EAGAIN);
1993 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1994 &data->f_attr, data->f_label);
1995 break;
1996 default:
1997 inode = d_inode(data->dentry);
1998 ihold(inode);
1999 nfs_refresh_inode(inode, &data->f_attr);
2000 }
2001 return inode;
2002}
2003
Andy Adamsone23008e2012-10-02 21:07:32 -04002004static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04002005nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2006{
2007 struct nfs4_state *state;
2008 struct inode *inode;
2009
2010 inode = nfs4_opendata_get_inode(data);
2011 if (IS_ERR(inode))
2012 return ERR_CAST(inode);
2013 if (data->state != NULL && data->state->inode == inode) {
2014 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002015 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002016 } else
2017 state = nfs4_get_open_state(inode, data->owner);
2018 iput(inode);
2019 if (state == NULL)
2020 state = ERR_PTR(-ENOMEM);
2021 return state;
2022}
2023
Andy Adamsone23008e2012-10-02 21:07:32 -04002024static struct nfs4_state *
2025_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002026{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002027 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002028
Trond Myklebustaac00a82007-07-05 19:02:21 -04002029 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002030 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002031 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002032 goto out;
2033 }
2034
Trond Myklebust75e8c482017-08-08 10:38:07 -04002035 state = nfs4_opendata_find_nfs4_state(data);
2036 if (IS_ERR(state))
2037 goto out;
2038
Andy Adamsone23008e2012-10-02 21:07:32 -04002039 if (data->o_res.delegation_type != 0)
2040 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002041 if (!update_open_stateid(state, &data->o_res.stateid,
2042 NULL, data->o_arg.fmode)) {
2043 nfs4_put_open_state(state);
2044 state = ERR_PTR(-EAGAIN);
2045 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002046out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002047 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002048 return state;
2049}
2050
Andy Adamsone23008e2012-10-02 21:07:32 -04002051static struct nfs4_state *
2052nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2053{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002054 struct nfs4_state *ret;
2055
Andy Adamsone23008e2012-10-02 21:07:32 -04002056 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002057 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2058 else
2059 ret = _nfs4_opendata_to_nfs4_state(data);
2060 nfs4_sequence_free_slot(&data->o_res.seq_res);
2061 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002062}
2063
Trond Myklebust0de43972018-09-02 15:57:01 -04002064static struct nfs_open_context *
2065nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002066{
2067 struct nfs_inode *nfsi = NFS_I(state->inode);
2068 struct nfs_open_context *ctx;
2069
Trond Myklebust0de43972018-09-02 15:57:01 -04002070 rcu_read_lock();
2071 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002072 if (ctx->state != state)
2073 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002074 if ((ctx->mode & mode) != mode)
2075 continue;
2076 if (!get_nfs_open_context(ctx))
2077 continue;
2078 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002079 return ctx;
2080 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002081 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002082 return ERR_PTR(-ENOENT);
2083}
2084
Trond Myklebust0de43972018-09-02 15:57:01 -04002085static struct nfs_open_context *
2086nfs4_state_find_open_context(struct nfs4_state *state)
2087{
2088 struct nfs_open_context *ctx;
2089
2090 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2091 if (!IS_ERR(ctx))
2092 return ctx;
2093 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2094 if (!IS_ERR(ctx))
2095 return ctx;
2096 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2097}
2098
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002099static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2100 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002101{
2102 struct nfs4_opendata *opendata;
2103
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002104 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002105 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002106 if (opendata == NULL)
2107 return ERR_PTR(-ENOMEM);
2108 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002109 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002110 return opendata;
2111}
2112
Trond Myklebust24311f82015-09-20 10:50:17 -04002113static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2114 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002115{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002116 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002117 int ret;
2118
Trond Myklebust24311f82015-09-20 10:50:17 -04002119 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002120 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002121 opendata->o_arg.open_flags = 0;
2122 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002123 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2124 NFS_SB(opendata->dentry->d_sb),
2125 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002126 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2127 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2128 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002129 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002130 if (ret != 0)
2131 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002132 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002133 if (IS_ERR(newstate))
2134 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002135 if (newstate != opendata->state)
2136 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002137 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002138 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002139}
2140
2141static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2142{
Trond Myklebust864472e2006-01-03 09:55:15 +01002143 int ret;
2144
2145 /* memory barrier prior to reading state->n_* */
2146 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002147 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2148 if (ret != 0)
2149 return ret;
2150 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2151 if (ret != 0)
2152 return ret;
2153 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2154 if (ret != 0)
2155 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002156 /*
2157 * We may have performed cached opens for all three recoveries.
2158 * Check if we need to update the current stateid.
2159 */
2160 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002161 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002162 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002163 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002164 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002165 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002166 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002167 return 0;
2168}
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170/*
2171 * OPEN_RECLAIM:
2172 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002174static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002176 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002177 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002178 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 int status;
2180
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002181 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2182 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002183 if (IS_ERR(opendata))
2184 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002185 rcu_read_lock();
2186 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002187 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002188 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002189 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002190 opendata->o_arg.u.delegation_type = delegation_type;
2191 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002192 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return status;
2194}
2195
Trond Myklebust539cd032007-06-05 11:46:42 -04002196static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 struct nfs_server *server = NFS_SERVER(state->inode);
2199 struct nfs4_exception exception = { };
2200 int err;
2201 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002202 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002203 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002204 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2205 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002206 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002207 break;
2208 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 } while (exception.retry);
2210 return err;
2211}
2212
Trond Myklebust864472e2006-01-03 09:55:15 +01002213static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2214{
2215 struct nfs_open_context *ctx;
2216 int ret;
2217
2218 ctx = nfs4_state_find_open_context(state);
2219 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002220 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002221 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2222 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002223 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002224 put_nfs_open_context(ctx);
2225 return ret;
2226}
2227
NeilBrowndce26302017-12-13 09:57:09 +11002228static 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 -07002229{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002230 switch (err) {
2231 default:
2232 printk(KERN_ERR "NFS: %s: unhandled error "
2233 "%d.\n", __func__, err);
2234 case 0:
2235 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002236 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002237 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002238 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002239 break;
2240 case -NFS4ERR_BADSESSION:
2241 case -NFS4ERR_BADSLOT:
2242 case -NFS4ERR_BAD_HIGH_SLOT:
2243 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2244 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002245 return -EAGAIN;
2246 case -NFS4ERR_STALE_CLIENTID:
2247 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002248 /* Don't recall a delegation if it was lost */
2249 nfs4_schedule_lease_recovery(server->nfs_client);
2250 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002251 case -NFS4ERR_MOVED:
2252 nfs4_schedule_migration_recovery(server);
2253 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002254 case -NFS4ERR_LEASE_MOVED:
2255 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2256 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002257 case -NFS4ERR_DELEG_REVOKED:
2258 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002259 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002260 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002261 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002262 nfs_inode_find_state_and_recover(state->inode,
2263 stateid);
2264 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002265 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002266 case -NFS4ERR_DELAY:
2267 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002268 ssleep(1);
2269 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002270 case -ENOMEM:
2271 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002272 if (fl) {
2273 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2274 if (lsp)
2275 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2276 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002277 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return err;
2280}
2281
Trond Myklebust24311f82015-09-20 10:50:17 -04002282int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002283 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002284{
2285 struct nfs_server *server = NFS_SERVER(state->inode);
2286 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002287 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002288
2289 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2290 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2291 if (IS_ERR(opendata))
2292 return PTR_ERR(opendata);
2293 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002294 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002295 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2296 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002297 goto out;
2298 }
2299 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002300 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2301 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002302 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002303 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002304 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2305 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2306 if (err)
2307 goto out;
2308 }
2309 nfs_state_clear_delegation(state);
2310out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002311 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002312 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002313}
2314
Chuck Leverbe05c862013-08-09 12:49:47 -04002315static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2316{
2317 struct nfs4_opendata *data = calldata;
2318
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002319 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2320 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002321}
2322
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002323static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2324{
2325 struct nfs4_opendata *data = calldata;
2326
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002327 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002328
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002329 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002330 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002331 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002332 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002333 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002334 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002335 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002336}
2337
2338static void nfs4_open_confirm_release(void *calldata)
2339{
2340 struct nfs4_opendata *data = calldata;
2341 struct nfs4_state *state = NULL;
2342
2343 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002344 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002345 goto out_free;
2346 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002347 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002348 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002349 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002350 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002351 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002352out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002353 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002354}
2355
2356static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002357 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002358 .rpc_call_done = nfs4_open_confirm_done,
2359 .rpc_release = nfs4_open_confirm_release,
2360};
2361
2362/*
2363 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2364 */
2365static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2366{
David Howells2b0143b2015-03-17 22:25:59 +00002367 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002368 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002369 struct rpc_message msg = {
2370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2371 .rpc_argp = &data->c_arg,
2372 .rpc_resp = &data->c_res,
2373 .rpc_cred = data->owner->so_cred,
2374 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002375 struct rpc_task_setup task_setup_data = {
2376 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002377 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002378 .callback_ops = &nfs4_open_confirm_ops,
2379 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002380 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002381 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 int status;
2384
Anna Schumakerfba83f32018-05-04 16:22:50 -04002385 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2386 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002387 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002388 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002389 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002390 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002391 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002392 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002393 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002394 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002395 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002396 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002397 smp_wmb();
2398 } else
2399 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002400 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return status;
2402}
2403
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002404static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002406 struct nfs4_opendata *data = calldata;
2407 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002408 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002409 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002410
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002411 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002412 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002413 /*
2414 * Check if we still need to send an OPEN call, or if we can use
2415 * a delegation instead.
2416 */
2417 if (data->state != NULL) {
2418 struct nfs_delegation *delegation;
2419
Trond Myklebustbe189f72018-09-27 17:12:33 -04002420 if (can_open_cached(data->state, data->o_arg.fmode,
2421 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002422 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002423 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002424 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002425 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002426 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002427 rcu_read_unlock();
2428 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002429 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002430 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002431 switch (claim) {
2432 default:
2433 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002434 case NFS4_OPEN_CLAIM_PREVIOUS:
2435 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2436 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002437 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002438 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002439 case NFS4_OPEN_CLAIM_FH:
2440 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002441 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002442 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002443 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002444 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002445 &data->o_res.seq_res,
2446 task) != 0)
2447 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002448
2449 /* Set the create mode (note dependency on the session type) */
2450 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2451 if (data->o_arg.open_flags & O_EXCL) {
2452 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2453 if (nfs4_has_persistent_session(clp))
2454 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2455 else if (clp->cl_mvops->minor_version > 0)
2456 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2457 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002458 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002459unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002460 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002461 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002462out_no_action:
2463 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002464out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002465 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002466}
2467
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002468static void nfs4_open_done(struct rpc_task *task, void *calldata)
2469{
2470 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002472 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002473
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002474 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002475 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002476
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002477 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002478 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2479 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002480 case S_IFREG:
2481 break;
2482 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002483 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002484 break;
2485 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002486 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002487 break;
2488 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002489 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002490 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002491 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002492 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002493 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2494 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002495 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002496 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002497}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002498
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002499static void nfs4_open_release(void *calldata)
2500{
2501 struct nfs4_opendata *data = calldata;
2502 struct nfs4_state *state = NULL;
2503
2504 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002505 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002506 goto out_free;
2507 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002508 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002509 goto out_free;
2510 /* In case we need an open_confirm, no cleanup! */
2511 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2512 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002513 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002514 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002515 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002516out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002517 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002518}
2519
2520static const struct rpc_call_ops nfs4_open_ops = {
2521 .rpc_call_prepare = nfs4_open_prepare,
2522 .rpc_call_done = nfs4_open_done,
2523 .rpc_release = nfs4_open_release,
2524};
2525
Fred Isaman3b65a302016-09-19 10:06:49 -04002526static int nfs4_run_open_task(struct nfs4_opendata *data,
2527 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002528{
David Howells2b0143b2015-03-17 22:25:59 +00002529 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002530 struct nfs_server *server = NFS_SERVER(dir);
2531 struct nfs_openargs *o_arg = &data->o_arg;
2532 struct nfs_openres *o_res = &data->o_res;
2533 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002534 struct rpc_message msg = {
2535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2536 .rpc_argp = o_arg,
2537 .rpc_resp = o_res,
2538 .rpc_cred = data->owner->so_cred,
2539 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002540 struct rpc_task_setup task_setup_data = {
2541 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002542 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002543 .callback_ops = &nfs4_open_ops,
2544 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002545 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002546 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002547 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002548 int status;
2549
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002550 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002551 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002552 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002553 data->cancelled = false;
2554 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002555 if (!ctx) {
2556 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002557 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002558 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002559 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002560 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002561 pnfs_lgopen_prepare(data, ctx);
2562 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002563 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002564 if (IS_ERR(task))
2565 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002566 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002567 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002568 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002569 smp_wmb();
2570 } else
2571 status = data->rpc_status;
2572 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002573
2574 return status;
2575}
2576
2577static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2578{
David Howells2b0143b2015-03-17 22:25:59 +00002579 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002580 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002581 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002582
Fred Isaman3b65a302016-09-19 10:06:49 -04002583 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002584 if (status != 0 || !data->rpc_done)
2585 return status;
2586
Trond Myklebust6926afd12012-01-07 13:22:46 -05002587 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2588
Anna Schumakerd7e98252017-01-11 16:13:29 -05002589 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002590 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002591
2592 return status;
2593}
2594
Trond Myklebustf3792d62014-07-10 08:54:32 -04002595/*
2596 * Additional permission checks in order to distinguish between an
2597 * open for read, and an open for execute. This works around the
2598 * fact that NFSv4 OPEN treats read and execute permissions as being
2599 * the same.
2600 * Note that in the non-execute case, we want to turn off permission
2601 * checking if we just created a new file (POSIX open() semantics).
2602 */
NeilBrowna52458b2018-12-03 11:30:31 +11002603static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002604 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002605 struct nfs4_state *state, fmode_t fmode,
2606 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002607{
2608 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002609 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002610
2611 /* access call failed or for some reason the server doesn't
2612 * support any access modes -- defer access call until later */
2613 if (opendata->o_res.access_supported == 0)
2614 return 0;
2615
2616 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002617 /*
2618 * Use openflags to check for exec, because fmode won't
2619 * always have FMODE_EXEC set when file open for exec.
2620 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002621 if (openflags & __FMODE_EXEC) {
2622 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002623 if (S_ISDIR(state->inode->i_mode))
2624 mask = NFS4_ACCESS_LOOKUP;
2625 else
2626 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002627 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002628 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002629
2630 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002631 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2632 nfs_access_add_cache(state->inode, &cache);
2633
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002634 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2635 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002636 return 0;
2637
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002638 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002639}
2640
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002641/*
2642 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2643 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002644static int _nfs4_proc_open(struct nfs4_opendata *data,
2645 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002646{
David Howells2b0143b2015-03-17 22:25:59 +00002647 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002648 struct nfs_server *server = NFS_SERVER(dir);
2649 struct nfs_openargs *o_arg = &data->o_arg;
2650 struct nfs_openres *o_res = &data->o_res;
2651 int status;
2652
Fred Isaman3b65a302016-09-19 10:06:49 -04002653 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002654 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002655 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002656 if (status != 0) {
2657 if (status == -NFS4ERR_BADNAME &&
2658 !(o_arg->open_flags & O_CREAT))
2659 return -ENOENT;
2660 return status;
2661 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002662
Trond Myklebust6926afd12012-01-07 13:22:46 -05002663 nfs_fattr_map_and_free_names(server, &data->f_attr);
2664
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002665 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002666 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002667 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002668 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002669 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002670 if (data->file_created ||
2671 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002672 nfs4_update_changeattr(dir, &o_res->cinfo,
2673 o_res->f_attr->time_start,
2674 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002675 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002676 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2677 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002679 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002681 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002683 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2684 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002685 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2686 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002687 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002688 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691/*
2692 * OPEN_EXPIRED:
2693 * reclaim state on the server after a network partition.
2694 * Assumes caller holds the appropriate lock
2695 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002696static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002698 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002699 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002701 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002702 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002703 if (IS_ERR(opendata))
2704 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002705 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002706 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002707 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002708 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002709 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002712static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002713{
Trond Myklebust539cd032007-06-05 11:46:42 -04002714 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002715 struct nfs4_exception exception = { };
2716 int err;
2717
2718 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002719 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002720 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002721 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2722 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002723 switch (err) {
2724 default:
2725 goto out;
2726 case -NFS4ERR_GRACE:
2727 case -NFS4ERR_DELAY:
2728 nfs4_handle_exception(server, err, &exception);
2729 err = 0;
2730 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002731 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002732out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002733 return err;
2734}
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2737{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002739 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Trond Myklebust864472e2006-01-03 09:55:15 +01002741 ctx = nfs4_state_find_open_context(state);
2742 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002743 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002744 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002745 put_nfs_open_context(ctx);
2746 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
Trond Myklebust41020b62016-09-22 13:38:58 -04002749static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2750 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002751{
Trond Myklebust41020b62016-09-22 13:38:58 -04002752 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002753 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002754}
2755
2756static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2757{
2758 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002759 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002760}
2761
2762static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2763{
2764 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2765 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002766 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002767 return nfs4_open_expired(sp, state);
2768}
2769
Trond Myklebust45870d62016-09-22 13:38:59 -04002770static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2771 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002772 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002773{
2774 return -NFS4ERR_BAD_STATEID;
2775}
2776
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002777#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002778static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2779 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002780 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002781{
2782 int status;
2783
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002784 switch (stateid->type) {
2785 default:
2786 break;
2787 case NFS4_INVALID_STATEID_TYPE:
2788 case NFS4_SPECIAL_STATEID_TYPE:
2789 return -NFS4ERR_BAD_STATEID;
2790 case NFS4_REVOKED_STATEID_TYPE:
2791 goto out_free;
2792 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002793
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002794 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002795 switch (status) {
2796 case -NFS4ERR_EXPIRED:
2797 case -NFS4ERR_ADMIN_REVOKED:
2798 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002799 break;
2800 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002801 return status;
2802 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002803out_free:
2804 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002805 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002806 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002807}
2808
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002809static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002810{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002811 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002812 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002813 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002814 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002815 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002816
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002817 /* Get the delegation credential for use by test/free_stateid */
2818 rcu_read_lock();
2819 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002820 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002821 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002822 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002823 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002824 }
2825
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002826 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002827 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002828
Trond Myklebust994b15b2018-09-05 14:07:14 -04002829 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2830 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002831 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002832 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002833 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002834 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002835
NeilBrowna52458b2018-12-03 11:30:31 +11002836 if (delegation->cred)
2837 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002838 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002839 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002840 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002841 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002842 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002843 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002844 else
2845 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002846
Trond Myklebust8c39a392019-07-19 13:48:44 -04002847 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002848 return ret;
2849}
2850
2851static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2852{
2853 nfs4_stateid tmp;
2854
2855 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2856 nfs4_copy_delegation_stateid(state->inode, state->state,
2857 &tmp, NULL) &&
2858 nfs4_stateid_match_other(&state->stateid, &tmp))
2859 nfs_state_set_delegation(state, &tmp, state->state);
2860 else
2861 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002862}
2863
2864/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002865 * nfs41_check_expired_locks - possibly free a lock stateid
2866 *
2867 * @state: NFSv4 state for an inode
2868 *
2869 * Returns NFS_OK if recovery for this stateid is now finished.
2870 * Otherwise a negative NFS4ERR value is returned.
2871 */
2872static int nfs41_check_expired_locks(struct nfs4_state *state)
2873{
2874 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002875 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002876 struct nfs_server *server = NFS_SERVER(state->inode);
2877
2878 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2879 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002880
2881 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002882 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2883 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002884 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002885
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002886 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002887 spin_unlock(&state->state_lock);
2888
2889 nfs4_put_lock_state(prev);
2890 prev = lsp;
2891
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002892 status = nfs41_test_and_free_expired_stateid(server,
2893 &lsp->ls_stateid,
2894 cred);
2895 trace_nfs4_test_lock_stateid(state, lsp, status);
2896 if (status == -NFS4ERR_EXPIRED ||
2897 status == -NFS4ERR_BAD_STATEID) {
2898 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002899 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002900 if (!recover_lost_locks)
2901 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2902 } else if (status != NFS_OK) {
2903 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002904 nfs4_put_lock_state(prev);
2905 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002906 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002907 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002908 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002909 }
2910 spin_unlock(&state->state_lock);
2911 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002912out:
2913 return ret;
2914}
2915
2916/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002917 * nfs41_check_open_stateid - possibly free an open stateid
2918 *
2919 * @state: NFSv4 state for an inode
2920 *
2921 * Returns NFS_OK if recovery for this stateid is now finished.
2922 * Otherwise a negative NFS4ERR value is returned.
2923 */
2924static int nfs41_check_open_stateid(struct nfs4_state *state)
2925{
2926 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002927 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002928 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002929 int status;
2930
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002931 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002932 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002933 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002934 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002935 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002936 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002937 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002938 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002939 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002940 if (nfs_open_stateid_recover_openmode(state))
2941 return -NFS4ERR_OPENMODE;
2942 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002943}
2944
2945static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2946{
Chuck Levereb64cf92012-07-11 16:30:05 -04002947 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002948
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002949 status = nfs41_check_delegation_stateid(state);
2950 if (status != NFS_OK)
2951 return status;
2952 nfs41_delegation_recover_stateid(state);
2953
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002954 status = nfs41_check_expired_locks(state);
2955 if (status != NFS_OK)
2956 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002957 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002958 if (status != NFS_OK)
2959 status = nfs4_open_expired(sp, state);
2960 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002961}
2962#endif
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002965 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2966 * fields corresponding to attributes that were used to store the verifier.
2967 * Make sure we clobber those fields in the later setattr call
2968 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002969static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002970 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002971{
Trond Myklebust609339c2018-03-28 16:18:17 -04002972 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2973 __u32 attrset[3];
2974 unsigned ret;
2975 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002976
Trond Myklebust609339c2018-03-28 16:18:17 -04002977 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2978 attrset[i] = opendata->o_res.attrset[i];
2979 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2980 attrset[i] &= ~bitmask[i];
2981 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002982
Trond Myklebust609339c2018-03-28 16:18:17 -04002983 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2984 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002985
Trond Myklebust609339c2018-03-28 16:18:17 -04002986 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2987 if (sattr->ia_valid & ATTR_ATIME_SET)
2988 ret |= ATTR_ATIME_SET;
2989 else
2990 ret |= ATTR_ATIME;
2991 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002992
Trond Myklebust609339c2018-03-28 16:18:17 -04002993 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2994 if (sattr->ia_valid & ATTR_MTIME_SET)
2995 ret |= ATTR_MTIME_SET;
2996 else
2997 ret |= ATTR_MTIME;
2998 }
2999
3000 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003001 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04003002 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003003}
3004
Trond Myklebustc21443c2013-02-07 14:26:21 -05003005static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003006 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003007{
3008 struct nfs4_state_owner *sp = opendata->owner;
3009 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003010 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003011 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003012 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003013 struct inode *dir = d_inode(opendata->dir);
3014 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003015 unsigned int seq;
3016 int ret;
3017
3018 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003019 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003020
Fred Isaman3b65a302016-09-19 10:06:49 -04003021 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003022 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003023 goto out;
3024
Trond Myklebustae55e592018-05-22 11:17:16 -04003025 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003026 ret = PTR_ERR(state);
3027 if (IS_ERR(state))
3028 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003029 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003030 if (server->caps & NFS_CAP_POSIX_LOCK)
3031 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003032 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3033 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003034
Trond Myklebust275bb302013-05-29 13:11:28 -04003035 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003036 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003037 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003038 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003039 alias = d_exact_alias(dentry, state->inode);
3040 if (!alias)
3041 alias = d_splice_alias(igrab(state->inode), dentry);
3042 /* d_splice_alias() can't fail here - it's a non-directory */
3043 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003044 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003045 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003046 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003047 }
3048
3049 switch(opendata->o_arg.claim) {
3050 default:
3051 break;
3052 case NFS4_OPEN_CLAIM_NULL:
3053 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3054 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3055 if (!opendata->rpc_done)
3056 break;
3057 if (opendata->o_res.delegation_type != 0)
3058 dir_verifier = nfs_save_change_attribute(dir);
3059 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003060 }
3061
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003062 /* Parse layoutget results before we check for access */
3063 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3064
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003065 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3066 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003067 if (ret != 0)
3068 goto out;
3069
David Howells2b0143b2015-03-17 22:25:59 +00003070 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003071 nfs_inode_attach_open_context(ctx);
3072 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3073 nfs4_schedule_stateid_recovery(server, state);
3074 }
Fred Isaman2409a972016-10-06 12:11:21 -04003075
Trond Myklebustc21443c2013-02-07 14:26:21 -05003076out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003077 if (!opendata->cancelled)
3078 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003079 return ret;
3080}
3081
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003082/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003083 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 */
Andy Adamson82be4172012-05-23 05:02:35 -04003085static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003086 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003087 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003088 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003089 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
3091 struct nfs4_state_owner *sp;
3092 struct nfs4_state *state = NULL;
3093 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003094 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003095 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003096 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003097 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003098 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003099 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003100 struct iattr *sattr = c->sattr;
3101 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003102 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003103 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003107 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3108 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3110 goto out_err;
3111 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003112 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003113 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003114 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003115 if (d_really_is_positive(dentry))
3116 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003117 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003118 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003119 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003120 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3121 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003122 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003123 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
David Quigley14c43f72013-05-22 12:50:43 -04003125 if (label) {
3126 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3127 if (IS_ERR(olabel)) {
3128 status = PTR_ERR(olabel);
3129 goto err_opendata_put;
3130 }
3131 }
3132
Trond Myklebuste911b812014-03-26 13:24:37 -07003133 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3134 if (!opendata->f_attr.mdsthreshold) {
3135 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3136 if (!opendata->f_attr.mdsthreshold)
3137 goto err_free_label;
3138 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003139 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003140 }
David Howells2b0143b2015-03-17 22:25:59 +00003141 if (d_really_is_positive(dentry))
3142 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003143
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003144 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003145 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003146 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003147 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003148
NeilBrownefcbc042015-07-30 13:00:56 +10003149 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003150 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003151 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003152 /*
3153 * send create attributes which was not set by open
3154 * with an extra setattr.
3155 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003156 if (attrs || label) {
3157 unsigned ia_old = sattr->ia_valid;
3158
3159 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003160 nfs_fattr_init(opendata->o_res.f_attr);
3161 status = nfs4_do_setattr(state->inode, cred,
3162 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003163 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003164 if (status == 0) {
3165 nfs_setattr_update_inode(state->inode, sattr,
3166 opendata->o_res.f_attr);
3167 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3168 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003169 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003170 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003171 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003172 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003173 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003174
Trond Myklebuste911b812014-03-26 13:24:37 -07003175 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003176 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003177 opendata->f_attr.mdsthreshold = NULL;
3178 }
Andy Adamson82be4172012-05-23 05:02:35 -04003179
David Quigley14c43f72013-05-22 12:50:43 -04003180 nfs4_label_free(olabel);
3181
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003182 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003185err_free_label:
3186 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003187err_opendata_put:
3188 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003189err_put_state_owner:
3190 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return status;
3193}
3194
3195
Andy Adamson82be4172012-05-23 05:02:35 -04003196static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003197 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003198 int flags,
3199 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003200 struct nfs4_label *label,
3201 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003203 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003204 struct nfs4_exception exception = {
3205 .interruptible = true,
3206 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003208 struct nfs4_open_createattrs c = {
3209 .label = label,
3210 .sattr = sattr,
3211 .verf = {
3212 [0] = (__u32)jiffies,
3213 [1] = (__u32)current->pid,
3214 },
3215 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 int status;
3217
3218 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003219 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003220 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003221 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 if (status == 0)
3223 break;
3224 /* NOTE: BAD_SEQID means the server and client disagree about the
3225 * book-keeping w.r.t. state-changing operations
3226 * (OPEN/CLOSE/LOCK/LOCKU...)
3227 * It is actually a sign of a bug on the client or on the server.
3228 *
3229 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003230 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 * have unhashed the old state_owner for us, and that we can
3232 * therefore safely retry using a new one. We should still warn
3233 * the user though...
3234 */
3235 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003236 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003237 " returned a bad sequence-id error!\n",
3238 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 exception.retry = 1;
3240 continue;
3241 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003242 /*
3243 * BAD_STATEID on OPEN means that the server cancelled our
3244 * state before it received the OPEN_CONFIRM.
3245 * Recover by retrying the request as per the discussion
3246 * on Page 181 of RFC3530.
3247 */
3248 if (status == -NFS4ERR_BAD_STATEID) {
3249 exception.retry = 1;
3250 continue;
3251 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003252 if (status == -NFS4ERR_EXPIRED) {
3253 nfs4_schedule_lease_recovery(server->nfs_client);
3254 exception.retry = 1;
3255 continue;
3256 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003257 if (status == -EAGAIN) {
3258 /* We must have found a delegation */
3259 exception.retry = 1;
3260 continue;
3261 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003262 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3263 continue;
3264 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 status, &exception));
3266 } while (exception.retry);
3267 return res;
3268}
3269
Trond Myklebust8487c472016-06-26 08:44:35 -04003270static int _nfs4_do_setattr(struct inode *inode,
3271 struct nfs_setattrargs *arg,
3272 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003273 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003274 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003275{
3276 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003277 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3279 .rpc_argp = arg,
3280 .rpc_resp = res,
3281 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003282 };
NeilBrowna52458b2018-12-03 11:30:31 +11003283 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003284 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003285 bool truncate;
3286 int status;
3287
3288 nfs_fattr_init(res->fattr);
3289
3290 /* Servers should only apply open mode checks for file size changes */
3291 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003292 if (!truncate) {
3293 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003294 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003295 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003296
Trond Myklebust991eedb2018-04-09 11:15:30 -04003297 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003298 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003299 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003300 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003301 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003302 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003303 l_ctx = nfs_get_lock_context(ctx);
3304 if (IS_ERR(l_ctx))
3305 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003306 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3307 &arg->stateid, &delegation_cred);
3308 nfs_put_lock_context(l_ctx);
3309 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003310 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003311 else if (status == -EAGAIN)
3312 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003313 } else {
3314zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003315 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003316 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003317 if (delegation_cred)
3318 msg.rpc_cred = delegation_cred;
3319
3320 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3321
NeilBrowna52458b2018-12-03 11:30:31 +11003322 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003323 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003324 renew_lease(server, timestamp);
3325 trace_nfs4_setattr(inode, &arg->stateid, status);
3326 return status;
3327}
3328
NeilBrowna52458b2018-12-03 11:30:31 +11003329static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003330 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003331 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003332 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003334 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003335 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003336 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003337 struct nfs_setattrargs arg = {
3338 .fh = NFS_FH(inode),
3339 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003341 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003342 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003343 };
3344 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003346 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003348 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003349 struct nfs4_exception exception = {
3350 .state = state,
3351 .inode = inode,
3352 .stateid = &arg.stateid,
3353 };
3354 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003357 nfs4_bitmap_copy_adjust_setattr(bitmask,
3358 nfs4_bitmask(server, olabel),
3359 inode);
3360
NeilBrown29b59f92016-10-13 15:26:47 +11003361 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003362 switch (err) {
3363 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003364 if (!(sattr->ia_valid & ATTR_SIZE)) {
3365 pr_warn_once("NFSv4: server %s is incorrectly "
3366 "applying open mode checks to "
3367 "a SETATTR that is not "
3368 "changing file size.\n",
3369 server->nfs_client->cl_hostname);
3370 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003371 if (state && !(state->state & FMODE_WRITE)) {
3372 err = -EBADF;
3373 if (sattr->ia_valid & ATTR_OPEN)
3374 err = -EACCES;
3375 goto out;
3376 }
3377 }
3378 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003380out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 return err;
3382}
3383
Peng Tao500d7012015-09-22 11:35:22 +08003384static bool
3385nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3386{
3387 if (inode == NULL || !nfs_have_layout(inode))
3388 return false;
3389
3390 return pnfs_wait_on_layoutreturn(inode, task);
3391}
3392
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003393/*
3394 * Update the seqid of an open stateid
3395 */
3396static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3397 struct nfs4_state *state)
3398{
3399 __be32 seqid_open;
3400 u32 dst_seqid;
3401 int seq;
3402
3403 for (;;) {
3404 if (!nfs4_valid_open_stateid(state))
3405 break;
3406 seq = read_seqbegin(&state->seqlock);
3407 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3408 nfs4_stateid_copy(dst, &state->open_stateid);
3409 if (read_seqretry(&state->seqlock, seq))
3410 continue;
3411 break;
3412 }
3413 seqid_open = state->open_stateid.seqid;
3414 if (read_seqretry(&state->seqlock, seq))
3415 continue;
3416
3417 dst_seqid = be32_to_cpu(dst->seqid);
3418 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3419 dst->seqid = seqid_open;
3420 break;
3421 }
3422}
3423
3424/*
3425 * Update the seqid of an open stateid after receiving
3426 * NFS4ERR_OLD_STATEID
3427 */
3428static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3429 struct nfs4_state *state)
3430{
3431 __be32 seqid_open;
3432 u32 dst_seqid;
3433 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003434 int seq, status = -EAGAIN;
3435 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003436
3437 for (;;) {
3438 ret = false;
3439 if (!nfs4_valid_open_stateid(state))
3440 break;
3441 seq = read_seqbegin(&state->seqlock);
3442 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3443 if (read_seqretry(&state->seqlock, seq))
3444 continue;
3445 break;
3446 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003447
3448 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003449 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003450
3451 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003452
3453 /* Did another OPEN bump the state's seqid? try again: */
3454 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003455 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003456 write_sequnlock(&state->seqlock);
3457 ret = true;
3458 break;
3459 }
3460
3461 /* server says we're behind but we haven't seen the update yet */
3462 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3463 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3464 write_sequnlock(&state->seqlock);
3465 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3466
3467 if (signal_pending(current))
3468 status = -EINTR;
3469 else
3470 if (schedule_timeout(5*HZ) != 0)
3471 status = 0;
3472
3473 finish_wait(&state->waitq, &wait);
3474
3475 if (!status)
3476 continue;
3477 if (status == -EINTR)
3478 break;
3479
3480 /* we slept the whole 5 seconds, we must have lost a seqid */
3481 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003482 ret = true;
3483 break;
3484 }
3485
3486 return ret;
3487}
3488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489struct nfs4_closedata {
3490 struct inode *inode;
3491 struct nfs4_state *state;
3492 struct nfs_closeargs arg;
3493 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003494 struct {
3495 struct nfs4_layoutreturn_args arg;
3496 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003497 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003498 u32 roc_barrier;
3499 bool roc;
3500 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003501 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003502 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503};
3504
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003505static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003506{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003507 struct nfs4_closedata *calldata = data;
3508 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003509 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003510
Trond Myklebustcf805162016-11-15 14:56:07 -05003511 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003512 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3513 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003514 nfs4_put_open_state(calldata->state);
3515 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003516 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003517 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003518 kfree(calldata);
3519}
3520
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003521static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003523 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003526 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003527 struct nfs4_exception exception = {
3528 .state = state,
3529 .inode = calldata->inode,
3530 .stateid = &calldata->arg.stateid,
3531 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Chuck Levera3ca5652012-03-01 17:00:40 -05003533 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003534 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3535 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003536 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003537
3538 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04003539 if (pnfs_roc_done(task, calldata->inode,
3540 &calldata->arg.lr_args,
3541 &calldata->res.lr_res,
3542 &calldata->res.lr_ret) == -EAGAIN)
3543 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003544
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003545 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 * the state_owner. we keep this around to process errors
3547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 switch (task->tk_status) {
3549 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003550 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003551 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003552 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003553 case -NFS4ERR_ACCESS:
3554 if (calldata->arg.bitmask != NULL) {
3555 calldata->arg.bitmask = NULL;
3556 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003557 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003558
3559 }
3560 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003561 case -NFS4ERR_OLD_STATEID:
3562 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003563 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003564 state))
3565 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003566 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003567 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003569 case -NFS4ERR_EXPIRED:
3570 nfs4_free_revoked_stateid(server,
3571 &calldata->arg.stateid,
3572 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003573 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003574 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003575 if (calldata->arg.fmode == 0)
3576 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003577 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003579 task->tk_status = nfs4_async_handle_exception(task,
3580 server, task->tk_status, &exception);
3581 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003582 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003584 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3585 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003586out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003587 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003588 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003589 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003590 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003591 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003592out_restart:
3593 task->tk_status = 0;
3594 rpc_restart_call_prepare(task);
3595 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596}
3597
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003598static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003600 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003601 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003602 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003603 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003604 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003605 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003606
Chuck Levera3ca5652012-03-01 17:00:40 -05003607 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003608 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003609 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003610
Trond Myklebust88069f72009-12-08 08:33:16 -05003611 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003612 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003613 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3614 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3615 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003616 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003617 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003618 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003619 if (state->n_rdonly == 0)
3620 call_close |= is_rdonly;
3621 else if (is_rdonly)
3622 calldata->arg.fmode |= FMODE_READ;
3623 if (state->n_wronly == 0)
3624 call_close |= is_wronly;
3625 else if (is_wronly)
3626 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003627 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3628 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003629 } else if (is_rdwr)
3630 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3631
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003632 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3633 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003634 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003635 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003636
3637 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003638 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003639 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003640 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003641
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003642 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003643 nfs_release_seqid(calldata->arg.seqid);
3644 goto out_wait;
3645 }
3646
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003647 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3648 if (lo && !pnfs_layout_is_valid(lo)) {
3649 calldata->arg.lr_args = NULL;
3650 calldata->res.lr_res = NULL;
3651 }
3652
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003653 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003654 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003655
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003656 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003657 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003658 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003659 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003660 nfs4_bitmask_adjust(calldata->arg.bitmask, inode, NFS_SERVER(inode), NULL);
3661 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003662 calldata->arg.bitmask = NULL;
3663 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003664
Trond Myklebust6ae37332015-01-30 14:21:14 -05003665 calldata->arg.share_access =
3666 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3667 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003668
Trond Myklebustd8d84982016-12-19 12:14:44 -05003669 if (calldata->res.fattr == NULL)
3670 calldata->arg.bitmask = NULL;
3671 else if (calldata->arg.bitmask == NULL)
3672 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003673 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003674 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003675 &calldata->arg.seq_args,
3676 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003677 task) != 0)
3678 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003679 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003680 return;
3681out_no_action:
3682 task->tk_action = NULL;
3683out_wait:
3684 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685}
3686
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003687static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003688 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003689 .rpc_call_done = nfs4_close_done,
3690 .rpc_release = nfs4_free_closedata,
3691};
3692
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693/*
3694 * It is possible for data to be read/written from a mem-mapped file
3695 * after the sys_close call (which hits the vfs layer as a flush).
3696 * This means that we can't safely call nfsv4 close on a file until
3697 * the inode is cleared. This in turn means that we are not good
3698 * NFSv4 citizens - we do not indicate to the server to update the file's
3699 * share state even when we are done with one of the three share
3700 * stateid's in the inode.
3701 *
3702 * NOTE: Caller must be holding the sp->so_owner semaphore!
3703 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003704int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003706 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003707 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003709 struct nfs4_state_owner *sp = state->owner;
3710 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003711 struct rpc_message msg = {
3712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3713 .rpc_cred = state->owner->so_cred,
3714 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003715 struct rpc_task_setup task_setup_data = {
3716 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003717 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003718 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003719 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003720 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003721 };
Trond Myklebust95121352005-10-18 14:20:12 -07003722 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003724 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3725 &task_setup_data.rpc_client, &msg);
3726
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003727 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003729 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003730 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003731 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003733 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003734 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3735 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003737 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3738 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003739 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003740 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003741 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003742 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003743 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003744 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003745 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003746 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003747 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003748 calldata->lr.roc = pnfs_roc(state->inode,
3749 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3750 if (calldata->lr.roc) {
3751 calldata->arg.lr_args = &calldata->lr.arg;
3752 calldata->res.lr_res = &calldata->lr.res;
3753 }
Al Viro643168c2011-06-22 18:20:23 -04003754 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003755
Trond Myklebust1174dd12010-12-21 10:52:24 -05003756 msg.rpc_argp = &calldata->arg;
3757 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003758 task_setup_data.callback_data = calldata;
3759 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003760 if (IS_ERR(task))
3761 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003762 status = 0;
3763 if (wait)
3764 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003765 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003766 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003767out_free_calldata:
3768 kfree(calldata);
3769out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003770 nfs4_put_open_state(state);
3771 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003772 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773}
3774
Trond Myklebust2b484292010-09-17 10:56:51 -04003775static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003776nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3777 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003780 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3781
3782 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Trond Myklebust565277f2007-10-15 18:17:53 -04003784 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003785 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003786
3787 nfs4_label_release_security(label);
3788
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003789 if (IS_ERR(state))
3790 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003791 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
Trond Myklebust1185a552009-12-03 15:54:02 -05003794static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003795{
3796 if (ctx->state == NULL)
3797 return;
3798 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003799 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003800 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003801 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003802}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
Trond Myklebustb944dba2013-11-04 15:20:20 -05003804#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3805#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003806#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3809{
Kinglong Mee8c612822015-08-26 21:12:58 +08003810 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003811 struct nfs4_server_caps_arg args = {
3812 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003813 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003814 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 struct nfs4_server_caps_res res = {};
3816 struct rpc_message msg = {
3817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003818 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 .rpc_resp = &res,
3820 };
3821 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003822 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Kinglong Mee8c612822015-08-26 21:12:58 +08003824 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3825 FATTR4_WORD0_FH_EXPIRE_TYPE |
3826 FATTR4_WORD0_LINK_SUPPORT |
3827 FATTR4_WORD0_SYMLINK_SUPPORT |
3828 FATTR4_WORD0_ACLSUPPORT;
3829 if (minorversion)
3830 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3831
Bryan Schumaker7c513052011-03-24 17:12:24 +00003832 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003834 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003835 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003836 case 0:
3837 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3838 res.attr_bitmask[2] = 0;
3839 break;
3840 case 1:
3841 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3842 break;
3843 case 2:
3844 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003847 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3848 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3849 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3850 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003851 NFS_CAP_CTIME|NFS_CAP_MTIME|
3852 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003853 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3854 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 server->caps |= NFS_CAP_ACLS;
3856 if (res.has_links != 0)
3857 server->caps |= NFS_CAP_HARDLINKS;
3858 if (res.has_symlinks != 0)
3859 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003860 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3861 server->caps |= NFS_CAP_FILEID;
3862 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3863 server->caps |= NFS_CAP_MODE;
3864 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3865 server->caps |= NFS_CAP_NLINK;
3866 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3867 server->caps |= NFS_CAP_OWNER;
3868 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3869 server->caps |= NFS_CAP_OWNER_GROUP;
3870 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3871 server->caps |= NFS_CAP_ATIME;
3872 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3873 server->caps |= NFS_CAP_CTIME;
3874 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3875 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003876#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3877 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3878 server->caps |= NFS_CAP_SECURITY_LABEL;
3879#endif
3880 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3881 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003882 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003883
Trond Myklebusta65318b2009-03-11 14:10:28 -04003884 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3885 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3886 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003887 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003888
3889 /* Avoid a regression due to buggy server */
3890 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3891 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003892 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3893 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003896 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return status;
3900}
3901
Trond Myklebust55a97592006-06-09 09:34:19 -04003902int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
Trond Myklebust0688e642019-04-07 13:59:09 -04003904 struct nfs4_exception exception = {
3905 .interruptible = true,
3906 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 int err;
3908 do {
3909 err = nfs4_handle_exception(server,
3910 _nfs4_server_capabilities(server, fhandle),
3911 &exception);
3912 } while (exception.retry);
3913 return err;
3914}
3915
3916static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3917 struct nfs_fsinfo *info)
3918{
David Quigleyaa9c2662013-05-22 12:50:44 -04003919 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003921 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 };
3923 struct nfs4_lookup_res res = {
3924 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003925 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 .fh = fhandle,
3927 };
3928 struct rpc_message msg = {
3929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3930 .rpc_argp = &args,
3931 .rpc_resp = &res,
3932 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003933
David Quigleyaa9c2662013-05-22 12:50:44 -04003934 bitmask[0] = nfs4_fattr_bitmap[0];
3935 bitmask[1] = nfs4_fattr_bitmap[1];
3936 /*
3937 * Process the label in the upcoming getfattr
3938 */
3939 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3940
Trond Myklebust0e574af2005-10-27 22:12:38 -04003941 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003942 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943}
3944
3945static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3946 struct nfs_fsinfo *info)
3947{
Trond Myklebust0688e642019-04-07 13:59:09 -04003948 struct nfs4_exception exception = {
3949 .interruptible = true,
3950 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 int err;
3952 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003953 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003954 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003955 switch (err) {
3956 case 0:
3957 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003958 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003959 default:
3960 err = nfs4_handle_exception(server, err, &exception);
3961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003963out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 return err;
3965}
3966
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003967static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3968 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3969{
Trond Myklebustc2190662013-08-26 19:23:04 -04003970 struct rpc_auth_create_args auth_args = {
3971 .pseudoflavor = flavor,
3972 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003973 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003974
Trond Myklebustc2190662013-08-26 19:23:04 -04003975 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003976 if (IS_ERR(auth))
3977 return -EACCES;
3978 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003979}
3980
Chuck Lever9a744ba2013-03-16 15:56:02 -04003981/*
3982 * Retry pseudoroot lookup with various security flavors. We do this when:
3983 *
3984 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3985 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3986 *
3987 * Returns zero on success, or a negative NFS4ERR value, or a
3988 * negative errno value.
3989 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003990static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003991 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003993 /* Per 3530bis 15.33.5 */
3994 static const rpc_authflavor_t flav_array[] = {
3995 RPC_AUTH_GSS_KRB5P,
3996 RPC_AUTH_GSS_KRB5I,
3997 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003998 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003999 RPC_AUTH_NULL,
4000 };
4001 int status = -EPERM;
4002 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04004004 if (server->auth_info.flavor_len > 0) {
4005 /* try each flavor specified by user */
4006 for (i = 0; i < server->auth_info.flavor_len; i++) {
4007 status = nfs4_lookup_root_sec(server, fhandle, info,
4008 server->auth_info.flavors[i]);
4009 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4010 continue;
4011 break;
4012 }
4013 } else {
4014 /* no flavors specified by user, try default list */
4015 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4016 status = nfs4_lookup_root_sec(server, fhandle, info,
4017 flav_array[i]);
4018 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4019 continue;
4020 break;
4021 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004022 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004023
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004024 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004025 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004026 * to access the mount. It could also mean that we tried to mount
4027 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4028 * existing mount programs don't handle -EACCES very well so it should
4029 * be mapped to -EPERM instead.
4030 */
4031 if (status == -EACCES)
4032 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004033 return status;
4034}
4035
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004036/**
4037 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4038 * @server: initialized nfs_server handle
4039 * @fhandle: we fill in the pseudo-fs root file handle
4040 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004041 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004042 *
4043 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004044 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004045int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004046 struct nfs_fsinfo *info,
4047 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004048{
Andre Przywarac7757072015-04-23 17:17:40 +01004049 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004050
Andre Przywarac7757072015-04-23 17:17:40 +01004051 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004052 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004053
4054 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004055 status = server->nfs_client->cl_mvops->find_root_sec(server,
4056 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004057
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 if (status == 0)
4059 status = nfs4_server_capabilities(server, fhandle);
4060 if (status == 0)
4061 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004062
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004063 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
Bryan Schumakerbae36242012-05-10 15:07:31 -04004066static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4067 struct nfs_fsinfo *info)
4068{
4069 int error;
4070 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004071 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004072
4073 error = nfs4_server_capabilities(server, mntfh);
4074 if (error < 0) {
4075 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4076 return error;
4077 }
4078
Trond Myklebusta841b542018-04-07 13:50:59 -04004079 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004080 if (error < 0) {
4081 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004082 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004083 }
4084
4085 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4086 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4087 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4088
Scott Mayhew779df6a2020-03-03 17:58:37 -05004089out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004090 return error;
4091}
4092
Manoj Naik6b97fd32006-06-09 09:34:29 -04004093/*
4094 * Get locations and (maybe) other attributes of a referral.
4095 * Note that we'll actually follow the referral later when
4096 * we detect fsid mismatch in inode revalidation
4097 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004098static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4099 const struct qstr *name, struct nfs_fattr *fattr,
4100 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004101{
4102 int status = -ENOMEM;
4103 struct page *page = NULL;
4104 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004105
4106 page = alloc_page(GFP_KERNEL);
4107 if (page == NULL)
4108 goto out;
4109 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4110 if (locations == NULL)
4111 goto out;
4112
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004113 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004114 if (status != 0)
4115 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004116
4117 /*
4118 * If the fsid didn't change, this is a migration event, not a
4119 * referral. Cause us to drop into the exception handler, which
4120 * will kick off migration recovery.
4121 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004122 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004123 dprintk("%s: server did not return a different fsid for"
4124 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004125 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004126 goto out;
4127 }
Andy Adamson533eb462011-06-13 18:25:56 -04004128 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4129 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004130
Andy Adamson533eb462011-06-13 18:25:56 -04004131 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004132 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004133 memset(fhandle, 0, sizeof(struct nfs_fh));
4134out:
4135 if (page)
4136 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004137 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004138 return status;
4139}
4140
David Quigley1775fd32013-05-22 12:50:42 -04004141static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004142 struct nfs_fattr *fattr, struct nfs4_label *label,
4143 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
Trond Myklebust771734f2018-04-07 13:54:23 -04004145 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 struct nfs4_getattr_arg args = {
4147 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004148 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 };
4150 struct nfs4_getattr_res res = {
4151 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004152 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 .server = server,
4154 };
4155 struct rpc_message msg = {
4156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4157 .rpc_argp = &args,
4158 .rpc_resp = &res,
4159 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004160 unsigned short task_flags = 0;
4161
4162 /* Is this is an attribute revalidation, subject to softreval? */
4163 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4164 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004165
Trond Myklebust771734f2018-04-07 13:54:23 -04004166 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004167
Trond Myklebust0e574af2005-10-27 22:12:38 -04004168 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004169 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4170 return nfs4_do_call_sync(server->client, server, &msg,
4171 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172}
4173
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004174int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004175 struct nfs_fattr *fattr, struct nfs4_label *label,
4176 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177{
Trond Myklebust0688e642019-04-07 13:59:09 -04004178 struct nfs4_exception exception = {
4179 .interruptible = true,
4180 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 int err;
4182 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004183 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004184 trace_nfs4_getattr(server, fhandle, fattr, err);
4185 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 &exception);
4187 } while (exception.retry);
4188 return err;
4189}
4190
4191/*
4192 * The file is not closed if it is opened due to the a request to change
4193 * the size of the file. The open call will not be needed once the
4194 * VFS layer lookup-intents are implemented.
4195 *
4196 * Close is called when the inode is destroyed.
4197 * If we haven't opened the file for O_WRONLY, we
4198 * need to in the size_change case to obtain a stateid.
4199 *
4200 * Got race?
4201 * Because OPEN is always done by name in nfsv4, it is
4202 * possible that we opened a different file by the same
4203 * name. We can recognize this race condition, but we
4204 * can't do anything about it besides returning an error.
4205 *
4206 * This will be fixed with VFS changes (lookup-intent).
4207 */
4208static int
4209nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4210 struct iattr *sattr)
4211{
David Howells2b0143b2015-03-17 22:25:59 +00004212 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004213 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004214 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004215 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 int status;
4217
Peng Tao88ac8152014-09-12 11:04:10 +08004218 if (pnfs_ld_layoutret_on_setattr(inode) &&
4219 sattr->ia_valid & ATTR_SIZE &&
4220 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004221 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004222
Trond Myklebust0e574af2005-10-27 22:12:38 -04004223 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Andy Adamson26699402012-05-30 16:12:24 -04004225 /* Deal with open(O_TRUNC) */
4226 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004227 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004228
4229 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004230 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004231 return 0;
4232
Trond Myklebustd5308382005-11-04 15:33:38 -05004233 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004234 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004235
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004236 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004237 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004238 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004239 }
4240
David Quigley14c43f72013-05-22 12:50:43 -04004241 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4242 if (IS_ERR(label))
4243 return PTR_ERR(label);
4244
Trond Myklebust199366f2018-03-20 16:43:18 -04004245 /* Return any delegations if we're going to change ACLs */
4246 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004247 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004248
NeilBrown29b59f92016-10-13 15:26:47 +11004249 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004250 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004251 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004252 nfs_setsecurity(inode, fattr, label);
4253 }
David Quigley14c43f72013-05-22 12:50:43 -04004254 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 return status;
4256}
4257
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004258static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004259 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004260 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004261{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004262 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004263 int status;
4264 struct nfs4_lookup_arg args = {
4265 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004266 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004267 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004268 };
4269 struct nfs4_lookup_res res = {
4270 .server = server,
4271 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004272 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004273 .fh = fhandle,
4274 };
4275 struct rpc_message msg = {
4276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4277 .rpc_argp = &args,
4278 .rpc_resp = &res,
4279 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004280 unsigned short task_flags = 0;
4281
4282 /* Is this is an attribute revalidation, subject to softreval? */
4283 if (nfs_lookup_is_soft_revalidate(dentry))
4284 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004285
David Quigleyaa9c2662013-05-22 12:50:44 -04004286 args.bitmask = nfs4_bitmask(server, label);
4287
David Howells2b3de442006-08-22 20:06:09 -04004288 nfs_fattr_init(fattr);
4289
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004290 dprintk("NFS call lookup %pd2\n", dentry);
4291 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4292 status = nfs4_do_call_sync(clnt, server, &msg,
4293 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 dprintk("NFS reply lookup: %d\n", status);
4295 return status;
4296}
4297
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004298static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004299{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004300 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004301 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004302 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4303 fattr->nlink = 2;
4304}
4305
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004306static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004307 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004308 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004309{
Trond Myklebust0688e642019-04-07 13:59:09 -04004310 struct nfs4_exception exception = {
4311 .interruptible = true,
4312 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004313 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004314 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004315 int err;
4316 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004317 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004318 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004319 switch (err) {
4320 case -NFS4ERR_BADNAME:
4321 err = -ENOENT;
4322 goto out;
4323 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004324 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004325 if (err == -NFS4ERR_MOVED)
4326 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004327 goto out;
4328 case -NFS4ERR_WRONGSEC:
4329 err = -EPERM;
4330 if (client != *clnt)
4331 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004332 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004333 if (IS_ERR(client))
4334 return PTR_ERR(client);
4335
4336 exception.retry = 1;
4337 break;
4338 default:
4339 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4340 }
4341 } while (exception.retry);
4342
4343out:
4344 if (err == 0)
4345 *clnt = client;
4346 else if (client != *clnt)
4347 rpc_shutdown_client(client);
4348
4349 return err;
4350}
4351
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004352static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004353 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4354 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004356 int status;
4357 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004358
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004359 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004360 if (client != NFS_CLIENT(dir)) {
4361 rpc_shutdown_client(client);
4362 nfs_fixup_secinfo_attributes(fattr);
4363 }
4364 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365}
4366
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004367struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004368nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004369 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4370{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004371 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004372 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004373
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004374 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004375 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004376 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004377 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004378}
4379
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004380static int _nfs4_proc_lookupp(struct inode *inode,
4381 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4382 struct nfs4_label *label)
4383{
4384 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4385 struct nfs_server *server = NFS_SERVER(inode);
4386 int status;
4387 struct nfs4_lookupp_arg args = {
4388 .bitmask = server->attr_bitmask,
4389 .fh = NFS_FH(inode),
4390 };
4391 struct nfs4_lookupp_res res = {
4392 .server = server,
4393 .fattr = fattr,
4394 .label = label,
4395 .fh = fhandle,
4396 };
4397 struct rpc_message msg = {
4398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4399 .rpc_argp = &args,
4400 .rpc_resp = &res,
4401 };
Trond Myklebust76998eb2020-10-20 14:30:35 -04004402 unsigned short task_flags = 0;
4403
4404 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4405 task_flags |= RPC_TASK_TIMEOUT;
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004406
4407 args.bitmask = nfs4_bitmask(server, label);
4408
4409 nfs_fattr_init(fattr);
4410
4411 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4412 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
Trond Myklebust76998eb2020-10-20 14:30:35 -04004413 &res.seq_res, task_flags);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004414 dprintk("NFS reply lookupp: %d\n", status);
4415 return status;
4416}
4417
4418static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4419 struct nfs_fattr *fattr, struct nfs4_label *label)
4420{
Trond Myklebust0688e642019-04-07 13:59:09 -04004421 struct nfs4_exception exception = {
4422 .interruptible = true,
4423 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004424 int err;
4425 do {
4426 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4427 trace_nfs4_lookupp(inode, err);
4428 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4429 &exception);
4430 } while (exception.retry);
4431 return err;
4432}
4433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4435{
Trond Myklebust76b32992007-08-10 17:45:11 -04004436 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 struct nfs4_accessargs args = {
4438 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004439 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004441 struct nfs4_accessres res = {
4442 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004443 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 struct rpc_message msg = {
4445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4446 .rpc_argp = &args,
4447 .rpc_resp = &res,
4448 .rpc_cred = entry->cred,
4449 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004450 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Trond Myklebust7c672652018-06-04 15:00:53 -04004452 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004453 res.fattr = nfs_alloc_fattr();
4454 if (res.fattr == NULL)
4455 return -ENOMEM;
4456 args.bitmask = server->cache_consistency_bitmask;
4457 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004458 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004460 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004461 if (res.fattr)
4462 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004464 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 return status;
4466}
4467
4468static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4469{
Trond Myklebust0688e642019-04-07 13:59:09 -04004470 struct nfs4_exception exception = {
4471 .interruptible = true,
4472 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 int err;
4474 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004475 err = _nfs4_proc_access(inode, entry);
4476 trace_nfs4_access(inode, err);
4477 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 &exception);
4479 } while (exception.retry);
4480 return err;
4481}
4482
4483/*
4484 * TODO: For the time being, we don't try to get any attributes
4485 * along with any of the zero-copy operations READ, READDIR,
4486 * READLINK, WRITE.
4487 *
4488 * In the case of the first three, we want to put the GETATTR
4489 * after the read-type operation -- this is because it is hard
4490 * to predict the length of a GETATTR response in v4, and thus
4491 * align the READ data correctly. This means that the GETATTR
4492 * may end up partially falling into the page cache, and we should
4493 * shift it into the 'tail' of the xdr_buf before processing.
4494 * To do this efficiently, we need to know the total length
4495 * of data received, which doesn't seem to be available outside
4496 * of the RPC layer.
4497 *
4498 * In the case of WRITE, we also want to put the GETATTR after
4499 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004500 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 *
4502 * Both of these changes to the XDR layer would in fact be quite
4503 * minor, but I decided to leave them for a subsequent patch.
4504 */
4505static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4506 unsigned int pgbase, unsigned int pglen)
4507{
4508 struct nfs4_readlink args = {
4509 .fh = NFS_FH(inode),
4510 .pgbase = pgbase,
4511 .pglen = pglen,
4512 .pages = &page,
4513 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004514 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 struct rpc_message msg = {
4516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4517 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004518 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 };
4520
Bryan Schumaker7c513052011-03-24 17:12:24 +00004521 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 -07004522}
4523
4524static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4525 unsigned int pgbase, unsigned int pglen)
4526{
Trond Myklebust0688e642019-04-07 13:59:09 -04004527 struct nfs4_exception exception = {
4528 .interruptible = true,
4529 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 int err;
4531 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004532 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4533 trace_nfs4_readlink(inode, err);
4534 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 &exception);
4536 } while (exception.retry);
4537 return err;
4538}
4539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004541 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543static int
4544nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004545 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004547 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004548 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004549 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 int status = 0;
4552
NeilBrown532d4de2016-10-13 15:26:47 +11004553 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004554 if (IS_ERR(ctx))
4555 return PTR_ERR(ctx);
4556
David Quigleyaa9c2662013-05-22 12:50:44 -04004557 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4558
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004559 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4560 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004561 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 if (IS_ERR(state)) {
4563 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004564 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004567 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004568 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 return status;
4570}
4571
Trond Myklebust3c591172018-07-31 15:54:10 -04004572static int
4573_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
Trond Myklebust16e42952005-10-27 22:12:44 -04004575 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004576 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004578 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004580 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004581 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004582 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4585 .rpc_argp = &args,
4586 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004588 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004589 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004590
Bryan Schumaker7c513052011-03-24 17:12:24 +00004591 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004592 if (status == 0) {
4593 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004594 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4595 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004596 /* Removing a directory decrements nlink in the parent */
4597 if (ftype == NF4DIR && dir->i_nlink > 2)
4598 nfs4_dec_nlink_locked(dir);
4599 spin_unlock(&dir->i_lock);
4600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 return status;
4602}
4603
Trond Myklebust912678d2018-03-20 16:43:15 -04004604static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4605{
Trond Myklebust0688e642019-04-07 13:59:09 -04004606 struct nfs4_exception exception = {
4607 .interruptible = true,
4608 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004609 struct inode *inode = d_inode(dentry);
4610 int err;
4611
Trond Myklebustc01d3642018-03-20 16:43:20 -04004612 if (inode) {
4613 if (inode->i_nlink == 1)
4614 nfs4_inode_return_delegation(inode);
4615 else
4616 nfs4_inode_make_writeable(inode);
4617 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004618 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004619 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004620 trace_nfs4_remove(dir, &dentry->d_name, err);
4621 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4622 &exception);
4623 } while (exception.retry);
4624 return err;
4625}
4626
4627static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628{
Trond Myklebust0688e642019-04-07 13:59:09 -04004629 struct nfs4_exception exception = {
4630 .interruptible = true,
4631 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004633
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004635 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004636 trace_nfs4_remove(dir, name, err);
4637 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 &exception);
4639 } while (exception.retry);
4640 return err;
4641}
4642
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004643static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4644 struct dentry *dentry,
4645 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004647 struct nfs_removeargs *args = msg->rpc_argp;
4648 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
Trond Myklebust977fcc22018-03-20 16:43:17 -04004650 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004652 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004653
4654 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004655
4656 if (inode)
4657 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658}
4659
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004660static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4661{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004662 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004663 &data->args.seq_args,
4664 &data->res.seq_res,
4665 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666}
4667
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004668static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004670 struct nfs_unlinkdata *data = task->tk_calldata;
4671 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004672
Trond Myklebust14516c32010-07-31 14:29:06 -04004673 if (!nfs4_sequence_done(task, &res->seq_res))
4674 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004675 if (nfs4_async_handle_error(task, res->server, NULL,
4676 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004677 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004678 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004679 nfs4_update_changeattr(dir, &res->cinfo,
4680 res->dir_attr->time_start,
4681 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004682 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683}
4684
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004685static void nfs4_proc_rename_setup(struct rpc_message *msg,
4686 struct dentry *old_dentry,
4687 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004688{
Jeff Laytond3d41522010-09-17 17:31:57 -04004689 struct nfs_renameargs *arg = msg->rpc_argp;
4690 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004691 struct inode *old_inode = d_inode(old_dentry);
4692 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004693
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004694 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004695 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004696 if (new_inode)
4697 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004698 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004699 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004700 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004701}
4702
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004703static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4704{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004705 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004706 &data->args.seq_args,
4707 &data->res.seq_res,
4708 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004709}
4710
4711static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4712 struct inode *new_dir)
4713{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004714 struct nfs_renamedata *data = task->tk_calldata;
4715 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004716
4717 if (!nfs4_sequence_done(task, &res->seq_res))
4718 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004719 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004720 return 0;
4721
Trond Myklebustc733c492017-01-11 12:32:26 -05004722 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004723 if (new_dir != old_dir) {
4724 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004725 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004726 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004727 NFS_INO_INVALID_OTHER |
4728 NFS_INO_INVALID_DATA);
4729 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004730 res->new_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004731 NFS_INO_INVALID_OTHER |
4732 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004733 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004734 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004735 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004736 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004737 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004738 return 1;
4739}
4740
Al Virobeffb8f2016-07-20 16:34:42 -04004741static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004743 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004744 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 struct nfs4_link_arg arg = {
4746 .fh = NFS_FH(inode),
4747 .dir_fh = NFS_FH(dir),
4748 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004749 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004751 struct nfs4_link_res res = {
4752 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004753 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004754 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 struct rpc_message msg = {
4756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4757 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004758 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004760 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Trond Myklebust136f2622010-04-16 16:22:49 -04004762 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004763 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004764 goto out;
4765
David Quigley14c43f72013-05-22 12:50:43 -04004766 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4767 if (IS_ERR(res.label)) {
4768 status = PTR_ERR(res.label);
4769 goto out;
4770 }
4771
Trond Myklebustc01d3642018-03-20 16:43:20 -04004772 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004773 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004774
Bryan Schumaker7c513052011-03-24 17:12:24 +00004775 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004776 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004777 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4778 NFS_INO_INVALID_DATA);
David Quigleyaa9c2662013-05-22 12:50:44 -04004779 status = nfs_post_op_update_inode(inode, res.fattr);
4780 if (!status)
4781 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004782 }
David Quigley14c43f72013-05-22 12:50:43 -04004783
4784
4785 nfs4_label_free(res.label);
4786
Trond Myklebust136f2622010-04-16 16:22:49 -04004787out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004788 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 return status;
4790}
4791
Al Virobeffb8f2016-07-20 16:34:42 -04004792static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793{
Trond Myklebust0688e642019-04-07 13:59:09 -04004794 struct nfs4_exception exception = {
4795 .interruptible = true,
4796 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 int err;
4798 do {
4799 err = nfs4_handle_exception(NFS_SERVER(inode),
4800 _nfs4_proc_link(inode, dir, name),
4801 &exception);
4802 } while (exception.retry);
4803 return err;
4804}
4805
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004806struct nfs4_createdata {
4807 struct rpc_message msg;
4808 struct nfs4_create_arg arg;
4809 struct nfs4_create_res res;
4810 struct nfs_fh fh;
4811 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004812 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004813};
4814
4815static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004816 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004817{
4818 struct nfs4_createdata *data;
4819
4820 data = kzalloc(sizeof(*data), GFP_KERNEL);
4821 if (data != NULL) {
4822 struct nfs_server *server = NFS_SERVER(dir);
4823
David Quigley14c43f72013-05-22 12:50:43 -04004824 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4825 if (IS_ERR(data->label))
4826 goto out_free;
4827
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004828 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4829 data->msg.rpc_argp = &data->arg;
4830 data->msg.rpc_resp = &data->res;
4831 data->arg.dir_fh = NFS_FH(dir);
4832 data->arg.server = server;
4833 data->arg.name = name;
4834 data->arg.attrs = sattr;
4835 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004836 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004837 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004838 data->res.server = server;
4839 data->res.fh = &data->fh;
4840 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004841 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004842 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004843 }
4844 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004845out_free:
4846 kfree(data);
4847 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004848}
4849
4850static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4851{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004852 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004853 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004854 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004855 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004856 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4857 data->res.fattr->time_start,
4858 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004859 /* Creating a directory bumps nlink in the parent */
4860 if (data->arg.ftype == NF4DIR)
4861 nfs4_inc_nlink_locked(dir);
4862 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004863 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004864 }
4865 return status;
4866}
4867
4868static void nfs4_free_createdata(struct nfs4_createdata *data)
4869{
David Quigley14c43f72013-05-22 12:50:43 -04004870 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004871 kfree(data);
4872}
4873
Chuck Lever4f390c12006-08-22 20:06:22 -04004874static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004875 struct page *page, unsigned int len, struct iattr *sattr,
4876 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004878 struct nfs4_createdata *data;
4879 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
Chuck Lever94a6d752006-08-22 20:06:23 -04004881 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004882 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004883
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004884 status = -ENOMEM;
4885 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4886 if (data == NULL)
4887 goto out;
4888
4889 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4890 data->arg.u.symlink.pages = &page;
4891 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004892 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004894 status = nfs4_do_create(dir, dentry, data);
4895
4896 nfs4_free_createdata(data);
4897out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 return status;
4899}
4900
Chuck Lever4f390c12006-08-22 20:06:22 -04004901static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004902 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903{
Trond Myklebust0688e642019-04-07 13:59:09 -04004904 struct nfs4_exception exception = {
4905 .interruptible = true,
4906 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004907 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004909
4910 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4911
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004913 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4914 trace_nfs4_symlink(dir, &dentry->d_name, err);
4915 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 &exception);
4917 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004918
4919 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 return err;
4921}
4922
4923static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004924 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004926 struct nfs4_createdata *data;
4927 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004929 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4930 if (data == NULL)
4931 goto out;
4932
David Quigley1775fd32013-05-22 12:50:42 -04004933 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004934 status = nfs4_do_create(dir, dentry, data);
4935
4936 nfs4_free_createdata(data);
4937out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 return status;
4939}
4940
4941static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4942 struct iattr *sattr)
4943{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004944 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004945 struct nfs4_exception exception = {
4946 .interruptible = true,
4947 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004948 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004950
David Quigleyaa9c2662013-05-22 12:50:44 -04004951 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4952
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004953 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4954 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004956 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4957 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4958 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 &exception);
4960 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004961 nfs4_label_release_security(label);
4962
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 return err;
4964}
4965
Trond Myklebust82e22a52020-11-02 17:34:23 -05004966static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4967 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968{
Trond Myklebust82e22a52020-11-02 17:34:23 -05004969 struct inode *dir = d_inode(nr_arg->dentry);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004970 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 struct nfs4_readdir_arg args = {
4972 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -05004973 .pages = nr_arg->pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 .pgbase = 0,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004975 .count = nr_arg->page_len,
4976 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 };
4978 struct nfs4_readdir_res res;
4979 struct rpc_message msg = {
4980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4981 .rpc_argp = &args,
4982 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004983 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 };
4985 int status;
4986
Trond Myklebust82e22a52020-11-02 17:34:23 -05004987 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
4988 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004989 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4990 args.bitmask = server->attr_bitmask_nl;
4991 else
4992 args.bitmask = server->attr_bitmask;
4993
Trond Myklebust82e22a52020-11-02 17:34:23 -05004994 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 res.pgbase = args.pgbase;
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004996 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4997 &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05004998 if (status >= 0) {
Trond Myklebust82e22a52020-11-02 17:34:23 -05004999 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05005000 status += args.pgbase;
5001 }
Trond Myklebustc4812992007-09-28 17:11:45 -04005002
5003 nfs_invalidate_atime(dir);
5004
Harvey Harrison3110ff82008-05-02 13:42:44 -07005005 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 return status;
5007}
5008
Trond Myklebust82e22a52020-11-02 17:34:23 -05005009static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5010 struct nfs_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011{
Trond Myklebust0688e642019-04-07 13:59:09 -04005012 struct nfs4_exception exception = {
5013 .interruptible = true,
5014 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 int err;
5016 do {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005017 err = _nfs4_proc_readdir(arg, res);
5018 trace_nfs4_readdir(d_inode(arg->dentry), err);
5019 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5020 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 } while (exception.retry);
5022 return err;
5023}
5024
5025static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005026 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005028 struct nfs4_createdata *data;
5029 int mode = sattr->ia_mode;
5030 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005032 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5033 if (data == NULL)
5034 goto out;
5035
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005037 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005039 data->arg.ftype = NF4BLK;
5040 data->arg.u.device.specdata1 = MAJOR(rdev);
5041 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 }
5043 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005044 data->arg.ftype = NF4CHR;
5045 data->arg.u.device.specdata1 = MAJOR(rdev);
5046 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005047 } else if (!S_ISSOCK(mode)) {
5048 status = -EINVAL;
5049 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 }
David Quigley1775fd32013-05-22 12:50:42 -04005051
David Quigleyaa9c2662013-05-22 12:50:44 -04005052 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005053 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005054out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005055 nfs4_free_createdata(data);
5056out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 return status;
5058}
5059
5060static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5061 struct iattr *sattr, dev_t rdev)
5062{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005063 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005064 struct nfs4_exception exception = {
5065 .interruptible = true,
5066 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005067 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005069
David Quigleyaa9c2662013-05-22 12:50:44 -04005070 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5071
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005072 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5073 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005075 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5076 trace_nfs4_mknod(dir, &dentry->d_name, err);
5077 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 &exception);
5079 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005080
5081 nfs4_label_release_security(label);
5082
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 return err;
5084}
5085
5086static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5087 struct nfs_fsstat *fsstat)
5088{
5089 struct nfs4_statfs_arg args = {
5090 .fh = fhandle,
5091 .bitmask = server->attr_bitmask,
5092 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005093 struct nfs4_statfs_res res = {
5094 .fsstat = fsstat,
5095 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 struct rpc_message msg = {
5097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5098 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005099 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 };
5101
Trond Myklebust0e574af2005-10-27 22:12:38 -04005102 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005103 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104}
5105
5106static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5107{
Trond Myklebust0688e642019-04-07 13:59:09 -04005108 struct nfs4_exception exception = {
5109 .interruptible = true,
5110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 int err;
5112 do {
5113 err = nfs4_handle_exception(server,
5114 _nfs4_proc_statfs(server, fhandle, fsstat),
5115 &exception);
5116 } while (exception.retry);
5117 return err;
5118}
5119
5120static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5121 struct nfs_fsinfo *fsinfo)
5122{
5123 struct nfs4_fsinfo_arg args = {
5124 .fh = fhandle,
5125 .bitmask = server->attr_bitmask,
5126 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005127 struct nfs4_fsinfo_res res = {
5128 .fsinfo = fsinfo,
5129 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 struct rpc_message msg = {
5131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5132 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005133 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 };
5135
Bryan Schumaker7c513052011-03-24 17:12:24 +00005136 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137}
5138
5139static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5140{
Trond Myklebust0688e642019-04-07 13:59:09 -04005141 struct nfs4_exception exception = {
5142 .interruptible = true,
5143 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 int err;
5145
5146 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005147 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005148 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005149 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005150 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005151 break;
5152 }
5153 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 } while (exception.retry);
5155 return err;
5156}
5157
5158static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5159{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005160 int error;
5161
Trond Myklebust0e574af2005-10-27 22:12:38 -04005162 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005163 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005164 if (error == 0) {
5165 /* block layout checks this! */
5166 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005167 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005168 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005169
5170 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171}
5172
5173static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5174 struct nfs_pathconf *pathconf)
5175{
5176 struct nfs4_pathconf_arg args = {
5177 .fh = fhandle,
5178 .bitmask = server->attr_bitmask,
5179 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005180 struct nfs4_pathconf_res res = {
5181 .pathconf = pathconf,
5182 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 struct rpc_message msg = {
5184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5185 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005186 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 };
5188
5189 /* None of the pathconf attributes are mandatory to implement */
5190 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5191 memset(pathconf, 0, sizeof(*pathconf));
5192 return 0;
5193 }
5194
Trond Myklebust0e574af2005-10-27 22:12:38 -04005195 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005196 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197}
5198
5199static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5200 struct nfs_pathconf *pathconf)
5201{
Trond Myklebust0688e642019-04-07 13:59:09 -04005202 struct nfs4_exception exception = {
5203 .interruptible = true,
5204 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 int err;
5206
5207 do {
5208 err = nfs4_handle_exception(server,
5209 _nfs4_proc_pathconf(server, fhandle, pathconf),
5210 &exception);
5211 } while (exception.retry);
5212 return err;
5213}
5214
Trond Myklebust5521abf2013-03-16 20:54:34 -04005215int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005216 const struct nfs_open_context *ctx,
5217 const struct nfs_lock_context *l_ctx,
5218 fmode_t fmode)
5219{
NeilBrown17393472016-10-13 15:26:47 +11005220 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005221}
5222EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5223
Trond Myklebust5521abf2013-03-16 20:54:34 -04005224static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5225 const struct nfs_open_context *ctx,
5226 const struct nfs_lock_context *l_ctx,
5227 fmode_t fmode)
5228{
Ben Dooksd49dd112019-10-16 17:28:21 +01005229 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005230
Trond Myklebuste1253be2014-03-05 08:44:23 -05005231 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005232 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005233 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005234 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005235}
5236
5237static bool nfs4_error_stateid_expired(int err)
5238{
5239 switch (err) {
5240 case -NFS4ERR_DELEG_REVOKED:
5241 case -NFS4ERR_ADMIN_REVOKED:
5242 case -NFS4ERR_BAD_STATEID:
5243 case -NFS4ERR_STALE_STATEID:
5244 case -NFS4ERR_OLD_STATEID:
5245 case -NFS4ERR_OPENMODE:
5246 case -NFS4ERR_EXPIRED:
5247 return true;
5248 }
5249 return false;
5250}
5251
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005252static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005254 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005256 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005257 if (task->tk_status < 0) {
5258 struct nfs4_exception exception = {
5259 .inode = hdr->inode,
5260 .state = hdr->args.context->state,
5261 .stateid = &hdr->args.stateid,
5262 };
5263 task->tk_status = nfs4_async_handle_exception(task,
5264 server, task->tk_status, &exception);
5265 if (exception.retry) {
5266 rpc_restart_call_prepare(task);
5267 return -EAGAIN;
5268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005270
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005272 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274}
5275
Trond Myklebust5521abf2013-03-16 20:54:34 -04005276static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005277 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005278{
5279
5280 if (!nfs4_error_stateid_expired(task->tk_status) ||
5281 nfs4_stateid_is_current(&args->stateid,
5282 args->context,
5283 args->lock_context,
5284 FMODE_READ))
5285 return false;
5286 rpc_restart_call_prepare(task);
5287 return true;
5288}
5289
Anna Schumakerc5675522014-05-28 13:41:22 -04005290static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5291 struct nfs_pgio_header *hdr)
5292{
5293 struct nfs_server *server = NFS_SERVER(hdr->inode);
5294 struct rpc_message *msg = &task->tk_msg;
5295
5296 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5297 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5298 server->caps &= ~NFS_CAP_READ_PLUS;
5299 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5300 rpc_restart_call_prepare(task);
5301 return true;
5302 }
5303 return false;
5304}
5305
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005306static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005307{
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005308 dprintk("--> %s\n", __func__);
5309
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005310 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005311 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005312 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005313 return -EAGAIN;
Anna Schumakerc5675522014-05-28 13:41:22 -04005314 if (nfs4_read_plus_not_supported(task, hdr))
5315 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005316 if (task->tk_status > 0)
5317 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005318 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5319 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005320}
5321
Anna Schumakerc5675522014-05-28 13:41:22 -04005322#ifdef CONFIG_NFS_V4_2
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005323static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5324 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005325{
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005326 /* Note: We don't use READ_PLUS with pNFS yet */
5327 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
Anna Schumakerc5675522014-05-28 13:41:22 -04005328 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
Anna Schumakerc5675522014-05-28 13:41:22 -04005329}
5330#else
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005331static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5332 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005333{
Anna Schumakerc5675522014-05-28 13:41:22 -04005334}
5335#endif /* CONFIG_NFS_V4_2 */
5336
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005337static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5338 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005340 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005341 if (!hdr->pgio_done_cb)
5342 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005343 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5344 nfs42_read_plus_support(hdr, msg);
Anna Schumakerfba83f32018-05-04 16:22:50 -04005345 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346}
5347
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005348static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5349 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005350{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005351 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005352 &hdr->args.seq_args,
5353 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005354 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005355 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005356 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5357 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005358 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005359 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005360 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005361 return -EIO;
5362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363}
5364
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005365static int nfs4_write_done_cb(struct rpc_task *task,
5366 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005368 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005369
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005370 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005371 if (task->tk_status < 0) {
5372 struct nfs4_exception exception = {
5373 .inode = hdr->inode,
5374 .state = hdr->args.context->state,
5375 .stateid = &hdr->args.stateid,
5376 };
5377 task->tk_status = nfs4_async_handle_exception(task,
5378 NFS_SERVER(inode), task->tk_status,
5379 &exception);
5380 if (exception.retry) {
5381 rpc_restart_call_prepare(task);
5382 return -EAGAIN;
5383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005385 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005386 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005387 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005388 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390}
5391
Trond Myklebust5521abf2013-03-16 20:54:34 -04005392static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005393 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005394{
5395
5396 if (!nfs4_error_stateid_expired(task->tk_status) ||
5397 nfs4_stateid_is_current(&args->stateid,
5398 args->context,
5399 args->lock_context,
5400 FMODE_WRITE))
5401 return false;
5402 rpc_restart_call_prepare(task);
5403 return true;
5404}
5405
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005406static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005407{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005408 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005409 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005410 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005411 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005412 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5413 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005414}
5415
Trond Myklebust5a37f852012-04-28 14:55:16 -04005416static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005417bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005418{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005419 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005420 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005421 return false;
5422 /* Otherwise, request attributes if and only if we don't hold
5423 * a delegation
5424 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005425 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005426}
Fred Isamana69aef12011-03-03 15:13:47 +00005427
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005428static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
5429 struct nfs_server *server,
5430 struct nfs4_label *label)
5431{
5432
5433 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5434
5435 if ((cache_validity & NFS_INO_INVALID_DATA) ||
5436 (cache_validity & NFS_INO_REVAL_PAGECACHE) ||
5437 (cache_validity & NFS_INO_REVAL_FORCED) ||
5438 (cache_validity & NFS_INO_INVALID_OTHER))
5439 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
5440
5441 if (cache_validity & NFS_INO_INVALID_ATIME)
5442 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5443 if (cache_validity & NFS_INO_INVALID_ACCESS)
5444 bitmask[0] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
5445 FATTR4_WORD1_OWNER_GROUP;
5446 if (cache_validity & NFS_INO_INVALID_ACL)
5447 bitmask[0] |= FATTR4_WORD0_ACL;
5448 if (cache_validity & NFS_INO_INVALID_LABEL)
5449 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5450 if (cache_validity & NFS_INO_INVALID_CTIME)
5451 bitmask[0] |= FATTR4_WORD0_CHANGE;
5452 if (cache_validity & NFS_INO_INVALID_MTIME)
5453 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5454 if (cache_validity & NFS_INO_INVALID_SIZE)
5455 bitmask[0] |= FATTR4_WORD0_SIZE;
5456 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5457 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5458}
5459
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005460static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005461 struct rpc_message *msg,
5462 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005464 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005465
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005466 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5467 hdr->args.bitmask = NULL;
5468 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005469 } else {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005470 hdr->args.bitmask = server->cache_consistency_bitmask;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005471 nfs4_bitmask_adjust(hdr->args.bitmask, hdr->inode, server, NULL);
5472 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005473
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005474 if (!hdr->pgio_done_cb)
5475 hdr->pgio_done_cb = nfs4_write_done_cb;
5476 hdr->res.server = server;
5477 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005479 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005480 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005481 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482}
5483
Fred Isaman0b7c0152012-04-20 14:47:39 -04005484static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5485{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005486 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005487 &data->args.seq_args,
5488 &data->res.seq_res,
5489 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490}
5491
Fred Isaman0b7c0152012-04-20 14:47:39 -04005492static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005495
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005496 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005497 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5498 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005499 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005500 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503}
5504
Fred Isaman0b7c0152012-04-20 14:47:39 -04005505static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005506{
5507 if (!nfs4_sequence_done(task, &data->res.seq_res))
5508 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005509 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005510}
5511
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005512static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5513 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005515 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005516
Fred Isaman0b7c0152012-04-20 14:47:39 -04005517 if (data->commit_done_cb == NULL)
5518 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005519 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005520 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005521 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005522 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523}
5524
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005525static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5526 struct nfs_commitres *res)
5527{
5528 struct inode *dst_inode = file_inode(dst);
5529 struct nfs_server *server = NFS_SERVER(dst_inode);
5530 struct rpc_message msg = {
5531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5532 .rpc_argp = args,
5533 .rpc_resp = res,
5534 };
5535
5536 args->fh = NFS_FH(dst_inode);
5537 return nfs4_call_sync(server->client, server, &msg,
5538 &args->seq_args, &res->seq_res, 1);
5539}
5540
5541int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5542{
5543 struct nfs_commitargs args = {
5544 .offset = offset,
5545 .count = count,
5546 };
5547 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5548 struct nfs4_exception exception = { };
5549 int status;
5550
5551 do {
5552 status = _nfs4_proc_commit(dst, &args, res);
5553 status = nfs4_handle_exception(dst_server, status, &exception);
5554 } while (exception.retry);
5555
5556 return status;
5557}
5558
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005559struct nfs4_renewdata {
5560 struct nfs_client *client;
5561 unsigned long timestamp;
5562};
5563
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564/*
5565 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5566 * standalone procedure for queueing an asynchronous RENEW.
5567 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005568static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005569{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005570 struct nfs4_renewdata *data = calldata;
5571 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005572
Elena Reshetova212bf412017-10-20 12:53:38 +03005573 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005574 nfs4_schedule_state_renewal(clp);
5575 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005576 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005577}
5578
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005579static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005581 struct nfs4_renewdata *data = calldata;
5582 struct nfs_client *clp = data->client;
5583 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005585 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005586 switch (task->tk_status) {
5587 case 0:
5588 break;
5589 case -NFS4ERR_LEASE_MOVED:
5590 nfs4_schedule_lease_moved_recovery(clp);
5591 break;
5592 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005593 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005594 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5595 return;
5596 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005597 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005598 return;
5599 }
5600 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005602 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603}
5604
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005605static const struct rpc_call_ops nfs4_renew_ops = {
5606 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005607 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005608};
5609
NeilBrowna52458b2018-12-03 11:30:31 +11005610static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611{
5612 struct rpc_message msg = {
5613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5614 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005615 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005617 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005619 if (renew_flags == 0)
5620 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005621 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005622 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005623 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005624 if (data == NULL) {
5625 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005626 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005627 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005628 data->client = clp;
5629 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005630 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005631 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632}
5633
NeilBrowna52458b2018-12-03 11:30:31 +11005634static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635{
5636 struct rpc_message msg = {
5637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5638 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005639 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 };
5641 unsigned long now = jiffies;
5642 int status;
5643
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005644 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 if (status < 0)
5646 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005647 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 return 0;
5649}
5650
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005651static inline int nfs4_server_supports_acls(struct nfs_server *server)
5652{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005653 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005654}
5655
Trond Myklebust21f498c2012-08-24 10:59:25 -04005656/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5657 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005658 * the stack.
5659 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005660#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005661
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005662int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005663 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005664{
5665 struct page *newpage, **spages;
5666 int rc = 0;
5667 size_t len;
5668 spages = pages;
5669
5670 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005671 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005672 newpage = alloc_page(GFP_KERNEL);
5673
5674 if (newpage == NULL)
5675 goto unwind;
5676 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005677 buf += len;
5678 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005679 *pages++ = newpage;
5680 rc++;
5681 } while (buflen != 0);
5682
5683 return rc;
5684
5685unwind:
5686 for(; rc > 0; rc--)
5687 __free_page(spages[rc-1]);
5688 return -ENOMEM;
5689}
5690
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005691struct nfs4_cached_acl {
5692 int cached;
5693 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005694 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005695};
5696
5697static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005698{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005699 struct nfs_inode *nfsi = NFS_I(inode);
5700
5701 spin_lock(&inode->i_lock);
5702 kfree(nfsi->nfs4_acl);
5703 nfsi->nfs4_acl = acl;
5704 spin_unlock(&inode->i_lock);
5705}
5706
5707static void nfs4_zap_acl_attr(struct inode *inode)
5708{
5709 nfs4_set_cached_acl(inode, NULL);
5710}
5711
5712static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5713{
5714 struct nfs_inode *nfsi = NFS_I(inode);
5715 struct nfs4_cached_acl *acl;
5716 int ret = -ENOENT;
5717
5718 spin_lock(&inode->i_lock);
5719 acl = nfsi->nfs4_acl;
5720 if (acl == NULL)
5721 goto out;
5722 if (buf == NULL) /* user is just asking for length */
5723 goto out_len;
5724 if (acl->cached == 0)
5725 goto out;
5726 ret = -ERANGE; /* see getxattr(2) man page */
5727 if (acl->len > buflen)
5728 goto out;
5729 memcpy(buf, acl->data, acl->len);
5730out_len:
5731 ret = acl->len;
5732out:
5733 spin_unlock(&inode->i_lock);
5734 return ret;
5735}
5736
Sachin Prabhu5794d212012-04-17 14:36:40 +01005737static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005738{
5739 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005740 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005741
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005742 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005743 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005744 if (acl == NULL)
5745 goto out;
5746 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005747 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005748 } else {
5749 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5750 if (acl == NULL)
5751 goto out;
5752 acl->cached = 0;
5753 }
5754 acl->len = acl_len;
5755out:
5756 nfs4_set_cached_acl(inode, acl);
5757}
5758
Andy Adamsonbf118a32011-12-07 11:55:27 -05005759/*
5760 * The getxattr API returns the required buffer length when called with a
5761 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5762 * the required buf. On a NULL buf, we send a page of data to the server
5763 * guessing that the ACL request can be serviced by a page. If so, we cache
5764 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5765 * the cache. If not so, we throw away the page, and cache the required
5766 * length. The next getxattr call will then produce another round trip to
5767 * the server, this time with the input buf of the required size.
5768 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005769static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005770{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005771 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005772 struct nfs_getaclargs args = {
5773 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005774 .acl_len = buflen,
5775 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005776 struct nfs_getaclres res = {
5777 .acl_len = buflen,
5778 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005779 struct rpc_message msg = {
5780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5781 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005782 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005783 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005784 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005785 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005786 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005787
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005788 if (buflen == 0)
5789 buflen = server->rsize;
5790
5791 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5792 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5793 if (!pages)
5794 return -ENOMEM;
5795
5796 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005797
Andy Adamsonbf118a32011-12-07 11:55:27 -05005798 for (i = 0; i < npages; i++) {
5799 pages[i] = alloc_page(GFP_KERNEL);
5800 if (!pages[i])
5801 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005802 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005803
5804 /* for decoding across pages */
5805 res.acl_scratch = alloc_page(GFP_KERNEL);
5806 if (!res.acl_scratch)
5807 goto out_free;
5808
Andy Adamsonbf118a32011-12-07 11:55:27 -05005809 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005810
Peng Taode040be2012-01-10 22:42:47 +08005811 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005812 __func__, buf, buflen, npages, args.acl_len);
5813 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5814 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005815 if (ret)
5816 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005817
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005818 /* Handle the case where the passed-in buffer is too short */
5819 if (res.acl_flags & NFS4_ACL_TRUNC) {
5820 /* Did the user only issue a request for the acl length? */
5821 if (buf == NULL)
5822 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005823 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005824 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005825 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005826 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005827 if (buf) {
5828 if (res.acl_len > buflen) {
5829 ret = -ERANGE;
5830 goto out_free;
5831 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005832 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005833 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005834out_ok:
5835 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005836out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005837 for (i = 0; i < npages; i++)
5838 if (pages[i])
5839 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005840 if (res.acl_scratch)
5841 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005842 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005843 return ret;
5844}
5845
Trond Myklebust16b42892006-08-24 12:27:15 -04005846static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5847{
Trond Myklebust0688e642019-04-07 13:59:09 -04005848 struct nfs4_exception exception = {
5849 .interruptible = true,
5850 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005851 ssize_t ret;
5852 do {
5853 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005854 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005855 if (ret >= 0)
5856 break;
5857 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5858 } while (exception.retry);
5859 return ret;
5860}
5861
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005862static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5863{
5864 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005865 int ret;
5866
5867 if (!nfs4_server_supports_acls(server))
5868 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005869 ret = nfs_revalidate_inode(server, inode);
5870 if (ret < 0)
5871 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005872 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5873 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005874 ret = nfs4_read_cached_acl(inode, buf, buflen);
5875 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005876 /* -ENOENT is returned if there is no ACL or if there is an ACL
5877 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005878 return ret;
5879 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005880}
5881
Trond Myklebust16b42892006-08-24 12:27:15 -04005882static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005883{
5884 struct nfs_server *server = NFS_SERVER(inode);
5885 struct page *pages[NFS4ACL_MAXPAGES];
5886 struct nfs_setaclargs arg = {
5887 .fh = NFS_FH(inode),
5888 .acl_pages = pages,
5889 .acl_len = buflen,
5890 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005891 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005892 struct rpc_message msg = {
5893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5894 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005895 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005896 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005897 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005898 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005899
5900 if (!nfs4_server_supports_acls(server))
5901 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005902 if (npages > ARRAY_SIZE(pages))
5903 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005904 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005905 if (i < 0)
5906 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005907 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005908 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005909
5910 /*
5911 * Free each page after tx, so the only ref left is
5912 * held by the network stack
5913 */
5914 for (; i > 0; i--)
5915 put_page(pages[i-1]);
5916
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005917 /*
5918 * Acl update can result in inode attribute update.
5919 * so mark the attribute cache invalid.
5920 */
5921 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005922 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005923 | NFS_INO_INVALID_CTIME
5924 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005925 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005926 nfs_access_zap_cache(inode);
5927 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005928 return ret;
5929}
5930
Trond Myklebust16b42892006-08-24 12:27:15 -04005931static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5932{
5933 struct nfs4_exception exception = { };
5934 int err;
5935 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005936 err = __nfs4_proc_set_acl(inode, buf, buflen);
5937 trace_nfs4_set_acl(inode, err);
5938 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005939 &exception);
5940 } while (exception.retry);
5941 return err;
5942}
5943
David Quigleyaa9c2662013-05-22 12:50:44 -04005944#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5945static int _nfs4_get_security_label(struct inode *inode, void *buf,
5946 size_t buflen)
5947{
5948 struct nfs_server *server = NFS_SERVER(inode);
5949 struct nfs_fattr fattr;
5950 struct nfs4_label label = {0, 0, buflen, buf};
5951
5952 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005953 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005954 .fh = NFS_FH(inode),
5955 .bitmask = bitmask,
5956 };
5957 struct nfs4_getattr_res res = {
5958 .fattr = &fattr,
5959 .label = &label,
5960 .server = server,
5961 };
5962 struct rpc_message msg = {
5963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005964 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005965 .rpc_resp = &res,
5966 };
5967 int ret;
5968
5969 nfs_fattr_init(&fattr);
5970
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005971 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005972 if (ret)
5973 return ret;
5974 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5975 return -ENOENT;
David Quigleyaa9c2662013-05-22 12:50:44 -04005976 return 0;
5977}
5978
5979static int nfs4_get_security_label(struct inode *inode, void *buf,
5980 size_t buflen)
5981{
Trond Myklebust0688e642019-04-07 13:59:09 -04005982 struct nfs4_exception exception = {
5983 .interruptible = true,
5984 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005985 int err;
5986
5987 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5988 return -EOPNOTSUPP;
5989
5990 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005991 err = _nfs4_get_security_label(inode, buf, buflen);
5992 trace_nfs4_get_security_label(inode, err);
5993 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005994 &exception);
5995 } while (exception.retry);
5996 return err;
5997}
5998
5999static int _nfs4_do_set_security_label(struct inode *inode,
6000 struct nfs4_label *ilabel,
6001 struct nfs_fattr *fattr,
6002 struct nfs4_label *olabel)
6003{
6004
6005 struct iattr sattr = {0};
6006 struct nfs_server *server = NFS_SERVER(inode);
6007 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04006008 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006009 .fh = NFS_FH(inode),
6010 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006011 .server = server,
6012 .bitmask = bitmask,
6013 .label = ilabel,
6014 };
6015 struct nfs_setattrres res = {
6016 .fattr = fattr,
6017 .label = olabel,
6018 .server = server,
6019 };
6020 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6022 .rpc_argp = &arg,
6023 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04006024 };
6025 int status;
6026
Jeff Layton12207f62013-11-01 10:49:32 -04006027 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04006028
Jeff Layton12207f62013-11-01 10:49:32 -04006029 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04006030 if (status)
6031 dprintk("%s failed: %d\n", __func__, status);
6032
6033 return status;
6034}
6035
6036static int nfs4_do_set_security_label(struct inode *inode,
6037 struct nfs4_label *ilabel,
6038 struct nfs_fattr *fattr,
6039 struct nfs4_label *olabel)
6040{
6041 struct nfs4_exception exception = { };
6042 int err;
6043
6044 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006045 err = _nfs4_do_set_security_label(inode, ilabel,
6046 fattr, olabel);
6047 trace_nfs4_set_security_label(inode, err);
6048 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006049 &exception);
6050 } while (exception.retry);
6051 return err;
6052}
6053
6054static int
Al Viro59301222016-05-27 10:19:30 -04006055nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006056{
6057 struct nfs4_label ilabel, *olabel = NULL;
6058 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006059 int status;
6060
6061 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6062 return -EOPNOTSUPP;
6063
6064 nfs_fattr_init(&fattr);
6065
6066 ilabel.pi = 0;
6067 ilabel.lfs = 0;
6068 ilabel.label = (char *)buf;
6069 ilabel.len = buflen;
6070
David Quigleyaa9c2662013-05-22 12:50:44 -04006071 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6072 if (IS_ERR(olabel)) {
6073 status = -PTR_ERR(olabel);
6074 goto out;
6075 }
6076
6077 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6078 if (status == 0)
6079 nfs_setsecurity(inode, &fattr, olabel);
6080
6081 nfs4_label_free(olabel);
6082out:
David Quigleyaa9c2662013-05-22 12:50:44 -04006083 return status;
6084}
6085#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6086
6087
Chuck Leverf0920752012-05-21 22:45:41 -04006088static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6089 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006090{
6091 __be32 verf[2];
6092
Chuck Lever2c820d92012-05-21 22:45:33 -04006093 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6094 /* An impossible timestamp guarantees this value
6095 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006096 verf[0] = cpu_to_be32(U32_MAX);
6097 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006098 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006099 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006100 u64 ns = ktime_to_ns(nn->boot_time);
6101
6102 verf[0] = cpu_to_be32(ns >> 32);
6103 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006104 }
Chuck Levercd937102012-03-02 17:14:31 -05006105 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6106}
6107
Trond Myklebust1aee5512020-10-07 18:24:17 -04006108static size_t
Trond Myklebust39d43d12020-10-07 18:24:18 -04006109nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
Trond Myklebust1aee5512020-10-07 18:24:17 -04006110{
Trond Myklebust39d43d12020-10-07 18:24:18 -04006111 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6112 struct nfs_netns_client *nn_clp = nn->nfs_client;
6113 const char *id;
6114
Trond Myklebust1aee5512020-10-07 18:24:17 -04006115 buf[0] = '\0';
6116
Trond Myklebust39d43d12020-10-07 18:24:18 -04006117 if (nn_clp) {
6118 rcu_read_lock();
6119 id = rcu_dereference(nn_clp->identifier);
6120 if (id)
6121 strscpy(buf, id, buflen);
6122 rcu_read_unlock();
6123 }
6124
6125 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
Trond Myklebust1aee5512020-10-07 18:24:17 -04006126 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6127
6128 return strlen(buf);
6129}
6130
Jeff Laytona3192682015-06-09 19:43:59 -04006131static int
6132nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006133{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006134 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6135 size_t buflen;
Jeff Laytona3192682015-06-09 19:43:59 -04006136 size_t len;
6137 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006138
Trond Myklebustceb3a162015-01-03 15:16:04 -05006139 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006140 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006141
Jeff Laytona3192682015-06-09 19:43:59 -04006142 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006143 len = 14 +
6144 strlen(clp->cl_rpcclient->cl_nodename) +
6145 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006146 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006147 1;
6148 rcu_read_unlock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006149
Trond Myklebust39d43d12020-10-07 18:24:18 -04006150 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006151 if (buflen)
6152 len += buflen + 1;
6153
Jeff Laytona3192682015-06-09 19:43:59 -04006154 if (len > NFS4_OPAQUE_LIMIT + 1)
6155 return -EINVAL;
6156
6157 /*
6158 * Since this string is allocated at mount time, and held until the
6159 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6160 * about a memory-reclaim deadlock.
6161 */
6162 str = kmalloc(len, GFP_KERNEL);
6163 if (!str)
6164 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006165
Chuck Levere984a552012-09-14 17:24:21 -04006166 rcu_read_lock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006167 if (buflen)
Chuck Lever025bb9f2018-06-04 10:53:34 -04006168 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Trond Myklebust1aee5512020-10-07 18:24:17 -04006169 clp->cl_rpcclient->cl_nodename, buf,
Chuck Lever848a4eb2018-06-04 10:53:29 -04006170 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006171 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006172 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006173 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006174 clp->cl_rpcclient->cl_nodename,
6175 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006176 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006177 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006178
Jeff Laytona3192682015-06-09 19:43:59 -04006179 clp->cl_owner_id = str;
6180 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006181}
6182
Jeff Layton873e3852015-06-09 19:44:00 -04006183static int
Jeff Layton873e3852015-06-09 19:44:00 -04006184nfs4_init_uniform_client_string(struct nfs_client *clp)
6185{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006186 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6187 size_t buflen;
Jeff Layton873e3852015-06-09 19:44:00 -04006188 size_t len;
6189 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006190
6191 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006192 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006193
Jeff Layton873e3852015-06-09 19:44:00 -04006194 len = 10 + 10 + 1 + 10 + 1 +
6195 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6196
Trond Myklebust39d43d12020-10-07 18:24:18 -04006197 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006198 if (buflen)
6199 len += buflen + 1;
6200
Jeff Layton873e3852015-06-09 19:44:00 -04006201 if (len > NFS4_OPAQUE_LIMIT + 1)
6202 return -EINVAL;
6203
6204 /*
6205 * Since this string is allocated at mount time, and held until the
6206 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6207 * about a memory-reclaim deadlock.
6208 */
6209 str = kmalloc(len, GFP_KERNEL);
6210 if (!str)
6211 return -ENOMEM;
6212
Trond Myklebust1aee5512020-10-07 18:24:17 -04006213 if (buflen)
6214 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6215 clp->rpc_ops->version, clp->cl_minorversion,
6216 buf, clp->cl_rpcclient->cl_nodename);
6217 else
6218 scnprintf(str, len, "Linux NFSv%u.%u %s",
6219 clp->rpc_ops->version, clp->cl_minorversion,
6220 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006221 clp->cl_owner_id = str;
6222 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006223}
6224
Chuck Lever706cb8d2014-03-12 12:51:47 -04006225/*
6226 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6227 * services. Advertise one based on the address family of the
6228 * clientaddr.
6229 */
6230static unsigned int
6231nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6232{
6233 if (strchr(clp->cl_ipaddr, ':') != NULL)
6234 return scnprintf(buf, len, "tcp6");
6235 else
6236 return scnprintf(buf, len, "tcp");
6237}
6238
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006239static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6240{
6241 struct nfs4_setclientid *sc = calldata;
6242
6243 if (task->tk_status == 0)
6244 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6245}
6246
6247static const struct rpc_call_ops nfs4_setclientid_ops = {
6248 .rpc_call_done = nfs4_setclientid_done,
6249};
6250
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006251/**
6252 * nfs4_proc_setclientid - Negotiate client ID
6253 * @clp: state data structure
6254 * @program: RPC program for NFSv4 callback service
6255 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006256 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006257 * @res: where to place the result
6258 *
6259 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6260 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006261int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006262 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006263 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264{
6265 nfs4_verifier sc_verifier;
6266 struct nfs4_setclientid setclientid = {
6267 .sc_verifier = &sc_verifier,
6268 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006269 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 };
6271 struct rpc_message msg = {
6272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6273 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006274 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006275 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006277 struct rpc_task_setup task_setup_data = {
6278 .rpc_client = clp->cl_rpcclient,
6279 .rpc_message = &msg,
6280 .callback_ops = &nfs4_setclientid_ops,
6281 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006282 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006283 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006284 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006285 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286
Chuck Leverde734832012-07-11 16:30:50 -04006287 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006288 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006289
6290 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6291 status = nfs4_init_uniform_client_string(clp);
6292 else
Jeff Laytona3192682015-06-09 19:43:59 -04006293 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006294
6295 if (status)
6296 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006297
Chuck Leverde734832012-07-11 16:30:50 -04006298 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006299 setclientid.sc_netid_len =
6300 nfs4_init_callback_netid(clp,
6301 setclientid.sc_netid,
6302 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006303 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006304 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 clp->cl_ipaddr, port >> 8, port & 255);
6306
Jeff Layton3a6bb732015-06-09 19:43:57 -04006307 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006308 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006309 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006310
6311 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006312 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006313 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006314 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6315 put_rpccred(setclientid.sc_cred);
6316 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006317
6318 if (status == 0)
6319 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006320out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006321 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006322 dprintk("NFS reply setclientid: %d\n", status);
6323 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324}
6325
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006326/**
6327 * nfs4_proc_setclientid_confirm - Confirm client ID
6328 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006329 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006330 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006331 *
6332 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6333 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006334int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006335 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006336 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 struct rpc_message msg = {
6339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006340 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006341 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 int status;
6344
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006345 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6346 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6347 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006348 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6349 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006350 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006351 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 return status;
6353}
6354
Trond Myklebustfe650402006-01-03 09:55:18 +01006355struct nfs4_delegreturndata {
6356 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006357 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006358 struct nfs_fh fh;
6359 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006360 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006361 struct {
6362 struct nfs4_layoutreturn_args arg;
6363 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006364 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006365 u32 roc_barrier;
6366 bool roc;
6367 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006368 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006369 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006370 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006371};
6372
Trond Myklebustfe650402006-01-03 09:55:18 +01006373static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6374{
6375 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006376 struct nfs4_exception exception = {
6377 .inode = data->inode,
6378 .stateid = &data->stateid,
6379 };
Andy Adamson938e1012009-04-01 09:22:28 -04006380
Trond Myklebust14516c32010-07-31 14:29:06 -04006381 if (!nfs4_sequence_done(task, &data->res.seq_res))
6382 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006383
Trond Myklebustca8acf82013-08-13 10:36:56 -04006384 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006385
6386 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04006387 if (pnfs_roc_done(task, data->inode,
6388 &data->args.lr_args,
6389 &data->res.lr_res,
6390 &data->res.lr_ret) == -EAGAIN)
6391 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006392
Ricardo Labiaga79708862009-12-07 09:23:21 -05006393 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006394 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006395 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006396 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006397 case -NFS4ERR_ADMIN_REVOKED:
6398 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006399 case -NFS4ERR_EXPIRED:
6400 nfs4_free_revoked_stateid(data->res.server,
6401 data->args.stateid,
6402 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006403 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006404 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006405 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006406 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006407 task->tk_status = 0;
6408 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006409 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006410 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6411 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006412 if (data->args.bitmask) {
6413 data->args.bitmask = NULL;
6414 data->res.fattr = NULL;
6415 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006416 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006417 case -NFS4ERR_ACCESS:
6418 if (data->args.bitmask) {
6419 data->args.bitmask = NULL;
6420 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006421 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006422 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006423 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006424 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006425 task->tk_status = nfs4_async_handle_exception(task,
6426 data->res.server, task->tk_status,
6427 &exception);
6428 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006429 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006430 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006431 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006432 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006433 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006434out_restart:
6435 task->tk_status = 0;
6436 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006437}
6438
6439static void nfs4_delegreturn_release(void *calldata)
6440{
Peng Tao039b7562014-07-03 13:05:02 +08006441 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006442 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006443
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006444 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006445 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006446 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6447 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006448 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006449 nfs_iput_and_deactive(inode);
6450 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006451 kfree(calldata);
6452}
6453
Andy Adamson938e1012009-04-01 09:22:28 -04006454static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6455{
6456 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006457 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006458
6459 d_data = (struct nfs4_delegreturndata *)data;
6460
Trond Myklebust5326de92019-11-13 09:39:36 +01006461 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6462 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006463 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006464 }
Peng Tao500d7012015-09-22 11:35:22 +08006465
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006466 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6467 if (lo && !pnfs_layout_is_valid(lo)) {
6468 d_data->args.lr_args = NULL;
6469 d_data->res.lr_res = NULL;
6470 }
6471
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006472 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006473 &d_data->args.seq_args,
6474 &d_data->res.seq_res,
6475 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006476}
Andy Adamson938e1012009-04-01 09:22:28 -04006477
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006478static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006479 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006480 .rpc_call_done = nfs4_delegreturn_done,
6481 .rpc_release = nfs4_delegreturn_release,
6482};
6483
NeilBrowna52458b2018-12-03 11:30:31 +11006484static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006485{
6486 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006487 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006488 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006489 struct rpc_message msg = {
6490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6491 .rpc_cred = cred,
6492 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006493 struct rpc_task_setup task_setup_data = {
6494 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006495 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006496 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebustf304a802020-05-11 10:42:04 -04006497 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006498 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006499 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006500
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006501 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006502 if (data == NULL)
6503 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006504 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006505
6506 nfs4_state_protect(server->nfs_client,
6507 NFS_SP4_MACH_CRED_CLEANUP,
6508 &task_setup_data.rpc_client, &msg);
6509
Trond Myklebustfe650402006-01-03 09:55:18 +01006510 data->args.fhandle = &data->fh;
6511 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006512 data->args.bitmask = server->cache_consistency_bitmask;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04006513 nfs4_bitmask_adjust(data->args.bitmask, inode, server, NULL);
Trond Myklebustfe650402006-01-03 09:55:18 +01006514 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006515 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006516 data->res.fattr = &data->fattr;
6517 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006518 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006519 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006520 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006521 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006522 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006523 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006524 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006525 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006526 if (data->lr.roc) {
6527 data->args.lr_args = &data->lr.arg;
6528 data->res.lr_res = &data->lr.res;
6529 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006530 } else if (data->lr.roc) {
6531 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6532 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006533 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006534
Trond Myklebustc970aa82007-07-14 15:39:59 -04006535 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006536 msg.rpc_argp = &data->args;
6537 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006538 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006539 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006540 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006541 if (!issync)
6542 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006543 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006544 if (status != 0)
6545 goto out;
6546 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006547out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006548 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006549 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550}
6551
NeilBrowna52458b2018-12-03 11:30:31 +11006552int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553{
6554 struct nfs_server *server = NFS_SERVER(inode);
6555 struct nfs4_exception exception = { };
6556 int err;
6557 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006558 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006559 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 switch (err) {
6561 case -NFS4ERR_STALE_STATEID:
6562 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 case 0:
6564 return 0;
6565 }
6566 err = nfs4_handle_exception(server, err, &exception);
6567 } while (exception.retry);
6568 return err;
6569}
6570
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6572{
6573 struct inode *inode = state->inode;
6574 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006575 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006576 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006578 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006580 struct nfs_lockt_res res = {
6581 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 };
6583 struct rpc_message msg = {
6584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006585 .rpc_argp = &arg,
6586 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 .rpc_cred = state->owner->so_cred,
6588 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 struct nfs4_lock_state *lsp;
6590 int status;
6591
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006592 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006593 status = nfs4_set_lock_state(state, request);
6594 if (status != 0)
6595 goto out;
6596 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006597 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006598 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006599 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006600 switch (status) {
6601 case 0:
6602 request->fl_type = F_UNLCK;
6603 break;
6604 case -NFS4ERR_DENIED:
6605 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006607 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006608 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006609out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 return status;
6611}
6612
6613static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6614{
Trond Myklebust0688e642019-04-07 13:59:09 -04006615 struct nfs4_exception exception = {
6616 .interruptible = true,
6617 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 int err;
6619
6620 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006621 err = _nfs4_proc_getlk(state, cmd, request);
6622 trace_nfs4_get_lock(request, state, cmd, err);
6623 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624 &exception);
6625 } while (exception.retry);
6626 return err;
6627}
6628
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006629/*
6630 * Update the seqid of a lock stateid after receiving
6631 * NFS4ERR_OLD_STATEID
6632 */
6633static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6634 struct nfs4_lock_state *lsp)
6635{
6636 struct nfs4_state *state = lsp->ls_state;
6637 bool ret = false;
6638
6639 spin_lock(&state->state_lock);
6640 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6641 goto out;
6642 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6643 nfs4_stateid_seqid_inc(dst);
6644 else
6645 dst->seqid = lsp->ls_stateid.seqid;
6646 ret = true;
6647out:
6648 spin_unlock(&state->state_lock);
6649 return ret;
6650}
6651
6652static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6653 struct nfs4_lock_state *lsp)
6654{
6655 struct nfs4_state *state = lsp->ls_state;
6656 bool ret;
6657
6658 spin_lock(&state->state_lock);
6659 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6660 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6661 spin_unlock(&state->state_lock);
6662 return ret;
6663}
6664
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006665struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006666 struct nfs_locku_args arg;
6667 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006668 struct nfs4_lock_state *lsp;
6669 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006670 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006671 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006672 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006673 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006674};
6675
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006676static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6677 struct nfs_open_context *ctx,
6678 struct nfs4_lock_state *lsp,
6679 struct nfs_seqid *seqid)
6680{
6681 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006682 struct nfs4_state *state = lsp->ls_state;
6683 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006684
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006685 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006686 if (p == NULL)
6687 return NULL;
6688 p->arg.fh = NFS_FH(inode);
6689 p->arg.fl = &p->fl;
6690 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006691 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006692 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006693 /* Ensure we don't close file until we're done freeing locks! */
6694 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006695 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006696 locks_init_lock(&p->fl);
6697 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006698 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006699 spin_lock(&state->state_lock);
6700 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6701 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006702 return p;
6703}
6704
Trond Myklebust06f814a2006-01-03 09:55:07 +01006705static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006706{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006707 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006708 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006709 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006710 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006711 put_nfs_open_context(calldata->ctx);
6712 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006713}
6714
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006715static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006716{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006717 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006718 struct nfs4_exception exception = {
6719 .inode = calldata->lsp->ls_state->inode,
6720 .stateid = &calldata->arg.stateid,
6721 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006722
Trond Myklebust14516c32010-07-31 14:29:06 -04006723 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6724 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006725 switch (task->tk_status) {
6726 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006727 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006728 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006729 if (nfs4_update_lock_stateid(calldata->lsp,
6730 &calldata->res.stateid))
6731 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006732 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006733 case -NFS4ERR_ADMIN_REVOKED:
6734 case -NFS4ERR_EXPIRED:
6735 nfs4_free_revoked_stateid(calldata->server,
6736 &calldata->arg.stateid,
6737 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006738 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006739 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006740 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006741 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6742 calldata->lsp))
6743 rpc_restart_call_prepare(task);
6744 break;
6745 case -NFS4ERR_OLD_STATEID:
6746 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6747 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006748 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006749 break;
6750 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006751 task->tk_status = nfs4_async_handle_exception(task,
6752 calldata->server, task->tk_status,
6753 &exception);
6754 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006755 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006756 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006757 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006758}
6759
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006760static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006761{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006762 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006764 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6765 nfs_async_iocounter_wait(task, calldata->l_ctx))
6766 return;
6767
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006768 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006769 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006770 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006771 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006772 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006773 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006774 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006775 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006776 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006777 &calldata->res.seq_res,
6778 task) != 0)
6779 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006780 return;
6781out_no_action:
6782 task->tk_action = NULL;
6783out_wait:
6784 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785}
6786
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006787static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006788 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006789 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006790 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006791};
6792
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006793static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6794 struct nfs_open_context *ctx,
6795 struct nfs4_lock_state *lsp,
6796 struct nfs_seqid *seqid)
6797{
6798 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006799 struct rpc_message msg = {
6800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6801 .rpc_cred = ctx->cred,
6802 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006803 struct rpc_task_setup task_setup_data = {
6804 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006805 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006806 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006807 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006808 .flags = RPC_TASK_ASYNC,
6809 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006810
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006811 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6812 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6813
Frank Filz137d6ac2007-07-09 15:32:29 -07006814 /* Ensure this is an unlock - when canceling a lock, the
6815 * canceled lock is passed in, and it won't be an unlock.
6816 */
6817 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006818 if (fl->fl_flags & FL_CLOSE)
6819 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006820
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006821 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6822 if (data == NULL) {
6823 nfs_free_seqid(seqid);
6824 return ERR_PTR(-ENOMEM);
6825 }
6826
Anna Schumakerfba83f32018-05-04 16:22:50 -04006827 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006828 msg.rpc_argp = &data->arg;
6829 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006830 task_setup_data.callback_data = data;
6831 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006832}
6833
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6835{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006836 struct inode *inode = state->inode;
6837 struct nfs4_state_owner *sp = state->owner;
6838 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006839 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006840 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006841 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006842 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006843 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006844 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845
Trond Myklebust9b073572006-06-29 16:38:34 -04006846 status = nfs4_set_lock_state(state, request);
6847 /* Unlock _before_ we do the RPC call */
6848 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006849 /* Exclude nfs_delegation_claim_locks() */
6850 mutex_lock(&sp->so_delegreturn_mutex);
6851 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006852 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006853 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006854 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006855 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006856 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006857 }
6858 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006859 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006860 if (status != 0)
6861 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006862 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006863 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006864 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6865 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006866 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6867 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006868 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006869 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006870 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006871 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006872 status = PTR_ERR(task);
6873 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006874 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006875 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006876 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006877out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006878 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006879 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006880 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881}
6882
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006883struct nfs4_lockdata {
6884 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006885 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006886 struct nfs4_lock_state *lsp;
6887 struct nfs_open_context *ctx;
6888 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006889 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006890 int rpc_status;
6891 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006892 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006893};
6894
6895static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006896 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6897 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006898{
6899 struct nfs4_lockdata *p;
6900 struct inode *inode = lsp->ls_state->inode;
6901 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006902 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006903
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006904 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006905 if (p == NULL)
6906 return NULL;
6907
6908 p->arg.fh = NFS_FH(inode);
6909 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006910 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006911 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006912 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006913 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6914 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006915 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006916 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006917 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006918 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006919 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006920 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006921 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006922 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006923 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006924 locks_init_lock(&p->fl);
6925 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006926 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006927out_free_seqid:
6928 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006929out_free:
6930 kfree(p);
6931 return NULL;
6932}
6933
6934static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6935{
6936 struct nfs4_lockdata *data = calldata;
6937 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938
Harvey Harrison3110ff82008-05-02 13:42:44 -07006939 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006940 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006941 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006942 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006943 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006944 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006945 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006946 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006947 nfs4_stateid_copy(&data->arg.open_stateid,
6948 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006949 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006950 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006951 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006952 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006953 nfs4_stateid_copy(&data->arg.lock_stateid,
6954 &data->lsp->ls_stateid);
6955 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006956 if (!nfs4_valid_open_stateid(state)) {
6957 data->rpc_status = -EBADF;
6958 task->tk_action = NULL;
6959 goto out_release_open_seqid;
6960 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006961 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006962 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006963 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006964 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006965 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006966 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006967out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006968 nfs_release_seqid(data->arg.open_seqid);
6969out_release_lock_seqid:
6970 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006971out_wait:
6972 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006973 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006974}
6975
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006976static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6977{
6978 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006979 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006980
Harvey Harrison3110ff82008-05-02 13:42:44 -07006981 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006982
Trond Myklebust14516c32010-07-31 14:29:06 -04006983 if (!nfs4_sequence_done(task, &data->res.seq_res))
6984 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006985
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006986 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006987 switch (task->tk_status) {
6988 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006989 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006990 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006991 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006992 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006993 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006994 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006995 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006996 if (data->arg.new_lock_owner != 0) {
6997 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6998 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6999 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007000 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7001 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05007002 break;
7003 case -NFS4ERR_BAD_STATEID:
7004 case -NFS4ERR_OLD_STATEID:
7005 case -NFS4ERR_STALE_STATEID:
7006 case -NFS4ERR_EXPIRED:
7007 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007008 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007009 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007010 goto out_restart;
7011 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007012 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007013 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007014 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007015out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07007016 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007017 return;
7018out_restart:
7019 if (!data->cancelled)
7020 rpc_restart_call_prepare(task);
7021 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007022}
7023
7024static void nfs4_lock_release(void *calldata)
7025{
7026 struct nfs4_lockdata *data = calldata;
7027
Harvey Harrison3110ff82008-05-02 13:42:44 -07007028 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007029 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007030 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007031 struct rpc_task *task;
7032 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7033 data->arg.lock_seqid);
7034 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08007035 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007036 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007037 } else
7038 nfs_free_seqid(data->arg.lock_seqid);
7039 nfs4_put_lock_state(data->lsp);
7040 put_nfs_open_context(data->ctx);
7041 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007042 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007043}
7044
7045static const struct rpc_call_ops nfs4_lock_ops = {
7046 .rpc_call_prepare = nfs4_lock_prepare,
7047 .rpc_call_done = nfs4_lock_done,
7048 .rpc_release = nfs4_lock_release,
7049};
7050
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007051static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7052{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007053 switch (error) {
7054 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007055 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007056 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007057 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007058 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007059 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007060 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007061 break;
7062 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007063 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007064 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007065 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007066}
7067
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007068static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007069{
7070 struct nfs4_lockdata *data;
7071 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007072 struct rpc_message msg = {
7073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7074 .rpc_cred = state->owner->so_cred,
7075 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007076 struct rpc_task_setup task_setup_data = {
7077 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007078 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007079 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007080 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007081 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007082 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007083 int ret;
7084
Harvey Harrison3110ff82008-05-02 13:42:44 -07007085 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007086 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007087 fl->fl_u.nfs4_fl.owner,
7088 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007089 if (data == NULL)
7090 return -ENOMEM;
7091 if (IS_SETLKW(cmd))
7092 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007093 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7094 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007095 msg.rpc_argp = &data->arg;
7096 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007097 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007098 if (recovery_type > NFS_LOCK_NEW) {
7099 if (recovery_type == NFS_LOCK_RECLAIM)
7100 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007101 } else
7102 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007103 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007104 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007105 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007106 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007107 if (ret == 0) {
7108 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007109 if (ret)
7110 nfs4_handle_setlk_error(data->server, data->lsp,
7111 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007112 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007113 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007114 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007115 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05007116 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118}
7119
7120static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7121{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007122 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007123 struct nfs4_exception exception = {
7124 .inode = state->inode,
7125 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007126 int err;
7127
7128 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007129 /* Cache the lock if possible... */
7130 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7131 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007132 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007133 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007134 break;
7135 nfs4_handle_exception(server, err, &exception);
7136 } while (exception.retry);
7137 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138}
7139
7140static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7141{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007142 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007143 struct nfs4_exception exception = {
7144 .inode = state->inode,
7145 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007146 int err;
7147
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007148 err = nfs4_set_lock_state(state, request);
7149 if (err != 0)
7150 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007151 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007152 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7153 return 0;
7154 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007155 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007156 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7157 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007158 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007159 switch (err) {
7160 default:
7161 goto out;
7162 case -NFS4ERR_GRACE:
7163 case -NFS4ERR_DELAY:
7164 nfs4_handle_exception(server, err, &exception);
7165 err = 0;
7166 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007167 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007168out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170}
7171
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007172#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007173static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7174{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007175 struct nfs4_lock_state *lsp;
7176 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007177
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007178 status = nfs4_set_lock_state(state, request);
7179 if (status != 0)
7180 return status;
7181 lsp = request->fl_u.nfs4_fl.owner;
7182 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7183 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7184 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007185 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007186}
7187#endif
7188
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7190{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007191 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007192 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007193 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007194 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195
Trond Myklebust01c3b862006-06-29 16:38:39 -04007196 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007197 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007198 if (status < 0)
7199 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007200 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007201 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007202 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007203 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007204 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007205 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007206 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007207 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007208 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007209 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007210 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007211 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007212 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007213 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007214out:
7215 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 return status;
7217}
7218
7219static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7220{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007221 struct nfs4_exception exception = {
7222 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007223 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007224 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007225 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 int err;
7227
7228 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007229 err = _nfs4_proc_setlk(state, cmd, request);
7230 if (err == -NFS4ERR_DENIED)
7231 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007233 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 } while (exception.retry);
7235 return err;
7236}
7237
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007238#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7239#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7240
7241static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007242nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7243 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007244{
7245 int status = -ERESTARTSYS;
7246 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7247
7248 while(!signalled()) {
7249 status = nfs4_proc_setlk(state, cmd, request);
7250 if ((status != -EAGAIN) || IS_SETLK(cmd))
7251 break;
7252 freezable_schedule_timeout_interruptible(timeout);
7253 timeout *= 2;
7254 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7255 status = -ERESTARTSYS;
7256 }
7257 return status;
7258}
7259
Jeff Laytona1d617d82016-09-17 18:17:39 -04007260#ifdef CONFIG_NFS_V4_1
7261struct nfs4_lock_waiter {
7262 struct task_struct *task;
7263 struct inode *inode;
7264 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007265};
7266
7267static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007268nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007269{
7270 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007271 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007272
Jeff Layton57174592018-03-18 08:37:03 -04007273 /* NULL key means to wake up everyone */
7274 if (key) {
7275 struct cb_notify_lock_args *cbnl = key;
7276 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7277 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007278
Jeff Layton57174592018-03-18 08:37:03 -04007279 /* Only wake if the callback was for the same owner. */
7280 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7281 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007282
Jeff Layton57174592018-03-18 08:37:03 -04007283 /* Make sure it's for the right inode */
7284 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7285 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007286 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007287
7288 /* override "private" so we can use default_wake_function */
7289 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007290 ret = woken_wake_function(wait, mode, flags, key);
7291 if (ret)
7292 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007293 wait->private = waiter;
7294 return ret;
7295}
7296
7297static int
7298nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7299{
7300 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007301 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7302 struct nfs_server *server = NFS_SERVER(state->inode);
7303 struct nfs_client *clp = server->nfs_client;
7304 wait_queue_head_t *q = &clp->cl_lock_waitq;
7305 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7306 .id = lsp->ls_seqid.owner_id,
7307 .s_dev = server->s_dev };
7308 struct nfs4_lock_waiter waiter = { .task = current,
7309 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007310 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007311 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007312
7313 /* Don't bother with waitqueue if we don't expect a callback */
7314 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7315 return nfs4_retry_setlk_simple(state, cmd, request);
7316
7317 init_wait(&wait);
7318 wait.private = &waiter;
7319 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007320
7321 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007322 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007323 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007324 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7325 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007326 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007327 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007328
7329 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007330 freezer_do_not_count();
7331 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7332 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007333 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007334 }
7335
Jeff Laytona1d617d82016-09-17 18:17:39 -04007336 return status;
7337}
7338#else /* !CONFIG_NFS_V4_1 */
7339static inline int
7340nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7341{
7342 return nfs4_retry_setlk_simple(state, cmd, request);
7343}
7344#endif
7345
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346static int
7347nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7348{
7349 struct nfs_open_context *ctx;
7350 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 int status;
7352
7353 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007354 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 state = ctx->state;
7356
Trond Myklebustd9531262009-07-21 19:22:38 -04007357 if (IS_GETLK(cmd)) {
7358 if (state != NULL)
7359 return nfs4_proc_getlk(state, F_GETLK, request);
7360 return 0;
7361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362
7363 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7364 return -EINVAL;
7365
Trond Myklebustd9531262009-07-21 19:22:38 -04007366 if (request->fl_type == F_UNLCK) {
7367 if (state != NULL)
7368 return nfs4_proc_unlck(state, cmd, request);
7369 return 0;
7370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371
Trond Myklebustd9531262009-07-21 19:22:38 -04007372 if (state == NULL)
7373 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007374
7375 if ((request->fl_flags & FL_POSIX) &&
7376 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7377 return -ENOLCK;
7378
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007379 /*
7380 * Don't rely on the VFS having checked the file open mode,
7381 * since it won't do this for flock() locks.
7382 */
7383 switch (request->fl_type) {
7384 case F_RDLCK:
7385 if (!(filp->f_mode & FMODE_READ))
7386 return -EBADF;
7387 break;
7388 case F_WRLCK:
7389 if (!(filp->f_mode & FMODE_WRITE))
7390 return -EBADF;
7391 }
7392
Jeff Layton1ea67db2016-09-17 18:17:37 -04007393 status = nfs4_set_lock_state(state, request);
7394 if (status != 0)
7395 return status;
7396
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007397 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398}
7399
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007400int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007401{
7402 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007403 int err;
7404
7405 err = nfs4_set_lock_state(state, fl);
7406 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007407 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007408 do {
7409 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7410 if (err != -NFS4ERR_DELAY)
7411 break;
7412 ssleep(1);
7413 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007414 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007415}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007416
Trond Myklebustcf470c32012-03-07 13:49:12 -05007417struct nfs_release_lockowner_data {
7418 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007419 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007420 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007421 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007422 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007423};
7424
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007425static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7426{
7427 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007428 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007429 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7430 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007431 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007432 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007433}
7434
7435static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7436{
7437 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007438 struct nfs_server *server = data->server;
7439
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007440 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007441
7442 switch (task->tk_status) {
7443 case 0:
7444 renew_lease(server, data->timestamp);
7445 break;
7446 case -NFS4ERR_STALE_CLIENTID:
7447 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007448 nfs4_schedule_lease_recovery(server->nfs_client);
7449 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007450 case -NFS4ERR_LEASE_MOVED:
7451 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007452 if (nfs4_async_handle_error(task, server,
7453 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007454 rpc_restart_call_prepare(task);
7455 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007456}
7457
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007458static void nfs4_release_lockowner_release(void *calldata)
7459{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007460 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007461 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007462 kfree(calldata);
7463}
7464
Trond Myklebust17280172012-03-11 13:11:00 -04007465static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007466 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7467 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007468 .rpc_release = nfs4_release_lockowner_release,
7469};
7470
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007471static void
7472nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007473{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007474 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007475 struct rpc_message msg = {
7476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7477 };
7478
7479 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007480 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007481
Trond Myklebustcf470c32012-03-07 13:49:12 -05007482 data = kmalloc(sizeof(*data), GFP_NOFS);
7483 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007484 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007485 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007486 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007487 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7488 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7489 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007490
Trond Myklebustcf470c32012-03-07 13:49:12 -05007491 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007492 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007493 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007494 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007495}
7496
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007497#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7498
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007499static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007500 struct dentry *unused, struct inode *inode,
7501 const char *key, const void *buf,
7502 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007503{
Al Viro59301222016-05-27 10:19:30 -04007504 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007505}
7506
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007507static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007508 struct dentry *unused, struct inode *inode,
7509 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007510{
Al Virob2968212016-04-10 20:48:24 -04007511 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007512}
7513
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007514static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007515{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007516 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007517}
7518
David Quigleyc9bccef2013-05-22 12:50:45 -04007519#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007520
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007521static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007522 struct dentry *unused, struct inode *inode,
7523 const char *key, const void *buf,
7524 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007525{
7526 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007527 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007528
7529 return -EOPNOTSUPP;
7530}
7531
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007532static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007533 struct dentry *unused, struct inode *inode,
7534 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007535{
7536 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007537 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007538 return -EOPNOTSUPP;
7539}
7540
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007541static ssize_t
7542nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007543{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007544 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007545
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007546 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7547 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007548 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007549 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007550 }
7551 return len;
7552}
7553
7554static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7555 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007556 .get = nfs4_xattr_get_nfs4_label,
7557 .set = nfs4_xattr_set_nfs4_label,
7558};
David Quigleyc9bccef2013-05-22 12:50:45 -04007559
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007560#else
7561
7562static ssize_t
7563nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7564{
7565 return 0;
7566}
7567
7568#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007569
Frank van der Linden012a2112020-06-23 22:39:03 +00007570#ifdef CONFIG_NFS_V4_2
7571static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7572 struct dentry *unused, struct inode *inode,
7573 const char *key, const void *buf,
7574 size_t buflen, int flags)
7575{
7576 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007577 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007578
7579 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7580 return -EOPNOTSUPP;
7581
7582 /*
7583 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7584 * flags right now. Handling of xattr operations use the normal
7585 * file read/write permissions.
7586 *
7587 * Just in case the server has other ideas (which RFC 8276 allows),
7588 * do a cached access check for the XA* flags to possibly avoid
7589 * doing an RPC and getting EACCES back.
7590 */
7591 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7592 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7593 return -EACCES;
7594 }
7595
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007596 if (buf == NULL) {
7597 ret = nfs42_proc_removexattr(inode, key);
7598 if (!ret)
7599 nfs4_xattr_cache_remove(inode, key);
7600 } else {
7601 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7602 if (!ret)
7603 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7604 }
7605
7606 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007607}
7608
7609static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7610 struct dentry *unused, struct inode *inode,
7611 const char *key, void *buf, size_t buflen)
7612{
7613 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007614 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007615
7616 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7617 return -EOPNOTSUPP;
7618
7619 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7620 if (!(cache.mask & NFS_ACCESS_XAREAD))
7621 return -EACCES;
7622 }
7623
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007624 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7625 if (ret)
7626 return ret;
7627
7628 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7629 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7630 return ret;
7631
7632 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7633
7634 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007635}
7636
7637static ssize_t
7638nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7639{
7640 u64 cookie;
7641 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007642 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007643 char *buf;
7644 size_t buflen;
7645 struct nfs_access_entry cache;
7646
7647 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7648 return 0;
7649
7650 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7651 if (!(cache.mask & NFS_ACCESS_XALIST))
7652 return 0;
7653 }
7654
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007655 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7656 if (ret)
7657 return ret;
7658
7659 ret = nfs4_xattr_cache_list(inode, list, list_len);
7660 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7661 return ret;
7662
Frank van der Linden012a2112020-06-23 22:39:03 +00007663 cookie = 0;
7664 eof = false;
7665 buflen = list_len ? list_len : XATTR_LIST_MAX;
7666 buf = list_len ? list : NULL;
7667 size = 0;
7668
7669 while (!eof) {
7670 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7671 &cookie, &eof);
7672 if (ret < 0)
7673 return ret;
7674
7675 if (list_len) {
7676 buf += ret;
7677 buflen -= ret;
7678 }
7679 size += ret;
7680 }
7681
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007682 if (list_len)
7683 nfs4_xattr_cache_set_list(inode, list, size);
7684
Frank van der Linden012a2112020-06-23 22:39:03 +00007685 return size;
7686}
7687
7688#else
7689
7690static ssize_t
7691nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7692{
7693 return 0;
7694}
7695#endif /* CONFIG_NFS_V4_2 */
7696
Andy Adamson533eb462011-06-13 18:25:56 -04007697/*
7698 * nfs_fhget will use either the mounted_on_fileid or the fileid
7699 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007700static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7701{
Andy Adamson533eb462011-06-13 18:25:56 -04007702 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7703 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7704 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007705 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007706 return;
7707
7708 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007709 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007710 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7711 fattr->nlink = 2;
7712}
7713
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007714static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7715 const struct qstr *name,
7716 struct nfs4_fs_locations *fs_locations,
7717 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007718{
7719 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007720 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007721 struct nfs4_fs_locations_arg args = {
7722 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007723 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007724 .page = page,
7725 .bitmask = bitmask,
7726 };
Benny Halevy22958462009-04-01 09:22:02 -04007727 struct nfs4_fs_locations_res res = {
7728 .fs_locations = fs_locations,
7729 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007730 struct rpc_message msg = {
7731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7732 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007733 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007734 };
7735 int status;
7736
Harvey Harrison3110ff82008-05-02 13:42:44 -07007737 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007738
Chuck Leverc05cefc2017-11-05 15:45:22 -05007739 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7740 bitmask[1] = nfs4_fattr_bitmap[1];
7741
Andy Adamson533eb462011-06-13 18:25:56 -04007742 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7743 * is not supported */
7744 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007745 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007746 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007747 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007748
Trond Myklebustc228fd32007-01-13 02:28:11 -05007749 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007750 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007751 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007752 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007753 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007754 return status;
7755}
7756
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007757int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7758 const struct qstr *name,
7759 struct nfs4_fs_locations *fs_locations,
7760 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007761{
Trond Myklebust0688e642019-04-07 13:59:09 -04007762 struct nfs4_exception exception = {
7763 .interruptible = true,
7764 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007765 int err;
7766 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007767 err = _nfs4_proc_fs_locations(client, dir, name,
7768 fs_locations, page);
7769 trace_nfs4_get_fs_locations(dir, name, err);
7770 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007771 &exception);
7772 } while (exception.retry);
7773 return err;
7774}
7775
Chuck Leverb03d7352013-10-17 14:12:50 -04007776/*
7777 * This operation also signals the server that this client is
7778 * performing migration recovery. The server can stop returning
7779 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7780 * appended to this compound to identify the client ID which is
7781 * performing recovery.
7782 */
7783static int _nfs40_proc_get_locations(struct inode *inode,
7784 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007785 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007786{
7787 struct nfs_server *server = NFS_SERVER(inode);
7788 struct rpc_clnt *clnt = server->client;
7789 u32 bitmask[2] = {
7790 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7791 };
7792 struct nfs4_fs_locations_arg args = {
7793 .clientid = server->nfs_client->cl_clientid,
7794 .fh = NFS_FH(inode),
7795 .page = page,
7796 .bitmask = bitmask,
7797 .migration = 1, /* skip LOOKUP */
7798 .renew = 1, /* append RENEW */
7799 };
7800 struct nfs4_fs_locations_res res = {
7801 .fs_locations = locations,
7802 .migration = 1,
7803 .renew = 1,
7804 };
7805 struct rpc_message msg = {
7806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7807 .rpc_argp = &args,
7808 .rpc_resp = &res,
7809 .rpc_cred = cred,
7810 };
7811 unsigned long now = jiffies;
7812 int status;
7813
7814 nfs_fattr_init(&locations->fattr);
7815 locations->server = server;
7816 locations->nlocations = 0;
7817
Anna Schumakerfba83f32018-05-04 16:22:50 -04007818 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007819 status = nfs4_call_sync_sequence(clnt, server, &msg,
7820 &args.seq_args, &res.seq_res);
7821 if (status)
7822 return status;
7823
7824 renew_lease(server, now);
7825 return 0;
7826}
7827
7828#ifdef CONFIG_NFS_V4_1
7829
7830/*
7831 * This operation also signals the server that this client is
7832 * performing migration recovery. The server can stop asserting
7833 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7834 * performing this operation is identified in the SEQUENCE
7835 * operation in this compound.
7836 *
7837 * When the client supports GETATTR(fs_locations_info), it can
7838 * be plumbed in here.
7839 */
7840static int _nfs41_proc_get_locations(struct inode *inode,
7841 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007842 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007843{
7844 struct nfs_server *server = NFS_SERVER(inode);
7845 struct rpc_clnt *clnt = server->client;
7846 u32 bitmask[2] = {
7847 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7848 };
7849 struct nfs4_fs_locations_arg args = {
7850 .fh = NFS_FH(inode),
7851 .page = page,
7852 .bitmask = bitmask,
7853 .migration = 1, /* skip LOOKUP */
7854 };
7855 struct nfs4_fs_locations_res res = {
7856 .fs_locations = locations,
7857 .migration = 1,
7858 };
7859 struct rpc_message msg = {
7860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7861 .rpc_argp = &args,
7862 .rpc_resp = &res,
7863 .rpc_cred = cred,
7864 };
7865 int status;
7866
7867 nfs_fattr_init(&locations->fattr);
7868 locations->server = server;
7869 locations->nlocations = 0;
7870
Anna Schumakerfba83f32018-05-04 16:22:50 -04007871 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007872 status = nfs4_call_sync_sequence(clnt, server, &msg,
7873 &args.seq_args, &res.seq_res);
7874 if (status == NFS4_OK &&
7875 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7876 status = -NFS4ERR_LEASE_MOVED;
7877 return status;
7878}
7879
7880#endif /* CONFIG_NFS_V4_1 */
7881
7882/**
7883 * nfs4_proc_get_locations - discover locations for a migrated FSID
7884 * @inode: inode on FSID that is migrating
7885 * @locations: result of query
7886 * @page: buffer
7887 * @cred: credential to use for this operation
7888 *
7889 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7890 * operation failed, or a negative errno if a local error occurred.
7891 *
7892 * On success, "locations" is filled in, but if the server has
7893 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7894 * asserted.
7895 *
7896 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7897 * from this client that require migration recovery.
7898 */
7899int nfs4_proc_get_locations(struct inode *inode,
7900 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007901 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007902{
7903 struct nfs_server *server = NFS_SERVER(inode);
7904 struct nfs_client *clp = server->nfs_client;
7905 const struct nfs4_mig_recovery_ops *ops =
7906 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007907 struct nfs4_exception exception = {
7908 .interruptible = true,
7909 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007910 int status;
7911
7912 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7913 (unsigned long long)server->fsid.major,
7914 (unsigned long long)server->fsid.minor,
7915 clp->cl_hostname);
7916 nfs_display_fhandle(NFS_FH(inode), __func__);
7917
7918 do {
7919 status = ops->get_locations(inode, locations, page, cred);
7920 if (status != -NFS4ERR_DELAY)
7921 break;
7922 nfs4_handle_exception(server, status, &exception);
7923 } while (exception.retry);
7924 return status;
7925}
7926
Chuck Lever44c99932013-10-17 14:13:30 -04007927/*
7928 * This operation also signals the server that this client is
7929 * performing "lease moved" recovery. The server can stop
7930 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7931 * is appended to this compound to identify the client ID which is
7932 * performing recovery.
7933 */
NeilBrowna52458b2018-12-03 11:30:31 +11007934static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007935{
7936 struct nfs_server *server = NFS_SERVER(inode);
7937 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7938 struct rpc_clnt *clnt = server->client;
7939 struct nfs4_fsid_present_arg args = {
7940 .fh = NFS_FH(inode),
7941 .clientid = clp->cl_clientid,
7942 .renew = 1, /* append RENEW */
7943 };
7944 struct nfs4_fsid_present_res res = {
7945 .renew = 1,
7946 };
7947 struct rpc_message msg = {
7948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7949 .rpc_argp = &args,
7950 .rpc_resp = &res,
7951 .rpc_cred = cred,
7952 };
7953 unsigned long now = jiffies;
7954 int status;
7955
7956 res.fh = nfs_alloc_fhandle();
7957 if (res.fh == NULL)
7958 return -ENOMEM;
7959
Anna Schumakerfba83f32018-05-04 16:22:50 -04007960 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007961 status = nfs4_call_sync_sequence(clnt, server, &msg,
7962 &args.seq_args, &res.seq_res);
7963 nfs_free_fhandle(res.fh);
7964 if (status)
7965 return status;
7966
7967 do_renew_lease(clp, now);
7968 return 0;
7969}
7970
7971#ifdef CONFIG_NFS_V4_1
7972
7973/*
7974 * This operation also signals the server that this client is
7975 * performing "lease moved" recovery. The server can stop asserting
7976 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7977 * this operation is identified in the SEQUENCE operation in this
7978 * compound.
7979 */
NeilBrowna52458b2018-12-03 11:30:31 +11007980static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007981{
7982 struct nfs_server *server = NFS_SERVER(inode);
7983 struct rpc_clnt *clnt = server->client;
7984 struct nfs4_fsid_present_arg args = {
7985 .fh = NFS_FH(inode),
7986 };
7987 struct nfs4_fsid_present_res res = {
7988 };
7989 struct rpc_message msg = {
7990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7991 .rpc_argp = &args,
7992 .rpc_resp = &res,
7993 .rpc_cred = cred,
7994 };
7995 int status;
7996
7997 res.fh = nfs_alloc_fhandle();
7998 if (res.fh == NULL)
7999 return -ENOMEM;
8000
Anna Schumakerfba83f32018-05-04 16:22:50 -04008001 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04008002 status = nfs4_call_sync_sequence(clnt, server, &msg,
8003 &args.seq_args, &res.seq_res);
8004 nfs_free_fhandle(res.fh);
8005 if (status == NFS4_OK &&
8006 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8007 status = -NFS4ERR_LEASE_MOVED;
8008 return status;
8009}
8010
8011#endif /* CONFIG_NFS_V4_1 */
8012
8013/**
8014 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8015 * @inode: inode on FSID to check
8016 * @cred: credential to use for this operation
8017 *
8018 * Server indicates whether the FSID is present, moved, or not
8019 * recognized. This operation is necessary to clear a LEASE_MOVED
8020 * condition for this client ID.
8021 *
8022 * Returns NFS4_OK if the FSID is present on this server,
8023 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8024 * NFS4ERR code if some error occurred on the server, or a
8025 * negative errno if a local failure occurred.
8026 */
NeilBrowna52458b2018-12-03 11:30:31 +11008027int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008028{
8029 struct nfs_server *server = NFS_SERVER(inode);
8030 struct nfs_client *clp = server->nfs_client;
8031 const struct nfs4_mig_recovery_ops *ops =
8032 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008033 struct nfs4_exception exception = {
8034 .interruptible = true,
8035 };
Chuck Lever44c99932013-10-17 14:13:30 -04008036 int status;
8037
8038 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8039 (unsigned long long)server->fsid.major,
8040 (unsigned long long)server->fsid.minor,
8041 clp->cl_hostname);
8042 nfs_display_fhandle(NFS_FH(inode), __func__);
8043
8044 do {
8045 status = ops->fsid_present(inode, cred);
8046 if (status != -NFS4ERR_DELAY)
8047 break;
8048 nfs4_handle_exception(server, status, &exception);
8049 } while (exception.retry);
8050 return status;
8051}
8052
Trond Myklebust302fad72019-02-18 13:32:38 -05008053/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008054 * If 'use_integrity' is true and the state managment nfs_client
8055 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8056 * and the machine credential as per RFC3530bis and RFC5661 Security
8057 * Considerations sections. Otherwise, just use the user cred with the
8058 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008059 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008060static 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 +00008061{
8062 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008063 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8064 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008065 struct nfs4_secinfo_arg args = {
8066 .dir_fh = NFS_FH(dir),
8067 .name = name,
8068 };
8069 struct nfs4_secinfo_res res = {
8070 .flavors = flavors,
8071 };
8072 struct rpc_message msg = {
8073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8074 .rpc_argp = &args,
8075 .rpc_resp = &res,
8076 };
Anna Schumaker50493362019-08-14 15:30:16 -04008077 struct nfs4_call_sync_data data = {
8078 .seq_server = NFS_SERVER(dir),
8079 .seq_args = &args.seq_args,
8080 .seq_res = &res.seq_res,
8081 };
8082 struct rpc_task_setup task_setup = {
8083 .rpc_client = clnt,
8084 .rpc_message = &msg,
8085 .callback_ops = clp->cl_mvops->call_sync_ops,
8086 .callback_data = &data,
8087 .flags = RPC_TASK_NO_ROUND_ROBIN,
8088 };
NeilBrowna52458b2018-12-03 11:30:31 +11008089 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008090
8091 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008092 clnt = clp->cl_rpcclient;
8093 task_setup.rpc_client = clnt;
8094
8095 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008096 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008097 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008098
8099 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008100
Anna Schumaker50493362019-08-14 15:30:16 -04008101 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8102 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8103 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008104
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008105 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008106
NeilBrowna52458b2018-12-03 11:30:31 +11008107 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008108 return status;
8109}
8110
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008111int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8112 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008113{
Trond Myklebust0688e642019-04-07 13:59:09 -04008114 struct nfs4_exception exception = {
8115 .interruptible = true,
8116 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008117 int err;
8118 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008119 err = -NFS4ERR_WRONGSEC;
8120
8121 /* try to use integrity protection with machine cred */
8122 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8123 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8124
8125 /*
8126 * if unable to use integrity protection, or SECINFO with
8127 * integrity protection returns NFS4ERR_WRONGSEC (which is
8128 * disallowed by spec, but exists in deployed servers) use
8129 * the current filesystem's rpc_client and the user cred.
8130 */
8131 if (err == -NFS4ERR_WRONGSEC)
8132 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8133
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008134 trace_nfs4_secinfo(dir, name, err);
8135 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008136 &exception);
8137 } while (exception.retry);
8138 return err;
8139}
8140
Andy Adamson557134a2009-04-01 09:21:53 -04008141#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008142/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008143 * Check the exchange flags returned by the server for invalid flags, having
8144 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8145 * DS flags set.
8146 */
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008147static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
Andy Adamson357f54d2010-12-14 10:11:57 -05008148{
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008149 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8150 goto out_inval;
8151 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
Andy Adamson357f54d2010-12-14 10:11:57 -05008152 goto out_inval;
8153 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8154 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8155 goto out_inval;
8156 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8157 goto out_inval;
8158 return NFS_OK;
8159out_inval:
8160 return -NFS4ERR_INVAL;
8161}
8162
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008163static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008164nfs41_same_server_scope(struct nfs41_server_scope *a,
8165 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008166{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008167 if (a->server_scope_sz != b->server_scope_sz)
8168 return false;
8169 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008170}
8171
Andy Adamson02a95de2016-02-05 16:08:37 -05008172static void
8173nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8174{
Trond Myklebust5c441542019-11-13 08:34:00 +01008175 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008176 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008177 struct nfs_client *clp = args->client;
8178
8179 switch (task->tk_status) {
8180 case -NFS4ERR_BADSESSION:
8181 case -NFS4ERR_DEADSESSION:
8182 nfs4_schedule_session_recovery(clp->cl_session,
8183 task->tk_status);
8184 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008185 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8186 res->dir != NFS4_CDFS4_BOTH) {
8187 rpc_task_close_connection(task);
8188 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8189 rpc_restart_call(task);
8190 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008191}
8192
8193static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008194 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008195};
8196
Andy Adamson357f54d2010-12-14 10:11:57 -05008197/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008198 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008199 *
8200 * The 4.1 client currently uses the same TCP connection for the
8201 * fore and backchannel.
8202 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008203static
8204int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8205 struct rpc_xprt *xprt,
8206 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008207 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008208{
8209 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008210 struct nfs41_bind_conn_to_session_args args = {
8211 .client = clp,
8212 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008213 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008214 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008215 struct nfs41_bind_conn_to_session_res res;
8216 struct rpc_message msg = {
8217 .rpc_proc =
8218 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008219 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008220 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008221 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008222 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008223 struct rpc_task_setup task_setup_data = {
8224 .rpc_client = clnt,
8225 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008226 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008227 .rpc_message = &msg,
8228 .flags = RPC_TASK_TIMEOUT,
8229 };
8230 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008231
Trond Myklebust71a097c2015-02-18 09:27:18 -08008232 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8233 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8234 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008235
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008236 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8237 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8238 args.dir = NFS4_CDFC4_FORE;
8239
8240 task = rpc_run_task(&task_setup_data);
8241 if (!IS_ERR(task)) {
8242 status = task->tk_status;
8243 rpc_put_task(task);
8244 } else
8245 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008246 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008247 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008248 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008249 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8250 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008251 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008252 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008253 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008254 dprintk("NFS: %s: Unexpected direction from server\n",
8255 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008256 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008257 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008258 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008259 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8260 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008261 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008262 }
8263 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008264
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008265 return status;
8266}
8267
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008268struct rpc_bind_conn_calldata {
8269 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008270 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008271};
8272
8273static int
8274nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8275 struct rpc_xprt *xprt,
8276 void *calldata)
8277{
8278 struct rpc_bind_conn_calldata *p = calldata;
8279
8280 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8281}
8282
NeilBrowna52458b2018-12-03 11:30:31 +11008283int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008284{
8285 struct rpc_bind_conn_calldata data = {
8286 .clp = clp,
8287 .cred = cred,
8288 };
8289 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8290 nfs4_proc_bind_conn_to_session_callback, &data);
8291}
8292
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008293/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008294 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8295 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008296 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008297static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8298 .how = SP4_MACH_CRED,
8299 .enforce.u.words = {
8300 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8301 1 << (OP_EXCHANGE_ID - 32) |
8302 1 << (OP_CREATE_SESSION - 32) |
8303 1 << (OP_DESTROY_SESSION - 32) |
8304 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008305 },
8306 .allow.u.words = {
8307 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008308 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008309 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008310 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008311 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008312 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008313 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008314 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008315 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008316 1 << (OP_FREE_STATEID - 32) |
8317 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008318 }
8319};
8320
8321/*
8322 * Select the state protection mode for client `clp' given the server results
8323 * from exchange_id in `sp'.
8324 *
8325 * Returns 0 on success, negative errno otherwise.
8326 */
8327static int nfs4_sp4_select_mode(struct nfs_client *clp,
8328 struct nfs41_state_protection *sp)
8329{
8330 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8331 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8332 1 << (OP_EXCHANGE_ID - 32) |
8333 1 << (OP_CREATE_SESSION - 32) |
8334 1 << (OP_DESTROY_SESSION - 32) |
8335 1 << (OP_DESTROY_CLIENTID - 32)
8336 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008337 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008338 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008339 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008340
8341 if (sp->how == SP4_MACH_CRED) {
8342 /* Print state protect result */
8343 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8344 for (i = 0; i <= LAST_NFS4_OP; i++) {
8345 if (test_bit(i, sp->enforce.u.longs))
8346 dfprintk(MOUNT, " enforce op %d\n", i);
8347 if (test_bit(i, sp->allow.u.longs))
8348 dfprintk(MOUNT, " allow op %d\n", i);
8349 }
8350
8351 /* make sure nothing is on enforce list that isn't supported */
8352 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8353 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8354 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008355 ret = -EINVAL;
8356 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008357 }
8358 }
8359
8360 /*
8361 * Minimal mode - state operations are allowed to use machine
8362 * credential. Note this already happens by default, so the
8363 * client doesn't have to do anything more than the negotiation.
8364 *
8365 * NOTE: we don't care if EXCHANGE_ID is in the list -
8366 * we're already using the machine cred for exchange_id
8367 * and will never use a different cred.
8368 */
8369 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8370 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8371 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8372 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8373 dfprintk(MOUNT, "sp4_mach_cred:\n");
8374 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008375 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008376 } else {
8377 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008378 ret = -EINVAL;
8379 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008380 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008381
8382 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008383 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8384 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008385 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8386 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008387 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008388 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008389
Andrew Elble99ade3c2015-12-02 09:39:51 -05008390 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8391 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008392 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008393 }
8394
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008395 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8396 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8397 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008398 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008399 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008400
8401 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8402 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8403 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008404 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008405 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008406
8407 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8408 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008409 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008410 }
8411
8412 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8413 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008414 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008415 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008416 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008417out:
8418 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008419 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008420}
8421
Andy Adamson8d89bd72016-09-09 09:22:18 -04008422struct nfs41_exchange_id_data {
8423 struct nfs41_exchange_id_res res;
8424 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008425};
8426
Andy Adamson8d89bd72016-09-09 09:22:18 -04008427static void nfs4_exchange_id_release(void *data)
8428{
8429 struct nfs41_exchange_id_data *cdata =
8430 (struct nfs41_exchange_id_data *)data;
8431
Olga Kornievskaia63513232017-03-13 10:36:19 -04008432 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008433 kfree(cdata->res.impl_id);
8434 kfree(cdata->res.server_scope);
8435 kfree(cdata->res.server_owner);
8436 kfree(cdata);
8437}
8438
8439static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008440 .rpc_release = nfs4_exchange_id_release,
8441};
8442
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008443/*
8444 * _nfs4_proc_exchange_id()
8445 *
8446 * Wrapper for EXCHANGE_ID operation.
8447 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008448static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008449nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008450 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008451{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008452 struct rpc_message msg = {
8453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008454 .rpc_cred = cred,
8455 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008456 struct rpc_task_setup task_setup_data = {
8457 .rpc_client = clp->cl_rpcclient,
8458 .callback_ops = &nfs4_exchange_id_call_ops,
8459 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008460 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008461 };
8462 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008463 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008464
Elena Reshetova212bf412017-10-20 12:53:38 +03008465 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008466 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008467
Trond Myklebust9c760d12017-07-31 18:38:50 -04008468 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008469 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008470 if (!calldata)
8471 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008472
Trond Myklebustfd405592017-08-01 16:02:47 -04008473 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008474
8475 status = nfs4_init_uniform_client_string(clp);
8476 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008477 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008478
Andy Adamson8d89bd72016-09-09 09:22:18 -04008479 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8480 GFP_NOFS);
8481 status = -ENOMEM;
8482 if (unlikely(calldata->res.server_owner == NULL))
8483 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008484
Andy Adamson8d89bd72016-09-09 09:22:18 -04008485 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008486 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008487 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008488 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008489
Andy Adamson8d89bd72016-09-09 09:22:18 -04008490 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8491 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008492 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008493
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008494 switch (sp4_how) {
8495 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008496 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008497 break;
8498
8499 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008500 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008501 break;
8502
8503 default:
8504 /* unsupported! */
8505 WARN_ON_ONCE(1);
8506 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008507 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008508 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008509 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008510 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008511 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008512 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8513 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008514 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008515 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008516 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8517 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008518#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008519 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008520#endif
8521 msg.rpc_argp = &calldata->args;
8522 msg.rpc_resp = &calldata->res;
8523 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008524
Trond Myklebust9c760d12017-07-31 18:38:50 -04008525 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008526
8527out_impl_id:
8528 kfree(calldata->res.impl_id);
8529out_server_scope:
8530 kfree(calldata->res.server_scope);
8531out_server_owner:
8532 kfree(calldata->res.server_owner);
8533out_calldata:
8534 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008535out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008536 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008537 return ERR_PTR(status);
8538}
8539
8540/*
8541 * _nfs4_proc_exchange_id()
8542 *
8543 * Wrapper for EXCHANGE_ID operation.
8544 */
NeilBrowna52458b2018-12-03 11:30:31 +11008545static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008546 u32 sp4_how)
8547{
8548 struct rpc_task *task;
8549 struct nfs41_exchange_id_args *argp;
8550 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008551 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008552 int status;
8553
8554 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8555 if (IS_ERR(task))
8556 return PTR_ERR(task);
8557
8558 argp = task->tk_msg.rpc_argp;
8559 resp = task->tk_msg.rpc_resp;
8560 status = task->tk_status;
8561 if (status != 0)
8562 goto out;
8563
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008564 status = nfs4_check_cl_exchange_flags(resp->flags,
8565 clp->cl_mvops->minor_version);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008566 if (status != 0)
8567 goto out;
8568
8569 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8570 if (status != 0)
8571 goto out;
8572
Robert Milkowski7dc29932020-01-30 09:43:25 +00008573 do_renew_lease(clp, now);
8574
Trond Myklebust9c760d12017-07-31 18:38:50 -04008575 clp->cl_clientid = resp->clientid;
8576 clp->cl_exchange_flags = resp->flags;
8577 clp->cl_seqid = resp->seqid;
8578 /* Client ID is not confirmed */
8579 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8580 clear_bit(NFS4_SESSION_ESTABLISHED,
8581 &clp->cl_session->session_state);
8582
8583 if (clp->cl_serverscope != NULL &&
8584 !nfs41_same_server_scope(clp->cl_serverscope,
8585 resp->server_scope)) {
8586 dprintk("%s: server_scope mismatch detected\n",
8587 __func__);
8588 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8589 }
8590
8591 swap(clp->cl_serverowner, resp->server_owner);
8592 swap(clp->cl_serverscope, resp->server_scope);
8593 swap(clp->cl_implid, resp->impl_id);
8594
8595 /* Save the EXCHANGE_ID verifier session trunk tests */
8596 memcpy(clp->cl_confirm.data, argp->verifier.data,
8597 sizeof(clp->cl_confirm.data));
8598out:
8599 trace_nfs4_exchange_id(clp, status);
8600 rpc_put_task(task);
8601 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008602}
8603
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008604/*
8605 * nfs4_proc_exchange_id()
8606 *
8607 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8608 *
8609 * Since the clientid has expired, all compounds using sessions
8610 * associated with the stale clientid will be returning
8611 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8612 * be in some phase of session reset.
8613 *
8614 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8615 */
NeilBrowna52458b2018-12-03 11:30:31 +11008616int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008617{
8618 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8619 int status;
8620
8621 /* try SP4_MACH_CRED if krb5i/p */
8622 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8623 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008624 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008625 if (!status)
8626 return 0;
8627 }
8628
8629 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008630 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008631}
8632
Andy Adamson04fa2c62016-09-09 09:22:29 -04008633/**
8634 * nfs4_test_session_trunk
8635 *
8636 * This is an add_xprt_test() test function called from
8637 * rpc_clnt_setup_test_and_add_xprt.
8638 *
8639 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8640 * and is dereferrenced in nfs4_exchange_id_release
8641 *
8642 * Upon success, add the new transport to the rpc_clnt
8643 *
8644 * @clnt: struct rpc_clnt to get new transport
8645 * @xprt: the rpc_xprt to test
8646 * @data: call data for _nfs4_proc_exchange_id.
8647 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308648void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008649 void *data)
8650{
8651 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008652 struct rpc_task *task;
8653 int status;
8654
Andy Adamson04fa2c62016-09-09 09:22:29 -04008655 u32 sp4_how;
8656
8657 dprintk("--> %s try %s\n", __func__,
8658 xprt->address_strings[RPC_DISPLAY_ADDR]);
8659
8660 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8661
8662 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008663 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8664 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308665 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008666
8667 status = task->tk_status;
8668 if (status == 0)
8669 status = nfs4_detect_session_trunking(adata->clp,
8670 task->tk_msg.rpc_resp, xprt);
8671
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308672 if (status == 0)
8673 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8674
Trond Myklebust9c760d12017-07-31 18:38:50 -04008675 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008676}
8677EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8678
Trond Myklebust66245532012-05-25 17:18:09 -04008679static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008680 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008681{
8682 struct rpc_message msg = {
8683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8684 .rpc_argp = clp,
8685 .rpc_cred = cred,
8686 };
8687 int status;
8688
NeilBrown5a0c2572019-05-30 10:41:28 +10008689 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8690 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008691 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008692 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008693 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008694 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8695 return status;
8696}
8697
8698static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008699 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008700{
8701 unsigned int loop;
8702 int ret;
8703
8704 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8705 ret = _nfs4_proc_destroy_clientid(clp, cred);
8706 switch (ret) {
8707 case -NFS4ERR_DELAY:
8708 case -NFS4ERR_CLIENTID_BUSY:
8709 ssleep(1);
8710 break;
8711 default:
8712 return ret;
8713 }
8714 }
8715 return 0;
8716}
8717
8718int nfs4_destroy_clientid(struct nfs_client *clp)
8719{
NeilBrowna52458b2018-12-03 11:30:31 +11008720 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008721 int ret = 0;
8722
8723 if (clp->cl_mvops->minor_version < 1)
8724 goto out;
8725 if (clp->cl_exchange_flags == 0)
8726 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008727 if (clp->cl_preserve_clid)
8728 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008729 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008730 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008731 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008732 switch (ret) {
8733 case 0:
8734 case -NFS4ERR_STALE_CLIENTID:
8735 clp->cl_exchange_flags = 0;
8736 }
8737out:
8738 return ret;
8739}
8740
Donald Buczek0efb01b2019-07-07 21:26:08 +02008741#endif /* CONFIG_NFS_V4_1 */
8742
Andy Adamson2050f0c2009-04-01 09:22:30 -04008743struct nfs4_get_lease_time_data {
8744 struct nfs4_get_lease_time_args *args;
8745 struct nfs4_get_lease_time_res *res;
8746 struct nfs_client *clp;
8747};
8748
8749static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8750 void *calldata)
8751{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008752 struct nfs4_get_lease_time_data *data =
8753 (struct nfs4_get_lease_time_data *)calldata;
8754
8755 dprintk("--> %s\n", __func__);
8756 /* just setup sequence, do not trigger session recovery
8757 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008758 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008759 &data->args->la_seq_args,
8760 &data->res->lr_seq_res,
8761 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008762 dprintk("<-- %s\n", __func__);
8763}
8764
8765/*
8766 * Called from nfs4_state_manager thread for session setup, so don't recover
8767 * from sequence operation or clientid errors.
8768 */
8769static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8770{
8771 struct nfs4_get_lease_time_data *data =
8772 (struct nfs4_get_lease_time_data *)calldata;
8773
8774 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008775 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008776 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008777 switch (task->tk_status) {
8778 case -NFS4ERR_DELAY:
8779 case -NFS4ERR_GRACE:
8780 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8781 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8782 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008783 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008784 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008785 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008786 return;
8787 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008788 dprintk("<-- %s\n", __func__);
8789}
8790
Trond Myklebust17280172012-03-11 13:11:00 -04008791static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008792 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8793 .rpc_call_done = nfs4_get_lease_time_done,
8794};
8795
8796int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8797{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008798 struct nfs4_get_lease_time_args args;
8799 struct nfs4_get_lease_time_res res = {
8800 .lr_fsinfo = fsinfo,
8801 };
8802 struct nfs4_get_lease_time_data data = {
8803 .args = &args,
8804 .res = &res,
8805 .clp = clp,
8806 };
8807 struct rpc_message msg = {
8808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8809 .rpc_argp = &args,
8810 .rpc_resp = &res,
8811 };
8812 struct rpc_task_setup task_setup = {
8813 .rpc_client = clp->cl_rpcclient,
8814 .rpc_message = &msg,
8815 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008816 .callback_data = &data,
8817 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008818 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008819
Anna Schumakerfba83f32018-05-04 16:22:50 -04008820 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008821 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008822}
8823
Donald Buczek0efb01b2019-07-07 21:26:08 +02008824#ifdef CONFIG_NFS_V4_1
8825
Andy Adamsonfc931582009-04-01 09:22:31 -04008826/*
8827 * Initialize the values to be used by the client in CREATE_SESSION
8828 * If nfs4_init_session set the fore channel request and response sizes,
8829 * use them.
8830 *
8831 * Set the back channel max_resp_sz_cached to zero to force the client to
8832 * always set csa_cachethis to FALSE because the current implementation
8833 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8834 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008835static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8836 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008837{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008838 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008839 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008840 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008841
Andy Adamson18aad3d2013-06-26 12:21:49 -04008842 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8843 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8844
Andy Adamsonfc931582009-04-01 09:22:31 -04008845 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008846 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8847 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008848 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008849 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008850
8851 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008852 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008853 __func__,
8854 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008855 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008856
8857 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008858 args->bc_attrs.max_rqst_sz = max_bc_payload;
8859 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008860 args->bc_attrs.max_resp_sz_cached = 0;
8861 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008862 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008863 if (args->bc_attrs.max_reqs > max_bc_slots)
8864 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008865
8866 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8867 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8868 __func__,
8869 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8870 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8871 args->bc_attrs.max_reqs);
8872}
8873
Trond Myklebust79969dd2015-02-18 11:30:18 -08008874static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8875 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008876{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008877 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008878 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008879
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008880 if (rcvd->max_resp_sz > sent->max_resp_sz)
8881 return -EINVAL;
8882 /*
8883 * Our requested max_ops is the minimum we need; we're not
8884 * prepared to break up compounds into smaller pieces than that.
8885 * So, no point even trying to continue if the server won't
8886 * cooperate:
8887 */
8888 if (rcvd->max_ops < sent->max_ops)
8889 return -EINVAL;
8890 if (rcvd->max_reqs == 0)
8891 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008892 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8893 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008894 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008895}
8896
Trond Myklebust79969dd2015-02-18 11:30:18 -08008897static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8898 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008899{
8900 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008901 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008902
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008903 if (!(res->flags & SESSION4_BACK_CHAN))
8904 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008905 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8906 return -EINVAL;
8907 if (rcvd->max_resp_sz < sent->max_resp_sz)
8908 return -EINVAL;
8909 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8910 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008911 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008912 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008913 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008914 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008915out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008916 return 0;
8917}
Andy Adamson8d353012009-04-01 09:22:32 -04008918
Andy Adamson8d353012009-04-01 09:22:32 -04008919static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008920 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008921{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008922 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008923
Trond Myklebust79969dd2015-02-18 11:30:18 -08008924 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008925 if (ret)
8926 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008927 return nfs4_verify_back_channel_attrs(args, res);
8928}
8929
8930static void nfs4_update_session(struct nfs4_session *session,
8931 struct nfs41_create_session_res *res)
8932{
8933 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008934 /* Mark client id and session as being confirmed */
8935 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8936 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008937 session->flags = res->flags;
8938 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008939 if (res->flags & SESSION4_BACK_CHAN)
8940 memcpy(&session->bc_attrs, &res->bc_attrs,
8941 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008942}
8943
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008944static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008945 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008946{
8947 struct nfs4_session *session = clp->cl_session;
8948 struct nfs41_create_session_args args = {
8949 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008950 .clientid = clp->cl_clientid,
8951 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008952 .cb_program = NFS4_CALLBACK,
8953 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008954 struct nfs41_create_session_res res;
8955
Andy Adamsonfc931582009-04-01 09:22:31 -04008956 struct rpc_message msg = {
8957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8958 .rpc_argp = &args,
8959 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008960 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008961 };
8962 int status;
8963
Chuck Lever6b26cc82016-05-02 14:40:40 -04008964 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008965 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008966
NeilBrown5a0c2572019-05-30 10:41:28 +10008967 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8968 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008969 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008970
Trond Myklebustb519d402016-09-11 14:50:01 -04008971 switch (status) {
8972 case -NFS4ERR_STALE_CLIENTID:
8973 case -NFS4ERR_DELAY:
8974 case -ETIMEDOUT:
8975 case -EACCES:
8976 case -EAGAIN:
8977 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08008978 }
Trond Myklebustb519d402016-09-11 14:50:01 -04008979
8980 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008981 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008982 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008983 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008984 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008985 if (status)
8986 goto out;
8987 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008988 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008989out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008990 return status;
8991}
8992
8993/*
8994 * Issues a CREATE_SESSION operation to the server.
8995 * It is the responsibility of the caller to verify the session is
8996 * expired before calling this routine.
8997 */
NeilBrowna52458b2018-12-03 11:30:31 +11008998int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008999{
9000 int status;
9001 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04009002 struct nfs4_session *session = clp->cl_session;
9003
9004 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9005
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009006 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04009007 if (status)
9008 goto out;
9009
Andy Adamsonaacd5532011-11-09 13:58:21 -05009010 /* Init or reset the session slot tables */
9011 status = nfs4_setup_session_slot_tables(session);
9012 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009013 if (status)
9014 goto out;
9015
9016 ptr = (unsigned *)&session->sess_id.data[0];
9017 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9018 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04009019out:
9020 dprintk("<-- %s\n", __func__);
9021 return status;
9022}
9023
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009024/*
9025 * Issue the over-the-wire RPC DESTROY_SESSION.
9026 * The caller must serialize access to this routine.
9027 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009028int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11009029 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009030{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009031 struct rpc_message msg = {
9032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9033 .rpc_argp = session,
9034 .rpc_cred = cred,
9035 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009036 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009037
9038 dprintk("--> nfs4_proc_destroy_session\n");
9039
9040 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05009041 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9042 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009043
NeilBrown5a0c2572019-05-30 10:41:28 +10009044 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9045 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009046 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009047
9048 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04009049 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009050 "Session has been destroyed regardless...\n", status);
9051
9052 dprintk("<-- nfs4_proc_destroy_session\n");
9053 return status;
9054}
9055
Trond Myklebust7b38c362012-05-23 13:23:31 -04009056/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009057 * Renew the cl_session lease.
9058 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009059struct nfs4_sequence_data {
9060 struct nfs_client *clp;
9061 struct nfs4_sequence_args args;
9062 struct nfs4_sequence_res res;
9063};
9064
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009065static void nfs41_sequence_release(void *data)
9066{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009067 struct nfs4_sequence_data *calldata = data;
9068 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009069
Elena Reshetova212bf412017-10-20 12:53:38 +03009070 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009071 nfs4_schedule_state_renewal(clp);
9072 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009073 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009074}
9075
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009076static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9077{
9078 switch(task->tk_status) {
9079 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009080 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9081 return -EAGAIN;
9082 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009083 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009084 }
9085 return 0;
9086}
9087
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009088static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009089{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009090 struct nfs4_sequence_data *calldata = data;
9091 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009092
Trond Myklebust14516c32010-07-31 14:29:06 -04009093 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9094 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009095
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009096 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009097 if (task->tk_status < 0) {
9098 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009099 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009100 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009101
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009102 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9103 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009104 return;
9105 }
9106 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009107 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08009108out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009109 dprintk("<-- %s\n", __func__);
9110}
9111
9112static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9113{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009114 struct nfs4_sequence_data *calldata = data;
9115 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009116 struct nfs4_sequence_args *args;
9117 struct nfs4_sequence_res *res;
9118
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009119 args = task->tk_msg.rpc_argp;
9120 res = task->tk_msg.rpc_resp;
9121
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009122 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009123}
9124
9125static const struct rpc_call_ops nfs41_sequence_ops = {
9126 .rpc_call_done = nfs41_sequence_call_done,
9127 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009128 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009129};
9130
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009131static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009132 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009133 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009134 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009135{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009136 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009137 struct rpc_message msg = {
9138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9139 .rpc_cred = cred,
9140 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009141 struct rpc_task_setup task_setup_data = {
9142 .rpc_client = clp->cl_rpcclient,
9143 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009144 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04009145 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009146 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009147 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009148
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009149 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009150 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009151 goto out_err;
9152
9153 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009154 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009155 if (calldata == NULL)
9156 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009157 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009158 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009159 msg.rpc_argp = &calldata->args;
9160 msg.rpc_resp = &calldata->res;
9161 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009162 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009163
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009164 ret = rpc_run_task(&task_setup_data);
9165 if (IS_ERR(ret))
9166 goto out_err;
9167 return ret;
9168out_put_clp:
9169 nfs_put_client(clp);
9170out_err:
9171 nfs41_release_slot(slot);
9172 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009173}
9174
NeilBrowna52458b2018-12-03 11:30:31 +11009175static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009176{
9177 struct rpc_task *task;
9178 int ret = 0;
9179
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009180 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009181 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009182 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009183 if (IS_ERR(task))
9184 ret = PTR_ERR(task);
9185 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009186 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009187 dprintk("<-- %s status=%d\n", __func__, ret);
9188 return ret;
9189}
9190
NeilBrowna52458b2018-12-03 11:30:31 +11009191static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009192{
9193 struct rpc_task *task;
9194 int ret;
9195
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009196 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009197 if (IS_ERR(task)) {
9198 ret = PTR_ERR(task);
9199 goto out;
9200 }
9201 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009202 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009203 ret = task->tk_status;
9204 rpc_put_task(task);
9205out:
9206 dprintk("<-- %s status=%d\n", __func__, ret);
9207 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009208}
9209
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009210struct nfs4_reclaim_complete_data {
9211 struct nfs_client *clp;
9212 struct nfs41_reclaim_complete_args arg;
9213 struct nfs41_reclaim_complete_res res;
9214};
9215
9216static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9217{
9218 struct nfs4_reclaim_complete_data *calldata = data;
9219
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009220 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009221 &calldata->arg.seq_args,
9222 &calldata->res.seq_res,
9223 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009224}
9225
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009226static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9227{
9228 switch(task->tk_status) {
9229 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009230 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009231 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009232 case -NFS4ERR_COMPLETE_ALREADY:
9233 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9234 break;
9235 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009236 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009237 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009238 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009239 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009240 case -NFS4ERR_BADSESSION:
9241 case -NFS4ERR_DEADSESSION:
9242 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009243 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009244 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009245 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009246 }
9247 return 0;
9248}
9249
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009250static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9251{
9252 struct nfs4_reclaim_complete_data *calldata = data;
9253 struct nfs_client *clp = calldata->clp;
9254 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9255
9256 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04009257 if (!nfs41_sequence_done(task, res))
9258 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009259
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009260 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009261 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9262 rpc_restart_call_prepare(task);
9263 return;
9264 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009265 dprintk("<-- %s\n", __func__);
9266}
9267
9268static void nfs4_free_reclaim_complete_data(void *data)
9269{
9270 struct nfs4_reclaim_complete_data *calldata = data;
9271
9272 kfree(calldata);
9273}
9274
9275static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9276 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9277 .rpc_call_done = nfs4_reclaim_complete_done,
9278 .rpc_release = nfs4_free_reclaim_complete_data,
9279};
9280
9281/*
9282 * Issue a global reclaim complete.
9283 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009284static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009285 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009286{
9287 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009288 struct rpc_message msg = {
9289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009290 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009291 };
9292 struct rpc_task_setup task_setup_data = {
9293 .rpc_client = clp->cl_rpcclient,
9294 .rpc_message = &msg,
9295 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009296 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009297 };
9298 int status = -ENOMEM;
9299
9300 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009301 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009302 if (calldata == NULL)
9303 goto out;
9304 calldata->clp = clp;
9305 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009306
Anna Schumakerfba83f32018-05-04 16:22:50 -04009307 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009308 msg.rpc_argp = &calldata->arg;
9309 msg.rpc_resp = &calldata->res;
9310 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009311 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009312out:
9313 dprintk("<-- %s status=%d\n", __func__, status);
9314 return status;
9315}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009316
9317static void
9318nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9319{
9320 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009321 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009322
9323 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009324 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009325 &lgp->res.seq_res, task);
9326 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009327}
9328
9329static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9330{
9331 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009332
9333 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009334 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009335 dprintk("<-- %s\n", __func__);
9336}
9337
9338static int
9339nfs4_layoutget_handle_exception(struct rpc_task *task,
9340 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9341{
Trond Myklebustee314c22012-10-01 17:25:48 -07009342 struct inode *inode = lgp->args.inode;
9343 struct nfs_server *server = NFS_SERVER(inode);
9344 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009345 int nfs4err = task->tk_status;
9346 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009347 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009348
Boaz Harroshed7e5422014-01-22 20:34:54 +02009349 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009350
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009351 nfs4_sequence_free_slot(&lgp->res.seq_res);
9352
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009353 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009354 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009355 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009356
9357 /*
9358 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9359 * on the file. set tk_status to -ENODATA to tell upper layer to
9360 * retry go inband.
9361 */
9362 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009363 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009364 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009365 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009366 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9367 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9368 */
9369 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009370 status = -EOVERFLOW;
9371 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009372 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009373 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009374 * (or clients) writing to the same RAID stripe except when
9375 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009376 *
9377 * Treat it like we would RECALLCONFLICT -- we retry for a little
9378 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009379 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009380 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009381 if (lgp->args.minlength == 0) {
9382 status = -EOVERFLOW;
9383 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009384 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009385 status = -EBUSY;
9386 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009387 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009388 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009389 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009390 case -NFS4ERR_DELEG_REVOKED:
9391 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009392 case -NFS4ERR_EXPIRED:
9393 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009394 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009395 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009396 lo = NFS_I(inode)->layout;
9397 /* If the open stateid was bad, then recover it. */
9398 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009399 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009400 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009401 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009402 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009403 break;
9404 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009405
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009406 /*
9407 * Mark the bad layout state as invalid, then retry
9408 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009409 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009410 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009411 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009412 pnfs_free_lseg_list(&head);
9413 status = -EAGAIN;
9414 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009415 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009416
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009417 err = nfs4_handle_exception(server, nfs4err, exception);
9418 if (!status) {
9419 if (exception->retry)
9420 status = -EAGAIN;
9421 else
9422 status = err;
9423 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009424out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009425 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009426 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009427}
9428
Fred Isamandacb4522016-09-19 17:47:09 -04009429size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009430{
9431 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9432 return nfs_page_array_len(0, max_resp_sz);
9433}
9434
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009435static void nfs4_layoutget_release(void *calldata)
9436{
9437 struct nfs4_layoutget *lgp = calldata;
9438
9439 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009440 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009441 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009442 dprintk("<-- %s\n", __func__);
9443}
9444
9445static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9446 .rpc_call_prepare = nfs4_layoutget_prepare,
9447 .rpc_call_done = nfs4_layoutget_done,
9448 .rpc_release = nfs4_layoutget_release,
9449};
9450
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009451struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009452nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009453{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009454 struct inode *inode = lgp->args.inode;
9455 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009456 struct rpc_task *task;
9457 struct rpc_message msg = {
9458 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9459 .rpc_argp = &lgp->args,
9460 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009461 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009462 };
9463 struct rpc_task_setup task_setup_data = {
9464 .rpc_client = server->client,
9465 .rpc_message = &msg,
9466 .callback_ops = &nfs4_layoutget_call_ops,
9467 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009468 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009469 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009470 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009471 struct nfs4_exception exception = {
9472 .inode = inode,
9473 .timeout = *timeout,
9474 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009475 int status = 0;
9476
9477 dprintk("--> %s\n", __func__);
9478
Peng Tao4bd5a982014-11-17 11:05:17 +08009479 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9480 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9481
Anna Schumakerfba83f32018-05-04 16:22:50 -04009482 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009483
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009484 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009485
Anna Schumaker820bf852017-01-11 15:01:43 -05009486 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009487 if (status != 0)
9488 goto out;
9489
Trond Myklebust18c07782019-02-13 07:55:31 -05009490 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009491 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9492 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009493 } else if (lgp->res.layoutp->len == 0) {
9494 status = -EAGAIN;
9495 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009496 } else
9497 lseg = pnfs_layout_process(lgp);
9498out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009499 trace_nfs4_layoutget(lgp->args.ctx,
9500 &lgp->args.range,
9501 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009502 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009503 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009504
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009505 rpc_put_task(task);
9506 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009507 if (status)
9508 return ERR_PTR(status);
9509 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009510}
9511
Benny Halevycbe82602011-05-22 19:52:37 +03009512static void
9513nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9514{
9515 struct nfs4_layoutreturn *lrp = calldata;
9516
9517 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009518 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009519 &lrp->args.seq_args,
9520 &lrp->res.seq_res,
9521 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009522 if (!pnfs_layout_is_valid(lrp->args.layout))
9523 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009524}
9525
9526static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9527{
9528 struct nfs4_layoutreturn *lrp = calldata;
9529 struct nfs_server *server;
9530
9531 dprintk("--> %s\n", __func__);
9532
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009533 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009534 return;
9535
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009536 /*
9537 * Was there an RPC level error? Assume the call succeeded,
9538 * and that we need to release the layout
9539 */
9540 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9541 lrp->res.lrs_present = 0;
9542 return;
9543 }
9544
Benny Halevycbe82602011-05-22 19:52:37 +03009545 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009546 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009547 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009548 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009549 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009550 lrp->args.inode))
9551 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009552 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009553 default:
9554 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009555 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009556 case 0:
9557 break;
9558 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009559 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009560 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009561 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009562 }
Benny Halevycbe82602011-05-22 19:52:37 +03009563 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009564 return;
9565out_restart:
9566 task->tk_status = 0;
9567 nfs4_sequence_free_slot(&lrp->res.seq_res);
9568 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009569}
9570
9571static void nfs4_layoutreturn_release(void *calldata)
9572{
9573 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009574 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009575
9576 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009577 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009578 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009579 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009580 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9581 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009582 pnfs_put_layout_hdr(lrp->args.layout);
9583 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009584 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009585 kfree(calldata);
9586 dprintk("<-- %s\n", __func__);
9587}
9588
9589static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9590 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9591 .rpc_call_done = nfs4_layoutreturn_done,
9592 .rpc_release = nfs4_layoutreturn_release,
9593};
9594
Peng Tao6c166052014-11-17 09:30:40 +08009595int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009596{
9597 struct rpc_task *task;
9598 struct rpc_message msg = {
9599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9600 .rpc_argp = &lrp->args,
9601 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009602 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009603 };
9604 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009605 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009606 .rpc_message = &msg,
9607 .callback_ops = &nfs4_layoutreturn_call_ops,
9608 .callback_data = lrp,
9609 };
Peng Tao6c166052014-11-17 09:30:40 +08009610 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009611
Andrew Elble99ade3c2015-12-02 09:39:51 -05009612 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9613 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9614 &task_setup_data.rpc_client, &msg);
9615
Benny Halevycbe82602011-05-22 19:52:37 +03009616 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009617 if (!sync) {
9618 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9619 if (!lrp->inode) {
9620 nfs4_layoutreturn_release(lrp);
9621 return -EAGAIN;
9622 }
9623 task_setup_data.flags |= RPC_TASK_ASYNC;
9624 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009625 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009626 task = rpc_run_task(&task_setup_data);
9627 if (IS_ERR(task))
9628 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009629 if (sync)
9630 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009631 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009632 dprintk("<-- %s status=%d\n", __func__, status);
9633 rpc_put_task(task);
9634 return status;
9635}
9636
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009637static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009638_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9639 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009640 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009641{
9642 struct nfs4_getdeviceinfo_args args = {
9643 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009644 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9645 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009646 };
9647 struct nfs4_getdeviceinfo_res res = {
9648 .pdev = pdev,
9649 };
9650 struct rpc_message msg = {
9651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9652 .rpc_argp = &args,
9653 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009654 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009655 };
9656 int status;
9657
9658 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009659 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009660 if (res.notification & ~args.notify_types)
9661 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009662 if (res.notification != args.notify_types)
9663 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009664
Trond Myklebustcac1d3a2020-12-16 16:31:26 -05009665 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9666
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009667 dprintk("<-- %s status=%d\n", __func__, status);
9668
9669 return status;
9670}
9671
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009672int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9673 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009674 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009675{
9676 struct nfs4_exception exception = { };
9677 int err;
9678
9679 do {
9680 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009681 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009682 &exception);
9683 } while (exception.retry);
9684 return err;
9685}
9686EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9687
Andy Adamson863a3c62011-03-23 13:27:54 +00009688static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9689{
9690 struct nfs4_layoutcommit_data *data = calldata;
9691 struct nfs_server *server = NFS_SERVER(data->args.inode);
9692
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009693 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009694 &data->args.seq_args,
9695 &data->res.seq_res,
9696 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009697}
9698
9699static void
9700nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9701{
9702 struct nfs4_layoutcommit_data *data = calldata;
9703 struct nfs_server *server = NFS_SERVER(data->args.inode);
9704
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009705 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009706 return;
9707
9708 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009709 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9710 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9711 case -NFS4ERR_BADLAYOUT: /* no layout */
9712 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009713 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009714 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009715 break;
9716 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009717 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009718 rpc_restart_call_prepare(task);
9719 return;
9720 }
9721 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009722}
9723
9724static void nfs4_layoutcommit_release(void *calldata)
9725{
9726 struct nfs4_layoutcommit_data *data = calldata;
9727
Andy Adamsondb29c082011-07-30 20:52:38 -04009728 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009729 nfs_post_op_update_inode_force_wcc(data->args.inode,
9730 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009731 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009732 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009733 kfree(data);
9734}
9735
9736static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9737 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9738 .rpc_call_done = nfs4_layoutcommit_done,
9739 .rpc_release = nfs4_layoutcommit_release,
9740};
9741
9742int
Andy Adamsonef311532011-03-12 02:58:10 -05009743nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009744{
9745 struct rpc_message msg = {
9746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9747 .rpc_argp = &data->args,
9748 .rpc_resp = &data->res,
9749 .rpc_cred = data->cred,
9750 };
9751 struct rpc_task_setup task_setup_data = {
9752 .task = &data->task,
9753 .rpc_client = NFS_CLIENT(data->args.inode),
9754 .rpc_message = &msg,
9755 .callback_ops = &nfs4_layoutcommit_ops,
9756 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009757 };
9758 struct rpc_task *task;
9759 int status = 0;
9760
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009761 dprintk("NFS: initiating layoutcommit call. sync %d "
9762 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009763 data->args.lastbytewritten,
9764 data->args.inode->i_ino);
9765
Trond Myklebust472e2592015-02-05 16:50:30 -05009766 if (!sync) {
9767 data->inode = nfs_igrab_and_active(data->args.inode);
9768 if (data->inode == NULL) {
9769 nfs4_layoutcommit_release(data);
9770 return -EAGAIN;
9771 }
9772 task_setup_data.flags = RPC_TASK_ASYNC;
9773 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009774 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009775 task = rpc_run_task(&task_setup_data);
9776 if (IS_ERR(task))
9777 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009778 if (sync)
9779 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009780 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009781 dprintk("%s: status %d\n", __func__, status);
9782 rpc_put_task(task);
9783 return status;
9784}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009785
Trond Myklebust302fad72019-02-18 13:32:38 -05009786/*
Andy Adamson97431202013-08-08 10:57:56 -04009787 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9788 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9789 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009790static int
9791_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009792 struct nfs_fsinfo *info,
9793 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009794{
9795 struct nfs41_secinfo_no_name_args args = {
9796 .style = SECINFO_STYLE_CURRENT_FH,
9797 };
9798 struct nfs4_secinfo_res res = {
9799 .flavors = flavors,
9800 };
9801 struct rpc_message msg = {
9802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9803 .rpc_argp = &args,
9804 .rpc_resp = &res,
9805 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009806 struct nfs4_call_sync_data data = {
9807 .seq_server = server,
9808 .seq_args = &args.seq_args,
9809 .seq_res = &res.seq_res,
9810 };
9811 struct rpc_task_setup task_setup = {
9812 .rpc_client = server->client,
9813 .rpc_message = &msg,
9814 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9815 .callback_data = &data,
9816 .flags = RPC_TASK_NO_ROUND_ROBIN,
9817 };
NeilBrowna52458b2018-12-03 11:30:31 +11009818 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009819 int status;
9820
9821 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009822 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009823
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009824 cred = nfs4_get_clid_cred(server->nfs_client);
9825 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009826 }
9827
9828 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009829 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9830 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009831 dprintk("<-- %s status=%d\n", __func__, status);
9832
NeilBrowna52458b2018-12-03 11:30:31 +11009833 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009834
9835 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009836}
9837
9838static int
9839nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9840 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9841{
Trond Myklebust0688e642019-04-07 13:59:09 -04009842 struct nfs4_exception exception = {
9843 .interruptible = true,
9844 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009845 int err;
9846 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009847 /* first try using integrity protection */
9848 err = -NFS4ERR_WRONGSEC;
9849
9850 /* try to use integrity protection with machine cred */
9851 if (_nfs4_is_integrity_protected(server->nfs_client))
9852 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9853 flavors, true);
9854
9855 /*
9856 * if unable to use integrity protection, or SECINFO with
9857 * integrity protection returns NFS4ERR_WRONGSEC (which is
9858 * disallowed by spec, but exists in deployed servers) use
9859 * the current filesystem's rpc_client and the user cred.
9860 */
9861 if (err == -NFS4ERR_WRONGSEC)
9862 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9863 flavors, false);
9864
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009865 switch (err) {
9866 case 0:
9867 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009868 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009869 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009870 default:
9871 err = nfs4_handle_exception(server, err, &exception);
9872 }
9873 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009874out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009875 return err;
9876}
9877
9878static int
9879nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9880 struct nfs_fsinfo *info)
9881{
9882 int err;
9883 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009884 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009885 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009886 struct nfs4_secinfo4 *secinfo;
9887 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009888
9889 page = alloc_page(GFP_KERNEL);
9890 if (!page) {
9891 err = -ENOMEM;
9892 goto out;
9893 }
9894
9895 flavors = page_address(page);
9896 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9897
9898 /*
9899 * Fall back on "guess and check" method if
9900 * the server doesn't support SECINFO_NO_NAME
9901 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009902 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009903 err = nfs4_find_root_sec(server, fhandle, info);
9904 goto out_freepage;
9905 }
9906 if (err)
9907 goto out_freepage;
9908
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009909 for (i = 0; i < flavors->num_flavors; i++) {
9910 secinfo = &flavors->flavors[i];
9911
9912 switch (secinfo->flavor) {
9913 case RPC_AUTH_NULL:
9914 case RPC_AUTH_UNIX:
9915 case RPC_AUTH_GSS:
9916 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9917 &secinfo->flavor_info);
9918 break;
9919 default:
9920 flavor = RPC_AUTH_MAXFLAVOR;
9921 break;
9922 }
9923
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009924 if (!nfs_auth_info_match(&server->auth_info, flavor))
9925 flavor = RPC_AUTH_MAXFLAVOR;
9926
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009927 if (flavor != RPC_AUTH_MAXFLAVOR) {
9928 err = nfs4_lookup_root_sec(server, fhandle,
9929 info, flavor);
9930 if (!err)
9931 break;
9932 }
9933 }
9934
9935 if (flavor == RPC_AUTH_MAXFLAVOR)
9936 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009937
9938out_freepage:
9939 put_page(page);
9940 if (err == -EACCES)
9941 return -EPERM;
9942out:
9943 return err;
9944}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009945
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009946static int _nfs41_test_stateid(struct nfs_server *server,
9947 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009948 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009949{
9950 int status;
9951 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009952 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009953 };
9954 struct nfs41_test_stateid_res res;
9955 struct rpc_message msg = {
9956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9957 .rpc_argp = &args,
9958 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009959 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009960 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009961 struct rpc_clnt *rpc_client = server->client;
9962
9963 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9964 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009965
Chuck Lever38527b12012-07-11 16:30:23 -04009966 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009967 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009968 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009969 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009970 if (status != NFS_OK) {
9971 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009972 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009973 }
9974 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009975 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009976}
9977
Trond Myklebust43912bb2016-09-22 13:38:56 -04009978static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9979 int err, struct nfs4_exception *exception)
9980{
9981 exception->retry = 0;
9982 switch(err) {
9983 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009984 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009985 nfs4_handle_exception(server, err, exception);
9986 break;
9987 case -NFS4ERR_BADSESSION:
9988 case -NFS4ERR_BADSLOT:
9989 case -NFS4ERR_BAD_HIGH_SLOT:
9990 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9991 case -NFS4ERR_DEADSESSION:
9992 nfs4_do_handle_exception(server, err, exception);
9993 }
9994}
9995
Chuck Lever38527b12012-07-11 16:30:23 -04009996/**
9997 * nfs41_test_stateid - perform a TEST_STATEID operation
9998 *
9999 * @server: server / transport on which to perform the operation
10000 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010001 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -040010002 *
10003 * Returns NFS_OK if the server recognizes that "stateid" is valid.
10004 * Otherwise a negative NFS4ERR value is returned if the operation
10005 * failed or the state ID is not currently valid.
10006 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010007static int nfs41_test_stateid(struct nfs_server *server,
10008 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010009 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010010{
Trond Myklebust0688e642019-04-07 13:59:09 -040010011 struct nfs4_exception exception = {
10012 .interruptible = true,
10013 };
Bryan Schumaker7d974792011-06-02 14:59:08 -040010014 int err;
10015 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010016 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -040010017 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -040010018 } while (exception.retry);
10019 return err;
10020}
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010021
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010022struct nfs_free_stateid_data {
10023 struct nfs_server *server;
10024 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010025 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010026};
10027
10028static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10029{
10030 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -050010031 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010032 &data->args.seq_args,
10033 &data->res.seq_res,
10034 task);
10035}
10036
10037static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10038{
10039 struct nfs_free_stateid_data *data = calldata;
10040
10041 nfs41_sequence_done(task, &data->res.seq_res);
10042
10043 switch (task->tk_status) {
10044 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +100010045 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010046 rpc_restart_call_prepare(task);
10047 }
10048}
10049
10050static void nfs41_free_stateid_release(void *calldata)
10051{
10052 kfree(calldata);
10053}
10054
Trond Myklebust17f26b12013-08-21 15:48:42 -040010055static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010056 .rpc_call_prepare = nfs41_free_stateid_prepare,
10057 .rpc_call_done = nfs41_free_stateid_done,
10058 .rpc_release = nfs41_free_stateid_release,
10059};
10060
Anna Schumaker2f261022018-05-15 13:03:39 -040010061/**
10062 * nfs41_free_stateid - perform a FREE_STATEID operation
10063 *
10064 * @server: server / transport on which to perform the operation
10065 * @stateid: state ID to release
10066 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010067 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010068 *
10069 * Note: this function is always asynchronous.
10070 */
10071static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010072 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010073 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010074 bool privileged)
10075{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010076 struct rpc_message msg = {
10077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010078 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010079 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010080 struct rpc_task_setup task_setup = {
10081 .rpc_client = server->client,
10082 .rpc_message = &msg,
10083 .callback_ops = &nfs41_free_stateid_ops,
10084 .flags = RPC_TASK_ASYNC,
10085 };
10086 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010087 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010088
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010089 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10090 &task_setup.rpc_client, &msg);
10091
Chuck Lever38527b12012-07-11 16:30:23 -040010092 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010093 data = kmalloc(sizeof(*data), GFP_NOFS);
10094 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010095 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010096 data->server = server;
10097 nfs4_stateid_copy(&data->args.stateid, stateid);
10098
10099 task_setup.callback_data = data;
10100
10101 msg.rpc_argp = &data->args;
10102 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010103 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010104 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010105 if (IS_ERR(task))
10106 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010107 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010108 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010109}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010110
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010111static void
10112nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010113{
NeilBrowna52458b2018-12-03 11:30:31 +110010114 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010115
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010116 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010117 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010118}
10119
Trond Myklebust36281ca2012-03-04 18:13:56 -050010120static bool nfs41_match_stateid(const nfs4_stateid *s1,
10121 const nfs4_stateid *s2)
10122{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010123 if (s1->type != s2->type)
10124 return false;
10125
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010126 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010127 return false;
10128
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010129 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010130 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010131
Anna Schumaker045c5512017-01-11 16:59:48 -050010132 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010133}
10134
Andy Adamson557134a2009-04-01 09:21:53 -040010135#endif /* CONFIG_NFS_V4_1 */
10136
Trond Myklebust36281ca2012-03-04 18:13:56 -050010137static bool nfs4_match_stateid(const nfs4_stateid *s1,
10138 const nfs4_stateid *s2)
10139{
Trond Myklebustf597c532012-03-04 18:13:56 -050010140 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010141}
10142
10143
Trond Myklebust17280172012-03-11 13:11:00 -040010144static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010145 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010146 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010147 .recover_open = nfs4_open_reclaim,
10148 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010149 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010150 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010151};
10152
Andy Adamson591d71c2009-04-01 09:22:47 -040010153#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010154static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010155 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10156 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10157 .recover_open = nfs4_open_reclaim,
10158 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010159 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010160 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010161 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010162};
10163#endif /* CONFIG_NFS_V4_1 */
10164
Trond Myklebust17280172012-03-11 13:11:00 -040010165static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010166 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010167 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010168 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010170 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171};
10172
Andy Adamson591d71c2009-04-01 09:22:47 -040010173#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010174static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010175 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10176 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010177 .recover_open = nfs41_open_expired,
10178 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010179 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010180};
10181#endif /* CONFIG_NFS_V4_1 */
10182
Trond Myklebust17280172012-03-11 13:11:00 -040010183static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010184 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010185 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010186 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010187};
10188
10189#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010190static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010191 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010192 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010193 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010194};
10195#endif
10196
Chuck Leverec011fe2013-10-17 14:12:39 -040010197static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010198 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010199 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010200};
10201
10202#if defined(CONFIG_NFS_V4_1)
10203static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010204 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010205 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010206};
10207#endif /* CONFIG_NFS_V4_1 */
10208
Trond Myklebust97dc1352010-06-16 09:52:26 -040010209static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10210 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010211 .init_caps = NFS_CAP_READDIRPLUS
10212 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010213 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010214 .init_client = nfs40_init_client,
10215 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010216 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010217 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010218 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010219 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010220 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010221 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010222 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10223 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10224 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010225 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010226};
10227
10228#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010229static struct nfs_seqid *
10230nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10231{
10232 return NULL;
10233}
10234
Trond Myklebust97dc1352010-06-16 09:52:26 -040010235static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10236 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010237 .init_caps = NFS_CAP_READDIRPLUS
10238 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010239 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010240 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010241 | NFS_CAP_ATOMIC_OPEN_V1
10242 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010243 .init_client = nfs41_init_client,
10244 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010245 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010246 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010247 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010248 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010249 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010250 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010251 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010252 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10253 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10254 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010255 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010256};
10257#endif
10258
Steve Dickson42c2c422013-05-22 12:50:38 -040010259#if defined(CONFIG_NFS_V4_2)
10260static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10261 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010262 .init_caps = NFS_CAP_READDIRPLUS
10263 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010264 | NFS_CAP_POSIX_LOCK
10265 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010266 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010267 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010268 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010269 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010270 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010271 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010272 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010273 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010274 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010275 | NFS_CAP_CLONE
Anna Schumakerc5675522014-05-28 13:41:22 -040010276 | NFS_CAP_LAYOUTERROR
10277 | NFS_CAP_READ_PLUS,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010278 .init_client = nfs41_init_client,
10279 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010280 .match_stateid = nfs41_match_stateid,
10281 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010282 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010283 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010284 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010285 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010286 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010287 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10288 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10289 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010290 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010291};
10292#endif
10293
Trond Myklebust97dc1352010-06-16 09:52:26 -040010294const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10295 [0] = &nfs_v4_0_minor_ops,
10296#if defined(CONFIG_NFS_V4_1)
10297 [1] = &nfs_v4_1_minor_ops,
10298#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010299#if defined(CONFIG_NFS_V4_2)
10300 [2] = &nfs_v4_2_minor_ops,
10301#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010302};
10303
Trond Myklebust13997822016-07-24 17:10:52 -040010304static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010305{
Frank van der Linden012a2112020-06-23 22:39:03 +000010306 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010307
10308 error = generic_listxattr(dentry, list, size);
10309 if (error < 0)
10310 return error;
10311 if (list) {
10312 list += error;
10313 size -= error;
10314 }
10315
10316 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10317 if (error2 < 0)
10318 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010319
10320 if (list) {
10321 list += error2;
10322 size -= error2;
10323 }
10324
10325 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10326 if (error3 < 0)
10327 return error3;
10328
10329 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010330}
10331
Trond Myklebust17f26b12013-08-21 15:48:42 -040010332static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010333 .create = nfs_create,
10334 .lookup = nfs_lookup,
10335 .atomic_open = nfs_atomic_open,
10336 .link = nfs_link,
10337 .unlink = nfs_unlink,
10338 .symlink = nfs_symlink,
10339 .mkdir = nfs_mkdir,
10340 .rmdir = nfs_rmdir,
10341 .mknod = nfs_mknod,
10342 .rename = nfs_rename,
10343 .permission = nfs_permission,
10344 .getattr = nfs_getattr,
10345 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010346 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010347};
10348
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010349static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010350 .permission = nfs_permission,
10351 .getattr = nfs_getattr,
10352 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010353 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010354};
10355
David Howells509de812006-08-22 20:06:11 -040010356const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010357 .version = 4, /* protocol version */
10358 .dentry_ops = &nfs4_dentry_operations,
10359 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010360 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010361 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010362 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010363 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010364 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010365 .getattr = nfs4_proc_getattr,
10366 .setattr = nfs4_proc_setattr,
10367 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010368 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010369 .access = nfs4_proc_access,
10370 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010371 .create = nfs4_proc_create,
10372 .remove = nfs4_proc_remove,
10373 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010374 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010375 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010376 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010377 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010378 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010379 .link = nfs4_proc_link,
10380 .symlink = nfs4_proc_symlink,
10381 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010382 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010383 .readdir = nfs4_proc_readdir,
10384 .mknod = nfs4_proc_mknod,
10385 .statfs = nfs4_proc_statfs,
10386 .fsinfo = nfs4_proc_fsinfo,
10387 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010388 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010389 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010390 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010391 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010392 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010393 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010394 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010395 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010396 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010397 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010398 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010399 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010400 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010401 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010402 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010403 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010404 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010405 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010406 .create_server = nfs4_create_server,
10407 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010408};
10409
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010410static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010411 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010412 .list = nfs4_xattr_list_nfs4_acl,
10413 .get = nfs4_xattr_get_nfs4_acl,
10414 .set = nfs4_xattr_set_nfs4_acl,
10415};
10416
Frank van der Linden012a2112020-06-23 22:39:03 +000010417#ifdef CONFIG_NFS_V4_2
10418static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10419 .prefix = XATTR_USER_PREFIX,
10420 .get = nfs4_xattr_get_nfs4_user,
10421 .set = nfs4_xattr_set_nfs4_user,
10422};
10423#endif
10424
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010425const struct xattr_handler *nfs4_xattr_handlers[] = {
10426 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010427#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10428 &nfs4_xattr_nfs4_label_handler,
10429#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010430#ifdef CONFIG_NFS_V4_2
10431 &nfs4_xattr_nfs4_user_handler,
10432#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010433 NULL
10434};
10435
Linus Torvalds1da177e2005-04-16 15:20:36 -070010436/*
10437 * Local variables:
10438 * c-basic-offset: 8
10439 * End:
10440 */