blob: 34570a7785faba905d25cdbe7a396152f84c253d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust39d43d12020-10-07 18:24:18 -040066#include "sysfs.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040067#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050068#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000069#include "fscache.h"
Frank van der Linden012a2112020-06-23 22:39:03 +000070#include "nfs42.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Trond Myklebustc6d01c62013-08-09 11:51:26 -040072#include "nfs4trace.h"
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFSDBG_FACILITY NFSDBG_PROC
75
Trond Myklebust30846df2018-04-07 13:44:28 -040076#define NFS4_BITMASK_SZ 3
77
Trond Myklebust2066fe82006-09-15 08:30:46 -040078#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define NFS4_POLL_RETRY_MAX (15*HZ)
80
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020081/* file attributes which can be mapped to nfs attributes */
82#define NFS4_VALID_ATTRS (ATTR_MODE \
83 | ATTR_UID \
84 | ATTR_GID \
85 | ATTR_SIZE \
86 | ATTR_ATIME \
87 | ATTR_MTIME \
88 | ATTR_CTIME \
89 | ATTR_ATIME_SET \
90 | ATTR_MTIME_SET)
91
Trond Myklebustcdd4e682006-01-03 09:55:12 +010092struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080093static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050095static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040096static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110097static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040098 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110099 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -0400100 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400101#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400102static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100103 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400104 struct nfs4_slot *slot,
105 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400106static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100107 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400108static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100109 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400110#endif
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -0400111static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
112 struct nfs_server *server,
113 struct nfs4_label *label);
David Quigleyaa9c2662013-05-22 12:50:44 -0400114
115#ifdef CONFIG_NFS_V4_SECURITY_LABEL
116static inline struct nfs4_label *
117nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
118 struct iattr *sattr, struct nfs4_label *label)
119{
120 int err;
121
122 if (label == NULL)
123 return NULL;
124
125 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
126 return NULL;
127
David Quigleyaa9c2662013-05-22 12:50:44 -0400128 err = security_dentry_init_security(dentry, sattr->ia_mode,
129 &dentry->d_name, (void **)&label->label, &label->len);
130 if (err == 0)
131 return label;
132
133 return NULL;
134}
135static inline void
136nfs4_label_release_security(struct nfs4_label *label)
137{
138 if (label)
139 security_release_secctx(label->label, label->len);
140}
141static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
142{
143 if (label)
144 return server->attr_bitmask;
145
146 return server->attr_bitmask_nl;
147}
148#else
149static inline struct nfs4_label *
150nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
151 struct iattr *sattr, struct nfs4_label *l)
152{ return NULL; }
153static inline void
154nfs4_label_release_security(struct nfs4_label *label)
155{ return; }
156static inline u32 *
157nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
158{ return server->attr_bitmask; }
159#endif
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500162static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Trond Myklebust52567b02009-10-23 14:46:42 -0400164 if (err >= -1000)
165 return err;
166 switch (err) {
167 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500168 case -NFS4ERR_LAYOUTTRYLATER:
169 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400170 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000171 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400172 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000173 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800174 case -NFS4ERR_BADOWNER:
175 case -NFS4ERR_BADNAME:
176 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400177 case -NFS4ERR_SHARE_DENIED:
178 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400179 case -NFS4ERR_MINOR_VERS_MISMATCH:
180 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400181 case -NFS4ERR_FILE_OPEN:
182 return -EBUSY;
Trond Myklebust9fff59e2020-11-02 20:11:32 -0500183 case -NFS4ERR_NOT_SAME:
184 return -ENOTSYNC;
Trond Myklebust52567b02009-10-23 14:46:42 -0400185 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700187 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400188 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400190 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193/*
194 * This is our standard bitmap for GETATTR requests.
195 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400196const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 FATTR4_WORD0_TYPE
198 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_SIZE
200 | FATTR4_WORD0_FSID
201 | FATTR4_WORD0_FILEID,
202 FATTR4_WORD1_MODE
203 | FATTR4_WORD1_NUMLINKS
204 | FATTR4_WORD1_OWNER
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400210 | FATTR4_WORD1_TIME_MODIFY
211 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400212#ifdef CONFIG_NFS_V4_SECURITY_LABEL
213 FATTR4_WORD2_SECURITY_LABEL
214#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
Trond Myklebust1549210f2012-06-05 09:16:47 -0400217static const u32 nfs4_pnfs_open_bitmap[3] = {
218 FATTR4_WORD0_TYPE
219 | FATTR4_WORD0_CHANGE
220 | FATTR4_WORD0_SIZE
221 | FATTR4_WORD0_FSID
222 | FATTR4_WORD0_FILEID,
223 FATTR4_WORD1_MODE
224 | FATTR4_WORD1_NUMLINKS
225 | FATTR4_WORD1_OWNER
226 | FATTR4_WORD1_OWNER_GROUP
227 | FATTR4_WORD1_RAWDEV
228 | FATTR4_WORD1_SPACE_USED
229 | FATTR4_WORD1_TIME_ACCESS
230 | FATTR4_WORD1_TIME_METADATA
231 | FATTR4_WORD1_TIME_MODIFY,
232 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500233#ifdef CONFIG_NFS_V4_SECURITY_LABEL
234 | FATTR4_WORD2_SECURITY_LABEL
235#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400236};
237
Andy Adamsone23008e2012-10-02 21:07:32 -0400238static const u32 nfs4_open_noattr_bitmap[3] = {
239 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400240 | FATTR4_WORD0_FILEID,
241};
242
David Quigleya09df2c2013-05-22 12:50:41 -0400243const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 FATTR4_WORD0_FILES_AVAIL
245 | FATTR4_WORD0_FILES_FREE
246 | FATTR4_WORD0_FILES_TOTAL,
247 FATTR4_WORD1_SPACE_AVAIL
248 | FATTR4_WORD1_SPACE_FREE
249 | FATTR4_WORD1_SPACE_TOTAL
250};
251
David Quigleya09df2c2013-05-22 12:50:41 -0400252const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 FATTR4_WORD0_MAXLINK
254 | FATTR4_WORD0_MAXNAME,
255 0
256};
257
Fred Isamandae100c2011-07-30 20:52:37 -0400258const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 | FATTR4_WORD0_MAXREAD
260 | FATTR4_WORD0_MAXWRITE
261 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700262 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400263 | FATTR4_WORD1_FS_LAYOUT_TYPES,
264 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800265 | FATTR4_WORD2_CLONE_BLKSIZE
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000266 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267};
268
David Quigleya09df2c2013-05-22 12:50:41 -0400269const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500270 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400271 | FATTR4_WORD0_SIZE
272 | FATTR4_WORD0_FSID
273 | FATTR4_WORD0_FILEID
274 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500275 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400276 | FATTR4_WORD1_OWNER_GROUP
277 | FATTR4_WORD1_RAWDEV
278 | FATTR4_WORD1_SPACE_USED
279 | FATTR4_WORD1_TIME_ACCESS
280 | FATTR4_WORD1_TIME_METADATA
281 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400282 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400283};
284
Trond Myklebust30846df2018-04-07 13:44:28 -0400285static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
286 struct inode *inode)
287{
288 unsigned long cache_validity;
289
290 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
291 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
292 return;
293
294 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
295 if (!(cache_validity & NFS_INO_REVAL_FORCED))
296 cache_validity &= ~(NFS_INO_INVALID_CHANGE
297 | NFS_INO_INVALID_SIZE);
298
299 if (!(cache_validity & NFS_INO_INVALID_SIZE))
300 dst[0] &= ~FATTR4_WORD0_SIZE;
301
302 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
303 dst[0] &= ~FATTR4_WORD0_CHANGE;
304}
305
306static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
307 const __u32 *src, struct inode *inode)
308{
309 nfs4_bitmap_copy_adjust(dst, src, inode);
310}
311
Al Virobc4785c2006-10-19 23:28:51 -0700312static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct nfs4_readdir_arg *readdir)
314{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400315 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700316 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000319 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
321 return;
322 }
323
324 readdir->cookie = 0;
325 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
326 if (cookie == 2)
327 return;
328
329 /*
330 * NFSv4 servers do not return entries for '.' and '..'
331 * Therefore, we fake these entries here. We let '.'
332 * have cookie 0 and '..' have cookie 1. Note that
333 * when talking to the server, we always send cookie 0
334 * instead of 1 or 2.
335 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800336 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 if (cookie == 0) {
339 *p++ = xdr_one; /* next */
340 *p++ = xdr_zero; /* cookie, first word */
341 *p++ = xdr_one; /* cookie, second word */
342 *p++ = xdr_one; /* entry len */
343 memcpy(p, ".\0\0\0", 4); /* entry */
344 p++;
345 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400346 *p++ = htonl(attrs); /* bitmap */
347 *p++ = htonl(12); /* attribute buffer length */
348 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000349 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 *p++ = xdr_one; /* next */
353 *p++ = xdr_zero; /* cookie, first word */
354 *p++ = xdr_two; /* cookie, second word */
355 *p++ = xdr_two; /* entry len */
356 memcpy(p, "..\0\0", 4); /* entry */
357 p++;
358 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400359 *p++ = htonl(attrs); /* bitmap */
360 *p++ = htonl(12); /* attribute buffer length */
361 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000362 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 readdir->pgbase = (char *)p - (char *)start;
365 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800366 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Trond Myklebust26d36302016-09-22 13:39:05 -0400369static void nfs4_test_and_free_stateid(struct nfs_server *server,
370 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100371 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400372{
373 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
374
375 ops->test_and_free_expired(server, stateid, cred);
376}
377
378static void __nfs4_free_revoked_stateid(struct nfs_server *server,
379 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100380 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400381{
382 stateid->type = NFS4_REVOKED_STATEID_TYPE;
383 nfs4_test_and_free_stateid(server, stateid, cred);
384}
385
386static void nfs4_free_revoked_stateid(struct nfs_server *server,
387 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100388 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400389{
390 nfs4_stateid tmp;
391
392 nfs4_stateid_copy(&tmp, stateid);
393 __nfs4_free_revoked_stateid(server, &tmp, cred);
394}
395
NeilBrown8478eaa2014-09-18 16:09:27 +1000396static long nfs4_update_delay(long *timeout)
397{
398 long ret;
399 if (!timeout)
400 return NFS4_POLL_RETRY_MAX;
401 if (*timeout <= 0)
402 *timeout = NFS4_POLL_RETRY_MIN;
403 if (*timeout > NFS4_POLL_RETRY_MAX)
404 *timeout = NFS4_POLL_RETRY_MAX;
405 ret = *timeout;
406 *timeout <<= 1;
407 return ret;
408}
409
Trond Myklebust0688e642019-04-07 13:59:09 -0400410static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500411{
Trond Myklebust65de8722008-12-23 15:21:44 -0500412 might_sleep();
413
NeilBrown8478eaa2014-09-18 16:09:27 +1000414 freezable_schedule_timeout_killable_unsafe(
415 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400416 if (!__fatal_signal_pending(current))
417 return 0;
418 return -EINTR;
419}
420
421static int nfs4_delay_interruptible(long *timeout)
422{
423 might_sleep();
424
He Zhe59679d92020-07-06 17:52:24 +0800425 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400426 if (!signal_pending(current))
427 return 0;
428 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
429}
430
431static int nfs4_delay(long *timeout, bool interruptible)
432{
433 if (interruptible)
434 return nfs4_delay_interruptible(timeout);
435 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500436}
437
Trond Myklebust50c80002019-07-11 19:02:18 -0400438static const nfs4_stateid *
439nfs4_recoverable_stateid(const nfs4_stateid *stateid)
440{
441 if (!stateid)
442 return NULL;
443 switch (stateid->type) {
444 case NFS4_OPEN_STATEID_TYPE:
445 case NFS4_LOCK_STATEID_TYPE:
446 case NFS4_DELEGATION_STATEID_TYPE:
447 return stateid;
448 default:
449 break;
450 }
451 return NULL;
452}
453
Trond Myklebust65de8722008-12-23 15:21:44 -0500454/* This is the error handling routine for processes that are allowed
455 * to sleep.
456 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400457static int nfs4_do_handle_exception(struct nfs_server *server,
458 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500459{
460 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500461 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400462 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500463 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500464 int ret = errorcode;
465
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400466 exception->delay = 0;
467 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400469
Trond Myklebust50c80002019-07-11 19:02:18 -0400470 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400471 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400472 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400473
Trond Myklebust65de8722008-12-23 15:21:44 -0500474 switch(errorcode) {
475 case 0:
476 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400477 case -NFS4ERR_BADHANDLE:
478 case -ESTALE:
479 if (inode != NULL && S_ISREG(inode->i_mode))
480 pnfs_destroy_layout(NFS_I(inode));
481 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400482 case -NFS4ERR_DELEG_REVOKED:
483 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400484 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400485 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400486 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400487 if (inode != NULL && stateid != NULL) {
488 nfs_inode_find_state_and_recover(inode,
489 stateid);
490 goto wait_on_recovery;
491 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500492 fallthrough;
Trond Myklebust272289a2016-09-22 13:39:15 -0400493 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400494 if (inode) {
495 int err;
496
497 err = nfs_async_inode_return_delegation(inode,
498 stateid);
499 if (err == 0)
500 goto wait_on_recovery;
501 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
502 exception->retry = 1;
503 break;
504 }
505 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500506 if (state == NULL)
507 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400508 ret = nfs4_schedule_stateid_recovery(server, state);
509 if (ret < 0)
510 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500511 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500512 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500513 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500514 nfs4_schedule_lease_recovery(clp);
515 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400516 case -NFS4ERR_MOVED:
517 ret = nfs4_schedule_migration_recovery(server);
518 if (ret < 0)
519 break;
520 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400521 case -NFS4ERR_LEASE_MOVED:
522 nfs4_schedule_lease_moved_recovery(clp);
523 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500524#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400525 case -NFS4ERR_BADSESSION:
526 case -NFS4ERR_BADSLOT:
527 case -NFS4ERR_BAD_HIGH_SLOT:
528 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
529 case -NFS4ERR_DEADSESSION:
530 case -NFS4ERR_SEQ_FALSE_RETRY:
531 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100532 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400533 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500534#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500535 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500536 if (exception->timeout > HZ) {
537 /* We have retried a decent amount, time to
538 * fail
539 */
540 ret = -EBUSY;
541 break;
542 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500543 fallthrough;
Trond Myklebust65de8722008-12-23 15:21:44 -0500544 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400545 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500546 fallthrough;
Trond Myklebust2598ed32015-09-20 16:10:18 -0400547 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400548 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400549 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400550 exception->delay = 1;
551 return 0;
552
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400553 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500554 case -NFS4ERR_OLD_STATEID:
555 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800556 break;
557 case -NFS4ERR_BADOWNER:
558 /* The following works around a Linux server bug! */
559 case -NFS4ERR_BADNAME:
560 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
561 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
562 exception->retry = 1;
563 printk(KERN_WARNING "NFS: v4 server %s "
564 "does not accept raw "
565 "uid/gids. "
566 "Reenabling the idmapper.\n",
567 server->nfs_client->cl_hostname);
568 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500569 }
570 /* We failed to handle the error */
571 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500572wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400573 exception->recovering = 1;
574 return 0;
575}
576
577/* This is the error handling routine for processes that are allowed
578 * to sleep.
579 */
580int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
581{
582 struct nfs_client *clp = server->nfs_client;
583 int ret;
584
585 ret = nfs4_do_handle_exception(server, errorcode, exception);
586 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400587 ret = nfs4_delay(&exception->timeout,
588 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400589 goto out_retry;
590 }
591 if (exception->recovering) {
592 ret = nfs4_wait_clnt_recover(clp);
593 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
594 return -EIO;
595 goto out_retry;
596 }
597 return ret;
598out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500599 if (ret == 0)
600 exception->retry = 1;
601 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500602}
603
Trond Myklebust037fc982015-09-20 15:51:00 -0400604static int
605nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
606 int errorcode, struct nfs4_exception *exception)
607{
608 struct nfs_client *clp = server->nfs_client;
609 int ret;
610
611 ret = nfs4_do_handle_exception(server, errorcode, exception);
612 if (exception->delay) {
613 rpc_delay(task, nfs4_update_delay(&exception->timeout));
614 goto out_retry;
615 }
616 if (exception->recovering) {
617 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
618 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
619 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
620 goto out_retry;
621 }
622 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
623 ret = -EIO;
624 return ret;
625out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500626 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400627 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500628 /*
629 * For NFS4ERR_MOVED, the client transport will need to
630 * be recomputed after migration recovery has completed.
631 */
632 if (errorcode == -NFS4ERR_MOVED)
633 rpc_task_release_transport(task);
634 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400635 return ret;
636}
637
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400638int
Trond Myklebust037fc982015-09-20 15:51:00 -0400639nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
640 struct nfs4_state *state, long *timeout)
641{
642 struct nfs4_exception exception = {
643 .state = state,
644 };
645
646 if (task->tk_status >= 0)
647 return 0;
648 if (timeout)
649 exception.timeout = *timeout;
650 task->tk_status = nfs4_async_handle_exception(task, server,
651 task->tk_status,
652 &exception);
653 if (exception.delay && timeout)
654 *timeout = exception.timeout;
655 if (exception.retry)
656 return -EAGAIN;
657 return 0;
658}
659
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400660/*
661 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
662 * or 'false' otherwise.
663 */
664static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
665{
666 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500667 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400668}
Trond Myklebust65de8722008-12-23 15:21:44 -0500669
Trond Myklebust452e9352010-07-31 14:29:06 -0400670static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 spin_lock(&clp->cl_lock);
673 if (time_before(clp->cl_last_renewal,timestamp))
674 clp->cl_last_renewal = timestamp;
675 spin_unlock(&clp->cl_lock);
676}
677
Trond Myklebust452e9352010-07-31 14:29:06 -0400678static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
679{
Trond Myklebustbe824162015-07-05 14:50:46 -0400680 struct nfs_client *clp = server->nfs_client;
681
682 if (!nfs4_has_session(clp))
683 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400684}
685
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400686struct nfs4_call_sync_data {
687 const struct nfs_server *seq_server;
688 struct nfs4_sequence_args *seq_args;
689 struct nfs4_sequence_res *seq_res;
690};
691
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800692void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400693 struct nfs4_sequence_res *res, int cache_reply,
694 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400695{
696 args->sa_slot = NULL;
697 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400698 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400699
700 res->sr_slot = NULL;
701}
702
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400703static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400704{
705 struct nfs4_slot *slot = res->sr_slot;
706 struct nfs4_slot_table *tbl;
707
Chuck Lever3bd23842013-08-09 12:49:19 -0400708 tbl = slot->table;
709 spin_lock(&tbl->slot_tbl_lock);
710 if (!nfs41_wake_and_assign_slot(tbl, slot))
711 nfs4_free_slot(tbl, slot);
712 spin_unlock(&tbl->slot_tbl_lock);
713
714 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400715}
716
717static int nfs40_sequence_done(struct rpc_task *task,
718 struct nfs4_sequence_res *res)
719{
720 if (res->sr_slot != NULL)
721 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400722 return 1;
723}
724
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400725#if defined(CONFIG_NFS_V4_1)
726
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400727static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400728{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500729 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400730 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500731 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400732
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400733 if (!slot)
734 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500735 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500736 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500737
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400738 /* Bump the slot sequence number */
739 if (slot->seq_done)
740 slot->seq_nr++;
741 slot->seq_done = 0;
742
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500743 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500744 /* Be nice to the server: try to ensure that the last transmitted
745 * value for highest_user_slotid <= target_highest_slotid
746 */
747 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
748 send_new_highest_used_slotid = true;
749
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500750 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500751 send_new_highest_used_slotid = false;
752 goto out_unlock;
753 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500754 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500755
756 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
757 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500758out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500759 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500760 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400761 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400762 if (waitqueue_active(&tbl->slot_waitq))
763 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400764}
765
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400766static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
767{
768 nfs41_release_slot(res->sr_slot);
769 res->sr_slot = NULL;
770}
771
Trond Myklebust3453d572018-06-20 17:53:34 -0400772static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
773 u32 seqnr)
774{
775 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
776 slot->seq_nr_highest_sent = seqnr;
777}
778static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
779 u32 seqnr)
780{
781 slot->seq_nr_highest_sent = seqnr;
782 slot->seq_nr_last_acked = seqnr;
783}
784
Anna Schumaker913fadc2020-07-08 10:33:40 -0400785static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
786 struct nfs4_slot *slot)
787{
788 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
789 if (!IS_ERR(task))
790 rpc_put_task_async(task);
791}
792
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400793static int nfs41_sequence_process(struct rpc_task *task,
794 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400795{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500796 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500797 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400798 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100799 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500800 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400801
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500802 if (slot == NULL)
803 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400804 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500805 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400806 goto out;
807
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500808 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400809 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500810
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400811 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100812
813 status = res->sr_status;
814 if (task->tk_status == -NFS4ERR_DEADSESSION)
815 status = -NFS4ERR_DEADSESSION;
816
Andy Adamson691daf32009-12-04 15:55:39 -0500817 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100818 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400819 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400820 /* Mark this sequence number as having been acked */
821 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400822 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400823 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500824 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500825 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400826 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
827 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500828 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400829 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500830 case 1:
831 /*
832 * sr_status remains 1 if an RPC level error occurred.
833 * The server may or may not have processed the sequence
834 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500835 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400836 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
837 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500838 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400839 case -NFS4ERR_DELAY:
840 /* The server detected a resend of the RPC call and
841 * returned NFS4ERR_DELAY as per Section 2.10.6.2
842 * of RFC5661.
843 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500844 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400845 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500846 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500847 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400848 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400849 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400850 case -NFS4ERR_RETRY_UNCACHED_REP:
851 case -NFS4ERR_SEQ_FALSE_RETRY:
852 /*
853 * The server thinks we tried to replay a request.
854 * Retry the call after bumping the sequence ID.
855 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400856 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400857 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500858 case -NFS4ERR_BADSLOT:
859 /*
860 * The slot id we used was probably retired. Try again
861 * using a different slot id.
862 */
Trond Myklebust99589102018-06-09 12:50:50 -0400863 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400864 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500865 goto retry_nowait;
866 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400867 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500868 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400869 * Were one or more calls using this slot interrupted?
870 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400871 * transmitted slot sequence number may be too high. However,
872 * if the server did receive the request then it might
873 * accidentally give us a reply with a mismatched operation.
874 * We can sort this out by sending a lone sequence operation
875 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500876 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400877 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
878 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400879 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
880 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
881 res->sr_slot = NULL;
882 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500883 goto retry_nowait;
884 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400885 /*
886 * RFC5661:
887 * A retry might be sent while the original request is
888 * still in progress on the replier. The replier SHOULD
889 * deal with the issue by returning NFS4ERR_DELAY as the
890 * reply to SEQUENCE or CB_SEQUENCE operation, but
891 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
892 *
893 * Restart the search after a delay.
894 */
895 slot->seq_nr = slot->seq_nr_highest_sent;
896 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100897 case -NFS4ERR_BADSESSION:
898 case -NFS4ERR_DEADSESSION:
899 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
900 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400901 default:
902 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400903 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400904 }
905out:
906 /* The session may be reset by one of the error handlers. */
907 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500908out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500909 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400910session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100911 nfs4_schedule_session_recovery(session, status);
912 dprintk("%s ERROR: %d Reset session\n", __func__, status);
913 nfs41_sequence_free_slot(res);
914 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400915retry_new_seq:
916 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500917retry_nowait:
918 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400919 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500920 task->tk_status = 0;
921 ret = 0;
922 }
923 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400924out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400925 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400926 goto out;
927 rpc_delay(task, NFS4_POLL_RETRY_MAX);
928 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400929}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400930
931int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
932{
933 if (!nfs41_sequence_process(task, res))
934 return 0;
935 if (res->sr_slot != NULL)
936 nfs41_sequence_free_slot(res);
937 return 1;
938
939}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500940EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400941
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400942static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
943{
944 if (res->sr_slot == NULL)
945 return 1;
946 if (res->sr_slot->table->session != NULL)
947 return nfs41_sequence_process(task, res);
948 return nfs40_sequence_done(task, res);
949}
950
951static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
952{
953 if (res->sr_slot != NULL) {
954 if (res->sr_slot->table->session != NULL)
955 nfs41_sequence_free_slot(res);
956 else
957 nfs40_sequence_free_slot(res);
958 }
959}
960
Peng Tao2c4b1312014-06-11 05:24:16 +0800961int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400962{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500963 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400964 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400965 if (!res->sr_slot->table->session)
966 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400967 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400968}
Peng Tao2c4b1312014-06-11 05:24:16 +0800969EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400970
Andy Adamsonce5039c2009-04-01 09:22:13 -0400971static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
972{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400973 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400974
Trond Myklebust035168ab2010-06-16 09:52:26 -0400975 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
976
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500977 nfs4_setup_sequence(data->seq_server->nfs_client,
978 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400979}
980
Andy Adamson69ab40c2009-04-01 09:22:19 -0400981static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
982{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400983 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400984
Trond Myklebust14516c32010-07-31 14:29:06 -0400985 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400986}
987
Trond Myklebust17280172012-03-11 13:11:00 -0400988static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400989 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400990 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400991};
992
Chuck Lever3bd23842013-08-09 12:49:19 -0400993#else /* !CONFIG_NFS_V4_1 */
994
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400995static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
996{
997 return nfs40_sequence_done(task, res);
998}
999
1000static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1001{
1002 if (res->sr_slot != NULL)
1003 nfs40_sequence_free_slot(res);
1004}
1005
Peng Tao2c4b1312014-06-11 05:24:16 +08001006int nfs4_sequence_done(struct rpc_task *task,
1007 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001008{
Chuck Lever3bd23842013-08-09 12:49:19 -04001009 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001010}
Peng Tao2c4b1312014-06-11 05:24:16 +08001011EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001012
1013#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001014
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001015static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1016{
1017 res->sr_timestamp = jiffies;
1018 res->sr_status_flags = 0;
1019 res->sr_status = 1;
1020}
1021
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001022static
1023void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1024 struct nfs4_sequence_res *res,
1025 struct nfs4_slot *slot)
1026{
1027 if (!slot)
1028 return;
1029 slot->privileged = args->sa_privileged ? 1 : 0;
1030 args->sa_slot = slot;
1031
1032 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001033}
1034
1035int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001036 struct nfs4_sequence_args *args,
1037 struct nfs4_sequence_res *res,
1038 struct rpc_task *task)
1039{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001040 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001041 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001042 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001043
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001044 /* slot already allocated? */
1045 if (res->sr_slot != NULL)
1046 goto out_start;
1047
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001048 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001049 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001050
Trond Myklebust3453d572018-06-20 17:53:34 -04001051 spin_lock(&tbl->slot_tbl_lock);
1052 /* The state manager will wait until the slot table is empty */
1053 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1054 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001055
Trond Myklebust3453d572018-06-20 17:53:34 -04001056 slot = nfs4_alloc_slot(tbl);
1057 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001058 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001059 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001060 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001061 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001062 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001063
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001064 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001065
Anna Schumakerad05cc02017-01-11 13:37:06 -05001066 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001067out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001068 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001069 rpc_call_start(task);
1070 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001071out_sleep_timeout:
1072 /* Try again in 1/4 second */
1073 if (args->sa_privileged)
1074 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1075 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1076 else
1077 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1078 NULL, jiffies + (HZ >> 2));
1079 spin_unlock(&tbl->slot_tbl_lock);
1080 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001081out_sleep:
1082 if (args->sa_privileged)
1083 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001084 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001085 else
1086 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1087 spin_unlock(&tbl->slot_tbl_lock);
1088 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001089}
1090EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1091
Chuck Lever9915ea72013-08-09 12:48:27 -04001092static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1093{
1094 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001095 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001096 data->seq_args, data->seq_res, task);
1097}
1098
1099static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1100{
1101 struct nfs4_call_sync_data *data = calldata;
1102 nfs4_sequence_done(task, data->seq_res);
1103}
1104
1105static const struct rpc_call_ops nfs40_call_sync_ops = {
1106 .rpc_call_prepare = nfs40_call_sync_prepare,
1107 .rpc_call_done = nfs40_call_sync_done,
1108};
1109
Anna Schumaker48c05852019-08-14 15:27:00 -04001110static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1111{
1112 int ret;
1113 struct rpc_task *task;
1114
1115 task = rpc_run_task(task_setup);
1116 if (IS_ERR(task))
1117 return PTR_ERR(task);
1118
1119 ret = task->tk_status;
1120 rpc_put_task(task);
1121 return ret;
1122}
1123
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001124static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1125 struct nfs_server *server,
1126 struct rpc_message *msg,
1127 struct nfs4_sequence_args *args,
1128 struct nfs4_sequence_res *res,
1129 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001130{
Chuck Lever9915ea72013-08-09 12:48:27 -04001131 struct nfs_client *clp = server->nfs_client;
1132 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001133 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001134 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001135 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001136 };
1137 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001138 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001139 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001140 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001141 .callback_data = &data,
1142 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001143 };
1144
Anna Schumaker48c05852019-08-14 15:27:00 -04001145 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001146}
1147
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001148static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1149 struct nfs_server *server,
1150 struct rpc_message *msg,
1151 struct nfs4_sequence_args *args,
1152 struct nfs4_sequence_res *res)
1153{
1154 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1155}
1156
1157
Bryan Schumaker7c513052011-03-24 17:12:24 +00001158int nfs4_call_sync(struct rpc_clnt *clnt,
1159 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001160 struct rpc_message *msg,
1161 struct nfs4_sequence_args *args,
1162 struct nfs4_sequence_res *res,
1163 int cache_reply)
1164{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001165 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001166 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Trond Myklebust3c591172018-07-31 15:54:10 -04001169static void
1170nfs4_inc_nlink_locked(struct inode *inode)
1171{
1172 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1173 inc_nlink(inode);
1174}
1175
1176static void
1177nfs4_dec_nlink_locked(struct inode *inode)
1178{
1179 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1180 drop_nlink(inode);
1181}
1182
1183static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001184nfs4_update_changeattr_locked(struct inode *inode,
1185 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001186 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001188 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Trond Myklebust16e14372018-03-20 16:53:31 -04001190 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1191 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001192 | cache_validity;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001193
1194 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001195 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1196 nfsi->attrtimeo_timestamp = jiffies;
1197 } else {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001198 if (S_ISDIR(inode->i_mode)) {
1199 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1200 nfs_force_lookup_revalidate(inode);
1201 } else {
1202 if (!NFS_PROTO(inode)->have_delegation(inode,
1203 FMODE_READ))
1204 nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1205 }
1206
1207 if (cinfo->before != inode_peek_iversion_raw(inode))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001208 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
Frank van der Linden0f44da52020-06-23 22:39:00 +00001209 NFS_INO_INVALID_ACL |
1210 NFS_INO_INVALID_XATTR;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001211 }
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001212 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001213 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001214 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001215 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001216
1217 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1218 nfs_fscache_invalidate(inode);
Trond Myklebust3c591172018-07-31 15:54:10 -04001219}
1220
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001221void
1222nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001223 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001224{
1225 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001226 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 spin_unlock(&dir->i_lock);
1228}
1229
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001230struct nfs4_open_createattrs {
1231 struct nfs4_label *label;
1232 struct iattr *sattr;
1233 const __u32 verf[2];
1234};
1235
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001236static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1237 int err, struct nfs4_exception *exception)
1238{
1239 if (err != -EINVAL)
1240 return false;
1241 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1242 return false;
1243 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1244 exception->retry = 1;
1245 return true;
1246}
1247
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001248static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1249{
1250 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1251}
1252
1253static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1254{
1255 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1256
1257 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1258}
1259
Trond Myklebust6ae37332015-01-30 14:21:14 -05001260static u32
1261nfs4_map_atomic_open_share(struct nfs_server *server,
1262 fmode_t fmode, int openflags)
1263{
1264 u32 res = 0;
1265
1266 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1267 case FMODE_READ:
1268 res = NFS4_SHARE_ACCESS_READ;
1269 break;
1270 case FMODE_WRITE:
1271 res = NFS4_SHARE_ACCESS_WRITE;
1272 break;
1273 case FMODE_READ|FMODE_WRITE:
1274 res = NFS4_SHARE_ACCESS_BOTH;
1275 }
1276 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1277 goto out;
1278 /* Want no delegation if we're using O_DIRECT */
1279 if (openflags & O_DIRECT)
1280 res |= NFS4_SHARE_WANT_NO_DELEG;
1281out:
1282 return res;
1283}
1284
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001285static enum open_claim_type4
1286nfs4_map_atomic_open_claim(struct nfs_server *server,
1287 enum open_claim_type4 claim)
1288{
1289 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1290 return claim;
1291 switch (claim) {
1292 default:
1293 return claim;
1294 case NFS4_OPEN_CLAIM_FH:
1295 return NFS4_OPEN_CLAIM_NULL;
1296 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1297 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1298 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1299 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1300 }
1301}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001304{
1305 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001306 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001307 p->o_res.seqid = p->o_arg.seqid;
1308 p->c_res.seqid = p->c_arg.seqid;
1309 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001310 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001311 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001312 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001313}
1314
Al Viro82a2c1b2011-06-22 18:30:55 -04001315static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001316 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001317 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001318 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001319 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001320{
Al Viro82a2c1b2011-06-22 18:30:55 -04001321 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001322 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001323 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001324 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001325 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001326 struct nfs4_opendata *p;
1327
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001328 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001329 if (p == NULL)
1330 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001331
1332 p->f_label = nfs4_label_alloc(server, gfp_mask);
1333 if (IS_ERR(p->f_label))
1334 goto err_free_p;
1335
Kinglong Meea49c2692015-07-27 15:31:38 +08001336 p->a_label = nfs4_label_alloc(server, gfp_mask);
1337 if (IS_ERR(p->a_label))
1338 goto err_free_f;
1339
Trond Myklebust63f5f792015-01-23 19:19:25 -05001340 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1341 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001342 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001343 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001344 nfs_sb_active(dentry->d_sb);
1345 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001346 p->dir = parent;
1347 p->owner = sp;
1348 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001349 p->o_arg.open_flags = flags;
1350 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001351 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001352 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1353 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001354 if (flags & O_CREAT) {
1355 p->o_arg.umask = current_umask();
1356 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1357 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1358 p->o_arg.u.attrs = &p->attrs;
1359 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1360
1361 memcpy(p->o_arg.u.verifier.data, c->verf,
1362 sizeof(p->o_arg.u.verifier.data));
1363 }
1364 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001365 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1366 * will return permission denied for all bits until close */
1367 if (!(flags & O_EXCL)) {
1368 /* ask server to check for all possible rights as results
1369 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001370 switch (p->o_arg.claim) {
1371 default:
1372 break;
1373 case NFS4_OPEN_CLAIM_NULL:
1374 case NFS4_OPEN_CLAIM_FH:
1375 p->o_arg.access = NFS4_ACCESS_READ |
1376 NFS4_ACCESS_MODIFY |
1377 NFS4_ACCESS_EXTEND |
1378 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001379#ifdef CONFIG_NFS_V4_2
1380 if (server->caps & NFS_CAP_XATTR)
1381 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1382 NFS4_ACCESS_XAWRITE |
1383 NFS4_ACCESS_XALIST;
1384#endif
Trond Myklebust536585c2016-11-10 15:40:34 -05001385 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001386 }
David Howells7539bba2006-08-22 20:06:09 -04001387 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001388 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1389 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001390 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001391 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001392 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001393 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001394 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001395 case NFS4_OPEN_CLAIM_NULL:
1396 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1397 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1398 p->o_arg.fh = NFS_FH(dir);
1399 break;
1400 case NFS4_OPEN_CLAIM_PREVIOUS:
1401 case NFS4_OPEN_CLAIM_FH:
1402 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1403 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001404 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001405 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001406 p->c_arg.fh = &p->o_res.fh;
1407 p->c_arg.stateid = &p->o_res.stateid;
1408 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001409 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001410 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001411 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001412
1413err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001414 nfs4_label_free(p->a_label);
1415err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001416 nfs4_label_free(p->f_label);
1417err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001418 kfree(p);
1419err:
1420 dput(parent);
1421 return NULL;
1422}
1423
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001424static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001425{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001426 struct nfs4_opendata *p = container_of(kref,
1427 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001428 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001429
Fred Isaman30ae2412016-10-18 13:39:51 -04001430 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001431 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001432 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001433 if (p->state != NULL)
1434 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001435 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001436
Kinglong Meea49c2692015-07-27 15:31:38 +08001437 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001438 nfs4_label_free(p->f_label);
1439
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001440 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001441 dput(p->dentry);
1442 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001443 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001444 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001445 kfree(p);
1446}
1447
1448static void nfs4_opendata_put(struct nfs4_opendata *p)
1449{
1450 if (p != NULL)
1451 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001452}
1453
Trond Myklebust24311f82015-09-20 10:50:17 -04001454static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1455 fmode_t fmode)
1456{
1457 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1458 case FMODE_READ|FMODE_WRITE:
1459 return state->n_rdwr != 0;
1460 case FMODE_WRITE:
1461 return state->n_wronly != 0;
1462 case FMODE_READ:
1463 return state->n_rdonly != 0;
1464 }
1465 WARN_ON_ONCE(1);
1466 return false;
1467}
1468
Trond Myklebustbe189f72018-09-27 17:12:33 -04001469static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1470 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001471{
1472 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001473
Trond Myklebust536e43d2012-01-17 22:04:26 -05001474 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001475 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001476 switch (claim) {
1477 case NFS4_OPEN_CLAIM_NULL:
1478 case NFS4_OPEN_CLAIM_FH:
1479 goto out;
1480 default:
1481 break;
1482 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001483 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001484 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001485 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1486 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001487 break;
1488 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001489 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1490 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001491 break;
1492 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001493 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1494 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001495 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001496out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001497 return ret;
1498}
1499
Trond Myklebust2a606182015-08-19 22:30:00 -05001500static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1501 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001502{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001503 if (delegation == NULL)
1504 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001505 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001506 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001507 switch (claim) {
1508 case NFS4_OPEN_CLAIM_NULL:
1509 case NFS4_OPEN_CLAIM_FH:
1510 break;
1511 case NFS4_OPEN_CLAIM_PREVIOUS:
1512 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1513 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001514 fallthrough;
Trond Myklebust2a606182015-08-19 22:30:00 -05001515 default:
1516 return 0;
1517 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001518 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001519 return 1;
1520}
1521
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001522static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001523{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001524 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001525 case FMODE_WRITE:
1526 state->n_wronly++;
1527 break;
1528 case FMODE_READ:
1529 state->n_rdonly++;
1530 break;
1531 case FMODE_READ|FMODE_WRITE:
1532 state->n_rdwr++;
1533 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001534 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001535}
1536
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001537#ifdef CONFIG_NFS_V4_1
1538static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1539{
1540 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1541 return true;
1542 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1543 return true;
1544 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1545 return true;
1546 return false;
1547}
1548#endif /* CONFIG_NFS_V4_1 */
1549
Trond Myklebustc9399f22017-11-06 15:28:01 -05001550static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1551{
1552 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1553 wake_up_all(&state->waitq);
1554}
1555
Trond Myklebust4f14c192014-02-12 19:15:06 -05001556static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001557{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001558 struct nfs_client *clp = state->owner->so_server->nfs_client;
1559 bool need_recover = false;
1560
1561 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1562 need_recover = true;
1563 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1564 need_recover = true;
1565 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1566 need_recover = true;
1567 if (need_recover)
1568 nfs4_state_mark_reclaim_nograce(clp, state);
1569}
1570
Trond Myklebustc9399f22017-11-06 15:28:01 -05001571/*
1572 * Check for whether or not the caller may update the open stateid
1573 * to the value passed in by stateid.
1574 *
1575 * Note: This function relies heavily on the server implementing
1576 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1577 * correctly.
1578 * i.e. The stateid seqids have to be initialised to 1, and
1579 * are then incremented on every state transition.
1580 */
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001581static bool nfs_stateid_is_sequential(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001582 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001583{
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001584 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1585 /* The common case - we're updating to a new sequence number */
1586 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1587 nfs4_stateid_is_next(&state->open_stateid, stateid)) {
1588 return true;
1589 }
1590 } else {
1591 /* This is the first OPEN in this generation */
Trond Myklebustc9399f22017-11-06 15:28:01 -05001592 if (stateid->seqid == cpu_to_be32(1))
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001593 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001594 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001595 return false;
1596}
1597
Trond Myklebustf95549c2015-01-23 18:06:09 -05001598static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1599{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001600 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1601 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001602 if (state->n_wronly)
1603 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1604 if (state->n_rdonly)
1605 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1606 if (state->n_rdwr)
1607 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001608 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001609}
1610
Trond Myklebust226056c2014-02-11 10:41:07 -05001611static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1612 nfs4_stateid *stateid, fmode_t fmode)
1613{
1614 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1615 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1616 case FMODE_WRITE:
1617 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1618 break;
1619 case FMODE_READ:
1620 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1621 break;
1622 case 0:
1623 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1624 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1625 clear_bit(NFS_OPEN_STATE, &state->flags);
1626 }
1627 if (stateid == NULL)
1628 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001629 /* Handle OPEN+OPEN_DOWNGRADE races */
1630 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1631 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001632 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001633 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001634 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001635 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001636 nfs4_stateid_copy(&state->stateid, stateid);
1637 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001638 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001639out:
1640 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001641}
1642
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001643static void nfs_clear_open_stateid(struct nfs4_state *state,
1644 nfs4_stateid *arg_stateid,
1645 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001646{
1647 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001648 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1649 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1650 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001651 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001652 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1653 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001654}
1655
Trond Myklebust1393d962016-09-22 13:39:13 -04001656static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001657 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001658 __must_hold(&state->owner->so_lock)
1659 __must_hold(&state->seqlock)
1660 __must_hold(RCU)
1661
Trond Myklebust003707c2007-07-05 18:07:55 -04001662{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001663 DEFINE_WAIT(wait);
1664 int status = 0;
1665 for (;;) {
1666
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001667 if (nfs_stateid_is_sequential(state, stateid))
Trond Myklebust003707c2007-07-05 18:07:55 -04001668 break;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001669
Trond Myklebustc9399f22017-11-06 15:28:01 -05001670 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001671 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001672 /* Rely on seqids for serialisation with NFSv4.0 */
1673 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1674 break;
1675
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001676 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001677 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1678 /*
1679 * Ensure we process the state changes in the same order
1680 * in which the server processed them by delaying the
1681 * update of the stateid until we are in sequence.
1682 */
1683 write_sequnlock(&state->seqlock);
1684 spin_unlock(&state->owner->so_lock);
1685 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001686 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04001687
Trond Myklebustc9399f22017-11-06 15:28:01 -05001688 if (!signal_pending(current)) {
1689 if (schedule_timeout(5*HZ) == 0)
1690 status = -EAGAIN;
1691 else
1692 status = 0;
1693 } else
1694 status = -EINTR;
1695 finish_wait(&state->waitq, &wait);
1696 rcu_read_lock();
1697 spin_lock(&state->owner->so_lock);
1698 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001699 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001700
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001701 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1702 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001703 nfs4_stateid_copy(freeme, &state->open_stateid);
1704 nfs_test_and_clear_all_open_stateid(state);
1705 }
1706
Trond Myklebuste999e802014-02-10 18:20:47 -05001707 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1708 nfs4_stateid_copy(&state->stateid, stateid);
1709 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001710 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001711 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001712}
1713
Trond Myklebustc9399f22017-11-06 15:28:01 -05001714static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001715 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001716 fmode_t fmode,
1717 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001719 /*
1720 * Protect the call to nfs4_state_set_mode_locked and
1721 * serialise the stateid update
1722 */
1723 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001724 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1725 switch (fmode) {
1726 case FMODE_READ:
1727 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1728 break;
1729 case FMODE_WRITE:
1730 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1731 break;
1732 case FMODE_READ|FMODE_WRITE:
1733 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001734 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001735 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001736 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001737}
1738
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001739static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1740{
1741 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1742 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1743 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1744 clear_bit(NFS_OPEN_STATE, &state->flags);
1745}
1746
Trond Myklebustc9399f22017-11-06 15:28:01 -05001747static void nfs_state_set_delegation(struct nfs4_state *state,
1748 const nfs4_stateid *deleg_stateid,
1749 fmode_t fmode)
1750{
1751 /*
1752 * Protect the call to nfs4_state_set_mode_locked and
1753 * serialise the stateid update
1754 */
1755 write_seqlock(&state->seqlock);
1756 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1757 set_bit(NFS_DELEGATED_STATE, &state->flags);
1758 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001761static void nfs_state_clear_delegation(struct nfs4_state *state)
1762{
1763 write_seqlock(&state->seqlock);
1764 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1765 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1766 write_sequnlock(&state->seqlock);
1767}
1768
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001769int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001770 const nfs4_stateid *open_stateid,
1771 const nfs4_stateid *delegation,
1772 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001773{
Trond Myklebust1393d962016-09-22 13:39:13 -04001774 struct nfs_server *server = NFS_SERVER(state->inode);
1775 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001776 struct nfs_inode *nfsi = NFS_I(state->inode);
1777 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001778 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001779 int ret = 0;
1780
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001781 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001782
1783 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001784 spin_lock(&state->owner->so_lock);
1785 if (open_stateid != NULL) {
1786 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1787 ret = 1;
1788 }
1789
Trond Myklebust333ac782019-10-22 12:12:17 -04001790 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001791 if (deleg_cur == NULL)
1792 goto no_delegation;
1793
1794 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001795 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001796 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001797 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001798 goto no_delegation_unlock;
1799
1800 if (delegation == NULL)
1801 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001802 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001803 goto no_delegation_unlock;
1804
Trond Myklebustb7391f42008-12-23 15:21:52 -05001805 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001806 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001807 ret = 1;
1808no_delegation_unlock:
1809 spin_unlock(&deleg_cur->lock);
1810no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001811 if (ret)
1812 update_open_stateflags(state, fmode);
1813 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001814 rcu_read_unlock();
1815
Trond Myklebust4f14c192014-02-12 19:15:06 -05001816 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001817 nfs4_schedule_state_manager(clp);
1818 if (freeme.type != 0)
1819 nfs4_test_and_free_stateid(server, &freeme,
1820 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001821
1822 return ret;
1823}
1824
Trond Myklebust39071e62015-01-24 15:07:56 -05001825static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1826 const nfs4_stateid *stateid)
1827{
1828 struct nfs4_state *state = lsp->ls_state;
1829 bool ret = false;
1830
1831 spin_lock(&state->state_lock);
1832 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1833 goto out_noupdate;
1834 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1835 goto out_noupdate;
1836 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1837 ret = true;
1838out_noupdate:
1839 spin_unlock(&state->state_lock);
1840 return ret;
1841}
Trond Myklebust34310432008-12-23 15:21:38 -05001842
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001843static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001844{
1845 struct nfs_delegation *delegation;
1846
Trond Myklebustf5086242018-03-20 16:43:13 -04001847 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001848 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001849 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001850 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001851 rcu_read_unlock();
1852 return;
1853 }
1854 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001855 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856}
1857
Trond Myklebust6ee41262007-07-08 14:11:36 -04001858static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001859{
1860 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001861 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001862 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001863 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001864 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001865 nfs4_stateid stateid;
1866 int ret = -EAGAIN;
1867
Trond Myklebustaac00a82007-07-05 19:02:21 -04001868 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001869 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001870 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001871 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001872 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001873 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001874 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001875 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001876 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001877 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001878 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001879 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001880 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001881 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001882 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001883 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001884 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001885 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001886 if (!opendata->is_recover) {
1887 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1888 if (ret != 0)
1889 goto out;
1890 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001891 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001892
1893 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001894 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001895 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001896 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001897out:
1898 return ERR_PTR(ret);
1899out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001900 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001901 return state;
1902}
1903
Andy Adamsone23008e2012-10-02 21:07:32 -04001904static void
1905nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1906{
1907 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1908 struct nfs_delegation *delegation;
1909 int delegation_flags = 0;
1910
1911 rcu_read_lock();
1912 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1913 if (delegation)
1914 delegation_flags = delegation->flags;
1915 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001916 switch (data->o_arg.claim) {
1917 default:
1918 break;
1919 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1920 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001921 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1922 "returning a delegation for "
1923 "OPEN(CLAIM_DELEGATE_CUR)\n",
1924 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001925 return;
1926 }
1927 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001928 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001929 data->owner->so_cred,
1930 data->o_res.delegation_type,
1931 &data->o_res.delegation,
1932 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001933 else
1934 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001935 data->owner->so_cred,
1936 data->o_res.delegation_type,
1937 &data->o_res.delegation,
1938 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001939
1940 if (data->o_res.do_recall)
1941 nfs_async_inode_return_delegation(state->inode,
1942 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001943}
1944
1945/*
1946 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1947 * and update the nfs4_state.
1948 */
1949static struct nfs4_state *
1950_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1951{
1952 struct inode *inode = data->state->inode;
1953 struct nfs4_state *state = data->state;
1954 int ret;
1955
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001956 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001957 if (data->rpc_status)
1958 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001959 /* cached opens have already been processed */
1960 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001961 }
1962
Andy Adamsone23008e2012-10-02 21:07:32 -04001963 ret = nfs_refresh_inode(inode, &data->f_attr);
1964 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001965 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001966
1967 if (data->o_res.delegation_type != 0)
1968 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001969update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001970 if (!update_open_stateid(state, &data->o_res.stateid,
1971 NULL, data->o_arg.fmode))
1972 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001973 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001974
1975 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001976}
1977
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001978static struct inode *
1979nfs4_opendata_get_inode(struct nfs4_opendata *data)
1980{
1981 struct inode *inode;
1982
1983 switch (data->o_arg.claim) {
1984 case NFS4_OPEN_CLAIM_NULL:
1985 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1986 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1987 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1988 return ERR_PTR(-EAGAIN);
1989 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1990 &data->f_attr, data->f_label);
1991 break;
1992 default:
1993 inode = d_inode(data->dentry);
1994 ihold(inode);
1995 nfs_refresh_inode(inode, &data->f_attr);
1996 }
1997 return inode;
1998}
1999
Andy Adamsone23008e2012-10-02 21:07:32 -04002000static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04002001nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2002{
2003 struct nfs4_state *state;
2004 struct inode *inode;
2005
2006 inode = nfs4_opendata_get_inode(data);
2007 if (IS_ERR(inode))
2008 return ERR_CAST(inode);
2009 if (data->state != NULL && data->state->inode == inode) {
2010 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002011 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002012 } else
2013 state = nfs4_get_open_state(inode, data->owner);
2014 iput(inode);
2015 if (state == NULL)
2016 state = ERR_PTR(-ENOMEM);
2017 return state;
2018}
2019
Andy Adamsone23008e2012-10-02 21:07:32 -04002020static struct nfs4_state *
2021_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002022{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002023 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002024
Trond Myklebustaac00a82007-07-05 19:02:21 -04002025 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002026 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002027 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002028 goto out;
2029 }
2030
Trond Myklebust75e8c482017-08-08 10:38:07 -04002031 state = nfs4_opendata_find_nfs4_state(data);
2032 if (IS_ERR(state))
2033 goto out;
2034
Andy Adamsone23008e2012-10-02 21:07:32 -04002035 if (data->o_res.delegation_type != 0)
2036 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002037 if (!update_open_stateid(state, &data->o_res.stateid,
2038 NULL, data->o_arg.fmode)) {
2039 nfs4_put_open_state(state);
2040 state = ERR_PTR(-EAGAIN);
2041 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002042out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002043 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002044 return state;
2045}
2046
Andy Adamsone23008e2012-10-02 21:07:32 -04002047static struct nfs4_state *
2048nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2049{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002050 struct nfs4_state *ret;
2051
Andy Adamsone23008e2012-10-02 21:07:32 -04002052 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002053 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2054 else
2055 ret = _nfs4_opendata_to_nfs4_state(data);
2056 nfs4_sequence_free_slot(&data->o_res.seq_res);
2057 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002058}
2059
Trond Myklebust0de43972018-09-02 15:57:01 -04002060static struct nfs_open_context *
2061nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002062{
2063 struct nfs_inode *nfsi = NFS_I(state->inode);
2064 struct nfs_open_context *ctx;
2065
Trond Myklebust0de43972018-09-02 15:57:01 -04002066 rcu_read_lock();
2067 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002068 if (ctx->state != state)
2069 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002070 if ((ctx->mode & mode) != mode)
2071 continue;
2072 if (!get_nfs_open_context(ctx))
2073 continue;
2074 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002075 return ctx;
2076 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002077 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002078 return ERR_PTR(-ENOENT);
2079}
2080
Trond Myklebust0de43972018-09-02 15:57:01 -04002081static struct nfs_open_context *
2082nfs4_state_find_open_context(struct nfs4_state *state)
2083{
2084 struct nfs_open_context *ctx;
2085
2086 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2087 if (!IS_ERR(ctx))
2088 return ctx;
2089 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2090 if (!IS_ERR(ctx))
2091 return ctx;
2092 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2093}
2094
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002095static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2096 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002097{
2098 struct nfs4_opendata *opendata;
2099
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002100 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002101 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002102 if (opendata == NULL)
2103 return ERR_PTR(-ENOMEM);
2104 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002105 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002106 return opendata;
2107}
2108
Trond Myklebust24311f82015-09-20 10:50:17 -04002109static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2110 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002111{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002112 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002113 int ret;
2114
Trond Myklebust24311f82015-09-20 10:50:17 -04002115 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002116 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002117 opendata->o_arg.open_flags = 0;
2118 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002119 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2120 NFS_SB(opendata->dentry->d_sb),
2121 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002122 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2123 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2124 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002125 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002126 if (ret != 0)
2127 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002128 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002129 if (IS_ERR(newstate))
2130 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002131 if (newstate != opendata->state)
2132 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002133 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002134 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002135}
2136
2137static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2138{
Trond Myklebust864472e2006-01-03 09:55:15 +01002139 int ret;
2140
2141 /* memory barrier prior to reading state->n_* */
2142 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002143 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2144 if (ret != 0)
2145 return ret;
2146 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2147 if (ret != 0)
2148 return ret;
2149 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2150 if (ret != 0)
2151 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002152 /*
2153 * We may have performed cached opens for all three recoveries.
2154 * Check if we need to update the current stateid.
2155 */
2156 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002157 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002158 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002159 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002160 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002161 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002162 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002163 return 0;
2164}
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166/*
2167 * OPEN_RECLAIM:
2168 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002170static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002172 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002173 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002174 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 int status;
2176
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002177 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2178 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002179 if (IS_ERR(opendata))
2180 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002181 rcu_read_lock();
2182 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002183 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002184 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002185 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002186 opendata->o_arg.u.delegation_type = delegation_type;
2187 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002188 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return status;
2190}
2191
Trond Myklebust539cd032007-06-05 11:46:42 -04002192static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 struct nfs_server *server = NFS_SERVER(state->inode);
2195 struct nfs4_exception exception = { };
2196 int err;
2197 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002198 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002199 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002200 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2201 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002202 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002203 break;
2204 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 } while (exception.retry);
2206 return err;
2207}
2208
Trond Myklebust864472e2006-01-03 09:55:15 +01002209static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2210{
2211 struct nfs_open_context *ctx;
2212 int ret;
2213
2214 ctx = nfs4_state_find_open_context(state);
2215 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002216 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002217 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2218 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002219 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002220 put_nfs_open_context(ctx);
2221 return ret;
2222}
2223
NeilBrowndce26302017-12-13 09:57:09 +11002224static 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 -07002225{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002226 switch (err) {
2227 default:
2228 printk(KERN_ERR "NFS: %s: unhandled error "
2229 "%d.\n", __func__, err);
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06002230 fallthrough;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002231 case 0:
2232 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002233 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002234 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002235 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002236 break;
2237 case -NFS4ERR_BADSESSION:
2238 case -NFS4ERR_BADSLOT:
2239 case -NFS4ERR_BAD_HIGH_SLOT:
2240 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2241 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002242 return -EAGAIN;
2243 case -NFS4ERR_STALE_CLIENTID:
2244 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002245 /* Don't recall a delegation if it was lost */
2246 nfs4_schedule_lease_recovery(server->nfs_client);
2247 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002248 case -NFS4ERR_MOVED:
2249 nfs4_schedule_migration_recovery(server);
2250 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002251 case -NFS4ERR_LEASE_MOVED:
2252 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2253 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002254 case -NFS4ERR_DELEG_REVOKED:
2255 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002256 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002257 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002258 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002259 nfs_inode_find_state_and_recover(state->inode,
2260 stateid);
2261 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002262 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002263 case -NFS4ERR_DELAY:
2264 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002265 ssleep(1);
2266 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002267 case -ENOMEM:
2268 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002269 if (fl) {
2270 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2271 if (lsp)
2272 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2273 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002274 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return err;
2277}
2278
Trond Myklebust24311f82015-09-20 10:50:17 -04002279int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002280 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002281{
2282 struct nfs_server *server = NFS_SERVER(state->inode);
2283 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002284 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002285
2286 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2287 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2288 if (IS_ERR(opendata))
2289 return PTR_ERR(opendata);
2290 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002291 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002292 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2293 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002294 goto out;
2295 }
2296 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002297 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2298 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002299 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002300 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002301 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2302 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2303 if (err)
2304 goto out;
2305 }
2306 nfs_state_clear_delegation(state);
2307out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002308 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002309 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002310}
2311
Chuck Leverbe05c862013-08-09 12:49:47 -04002312static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2313{
2314 struct nfs4_opendata *data = calldata;
2315
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002316 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2317 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002318}
2319
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002320static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2321{
2322 struct nfs4_opendata *data = calldata;
2323
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002324 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002325
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002326 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002327 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002328 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002329 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002330 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002331 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002332 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002333}
2334
2335static void nfs4_open_confirm_release(void *calldata)
2336{
2337 struct nfs4_opendata *data = calldata;
2338 struct nfs4_state *state = NULL;
2339
2340 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002341 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002342 goto out_free;
2343 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002344 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002345 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002346 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002347 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002348 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002349out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002350 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002351}
2352
2353static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002354 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002355 .rpc_call_done = nfs4_open_confirm_done,
2356 .rpc_release = nfs4_open_confirm_release,
2357};
2358
2359/*
2360 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2361 */
2362static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2363{
David Howells2b0143b2015-03-17 22:25:59 +00002364 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002365 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002366 struct rpc_message msg = {
2367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2368 .rpc_argp = &data->c_arg,
2369 .rpc_resp = &data->c_res,
2370 .rpc_cred = data->owner->so_cred,
2371 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002372 struct rpc_task_setup task_setup_data = {
2373 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002374 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002375 .callback_ops = &nfs4_open_confirm_ops,
2376 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002377 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002378 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002379 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 int status;
2381
Anna Schumakerfba83f32018-05-04 16:22:50 -04002382 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2383 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002384 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002385 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002386 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002387 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002388 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002389 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002390 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002391 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002392 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002393 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002394 smp_wmb();
2395 } else
2396 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002397 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return status;
2399}
2400
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002401static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002403 struct nfs4_opendata *data = calldata;
2404 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002405 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002406 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002407
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002408 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002409 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002410 /*
2411 * Check if we still need to send an OPEN call, or if we can use
2412 * a delegation instead.
2413 */
2414 if (data->state != NULL) {
2415 struct nfs_delegation *delegation;
2416
Trond Myklebustbe189f72018-09-27 17:12:33 -04002417 if (can_open_cached(data->state, data->o_arg.fmode,
2418 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002419 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002420 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002421 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002422 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002423 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002424 rcu_read_unlock();
2425 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002426 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002427 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002428 switch (claim) {
2429 default:
2430 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002431 case NFS4_OPEN_CLAIM_PREVIOUS:
2432 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2433 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002434 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002435 fallthrough;
Trond Myklebust8188df12013-04-23 14:31:19 -04002436 case NFS4_OPEN_CLAIM_FH:
2437 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002438 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002439 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002440 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002441 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002442 &data->o_res.seq_res,
2443 task) != 0)
2444 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002445
2446 /* Set the create mode (note dependency on the session type) */
2447 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2448 if (data->o_arg.open_flags & O_EXCL) {
2449 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2450 if (nfs4_has_persistent_session(clp))
2451 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2452 else if (clp->cl_mvops->minor_version > 0)
2453 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2454 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002455 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002456unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002457 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002458 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002459out_no_action:
2460 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002461out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002462 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002463}
2464
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002465static void nfs4_open_done(struct rpc_task *task, void *calldata)
2466{
2467 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002469 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002470
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002471 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002472 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002473
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002474 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002475 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2476 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002477 case S_IFREG:
2478 break;
2479 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002480 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002481 break;
2482 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002483 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002484 break;
2485 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002486 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002487 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002488 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002489 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002490 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2491 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002492 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002493 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002494}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002495
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002496static void nfs4_open_release(void *calldata)
2497{
2498 struct nfs4_opendata *data = calldata;
2499 struct nfs4_state *state = NULL;
2500
2501 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002502 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002503 goto out_free;
2504 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002505 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002506 goto out_free;
2507 /* In case we need an open_confirm, no cleanup! */
2508 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2509 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002510 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002511 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002512 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002513out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002514 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002515}
2516
2517static const struct rpc_call_ops nfs4_open_ops = {
2518 .rpc_call_prepare = nfs4_open_prepare,
2519 .rpc_call_done = nfs4_open_done,
2520 .rpc_release = nfs4_open_release,
2521};
2522
Fred Isaman3b65a302016-09-19 10:06:49 -04002523static int nfs4_run_open_task(struct nfs4_opendata *data,
2524 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002525{
David Howells2b0143b2015-03-17 22:25:59 +00002526 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002527 struct nfs_server *server = NFS_SERVER(dir);
2528 struct nfs_openargs *o_arg = &data->o_arg;
2529 struct nfs_openres *o_res = &data->o_res;
2530 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002531 struct rpc_message msg = {
2532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2533 .rpc_argp = o_arg,
2534 .rpc_resp = o_res,
2535 .rpc_cred = data->owner->so_cred,
2536 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002537 struct rpc_task_setup task_setup_data = {
2538 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002539 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002540 .callback_ops = &nfs4_open_ops,
2541 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002542 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002543 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002544 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002545 int status;
2546
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002547 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002548 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002549 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002550 data->cancelled = false;
2551 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002552 if (!ctx) {
2553 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002554 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002555 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002556 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002557 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002558 pnfs_lgopen_prepare(data, ctx);
2559 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002560 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002561 if (IS_ERR(task))
2562 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002563 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002564 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002565 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002566 smp_wmb();
2567 } else
2568 status = data->rpc_status;
2569 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002570
2571 return status;
2572}
2573
2574static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2575{
David Howells2b0143b2015-03-17 22:25:59 +00002576 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002577 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002578 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002579
Fred Isaman3b65a302016-09-19 10:06:49 -04002580 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002581 if (status != 0 || !data->rpc_done)
2582 return status;
2583
Trond Myklebust6926afd12012-01-07 13:22:46 -05002584 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2585
Anna Schumakerd7e98252017-01-11 16:13:29 -05002586 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002587 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002588
2589 return status;
2590}
2591
Trond Myklebustf3792d62014-07-10 08:54:32 -04002592/*
2593 * Additional permission checks in order to distinguish between an
2594 * open for read, and an open for execute. This works around the
2595 * fact that NFSv4 OPEN treats read and execute permissions as being
2596 * the same.
2597 * Note that in the non-execute case, we want to turn off permission
2598 * checking if we just created a new file (POSIX open() semantics).
2599 */
NeilBrowna52458b2018-12-03 11:30:31 +11002600static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002601 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002602 struct nfs4_state *state, fmode_t fmode,
2603 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002604{
2605 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002606 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002607
2608 /* access call failed or for some reason the server doesn't
2609 * support any access modes -- defer access call until later */
2610 if (opendata->o_res.access_supported == 0)
2611 return 0;
2612
2613 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002614 /*
2615 * Use openflags to check for exec, because fmode won't
2616 * always have FMODE_EXEC set when file open for exec.
2617 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002618 if (openflags & __FMODE_EXEC) {
2619 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002620 if (S_ISDIR(state->inode->i_mode))
2621 mask = NFS4_ACCESS_LOOKUP;
2622 else
2623 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002624 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002625 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002626
2627 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002628 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2629 nfs_access_add_cache(state->inode, &cache);
2630
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002631 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2632 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002633 return 0;
2634
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002635 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002636}
2637
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002638/*
2639 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2640 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002641static int _nfs4_proc_open(struct nfs4_opendata *data,
2642 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002643{
David Howells2b0143b2015-03-17 22:25:59 +00002644 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002645 struct nfs_server *server = NFS_SERVER(dir);
2646 struct nfs_openargs *o_arg = &data->o_arg;
2647 struct nfs_openres *o_res = &data->o_res;
2648 int status;
2649
Fred Isaman3b65a302016-09-19 10:06:49 -04002650 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002651 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002652 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002653 if (status != 0) {
2654 if (status == -NFS4ERR_BADNAME &&
2655 !(o_arg->open_flags & O_CREAT))
2656 return -ENOENT;
2657 return status;
2658 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002659
Trond Myklebust6926afd12012-01-07 13:22:46 -05002660 nfs_fattr_map_and_free_names(server, &data->f_attr);
2661
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002662 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002663 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002664 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002665 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002666 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002667 if (data->file_created ||
2668 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002669 nfs4_update_changeattr(dir, &o_res->cinfo,
2670 o_res->f_attr->time_start,
2671 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002672 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002673 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2674 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002676 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002678 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002680 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2681 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002682 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2683 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002684 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002685 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686}
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688/*
2689 * OPEN_EXPIRED:
2690 * reclaim state on the server after a network partition.
2691 * Assumes caller holds the appropriate lock
2692 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002693static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002695 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002696 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002698 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002699 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002700 if (IS_ERR(opendata))
2701 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002702 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002703 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002704 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002705 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002706 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002709static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002710{
Trond Myklebust539cd032007-06-05 11:46:42 -04002711 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002712 struct nfs4_exception exception = { };
2713 int err;
2714
2715 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002716 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002717 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002718 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2719 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002720 switch (err) {
2721 default:
2722 goto out;
2723 case -NFS4ERR_GRACE:
2724 case -NFS4ERR_DELAY:
2725 nfs4_handle_exception(server, err, &exception);
2726 err = 0;
2727 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002728 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002729out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002730 return err;
2731}
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2734{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002736 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Trond Myklebust864472e2006-01-03 09:55:15 +01002738 ctx = nfs4_state_find_open_context(state);
2739 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002740 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002741 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002742 put_nfs_open_context(ctx);
2743 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744}
2745
Trond Myklebust41020b62016-09-22 13:38:58 -04002746static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2747 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002748{
Trond Myklebust41020b62016-09-22 13:38:58 -04002749 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002750 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002751}
2752
2753static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2754{
2755 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002756 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002757}
2758
2759static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2760{
2761 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2762 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002763 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002764 return nfs4_open_expired(sp, state);
2765}
2766
Trond Myklebust45870d62016-09-22 13:38:59 -04002767static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2768 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002769 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002770{
2771 return -NFS4ERR_BAD_STATEID;
2772}
2773
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002774#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002775static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2776 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002777 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002778{
2779 int status;
2780
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002781 switch (stateid->type) {
2782 default:
2783 break;
2784 case NFS4_INVALID_STATEID_TYPE:
2785 case NFS4_SPECIAL_STATEID_TYPE:
2786 return -NFS4ERR_BAD_STATEID;
2787 case NFS4_REVOKED_STATEID_TYPE:
2788 goto out_free;
2789 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002790
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002791 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002792 switch (status) {
2793 case -NFS4ERR_EXPIRED:
2794 case -NFS4ERR_ADMIN_REVOKED:
2795 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002796 break;
2797 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002798 return status;
2799 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002800out_free:
2801 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002802 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002803 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002804}
2805
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002806static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002807{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002808 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002809 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002810 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002811 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002812 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002813
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002814 /* Get the delegation credential for use by test/free_stateid */
2815 rcu_read_lock();
2816 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002817 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002818 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002819 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002820 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002821 }
2822
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002823 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002824 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002825
Trond Myklebust994b15b2018-09-05 14:07:14 -04002826 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2827 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002828 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002829 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002830 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002831 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002832
NeilBrowna52458b2018-12-03 11:30:31 +11002833 if (delegation->cred)
2834 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002835 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002836 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002837 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002838 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002839 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002840 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002841 else
2842 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002843
Trond Myklebust8c39a392019-07-19 13:48:44 -04002844 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002845 return ret;
2846}
2847
2848static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2849{
2850 nfs4_stateid tmp;
2851
2852 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2853 nfs4_copy_delegation_stateid(state->inode, state->state,
2854 &tmp, NULL) &&
2855 nfs4_stateid_match_other(&state->stateid, &tmp))
2856 nfs_state_set_delegation(state, &tmp, state->state);
2857 else
2858 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002859}
2860
2861/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002862 * nfs41_check_expired_locks - possibly free a lock stateid
2863 *
2864 * @state: NFSv4 state for an inode
2865 *
2866 * Returns NFS_OK if recovery for this stateid is now finished.
2867 * Otherwise a negative NFS4ERR value is returned.
2868 */
2869static int nfs41_check_expired_locks(struct nfs4_state *state)
2870{
2871 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002872 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002873 struct nfs_server *server = NFS_SERVER(state->inode);
2874
2875 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2876 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002877
2878 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002879 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2880 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002881 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002882
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002883 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002884 spin_unlock(&state->state_lock);
2885
2886 nfs4_put_lock_state(prev);
2887 prev = lsp;
2888
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002889 status = nfs41_test_and_free_expired_stateid(server,
2890 &lsp->ls_stateid,
2891 cred);
2892 trace_nfs4_test_lock_stateid(state, lsp, status);
2893 if (status == -NFS4ERR_EXPIRED ||
2894 status == -NFS4ERR_BAD_STATEID) {
2895 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002896 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002897 if (!recover_lost_locks)
2898 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2899 } else if (status != NFS_OK) {
2900 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002901 nfs4_put_lock_state(prev);
2902 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002903 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002904 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002905 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002906 }
2907 spin_unlock(&state->state_lock);
2908 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002909out:
2910 return ret;
2911}
2912
2913/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002914 * nfs41_check_open_stateid - possibly free an open stateid
2915 *
2916 * @state: NFSv4 state for an inode
2917 *
2918 * Returns NFS_OK if recovery for this stateid is now finished.
2919 * Otherwise a negative NFS4ERR value is returned.
2920 */
2921static int nfs41_check_open_stateid(struct nfs4_state *state)
2922{
2923 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002924 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002925 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002926 int status;
2927
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002928 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002929 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002930 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002931 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002932 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002933 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002934 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002935 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002936 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002937 if (nfs_open_stateid_recover_openmode(state))
2938 return -NFS4ERR_OPENMODE;
2939 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002940}
2941
2942static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2943{
Chuck Levereb64cf92012-07-11 16:30:05 -04002944 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002945
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002946 status = nfs41_check_delegation_stateid(state);
2947 if (status != NFS_OK)
2948 return status;
2949 nfs41_delegation_recover_stateid(state);
2950
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002951 status = nfs41_check_expired_locks(state);
2952 if (status != NFS_OK)
2953 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002954 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002955 if (status != NFS_OK)
2956 status = nfs4_open_expired(sp, state);
2957 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002958}
2959#endif
2960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002962 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2963 * fields corresponding to attributes that were used to store the verifier.
2964 * Make sure we clobber those fields in the later setattr call
2965 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002966static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002967 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002968{
Trond Myklebust609339c2018-03-28 16:18:17 -04002969 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2970 __u32 attrset[3];
2971 unsigned ret;
2972 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002973
Trond Myklebust609339c2018-03-28 16:18:17 -04002974 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2975 attrset[i] = opendata->o_res.attrset[i];
2976 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2977 attrset[i] &= ~bitmask[i];
2978 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002979
Trond Myklebust609339c2018-03-28 16:18:17 -04002980 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2981 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002982
Trond Myklebust609339c2018-03-28 16:18:17 -04002983 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2984 if (sattr->ia_valid & ATTR_ATIME_SET)
2985 ret |= ATTR_ATIME_SET;
2986 else
2987 ret |= ATTR_ATIME;
2988 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002989
Trond Myklebust609339c2018-03-28 16:18:17 -04002990 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2991 if (sattr->ia_valid & ATTR_MTIME_SET)
2992 ret |= ATTR_MTIME_SET;
2993 else
2994 ret |= ATTR_MTIME;
2995 }
2996
2997 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002998 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002999 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003000}
3001
Trond Myklebustc21443c2013-02-07 14:26:21 -05003002static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003003 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003004{
3005 struct nfs4_state_owner *sp = opendata->owner;
3006 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003007 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003008 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003009 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003010 struct inode *dir = d_inode(opendata->dir);
3011 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003012 unsigned int seq;
3013 int ret;
3014
3015 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003016 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003017
Fred Isaman3b65a302016-09-19 10:06:49 -04003018 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003019 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003020 goto out;
3021
Trond Myklebustae55e592018-05-22 11:17:16 -04003022 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003023 ret = PTR_ERR(state);
3024 if (IS_ERR(state))
3025 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003026 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003027 if (server->caps & NFS_CAP_POSIX_LOCK)
3028 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003029 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3030 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003031
Trond Myklebust275bb302013-05-29 13:11:28 -04003032 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003033 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003034 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003035 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003036 alias = d_exact_alias(dentry, state->inode);
3037 if (!alias)
3038 alias = d_splice_alias(igrab(state->inode), dentry);
3039 /* d_splice_alias() can't fail here - it's a non-directory */
3040 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003041 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003042 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003043 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003044 }
3045
3046 switch(opendata->o_arg.claim) {
3047 default:
3048 break;
3049 case NFS4_OPEN_CLAIM_NULL:
3050 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3051 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3052 if (!opendata->rpc_done)
3053 break;
3054 if (opendata->o_res.delegation_type != 0)
3055 dir_verifier = nfs_save_change_attribute(dir);
3056 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003057 }
3058
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003059 /* Parse layoutget results before we check for access */
3060 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3061
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003062 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3063 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003064 if (ret != 0)
3065 goto out;
3066
David Howells2b0143b2015-03-17 22:25:59 +00003067 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003068 nfs_inode_attach_open_context(ctx);
3069 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3070 nfs4_schedule_stateid_recovery(server, state);
3071 }
Fred Isaman2409a972016-10-06 12:11:21 -04003072
Trond Myklebustc21443c2013-02-07 14:26:21 -05003073out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003074 if (!opendata->cancelled)
3075 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003076 return ret;
3077}
3078
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003079/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003080 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 */
Andy Adamson82be4172012-05-23 05:02:35 -04003082static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003083 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003084 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003085 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003086 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
3088 struct nfs4_state_owner *sp;
3089 struct nfs4_state *state = NULL;
3090 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003091 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003092 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003093 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003094 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003095 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003096 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003097 struct iattr *sattr = c->sattr;
3098 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003099 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003100 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003104 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3105 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3107 goto out_err;
3108 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003109 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003110 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003111 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003112 if (d_really_is_positive(dentry))
3113 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003114 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003115 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003116 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003117 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3118 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003119 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003120 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
David Quigley14c43f72013-05-22 12:50:43 -04003122 if (label) {
3123 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3124 if (IS_ERR(olabel)) {
3125 status = PTR_ERR(olabel);
3126 goto err_opendata_put;
3127 }
3128 }
3129
Trond Myklebuste911b812014-03-26 13:24:37 -07003130 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3131 if (!opendata->f_attr.mdsthreshold) {
3132 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3133 if (!opendata->f_attr.mdsthreshold)
3134 goto err_free_label;
3135 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003136 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003137 }
David Howells2b0143b2015-03-17 22:25:59 +00003138 if (d_really_is_positive(dentry))
3139 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003140
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003141 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003142 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003143 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003144 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003145
NeilBrownefcbc042015-07-30 13:00:56 +10003146 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003147 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003148 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003149 /*
3150 * send create attributes which was not set by open
3151 * with an extra setattr.
3152 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003153 if (attrs || label) {
3154 unsigned ia_old = sattr->ia_valid;
3155
3156 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003157 nfs_fattr_init(opendata->o_res.f_attr);
3158 status = nfs4_do_setattr(state->inode, cred,
3159 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003160 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003161 if (status == 0) {
3162 nfs_setattr_update_inode(state->inode, sattr,
3163 opendata->o_res.f_attr);
3164 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3165 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003166 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003167 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003168 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003169 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003170 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003171
Trond Myklebuste911b812014-03-26 13:24:37 -07003172 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003173 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003174 opendata->f_attr.mdsthreshold = NULL;
3175 }
Andy Adamson82be4172012-05-23 05:02:35 -04003176
David Quigley14c43f72013-05-22 12:50:43 -04003177 nfs4_label_free(olabel);
3178
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003179 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003182err_free_label:
3183 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003184err_opendata_put:
3185 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003186err_put_state_owner:
3187 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return status;
3190}
3191
3192
Andy Adamson82be4172012-05-23 05:02:35 -04003193static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003194 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003195 int flags,
3196 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003197 struct nfs4_label *label,
3198 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003200 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003201 struct nfs4_exception exception = {
3202 .interruptible = true,
3203 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003205 struct nfs4_open_createattrs c = {
3206 .label = label,
3207 .sattr = sattr,
3208 .verf = {
3209 [0] = (__u32)jiffies,
3210 [1] = (__u32)current->pid,
3211 },
3212 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 int status;
3214
3215 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003216 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003217 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003218 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (status == 0)
3220 break;
3221 /* NOTE: BAD_SEQID means the server and client disagree about the
3222 * book-keeping w.r.t. state-changing operations
3223 * (OPEN/CLOSE/LOCK/LOCKU...)
3224 * It is actually a sign of a bug on the client or on the server.
3225 *
3226 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003227 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 * have unhashed the old state_owner for us, and that we can
3229 * therefore safely retry using a new one. We should still warn
3230 * the user though...
3231 */
3232 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003233 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003234 " returned a bad sequence-id error!\n",
3235 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 exception.retry = 1;
3237 continue;
3238 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003239 /*
3240 * BAD_STATEID on OPEN means that the server cancelled our
3241 * state before it received the OPEN_CONFIRM.
3242 * Recover by retrying the request as per the discussion
3243 * on Page 181 of RFC3530.
3244 */
3245 if (status == -NFS4ERR_BAD_STATEID) {
3246 exception.retry = 1;
3247 continue;
3248 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003249 if (status == -NFS4ERR_EXPIRED) {
3250 nfs4_schedule_lease_recovery(server->nfs_client);
3251 exception.retry = 1;
3252 continue;
3253 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003254 if (status == -EAGAIN) {
3255 /* We must have found a delegation */
3256 exception.retry = 1;
3257 continue;
3258 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003259 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3260 continue;
3261 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 status, &exception));
3263 } while (exception.retry);
3264 return res;
3265}
3266
Trond Myklebust8487c472016-06-26 08:44:35 -04003267static int _nfs4_do_setattr(struct inode *inode,
3268 struct nfs_setattrargs *arg,
3269 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003270 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003271 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003272{
3273 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003274 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3276 .rpc_argp = arg,
3277 .rpc_resp = res,
3278 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003279 };
NeilBrowna52458b2018-12-03 11:30:31 +11003280 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003281 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003282 bool truncate;
3283 int status;
3284
3285 nfs_fattr_init(res->fattr);
3286
3287 /* Servers should only apply open mode checks for file size changes */
3288 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Chuck Lever644c9f42020-09-04 17:39:12 -04003289 if (!truncate) {
3290 nfs4_inode_make_writeable(inode);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003291 goto zero_stateid;
Chuck Lever644c9f42020-09-04 17:39:12 -04003292 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003293
Trond Myklebust991eedb2018-04-09 11:15:30 -04003294 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003295 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003296 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003297 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003298 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003299 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003300 l_ctx = nfs_get_lock_context(ctx);
3301 if (IS_ERR(l_ctx))
3302 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003303 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3304 &arg->stateid, &delegation_cred);
3305 nfs_put_lock_context(l_ctx);
3306 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003307 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003308 else if (status == -EAGAIN)
3309 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003310 } else {
3311zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003312 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003313 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003314 if (delegation_cred)
3315 msg.rpc_cred = delegation_cred;
3316
3317 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3318
NeilBrowna52458b2018-12-03 11:30:31 +11003319 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003320 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003321 renew_lease(server, timestamp);
3322 trace_nfs4_setattr(inode, &arg->stateid, status);
3323 return status;
3324}
3325
NeilBrowna52458b2018-12-03 11:30:31 +11003326static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003327 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003328 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003329 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003331 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003332 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003333 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003334 struct nfs_setattrargs arg = {
3335 .fh = NFS_FH(inode),
3336 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003338 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003339 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003340 };
3341 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003343 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003345 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003346 struct nfs4_exception exception = {
3347 .state = state,
3348 .inode = inode,
3349 .stateid = &arg.stateid,
3350 };
3351 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003354 nfs4_bitmap_copy_adjust_setattr(bitmask,
3355 nfs4_bitmask(server, olabel),
3356 inode);
3357
NeilBrown29b59f92016-10-13 15:26:47 +11003358 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003359 switch (err) {
3360 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003361 if (!(sattr->ia_valid & ATTR_SIZE)) {
3362 pr_warn_once("NFSv4: server %s is incorrectly "
3363 "applying open mode checks to "
3364 "a SETATTR that is not "
3365 "changing file size.\n",
3366 server->nfs_client->cl_hostname);
3367 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003368 if (state && !(state->state & FMODE_WRITE)) {
3369 err = -EBADF;
3370 if (sattr->ia_valid & ATTR_OPEN)
3371 err = -EACCES;
3372 goto out;
3373 }
3374 }
3375 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003377out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return err;
3379}
3380
Peng Tao500d7012015-09-22 11:35:22 +08003381static bool
3382nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3383{
3384 if (inode == NULL || !nfs_have_layout(inode))
3385 return false;
3386
3387 return pnfs_wait_on_layoutreturn(inode, task);
3388}
3389
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003390/*
3391 * Update the seqid of an open stateid
3392 */
3393static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3394 struct nfs4_state *state)
3395{
3396 __be32 seqid_open;
3397 u32 dst_seqid;
3398 int seq;
3399
3400 for (;;) {
3401 if (!nfs4_valid_open_stateid(state))
3402 break;
3403 seq = read_seqbegin(&state->seqlock);
3404 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3405 nfs4_stateid_copy(dst, &state->open_stateid);
3406 if (read_seqretry(&state->seqlock, seq))
3407 continue;
3408 break;
3409 }
3410 seqid_open = state->open_stateid.seqid;
3411 if (read_seqretry(&state->seqlock, seq))
3412 continue;
3413
3414 dst_seqid = be32_to_cpu(dst->seqid);
3415 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3416 dst->seqid = seqid_open;
3417 break;
3418 }
3419}
3420
3421/*
3422 * Update the seqid of an open stateid after receiving
3423 * NFS4ERR_OLD_STATEID
3424 */
3425static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3426 struct nfs4_state *state)
3427{
3428 __be32 seqid_open;
3429 u32 dst_seqid;
3430 bool ret;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003431 int seq, status = -EAGAIN;
3432 DEFINE_WAIT(wait);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003433
3434 for (;;) {
3435 ret = false;
3436 if (!nfs4_valid_open_stateid(state))
3437 break;
3438 seq = read_seqbegin(&state->seqlock);
3439 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3440 if (read_seqretry(&state->seqlock, seq))
3441 continue;
3442 break;
3443 }
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003444
3445 write_seqlock(&state->seqlock);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003446 seqid_open = state->open_stateid.seqid;
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003447
3448 dst_seqid = be32_to_cpu(dst->seqid);
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003449
3450 /* Did another OPEN bump the state's seqid? try again: */
3451 if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003452 dst->seqid = seqid_open;
Benjamin Coddingtonb4868b42020-09-25 15:48:39 -04003453 write_sequnlock(&state->seqlock);
3454 ret = true;
3455 break;
3456 }
3457
3458 /* server says we're behind but we haven't seen the update yet */
3459 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3460 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3461 write_sequnlock(&state->seqlock);
3462 trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3463
3464 if (signal_pending(current))
3465 status = -EINTR;
3466 else
3467 if (schedule_timeout(5*HZ) != 0)
3468 status = 0;
3469
3470 finish_wait(&state->waitq, &wait);
3471
3472 if (!status)
3473 continue;
3474 if (status == -EINTR)
3475 break;
3476
3477 /* we slept the whole 5 seconds, we must have lost a seqid */
3478 dst->seqid = cpu_to_be32(dst_seqid + 1);
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003479 ret = true;
3480 break;
3481 }
3482
3483 return ret;
3484}
3485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486struct nfs4_closedata {
3487 struct inode *inode;
3488 struct nfs4_state *state;
3489 struct nfs_closeargs arg;
3490 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003491 struct {
3492 struct nfs4_layoutreturn_args arg;
3493 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003494 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003495 u32 roc_barrier;
3496 bool roc;
3497 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003498 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003499 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500};
3501
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003502static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003503{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003504 struct nfs4_closedata *calldata = data;
3505 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003506 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003507
Trond Myklebustcf805162016-11-15 14:56:07 -05003508 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003509 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3510 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003511 nfs4_put_open_state(calldata->state);
3512 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003513 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003514 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003515 kfree(calldata);
3516}
3517
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003518static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003520 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003523 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003524 struct nfs4_exception exception = {
3525 .state = state,
3526 .inode = calldata->inode,
3527 .stateid = &calldata->arg.stateid,
3528 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Chuck Levera3ca5652012-03-01 17:00:40 -05003530 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003531 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3532 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003533 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003534
3535 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05003536 if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3537 &calldata->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04003538 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003539
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003540 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 * the state_owner. we keep this around to process errors
3542 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 switch (task->tk_status) {
3544 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003545 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003546 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003547 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003548 case -NFS4ERR_ACCESS:
3549 if (calldata->arg.bitmask != NULL) {
3550 calldata->arg.bitmask = NULL;
3551 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003552 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003553
3554 }
3555 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003556 case -NFS4ERR_OLD_STATEID:
3557 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003558 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003559 state))
3560 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003561 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003562 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003564 case -NFS4ERR_EXPIRED:
3565 nfs4_free_revoked_stateid(server,
3566 &calldata->arg.stateid,
3567 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003568 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003569 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003570 if (calldata->arg.fmode == 0)
3571 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003572 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003574 task->tk_status = nfs4_async_handle_exception(task,
3575 server, task->tk_status, &exception);
3576 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003577 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003579 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3580 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003581out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003582 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003583 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003584 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003585 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003586 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003587out_restart:
3588 task->tk_status = 0;
3589 rpc_restart_call_prepare(task);
3590 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003593static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003595 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003596 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003597 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003598 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003599 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003600 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003601
Chuck Levera3ca5652012-03-01 17:00:40 -05003602 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003603 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003604 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003605
Trond Myklebust88069f72009-12-08 08:33:16 -05003606 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003607 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003608 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3609 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3610 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003611 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003612 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003613 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003614 if (state->n_rdonly == 0)
3615 call_close |= is_rdonly;
3616 else if (is_rdonly)
3617 calldata->arg.fmode |= FMODE_READ;
3618 if (state->n_wronly == 0)
3619 call_close |= is_wronly;
3620 else if (is_wronly)
3621 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003622 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3623 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003624 } else if (is_rdwr)
3625 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3626
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003627 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3628 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003629 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003630 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003631
3632 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003633 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003634 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003635 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003636
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003637 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003638 nfs_release_seqid(calldata->arg.seqid);
3639 goto out_wait;
3640 }
3641
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003642 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3643 if (lo && !pnfs_layout_is_valid(lo)) {
3644 calldata->arg.lr_args = NULL;
3645 calldata->res.lr_res = NULL;
3646 }
3647
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003648 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003649 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003650
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003651 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003652 /* Close-to-open cache consistency revalidation */
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003653 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003654 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04003655 nfs4_bitmask_adjust(calldata->arg.bitmask, inode, NFS_SERVER(inode), NULL);
3656 } else
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003657 calldata->arg.bitmask = NULL;
3658 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003659
Trond Myklebust6ae37332015-01-30 14:21:14 -05003660 calldata->arg.share_access =
3661 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3662 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003663
Trond Myklebustd8d84982016-12-19 12:14:44 -05003664 if (calldata->res.fattr == NULL)
3665 calldata->arg.bitmask = NULL;
3666 else if (calldata->arg.bitmask == NULL)
3667 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003668 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003669 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003670 &calldata->arg.seq_args,
3671 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003672 task) != 0)
3673 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003674 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003675 return;
3676out_no_action:
3677 task->tk_action = NULL;
3678out_wait:
3679 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680}
3681
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003682static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003683 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003684 .rpc_call_done = nfs4_close_done,
3685 .rpc_release = nfs4_free_closedata,
3686};
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688/*
3689 * It is possible for data to be read/written from a mem-mapped file
3690 * after the sys_close call (which hits the vfs layer as a flush).
3691 * This means that we can't safely call nfsv4 close on a file until
3692 * the inode is cleared. This in turn means that we are not good
3693 * NFSv4 citizens - we do not indicate to the server to update the file's
3694 * share state even when we are done with one of the three share
3695 * stateid's in the inode.
3696 *
3697 * NOTE: Caller must be holding the sp->so_owner semaphore!
3698 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003699int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003701 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003702 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003704 struct nfs4_state_owner *sp = state->owner;
3705 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003706 struct rpc_message msg = {
3707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3708 .rpc_cred = state->owner->so_cred,
3709 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003710 struct rpc_task_setup task_setup_data = {
3711 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003712 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003713 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003714 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003715 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003716 };
Trond Myklebust95121352005-10-18 14:20:12 -07003717 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003719 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3720 &task_setup_data.rpc_client, &msg);
3721
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003722 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003724 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003725 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003726 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003728 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003729 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3730 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003732 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3733 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003734 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003735 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003736 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003737 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003738 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003739 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003740 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003741 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003742 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003743 calldata->lr.roc = pnfs_roc(state->inode,
3744 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3745 if (calldata->lr.roc) {
3746 calldata->arg.lr_args = &calldata->lr.arg;
3747 calldata->res.lr_res = &calldata->lr.res;
3748 }
Al Viro643168c2011-06-22 18:20:23 -04003749 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003750
Trond Myklebust1174dd12010-12-21 10:52:24 -05003751 msg.rpc_argp = &calldata->arg;
3752 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003753 task_setup_data.callback_data = calldata;
3754 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003755 if (IS_ERR(task))
3756 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003757 status = 0;
3758 if (wait)
3759 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003760 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003761 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003762out_free_calldata:
3763 kfree(calldata);
3764out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003765 nfs4_put_open_state(state);
3766 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003767 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768}
3769
Trond Myklebust2b484292010-09-17 10:56:51 -04003770static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003771nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3772 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003775 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3776
3777 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Trond Myklebust565277f2007-10-15 18:17:53 -04003779 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003780 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003781
3782 nfs4_label_release_security(label);
3783
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003784 if (IS_ERR(state))
3785 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003786 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
Trond Myklebust1185a552009-12-03 15:54:02 -05003789static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003790{
3791 if (ctx->state == NULL)
3792 return;
3793 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003794 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003795 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003796 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003797}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Trond Myklebustb944dba2013-11-04 15:20:20 -05003799#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3800#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003801#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3804{
Kinglong Mee8c612822015-08-26 21:12:58 +08003805 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003806 struct nfs4_server_caps_arg args = {
3807 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003808 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003809 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 struct nfs4_server_caps_res res = {};
3811 struct rpc_message msg = {
3812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003813 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 .rpc_resp = &res,
3815 };
3816 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003817 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Kinglong Mee8c612822015-08-26 21:12:58 +08003819 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3820 FATTR4_WORD0_FH_EXPIRE_TYPE |
3821 FATTR4_WORD0_LINK_SUPPORT |
3822 FATTR4_WORD0_SYMLINK_SUPPORT |
3823 FATTR4_WORD0_ACLSUPPORT;
3824 if (minorversion)
3825 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3826
Bryan Schumaker7c513052011-03-24 17:12:24 +00003827 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003829 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003830 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003831 case 0:
3832 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3833 res.attr_bitmask[2] = 0;
3834 break;
3835 case 1:
3836 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3837 break;
3838 case 2:
3839 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003842 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3843 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3844 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3845 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003846 NFS_CAP_CTIME|NFS_CAP_MTIME|
3847 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003848 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3849 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 server->caps |= NFS_CAP_ACLS;
3851 if (res.has_links != 0)
3852 server->caps |= NFS_CAP_HARDLINKS;
3853 if (res.has_symlinks != 0)
3854 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003855 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3856 server->caps |= NFS_CAP_FILEID;
3857 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3858 server->caps |= NFS_CAP_MODE;
3859 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3860 server->caps |= NFS_CAP_NLINK;
3861 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3862 server->caps |= NFS_CAP_OWNER;
3863 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3864 server->caps |= NFS_CAP_OWNER_GROUP;
3865 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3866 server->caps |= NFS_CAP_ATIME;
3867 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3868 server->caps |= NFS_CAP_CTIME;
3869 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3870 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003871#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3872 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3873 server->caps |= NFS_CAP_SECURITY_LABEL;
3874#endif
3875 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3876 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003877 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003878
Trond Myklebusta65318b2009-03-11 14:10:28 -04003879 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3880 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3881 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003882 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003883
3884 /* Avoid a regression due to buggy server */
3885 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3886 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003887 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3888 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003891 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 return status;
3895}
3896
Trond Myklebust55a97592006-06-09 09:34:19 -04003897int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898{
Trond Myklebust0688e642019-04-07 13:59:09 -04003899 struct nfs4_exception exception = {
3900 .interruptible = true,
3901 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 int err;
3903 do {
3904 err = nfs4_handle_exception(server,
3905 _nfs4_server_capabilities(server, fhandle),
3906 &exception);
3907 } while (exception.retry);
3908 return err;
3909}
3910
3911static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3912 struct nfs_fsinfo *info)
3913{
David Quigleyaa9c2662013-05-22 12:50:44 -04003914 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003916 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 };
3918 struct nfs4_lookup_res res = {
3919 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003920 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 .fh = fhandle,
3922 };
3923 struct rpc_message msg = {
3924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3925 .rpc_argp = &args,
3926 .rpc_resp = &res,
3927 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003928
David Quigleyaa9c2662013-05-22 12:50:44 -04003929 bitmask[0] = nfs4_fattr_bitmap[0];
3930 bitmask[1] = nfs4_fattr_bitmap[1];
3931 /*
3932 * Process the label in the upcoming getfattr
3933 */
3934 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3935
Trond Myklebust0e574af2005-10-27 22:12:38 -04003936 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003937 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938}
3939
3940static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3941 struct nfs_fsinfo *info)
3942{
Trond Myklebust0688e642019-04-07 13:59:09 -04003943 struct nfs4_exception exception = {
3944 .interruptible = true,
3945 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 int err;
3947 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003948 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003949 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003950 switch (err) {
3951 case 0:
3952 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003953 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003954 default:
3955 err = nfs4_handle_exception(server, err, &exception);
3956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003958out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 return err;
3960}
3961
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003962static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3963 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3964{
Trond Myklebustc2190662013-08-26 19:23:04 -04003965 struct rpc_auth_create_args auth_args = {
3966 .pseudoflavor = flavor,
3967 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003968 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003969
Trond Myklebustc2190662013-08-26 19:23:04 -04003970 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003971 if (IS_ERR(auth))
3972 return -EACCES;
3973 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003974}
3975
Chuck Lever9a744ba2013-03-16 15:56:02 -04003976/*
3977 * Retry pseudoroot lookup with various security flavors. We do this when:
3978 *
3979 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3980 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3981 *
3982 * Returns zero on success, or a negative NFS4ERR value, or a
3983 * negative errno value.
3984 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003985static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003986 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003988 /* Per 3530bis 15.33.5 */
3989 static const rpc_authflavor_t flav_array[] = {
3990 RPC_AUTH_GSS_KRB5P,
3991 RPC_AUTH_GSS_KRB5I,
3992 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003993 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003994 RPC_AUTH_NULL,
3995 };
3996 int status = -EPERM;
3997 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003999 if (server->auth_info.flavor_len > 0) {
4000 /* try each flavor specified by user */
4001 for (i = 0; i < server->auth_info.flavor_len; i++) {
4002 status = nfs4_lookup_root_sec(server, fhandle, info,
4003 server->auth_info.flavors[i]);
4004 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4005 continue;
4006 break;
4007 }
4008 } else {
4009 /* no flavors specified by user, try default list */
4010 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4011 status = nfs4_lookup_root_sec(server, fhandle, info,
4012 flav_array[i]);
4013 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4014 continue;
4015 break;
4016 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00004017 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04004018
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004019 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01004020 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004021 * to access the mount. It could also mean that we tried to mount
4022 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4023 * existing mount programs don't handle -EACCES very well so it should
4024 * be mapped to -EPERM instead.
4025 */
4026 if (status == -EACCES)
4027 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004028 return status;
4029}
4030
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004031/**
4032 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4033 * @server: initialized nfs_server handle
4034 * @fhandle: we fill in the pseudo-fs root file handle
4035 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004036 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004037 *
4038 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004039 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004040int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004041 struct nfs_fsinfo *info,
4042 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004043{
Andre Przywarac7757072015-04-23 17:17:40 +01004044 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004045
Andre Przywarac7757072015-04-23 17:17:40 +01004046 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004047 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004048
4049 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004050 status = server->nfs_client->cl_mvops->find_root_sec(server,
4051 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004052
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 if (status == 0)
4054 status = nfs4_server_capabilities(server, fhandle);
4055 if (status == 0)
4056 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004057
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004058 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059}
4060
Bryan Schumakerbae36242012-05-10 15:07:31 -04004061static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4062 struct nfs_fsinfo *info)
4063{
4064 int error;
4065 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004066 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004067
4068 error = nfs4_server_capabilities(server, mntfh);
4069 if (error < 0) {
4070 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4071 return error;
4072 }
4073
Trond Myklebusta841b542018-04-07 13:50:59 -04004074 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004075 if (error < 0) {
4076 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004077 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004078 }
4079
4080 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4081 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4082 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4083
Scott Mayhew779df6a2020-03-03 17:58:37 -05004084out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004085 return error;
4086}
4087
Manoj Naik6b97fd32006-06-09 09:34:29 -04004088/*
4089 * Get locations and (maybe) other attributes of a referral.
4090 * Note that we'll actually follow the referral later when
4091 * we detect fsid mismatch in inode revalidation
4092 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004093static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4094 const struct qstr *name, struct nfs_fattr *fattr,
4095 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004096{
4097 int status = -ENOMEM;
4098 struct page *page = NULL;
4099 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004100
4101 page = alloc_page(GFP_KERNEL);
4102 if (page == NULL)
4103 goto out;
4104 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4105 if (locations == NULL)
4106 goto out;
4107
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004108 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004109 if (status != 0)
4110 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004111
4112 /*
4113 * If the fsid didn't change, this is a migration event, not a
4114 * referral. Cause us to drop into the exception handler, which
4115 * will kick off migration recovery.
4116 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004117 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004118 dprintk("%s: server did not return a different fsid for"
4119 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004120 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004121 goto out;
4122 }
Andy Adamson533eb462011-06-13 18:25:56 -04004123 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4124 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004125
Andy Adamson533eb462011-06-13 18:25:56 -04004126 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004127 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004128 memset(fhandle, 0, sizeof(struct nfs_fh));
4129out:
4130 if (page)
4131 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004132 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004133 return status;
4134}
4135
David Quigley1775fd32013-05-22 12:50:42 -04004136static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004137 struct nfs_fattr *fattr, struct nfs4_label *label,
4138 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
Trond Myklebust771734f2018-04-07 13:54:23 -04004140 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 struct nfs4_getattr_arg args = {
4142 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004143 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 };
4145 struct nfs4_getattr_res res = {
4146 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004147 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 .server = server,
4149 };
4150 struct rpc_message msg = {
4151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4152 .rpc_argp = &args,
4153 .rpc_resp = &res,
4154 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004155 unsigned short task_flags = 0;
4156
4157 /* Is this is an attribute revalidation, subject to softreval? */
4158 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4159 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004160
Trond Myklebust771734f2018-04-07 13:54:23 -04004161 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004162
Trond Myklebust0e574af2005-10-27 22:12:38 -04004163 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004164 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4165 return nfs4_do_call_sync(server->client, server, &msg,
4166 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167}
4168
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004169int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004170 struct nfs_fattr *fattr, struct nfs4_label *label,
4171 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172{
Trond Myklebust0688e642019-04-07 13:59:09 -04004173 struct nfs4_exception exception = {
4174 .interruptible = true,
4175 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 int err;
4177 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004178 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004179 trace_nfs4_getattr(server, fhandle, fattr, err);
4180 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 &exception);
4182 } while (exception.retry);
4183 return err;
4184}
4185
4186/*
4187 * The file is not closed if it is opened due to the a request to change
4188 * the size of the file. The open call will not be needed once the
4189 * VFS layer lookup-intents are implemented.
4190 *
4191 * Close is called when the inode is destroyed.
4192 * If we haven't opened the file for O_WRONLY, we
4193 * need to in the size_change case to obtain a stateid.
4194 *
4195 * Got race?
4196 * Because OPEN is always done by name in nfsv4, it is
4197 * possible that we opened a different file by the same
4198 * name. We can recognize this race condition, but we
4199 * can't do anything about it besides returning an error.
4200 *
4201 * This will be fixed with VFS changes (lookup-intent).
4202 */
4203static int
4204nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4205 struct iattr *sattr)
4206{
David Howells2b0143b2015-03-17 22:25:59 +00004207 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004208 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004209 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004210 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 int status;
4212
Peng Tao88ac8152014-09-12 11:04:10 +08004213 if (pnfs_ld_layoutret_on_setattr(inode) &&
4214 sattr->ia_valid & ATTR_SIZE &&
4215 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004216 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004217
Trond Myklebust0e574af2005-10-27 22:12:38 -04004218 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
Andy Adamson26699402012-05-30 16:12:24 -04004220 /* Deal with open(O_TRUNC) */
4221 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004222 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004223
4224 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004225 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004226 return 0;
4227
Trond Myklebustd5308382005-11-04 15:33:38 -05004228 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004229 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004230
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004231 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004232 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004233 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004234 }
4235
David Quigley14c43f72013-05-22 12:50:43 -04004236 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4237 if (IS_ERR(label))
4238 return PTR_ERR(label);
4239
Trond Myklebust199366f2018-03-20 16:43:18 -04004240 /* Return any delegations if we're going to change ACLs */
4241 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004242 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004243
NeilBrown29b59f92016-10-13 15:26:47 +11004244 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004245 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004246 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004247 nfs_setsecurity(inode, fattr, label);
4248 }
David Quigley14c43f72013-05-22 12:50:43 -04004249 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 return status;
4251}
4252
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004253static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004254 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004255 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004256{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004257 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004258 int status;
4259 struct nfs4_lookup_arg args = {
4260 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004261 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004262 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004263 };
4264 struct nfs4_lookup_res res = {
4265 .server = server,
4266 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004267 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004268 .fh = fhandle,
4269 };
4270 struct rpc_message msg = {
4271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4272 .rpc_argp = &args,
4273 .rpc_resp = &res,
4274 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004275 unsigned short task_flags = 0;
4276
4277 /* Is this is an attribute revalidation, subject to softreval? */
4278 if (nfs_lookup_is_soft_revalidate(dentry))
4279 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004280
David Quigleyaa9c2662013-05-22 12:50:44 -04004281 args.bitmask = nfs4_bitmask(server, label);
4282
David Howells2b3de442006-08-22 20:06:09 -04004283 nfs_fattr_init(fattr);
4284
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004285 dprintk("NFS call lookup %pd2\n", dentry);
4286 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4287 status = nfs4_do_call_sync(clnt, server, &msg,
4288 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 dprintk("NFS reply lookup: %d\n", status);
4290 return status;
4291}
4292
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004293static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004294{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004295 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004296 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004297 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4298 fattr->nlink = 2;
4299}
4300
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004301static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004302 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004303 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004304{
Trond Myklebust0688e642019-04-07 13:59:09 -04004305 struct nfs4_exception exception = {
4306 .interruptible = true,
4307 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004308 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004309 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004310 int err;
4311 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004312 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004313 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004314 switch (err) {
4315 case -NFS4ERR_BADNAME:
4316 err = -ENOENT;
4317 goto out;
4318 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004319 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004320 if (err == -NFS4ERR_MOVED)
4321 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004322 goto out;
4323 case -NFS4ERR_WRONGSEC:
4324 err = -EPERM;
4325 if (client != *clnt)
4326 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004327 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004328 if (IS_ERR(client))
4329 return PTR_ERR(client);
4330
4331 exception.retry = 1;
4332 break;
4333 default:
4334 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4335 }
4336 } while (exception.retry);
4337
4338out:
4339 if (err == 0)
4340 *clnt = client;
4341 else if (client != *clnt)
4342 rpc_shutdown_client(client);
4343
4344 return err;
4345}
4346
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004347static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004348 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4349 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004351 int status;
4352 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004353
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004354 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004355 if (client != NFS_CLIENT(dir)) {
4356 rpc_shutdown_client(client);
4357 nfs_fixup_secinfo_attributes(fattr);
4358 }
4359 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004362struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004363nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004364 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4365{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004366 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004367 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004368
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004369 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004370 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004371 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004372 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004373}
4374
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004375static int _nfs4_proc_lookupp(struct inode *inode,
4376 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4377 struct nfs4_label *label)
4378{
4379 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4380 struct nfs_server *server = NFS_SERVER(inode);
4381 int status;
4382 struct nfs4_lookupp_arg args = {
4383 .bitmask = server->attr_bitmask,
4384 .fh = NFS_FH(inode),
4385 };
4386 struct nfs4_lookupp_res res = {
4387 .server = server,
4388 .fattr = fattr,
4389 .label = label,
4390 .fh = fhandle,
4391 };
4392 struct rpc_message msg = {
4393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4394 .rpc_argp = &args,
4395 .rpc_resp = &res,
4396 };
Trond Myklebust76998eb2020-10-20 14:30:35 -04004397 unsigned short task_flags = 0;
4398
4399 if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4400 task_flags |= RPC_TASK_TIMEOUT;
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004401
4402 args.bitmask = nfs4_bitmask(server, label);
4403
4404 nfs_fattr_init(fattr);
4405
4406 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4407 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
Trond Myklebust76998eb2020-10-20 14:30:35 -04004408 &res.seq_res, task_flags);
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004409 dprintk("NFS reply lookupp: %d\n", status);
4410 return status;
4411}
4412
4413static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4414 struct nfs_fattr *fattr, struct nfs4_label *label)
4415{
Trond Myklebust0688e642019-04-07 13:59:09 -04004416 struct nfs4_exception exception = {
4417 .interruptible = true,
4418 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004419 int err;
4420 do {
4421 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4422 trace_nfs4_lookupp(inode, err);
4423 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4424 &exception);
4425 } while (exception.retry);
4426 return err;
4427}
4428
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4430{
Trond Myklebust76b32992007-08-10 17:45:11 -04004431 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 struct nfs4_accessargs args = {
4433 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004434 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004436 struct nfs4_accessres res = {
4437 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004438 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 struct rpc_message msg = {
4440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4441 .rpc_argp = &args,
4442 .rpc_resp = &res,
4443 .rpc_cred = entry->cred,
4444 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004445 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Trond Myklebust7c672652018-06-04 15:00:53 -04004447 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004448 res.fattr = nfs_alloc_fattr();
4449 if (res.fattr == NULL)
4450 return -ENOMEM;
4451 args.bitmask = server->cache_consistency_bitmask;
4452 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004453 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004455 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004456 if (res.fattr)
4457 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004459 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 return status;
4461}
4462
4463static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4464{
Trond Myklebust0688e642019-04-07 13:59:09 -04004465 struct nfs4_exception exception = {
4466 .interruptible = true,
4467 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 int err;
4469 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004470 err = _nfs4_proc_access(inode, entry);
4471 trace_nfs4_access(inode, err);
4472 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 &exception);
4474 } while (exception.retry);
4475 return err;
4476}
4477
4478/*
4479 * TODO: For the time being, we don't try to get any attributes
4480 * along with any of the zero-copy operations READ, READDIR,
4481 * READLINK, WRITE.
4482 *
4483 * In the case of the first three, we want to put the GETATTR
4484 * after the read-type operation -- this is because it is hard
4485 * to predict the length of a GETATTR response in v4, and thus
4486 * align the READ data correctly. This means that the GETATTR
4487 * may end up partially falling into the page cache, and we should
4488 * shift it into the 'tail' of the xdr_buf before processing.
4489 * To do this efficiently, we need to know the total length
4490 * of data received, which doesn't seem to be available outside
4491 * of the RPC layer.
4492 *
4493 * In the case of WRITE, we also want to put the GETATTR after
4494 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004495 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 *
4497 * Both of these changes to the XDR layer would in fact be quite
4498 * minor, but I decided to leave them for a subsequent patch.
4499 */
4500static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4501 unsigned int pgbase, unsigned int pglen)
4502{
4503 struct nfs4_readlink args = {
4504 .fh = NFS_FH(inode),
4505 .pgbase = pgbase,
4506 .pglen = pglen,
4507 .pages = &page,
4508 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004509 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 struct rpc_message msg = {
4511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4512 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004513 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 };
4515
Bryan Schumaker7c513052011-03-24 17:12:24 +00004516 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517}
4518
4519static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4520 unsigned int pgbase, unsigned int pglen)
4521{
Trond Myklebust0688e642019-04-07 13:59:09 -04004522 struct nfs4_exception exception = {
4523 .interruptible = true,
4524 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 int err;
4526 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004527 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4528 trace_nfs4_readlink(inode, err);
4529 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 &exception);
4531 } while (exception.retry);
4532 return err;
4533}
4534
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004536 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538static int
4539nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004540 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004542 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004543 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004544 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 int status = 0;
4547
NeilBrown532d4de2016-10-13 15:26:47 +11004548 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004549 if (IS_ERR(ctx))
4550 return PTR_ERR(ctx);
4551
David Quigleyaa9c2662013-05-22 12:50:44 -04004552 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4553
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004554 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4555 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004556 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 if (IS_ERR(state)) {
4558 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004559 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004562 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004563 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 return status;
4565}
4566
Trond Myklebust3c591172018-07-31 15:54:10 -04004567static int
4568_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
Trond Myklebust16e42952005-10-27 22:12:44 -04004570 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004571 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004573 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004575 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004576 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004577 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4580 .rpc_argp = &args,
4581 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004583 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004584 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004585
Bryan Schumaker7c513052011-03-24 17:12:24 +00004586 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004587 if (status == 0) {
4588 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004589 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4590 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004591 /* Removing a directory decrements nlink in the parent */
4592 if (ftype == NF4DIR && dir->i_nlink > 2)
4593 nfs4_dec_nlink_locked(dir);
4594 spin_unlock(&dir->i_lock);
4595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 return status;
4597}
4598
Trond Myklebust912678d2018-03-20 16:43:15 -04004599static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4600{
Trond Myklebust0688e642019-04-07 13:59:09 -04004601 struct nfs4_exception exception = {
4602 .interruptible = true,
4603 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004604 struct inode *inode = d_inode(dentry);
4605 int err;
4606
Trond Myklebustc01d3642018-03-20 16:43:20 -04004607 if (inode) {
4608 if (inode->i_nlink == 1)
4609 nfs4_inode_return_delegation(inode);
4610 else
4611 nfs4_inode_make_writeable(inode);
4612 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004613 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004614 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004615 trace_nfs4_remove(dir, &dentry->d_name, err);
4616 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4617 &exception);
4618 } while (exception.retry);
4619 return err;
4620}
4621
4622static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623{
Trond Myklebust0688e642019-04-07 13:59:09 -04004624 struct nfs4_exception exception = {
4625 .interruptible = true,
4626 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004630 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004631 trace_nfs4_remove(dir, name, err);
4632 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 &exception);
4634 } while (exception.retry);
4635 return err;
4636}
4637
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004638static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4639 struct dentry *dentry,
4640 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004642 struct nfs_removeargs *args = msg->rpc_argp;
4643 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Trond Myklebust977fcc22018-03-20 16:43:17 -04004645 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004647 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004648
4649 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004650
4651 if (inode)
4652 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004655static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4656{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004657 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004658 &data->args.seq_args,
4659 &data->res.seq_res,
4660 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661}
4662
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004663static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004665 struct nfs_unlinkdata *data = task->tk_calldata;
4666 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004667
Trond Myklebust14516c32010-07-31 14:29:06 -04004668 if (!nfs4_sequence_done(task, &res->seq_res))
4669 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004670 if (nfs4_async_handle_error(task, res->server, NULL,
4671 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004672 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004673 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004674 nfs4_update_changeattr(dir, &res->cinfo,
4675 res->dir_attr->time_start,
4676 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004677 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678}
4679
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004680static void nfs4_proc_rename_setup(struct rpc_message *msg,
4681 struct dentry *old_dentry,
4682 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004683{
Jeff Laytond3d41522010-09-17 17:31:57 -04004684 struct nfs_renameargs *arg = msg->rpc_argp;
4685 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004686 struct inode *old_inode = d_inode(old_dentry);
4687 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004688
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004689 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004690 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004691 if (new_inode)
4692 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004693 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004694 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004695 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004696}
4697
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004698static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4699{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004700 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004701 &data->args.seq_args,
4702 &data->res.seq_res,
4703 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004704}
4705
4706static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4707 struct inode *new_dir)
4708{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004709 struct nfs_renamedata *data = task->tk_calldata;
4710 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004711
4712 if (!nfs4_sequence_done(task, &res->seq_res))
4713 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004714 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004715 return 0;
4716
Trond Myklebustc733c492017-01-11 12:32:26 -05004717 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004718 if (new_dir != old_dir) {
4719 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004720 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004721 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004722 NFS_INO_INVALID_OTHER |
4723 NFS_INO_INVALID_DATA);
4724 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004725 res->new_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004726 NFS_INO_INVALID_OTHER |
4727 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004728 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004729 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004730 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004731 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004732 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004733 return 1;
4734}
4735
Al Virobeffb8f2016-07-20 16:34:42 -04004736static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004738 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004739 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 struct nfs4_link_arg arg = {
4741 .fh = NFS_FH(inode),
4742 .dir_fh = NFS_FH(dir),
4743 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004744 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004746 struct nfs4_link_res res = {
4747 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004748 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004749 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 struct rpc_message msg = {
4751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4752 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004753 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004755 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Trond Myklebust136f2622010-04-16 16:22:49 -04004757 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004758 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004759 goto out;
4760
David Quigley14c43f72013-05-22 12:50:43 -04004761 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4762 if (IS_ERR(res.label)) {
4763 status = PTR_ERR(res.label);
4764 goto out;
4765 }
4766
Trond Myklebustc01d3642018-03-20 16:43:20 -04004767 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004768 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004769
Bryan Schumaker7c513052011-03-24 17:12:24 +00004770 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004771 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004772 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4773 NFS_INO_INVALID_DATA);
David Quigleyaa9c2662013-05-22 12:50:44 -04004774 status = nfs_post_op_update_inode(inode, res.fattr);
4775 if (!status)
4776 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004777 }
David Quigley14c43f72013-05-22 12:50:43 -04004778
4779
4780 nfs4_label_free(res.label);
4781
Trond Myklebust136f2622010-04-16 16:22:49 -04004782out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004783 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 return status;
4785}
4786
Al Virobeffb8f2016-07-20 16:34:42 -04004787static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788{
Trond Myklebust0688e642019-04-07 13:59:09 -04004789 struct nfs4_exception exception = {
4790 .interruptible = true,
4791 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 int err;
4793 do {
4794 err = nfs4_handle_exception(NFS_SERVER(inode),
4795 _nfs4_proc_link(inode, dir, name),
4796 &exception);
4797 } while (exception.retry);
4798 return err;
4799}
4800
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004801struct nfs4_createdata {
4802 struct rpc_message msg;
4803 struct nfs4_create_arg arg;
4804 struct nfs4_create_res res;
4805 struct nfs_fh fh;
4806 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004807 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004808};
4809
4810static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004811 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004812{
4813 struct nfs4_createdata *data;
4814
4815 data = kzalloc(sizeof(*data), GFP_KERNEL);
4816 if (data != NULL) {
4817 struct nfs_server *server = NFS_SERVER(dir);
4818
David Quigley14c43f72013-05-22 12:50:43 -04004819 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4820 if (IS_ERR(data->label))
4821 goto out_free;
4822
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004823 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4824 data->msg.rpc_argp = &data->arg;
4825 data->msg.rpc_resp = &data->res;
4826 data->arg.dir_fh = NFS_FH(dir);
4827 data->arg.server = server;
4828 data->arg.name = name;
4829 data->arg.attrs = sattr;
4830 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004831 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004832 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004833 data->res.server = server;
4834 data->res.fh = &data->fh;
4835 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004836 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004837 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004838 }
4839 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004840out_free:
4841 kfree(data);
4842 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004843}
4844
4845static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4846{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004847 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004848 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004849 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004850 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004851 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4852 data->res.fattr->time_start,
4853 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004854 /* Creating a directory bumps nlink in the parent */
4855 if (data->arg.ftype == NF4DIR)
4856 nfs4_inc_nlink_locked(dir);
4857 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004858 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004859 }
4860 return status;
4861}
4862
4863static void nfs4_free_createdata(struct nfs4_createdata *data)
4864{
David Quigley14c43f72013-05-22 12:50:43 -04004865 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004866 kfree(data);
4867}
4868
Chuck Lever4f390c12006-08-22 20:06:22 -04004869static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004870 struct page *page, unsigned int len, struct iattr *sattr,
4871 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004873 struct nfs4_createdata *data;
4874 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
Chuck Lever94a6d752006-08-22 20:06:23 -04004876 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004877 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004878
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004879 status = -ENOMEM;
4880 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4881 if (data == NULL)
4882 goto out;
4883
4884 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4885 data->arg.u.symlink.pages = &page;
4886 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004887 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004889 status = nfs4_do_create(dir, dentry, data);
4890
4891 nfs4_free_createdata(data);
4892out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 return status;
4894}
4895
Chuck Lever4f390c12006-08-22 20:06:22 -04004896static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004897 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898{
Trond Myklebust0688e642019-04-07 13:59:09 -04004899 struct nfs4_exception exception = {
4900 .interruptible = true,
4901 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004902 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004904
4905 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004908 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4909 trace_nfs4_symlink(dir, &dentry->d_name, err);
4910 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 &exception);
4912 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004913
4914 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 return err;
4916}
4917
4918static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004919 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004921 struct nfs4_createdata *data;
4922 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004924 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4925 if (data == NULL)
4926 goto out;
4927
David Quigley1775fd32013-05-22 12:50:42 -04004928 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004929 status = nfs4_do_create(dir, dentry, data);
4930
4931 nfs4_free_createdata(data);
4932out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 return status;
4934}
4935
4936static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4937 struct iattr *sattr)
4938{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004939 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004940 struct nfs4_exception exception = {
4941 .interruptible = true,
4942 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004943 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004945
David Quigleyaa9c2662013-05-22 12:50:44 -04004946 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4947
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004948 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4949 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004951 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4952 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4953 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 &exception);
4955 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004956 nfs4_label_release_security(label);
4957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 return err;
4959}
4960
Trond Myklebust82e22a52020-11-02 17:34:23 -05004961static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
4962 struct nfs_readdir_res *nr_res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963{
Trond Myklebust82e22a52020-11-02 17:34:23 -05004964 struct inode *dir = d_inode(nr_arg->dentry);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004965 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 struct nfs4_readdir_arg args = {
4967 .fh = NFS_FH(dir),
Trond Myklebust82e22a52020-11-02 17:34:23 -05004968 .pages = nr_arg->pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 .pgbase = 0,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004970 .count = nr_arg->page_len,
4971 .plus = nr_arg->plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 };
4973 struct nfs4_readdir_res res;
4974 struct rpc_message msg = {
4975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4976 .rpc_argp = &args,
4977 .rpc_resp = &res,
Trond Myklebust82e22a52020-11-02 17:34:23 -05004978 .rpc_cred = nr_arg->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 };
4980 int status;
4981
Trond Myklebust82e22a52020-11-02 17:34:23 -05004982 dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
4983 nr_arg->dentry, (unsigned long long)nr_arg->cookie);
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004984 if (!(server->caps & NFS_CAP_SECURITY_LABEL))
4985 args.bitmask = server->attr_bitmask_nl;
4986 else
4987 args.bitmask = server->attr_bitmask;
4988
Trond Myklebust82e22a52020-11-02 17:34:23 -05004989 nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 res.pgbase = args.pgbase;
Olga Kornievskaia05ad9172020-11-06 16:03:38 -05004991 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
4992 &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05004993 if (status >= 0) {
Trond Myklebust82e22a52020-11-02 17:34:23 -05004994 memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004995 status += args.pgbase;
4996 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004997
4998 nfs_invalidate_atime(dir);
4999
Harvey Harrison3110ff82008-05-02 13:42:44 -07005000 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 return status;
5002}
5003
Trond Myklebust82e22a52020-11-02 17:34:23 -05005004static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5005 struct nfs_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
Trond Myklebust0688e642019-04-07 13:59:09 -04005007 struct nfs4_exception exception = {
5008 .interruptible = true,
5009 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 int err;
5011 do {
Trond Myklebust82e22a52020-11-02 17:34:23 -05005012 err = _nfs4_proc_readdir(arg, res);
5013 trace_nfs4_readdir(d_inode(arg->dentry), err);
5014 err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5015 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 } while (exception.retry);
5017 return err;
5018}
5019
5020static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04005021 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005023 struct nfs4_createdata *data;
5024 int mode = sattr->ia_mode;
5025 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005027 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5028 if (data == NULL)
5029 goto out;
5030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005032 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005034 data->arg.ftype = NF4BLK;
5035 data->arg.u.device.specdata1 = MAJOR(rdev);
5036 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 }
5038 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005039 data->arg.ftype = NF4CHR;
5040 data->arg.u.device.specdata1 = MAJOR(rdev);
5041 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005042 } else if (!S_ISSOCK(mode)) {
5043 status = -EINVAL;
5044 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 }
David Quigley1775fd32013-05-22 12:50:42 -04005046
David Quigleyaa9c2662013-05-22 12:50:44 -04005047 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005048 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005049out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005050 nfs4_free_createdata(data);
5051out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 return status;
5053}
5054
5055static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5056 struct iattr *sattr, dev_t rdev)
5057{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005058 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005059 struct nfs4_exception exception = {
5060 .interruptible = true,
5061 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005062 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005064
David Quigleyaa9c2662013-05-22 12:50:44 -04005065 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5066
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005067 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5068 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005070 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5071 trace_nfs4_mknod(dir, &dentry->d_name, err);
5072 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 &exception);
5074 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005075
5076 nfs4_label_release_security(label);
5077
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return err;
5079}
5080
5081static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5082 struct nfs_fsstat *fsstat)
5083{
5084 struct nfs4_statfs_arg args = {
5085 .fh = fhandle,
5086 .bitmask = server->attr_bitmask,
5087 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005088 struct nfs4_statfs_res res = {
5089 .fsstat = fsstat,
5090 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 struct rpc_message msg = {
5092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5093 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005094 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 };
5096
Trond Myklebust0e574af2005-10-27 22:12:38 -04005097 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005098 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099}
5100
5101static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5102{
Trond Myklebust0688e642019-04-07 13:59:09 -04005103 struct nfs4_exception exception = {
5104 .interruptible = true,
5105 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 int err;
5107 do {
5108 err = nfs4_handle_exception(server,
5109 _nfs4_proc_statfs(server, fhandle, fsstat),
5110 &exception);
5111 } while (exception.retry);
5112 return err;
5113}
5114
5115static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5116 struct nfs_fsinfo *fsinfo)
5117{
5118 struct nfs4_fsinfo_arg args = {
5119 .fh = fhandle,
5120 .bitmask = server->attr_bitmask,
5121 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005122 struct nfs4_fsinfo_res res = {
5123 .fsinfo = fsinfo,
5124 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 struct rpc_message msg = {
5126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5127 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005128 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 };
5130
Bryan Schumaker7c513052011-03-24 17:12:24 +00005131 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132}
5133
5134static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5135{
Trond Myklebust0688e642019-04-07 13:59:09 -04005136 struct nfs4_exception exception = {
5137 .interruptible = true,
5138 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 int err;
5140
5141 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005142 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005143 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005144 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005145 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005146 break;
5147 }
5148 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 } while (exception.retry);
5150 return err;
5151}
5152
5153static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5154{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005155 int error;
5156
Trond Myklebust0e574af2005-10-27 22:12:38 -04005157 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005158 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005159 if (error == 0) {
5160 /* block layout checks this! */
5161 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005162 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005163 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005164
5165 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166}
5167
5168static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5169 struct nfs_pathconf *pathconf)
5170{
5171 struct nfs4_pathconf_arg args = {
5172 .fh = fhandle,
5173 .bitmask = server->attr_bitmask,
5174 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005175 struct nfs4_pathconf_res res = {
5176 .pathconf = pathconf,
5177 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 struct rpc_message msg = {
5179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5180 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005181 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 };
5183
5184 /* None of the pathconf attributes are mandatory to implement */
5185 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5186 memset(pathconf, 0, sizeof(*pathconf));
5187 return 0;
5188 }
5189
Trond Myklebust0e574af2005-10-27 22:12:38 -04005190 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005191 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192}
5193
5194static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5195 struct nfs_pathconf *pathconf)
5196{
Trond Myklebust0688e642019-04-07 13:59:09 -04005197 struct nfs4_exception exception = {
5198 .interruptible = true,
5199 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 int err;
5201
5202 do {
5203 err = nfs4_handle_exception(server,
5204 _nfs4_proc_pathconf(server, fhandle, pathconf),
5205 &exception);
5206 } while (exception.retry);
5207 return err;
5208}
5209
Trond Myklebust5521abf2013-03-16 20:54:34 -04005210int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005211 const struct nfs_open_context *ctx,
5212 const struct nfs_lock_context *l_ctx,
5213 fmode_t fmode)
5214{
NeilBrown17393472016-10-13 15:26:47 +11005215 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005216}
5217EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5218
Trond Myklebust5521abf2013-03-16 20:54:34 -04005219static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5220 const struct nfs_open_context *ctx,
5221 const struct nfs_lock_context *l_ctx,
5222 fmode_t fmode)
5223{
Ben Dooksd49dd112019-10-16 17:28:21 +01005224 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005225
Trond Myklebuste1253be2014-03-05 08:44:23 -05005226 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005227 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005228 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005229 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005230}
5231
5232static bool nfs4_error_stateid_expired(int err)
5233{
5234 switch (err) {
5235 case -NFS4ERR_DELEG_REVOKED:
5236 case -NFS4ERR_ADMIN_REVOKED:
5237 case -NFS4ERR_BAD_STATEID:
5238 case -NFS4ERR_STALE_STATEID:
5239 case -NFS4ERR_OLD_STATEID:
5240 case -NFS4ERR_OPENMODE:
5241 case -NFS4ERR_EXPIRED:
5242 return true;
5243 }
5244 return false;
5245}
5246
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005247static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005249 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005251 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005252 if (task->tk_status < 0) {
5253 struct nfs4_exception exception = {
5254 .inode = hdr->inode,
5255 .state = hdr->args.context->state,
5256 .stateid = &hdr->args.stateid,
5257 };
5258 task->tk_status = nfs4_async_handle_exception(task,
5259 server, task->tk_status, &exception);
5260 if (exception.retry) {
5261 rpc_restart_call_prepare(task);
5262 return -EAGAIN;
5263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005265
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005267 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269}
5270
Trond Myklebust5521abf2013-03-16 20:54:34 -04005271static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005272 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005273{
5274
5275 if (!nfs4_error_stateid_expired(task->tk_status) ||
5276 nfs4_stateid_is_current(&args->stateid,
5277 args->context,
5278 args->lock_context,
5279 FMODE_READ))
5280 return false;
5281 rpc_restart_call_prepare(task);
5282 return true;
5283}
5284
Anna Schumakerc5675522014-05-28 13:41:22 -04005285static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5286 struct nfs_pgio_header *hdr)
5287{
5288 struct nfs_server *server = NFS_SERVER(hdr->inode);
5289 struct rpc_message *msg = &task->tk_msg;
5290
5291 if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5292 server->caps & NFS_CAP_READ_PLUS && task->tk_status == -ENOTSUPP) {
5293 server->caps &= ~NFS_CAP_READ_PLUS;
5294 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5295 rpc_restart_call_prepare(task);
5296 return true;
5297 }
5298 return false;
5299}
5300
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005301static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005302{
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005303 dprintk("--> %s\n", __func__);
5304
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005305 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005306 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005307 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005308 return -EAGAIN;
Anna Schumakerc5675522014-05-28 13:41:22 -04005309 if (nfs4_read_plus_not_supported(task, hdr))
5310 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005311 if (task->tk_status > 0)
5312 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005313 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5314 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005315}
5316
Anna Schumaker21e31402020-12-03 15:18:39 -05005317#if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005318static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5319 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005320{
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005321 /* Note: We don't use READ_PLUS with pNFS yet */
5322 if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp)
Anna Schumakerc5675522014-05-28 13:41:22 -04005323 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
Anna Schumakerc5675522014-05-28 13:41:22 -04005324}
5325#else
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005326static void nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5327 struct rpc_message *msg)
Anna Schumakerc5675522014-05-28 13:41:22 -04005328{
Anna Schumakerc5675522014-05-28 13:41:22 -04005329}
5330#endif /* CONFIG_NFS_V4_2 */
5331
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005332static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5333 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005335 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005336 if (!hdr->pgio_done_cb)
5337 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebust5c3485b2020-12-10 09:34:34 -05005338 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5339 nfs42_read_plus_support(hdr, msg);
Anna Schumakerfba83f32018-05-04 16:22:50 -04005340 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341}
5342
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005343static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5344 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005345{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005346 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005347 &hdr->args.seq_args,
5348 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005349 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005350 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005351 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5352 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005353 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005354 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005355 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005356 return -EIO;
5357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358}
5359
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005360static int nfs4_write_done_cb(struct rpc_task *task,
5361 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005363 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005364
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005365 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005366 if (task->tk_status < 0) {
5367 struct nfs4_exception exception = {
5368 .inode = hdr->inode,
5369 .state = hdr->args.context->state,
5370 .stateid = &hdr->args.stateid,
5371 };
5372 task->tk_status = nfs4_async_handle_exception(task,
5373 NFS_SERVER(inode), task->tk_status,
5374 &exception);
5375 if (exception.retry) {
5376 rpc_restart_call_prepare(task);
5377 return -EAGAIN;
5378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005380 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005381 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005382 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005383 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385}
5386
Trond Myklebust5521abf2013-03-16 20:54:34 -04005387static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005388 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005389{
5390
5391 if (!nfs4_error_stateid_expired(task->tk_status) ||
5392 nfs4_stateid_is_current(&args->stateid,
5393 args->context,
5394 args->lock_context,
5395 FMODE_WRITE))
5396 return false;
5397 rpc_restart_call_prepare(task);
5398 return true;
5399}
5400
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005401static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005402{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005403 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005404 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005405 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005406 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005407 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5408 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005409}
5410
Trond Myklebust5a37f852012-04-28 14:55:16 -04005411static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005412bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005413{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005414 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005415 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005416 return false;
5417 /* Otherwise, request attributes if and only if we don't hold
5418 * a delegation
5419 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005420 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005421}
Fred Isamana69aef12011-03-03 15:13:47 +00005422
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005423static void nfs4_bitmask_adjust(__u32 *bitmask, struct inode *inode,
5424 struct nfs_server *server,
5425 struct nfs4_label *label)
5426{
5427
5428 unsigned long cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
5429
5430 if ((cache_validity & NFS_INO_INVALID_DATA) ||
5431 (cache_validity & NFS_INO_REVAL_PAGECACHE) ||
5432 (cache_validity & NFS_INO_REVAL_FORCED) ||
5433 (cache_validity & NFS_INO_INVALID_OTHER))
5434 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
5435
5436 if (cache_validity & NFS_INO_INVALID_ATIME)
5437 bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5438 if (cache_validity & NFS_INO_INVALID_ACCESS)
5439 bitmask[0] |= FATTR4_WORD1_MODE | FATTR4_WORD1_OWNER |
5440 FATTR4_WORD1_OWNER_GROUP;
5441 if (cache_validity & NFS_INO_INVALID_ACL)
5442 bitmask[0] |= FATTR4_WORD0_ACL;
5443 if (cache_validity & NFS_INO_INVALID_LABEL)
5444 bitmask[2] |= FATTR4_WORD2_SECURITY_LABEL;
5445 if (cache_validity & NFS_INO_INVALID_CTIME)
5446 bitmask[0] |= FATTR4_WORD0_CHANGE;
5447 if (cache_validity & NFS_INO_INVALID_MTIME)
5448 bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5449 if (cache_validity & NFS_INO_INVALID_SIZE)
5450 bitmask[0] |= FATTR4_WORD0_SIZE;
5451 if (cache_validity & NFS_INO_INVALID_BLOCKS)
5452 bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5453}
5454
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005455static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005456 struct rpc_message *msg,
5457 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005459 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005460
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005461 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5462 hdr->args.bitmask = NULL;
5463 hdr->res.fattr = NULL;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005464 } else {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005465 hdr->args.bitmask = server->cache_consistency_bitmask;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04005466 nfs4_bitmask_adjust(hdr->args.bitmask, hdr->inode, server, NULL);
5467 }
Trond Myklebust5a37f852012-04-28 14:55:16 -04005468
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005469 if (!hdr->pgio_done_cb)
5470 hdr->pgio_done_cb = nfs4_write_done_cb;
5471 hdr->res.server = server;
5472 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005474 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005475 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005476 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477}
5478
Fred Isaman0b7c0152012-04-20 14:47:39 -04005479static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5480{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005481 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005482 &data->args.seq_args,
5483 &data->res.seq_res,
5484 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485}
5486
Fred Isaman0b7c0152012-04-20 14:47:39 -04005487static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005490
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005491 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005492 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5493 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005494 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005495 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498}
5499
Fred Isaman0b7c0152012-04-20 14:47:39 -04005500static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005501{
5502 if (!nfs4_sequence_done(task, &data->res.seq_res))
5503 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005504 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005505}
5506
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005507static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5508 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005510 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005511
Fred Isaman0b7c0152012-04-20 14:47:39 -04005512 if (data->commit_done_cb == NULL)
5513 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005514 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005515 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005516 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005517 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518}
5519
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005520static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5521 struct nfs_commitres *res)
5522{
5523 struct inode *dst_inode = file_inode(dst);
5524 struct nfs_server *server = NFS_SERVER(dst_inode);
5525 struct rpc_message msg = {
5526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5527 .rpc_argp = args,
5528 .rpc_resp = res,
5529 };
5530
5531 args->fh = NFS_FH(dst_inode);
5532 return nfs4_call_sync(server->client, server, &msg,
5533 &args->seq_args, &res->seq_res, 1);
5534}
5535
5536int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5537{
5538 struct nfs_commitargs args = {
5539 .offset = offset,
5540 .count = count,
5541 };
5542 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5543 struct nfs4_exception exception = { };
5544 int status;
5545
5546 do {
5547 status = _nfs4_proc_commit(dst, &args, res);
5548 status = nfs4_handle_exception(dst_server, status, &exception);
5549 } while (exception.retry);
5550
5551 return status;
5552}
5553
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005554struct nfs4_renewdata {
5555 struct nfs_client *client;
5556 unsigned long timestamp;
5557};
5558
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559/*
5560 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5561 * standalone procedure for queueing an asynchronous RENEW.
5562 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005563static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005564{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005565 struct nfs4_renewdata *data = calldata;
5566 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005567
Elena Reshetova212bf412017-10-20 12:53:38 +03005568 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005569 nfs4_schedule_state_renewal(clp);
5570 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005571 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005572}
5573
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005574static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005576 struct nfs4_renewdata *data = calldata;
5577 struct nfs_client *clp = data->client;
5578 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005580 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005581 switch (task->tk_status) {
5582 case 0:
5583 break;
5584 case -NFS4ERR_LEASE_MOVED:
5585 nfs4_schedule_lease_moved_recovery(clp);
5586 break;
5587 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005588 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005589 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5590 return;
5591 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005592 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005593 return;
5594 }
5595 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005597 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598}
5599
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005600static const struct rpc_call_ops nfs4_renew_ops = {
5601 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005602 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005603};
5604
NeilBrowna52458b2018-12-03 11:30:31 +11005605static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606{
5607 struct rpc_message msg = {
5608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5609 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005610 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005612 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005614 if (renew_flags == 0)
5615 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005616 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005617 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005618 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005619 if (data == NULL) {
5620 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005621 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005622 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005623 data->client = clp;
5624 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005625 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005626 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627}
5628
NeilBrowna52458b2018-12-03 11:30:31 +11005629static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630{
5631 struct rpc_message msg = {
5632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5633 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005634 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 };
5636 unsigned long now = jiffies;
5637 int status;
5638
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005639 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 if (status < 0)
5641 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005642 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 return 0;
5644}
5645
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005646static inline int nfs4_server_supports_acls(struct nfs_server *server)
5647{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005648 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005649}
5650
Trond Myklebust21f498c2012-08-24 10:59:25 -04005651/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5652 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005653 * the stack.
5654 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005655#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005656
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005657int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005658 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005659{
5660 struct page *newpage, **spages;
5661 int rc = 0;
5662 size_t len;
5663 spages = pages;
5664
5665 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005666 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005667 newpage = alloc_page(GFP_KERNEL);
5668
5669 if (newpage == NULL)
5670 goto unwind;
5671 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005672 buf += len;
5673 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005674 *pages++ = newpage;
5675 rc++;
5676 } while (buflen != 0);
5677
5678 return rc;
5679
5680unwind:
5681 for(; rc > 0; rc--)
5682 __free_page(spages[rc-1]);
5683 return -ENOMEM;
5684}
5685
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005686struct nfs4_cached_acl {
5687 int cached;
5688 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005689 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005690};
5691
5692static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005693{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005694 struct nfs_inode *nfsi = NFS_I(inode);
5695
5696 spin_lock(&inode->i_lock);
5697 kfree(nfsi->nfs4_acl);
5698 nfsi->nfs4_acl = acl;
5699 spin_unlock(&inode->i_lock);
5700}
5701
5702static void nfs4_zap_acl_attr(struct inode *inode)
5703{
5704 nfs4_set_cached_acl(inode, NULL);
5705}
5706
5707static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5708{
5709 struct nfs_inode *nfsi = NFS_I(inode);
5710 struct nfs4_cached_acl *acl;
5711 int ret = -ENOENT;
5712
5713 spin_lock(&inode->i_lock);
5714 acl = nfsi->nfs4_acl;
5715 if (acl == NULL)
5716 goto out;
5717 if (buf == NULL) /* user is just asking for length */
5718 goto out_len;
5719 if (acl->cached == 0)
5720 goto out;
5721 ret = -ERANGE; /* see getxattr(2) man page */
5722 if (acl->len > buflen)
5723 goto out;
5724 memcpy(buf, acl->data, acl->len);
5725out_len:
5726 ret = acl->len;
5727out:
5728 spin_unlock(&inode->i_lock);
5729 return ret;
5730}
5731
Sachin Prabhu5794d212012-04-17 14:36:40 +01005732static 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 +00005733{
5734 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005735 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005736
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005737 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005738 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005739 if (acl == NULL)
5740 goto out;
5741 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005742 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005743 } else {
5744 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5745 if (acl == NULL)
5746 goto out;
5747 acl->cached = 0;
5748 }
5749 acl->len = acl_len;
5750out:
5751 nfs4_set_cached_acl(inode, acl);
5752}
5753
Andy Adamsonbf118a32011-12-07 11:55:27 -05005754/*
5755 * The getxattr API returns the required buffer length when called with a
5756 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5757 * the required buf. On a NULL buf, we send a page of data to the server
5758 * guessing that the ACL request can be serviced by a page. If so, we cache
5759 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5760 * the cache. If not so, we throw away the page, and cache the required
5761 * length. The next getxattr call will then produce another round trip to
5762 * the server, this time with the input buf of the required size.
5763 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005764static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005765{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005766 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005767 struct nfs_getaclargs args = {
5768 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005769 .acl_len = buflen,
5770 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005771 struct nfs_getaclres res = {
5772 .acl_len = buflen,
5773 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005774 struct rpc_message msg = {
5775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5776 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005777 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005778 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005779 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005780 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005781 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005782
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005783 if (buflen == 0)
5784 buflen = server->rsize;
5785
5786 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5787 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5788 if (!pages)
5789 return -ENOMEM;
5790
5791 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005792
Andy Adamsonbf118a32011-12-07 11:55:27 -05005793 for (i = 0; i < npages; i++) {
5794 pages[i] = alloc_page(GFP_KERNEL);
5795 if (!pages[i])
5796 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005797 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005798
5799 /* for decoding across pages */
5800 res.acl_scratch = alloc_page(GFP_KERNEL);
5801 if (!res.acl_scratch)
5802 goto out_free;
5803
Andy Adamsonbf118a32011-12-07 11:55:27 -05005804 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005805
Peng Taode040be2012-01-10 22:42:47 +08005806 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005807 __func__, buf, buflen, npages, args.acl_len);
5808 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5809 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005810 if (ret)
5811 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005812
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005813 /* Handle the case where the passed-in buffer is too short */
5814 if (res.acl_flags & NFS4_ACL_TRUNC) {
5815 /* Did the user only issue a request for the acl length? */
5816 if (buf == NULL)
5817 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005818 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005819 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005820 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005821 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005822 if (buf) {
5823 if (res.acl_len > buflen) {
5824 ret = -ERANGE;
5825 goto out_free;
5826 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005827 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005828 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005829out_ok:
5830 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005831out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005832 for (i = 0; i < npages; i++)
5833 if (pages[i])
5834 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005835 if (res.acl_scratch)
5836 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005837 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005838 return ret;
5839}
5840
Trond Myklebust16b42892006-08-24 12:27:15 -04005841static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5842{
Trond Myklebust0688e642019-04-07 13:59:09 -04005843 struct nfs4_exception exception = {
5844 .interruptible = true,
5845 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005846 ssize_t ret;
5847 do {
5848 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005849 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005850 if (ret >= 0)
5851 break;
5852 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5853 } while (exception.retry);
5854 return ret;
5855}
5856
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005857static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5858{
5859 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005860 int ret;
5861
5862 if (!nfs4_server_supports_acls(server))
5863 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005864 ret = nfs_revalidate_inode(server, inode);
5865 if (ret < 0)
5866 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005867 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5868 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005869 ret = nfs4_read_cached_acl(inode, buf, buflen);
5870 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005871 /* -ENOENT is returned if there is no ACL or if there is an ACL
5872 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005873 return ret;
5874 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005875}
5876
Trond Myklebust16b42892006-08-24 12:27:15 -04005877static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005878{
5879 struct nfs_server *server = NFS_SERVER(inode);
5880 struct page *pages[NFS4ACL_MAXPAGES];
5881 struct nfs_setaclargs arg = {
5882 .fh = NFS_FH(inode),
5883 .acl_pages = pages,
5884 .acl_len = buflen,
5885 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005886 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005887 struct rpc_message msg = {
5888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5889 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005890 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005891 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005892 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005893 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005894
5895 if (!nfs4_server_supports_acls(server))
5896 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005897 if (npages > ARRAY_SIZE(pages))
5898 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005899 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005900 if (i < 0)
5901 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005902 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005903 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005904
5905 /*
5906 * Free each page after tx, so the only ref left is
5907 * held by the network stack
5908 */
5909 for (; i > 0; i--)
5910 put_page(pages[i-1]);
5911
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005912 /*
5913 * Acl update can result in inode attribute update.
5914 * so mark the attribute cache invalid.
5915 */
5916 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005917 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005918 | NFS_INO_INVALID_CTIME
5919 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005920 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005921 nfs_access_zap_cache(inode);
5922 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005923 return ret;
5924}
5925
Trond Myklebust16b42892006-08-24 12:27:15 -04005926static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5927{
5928 struct nfs4_exception exception = { };
5929 int err;
5930 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005931 err = __nfs4_proc_set_acl(inode, buf, buflen);
5932 trace_nfs4_set_acl(inode, err);
5933 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005934 &exception);
5935 } while (exception.retry);
5936 return err;
5937}
5938
David Quigleyaa9c2662013-05-22 12:50:44 -04005939#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5940static int _nfs4_get_security_label(struct inode *inode, void *buf,
5941 size_t buflen)
5942{
5943 struct nfs_server *server = NFS_SERVER(inode);
5944 struct nfs_fattr fattr;
5945 struct nfs4_label label = {0, 0, buflen, buf};
5946
5947 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005948 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005949 .fh = NFS_FH(inode),
5950 .bitmask = bitmask,
5951 };
5952 struct nfs4_getattr_res res = {
5953 .fattr = &fattr,
5954 .label = &label,
5955 .server = server,
5956 };
5957 struct rpc_message msg = {
5958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005959 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005960 .rpc_resp = &res,
5961 };
5962 int ret;
5963
5964 nfs_fattr_init(&fattr);
5965
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005966 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005967 if (ret)
5968 return ret;
5969 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5970 return -ENOENT;
David Quigleyaa9c2662013-05-22 12:50:44 -04005971 return 0;
5972}
5973
5974static int nfs4_get_security_label(struct inode *inode, void *buf,
5975 size_t buflen)
5976{
Trond Myklebust0688e642019-04-07 13:59:09 -04005977 struct nfs4_exception exception = {
5978 .interruptible = true,
5979 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005980 int err;
5981
5982 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5983 return -EOPNOTSUPP;
5984
5985 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005986 err = _nfs4_get_security_label(inode, buf, buflen);
5987 trace_nfs4_get_security_label(inode, err);
5988 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005989 &exception);
5990 } while (exception.retry);
5991 return err;
5992}
5993
5994static int _nfs4_do_set_security_label(struct inode *inode,
5995 struct nfs4_label *ilabel,
5996 struct nfs_fattr *fattr,
5997 struct nfs4_label *olabel)
5998{
5999
6000 struct iattr sattr = {0};
6001 struct nfs_server *server = NFS_SERVER(inode);
6002 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04006003 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006004 .fh = NFS_FH(inode),
6005 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04006006 .server = server,
6007 .bitmask = bitmask,
6008 .label = ilabel,
6009 };
6010 struct nfs_setattrres res = {
6011 .fattr = fattr,
6012 .label = olabel,
6013 .server = server,
6014 };
6015 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6017 .rpc_argp = &arg,
6018 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04006019 };
6020 int status;
6021
Jeff Layton12207f62013-11-01 10:49:32 -04006022 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04006023
Jeff Layton12207f62013-11-01 10:49:32 -04006024 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04006025 if (status)
6026 dprintk("%s failed: %d\n", __func__, status);
6027
6028 return status;
6029}
6030
6031static int nfs4_do_set_security_label(struct inode *inode,
6032 struct nfs4_label *ilabel,
6033 struct nfs_fattr *fattr,
6034 struct nfs4_label *olabel)
6035{
6036 struct nfs4_exception exception = { };
6037 int err;
6038
6039 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04006040 err = _nfs4_do_set_security_label(inode, ilabel,
6041 fattr, olabel);
6042 trace_nfs4_set_security_label(inode, err);
6043 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04006044 &exception);
6045 } while (exception.retry);
6046 return err;
6047}
6048
6049static int
Al Viro59301222016-05-27 10:19:30 -04006050nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04006051{
6052 struct nfs4_label ilabel, *olabel = NULL;
6053 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04006054 int status;
6055
6056 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6057 return -EOPNOTSUPP;
6058
6059 nfs_fattr_init(&fattr);
6060
6061 ilabel.pi = 0;
6062 ilabel.lfs = 0;
6063 ilabel.label = (char *)buf;
6064 ilabel.len = buflen;
6065
David Quigleyaa9c2662013-05-22 12:50:44 -04006066 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
6067 if (IS_ERR(olabel)) {
6068 status = -PTR_ERR(olabel);
6069 goto out;
6070 }
6071
6072 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
6073 if (status == 0)
6074 nfs_setsecurity(inode, &fattr, olabel);
6075
6076 nfs4_label_free(olabel);
6077out:
David Quigleyaa9c2662013-05-22 12:50:44 -04006078 return status;
6079}
6080#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
6081
6082
Chuck Leverf0920752012-05-21 22:45:41 -04006083static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6084 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05006085{
6086 __be32 verf[2];
6087
Chuck Lever2c820d92012-05-21 22:45:33 -04006088 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6089 /* An impossible timestamp guarantees this value
6090 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006091 verf[0] = cpu_to_be32(U32_MAX);
6092 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04006093 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04006094 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07006095 u64 ns = ktime_to_ns(nn->boot_time);
6096
6097 verf[0] = cpu_to_be32(ns >> 32);
6098 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006099 }
Chuck Levercd937102012-03-02 17:14:31 -05006100 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6101}
6102
Trond Myklebust1aee5512020-10-07 18:24:17 -04006103static size_t
Trond Myklebust39d43d12020-10-07 18:24:18 -04006104nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
Trond Myklebust1aee5512020-10-07 18:24:17 -04006105{
Trond Myklebust39d43d12020-10-07 18:24:18 -04006106 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6107 struct nfs_netns_client *nn_clp = nn->nfs_client;
6108 const char *id;
6109
Trond Myklebust1aee5512020-10-07 18:24:17 -04006110 buf[0] = '\0';
6111
Trond Myklebust39d43d12020-10-07 18:24:18 -04006112 if (nn_clp) {
6113 rcu_read_lock();
6114 id = rcu_dereference(nn_clp->identifier);
6115 if (id)
6116 strscpy(buf, id, buflen);
6117 rcu_read_unlock();
6118 }
6119
6120 if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
Trond Myklebust1aee5512020-10-07 18:24:17 -04006121 strscpy(buf, nfs4_client_id_uniquifier, buflen);
6122
6123 return strlen(buf);
6124}
6125
Jeff Laytona3192682015-06-09 19:43:59 -04006126static int
6127nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006128{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006129 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6130 size_t buflen;
Jeff Laytona3192682015-06-09 19:43:59 -04006131 size_t len;
6132 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006133
Trond Myklebustceb3a162015-01-03 15:16:04 -05006134 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006135 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006136
Jeff Laytona3192682015-06-09 19:43:59 -04006137 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006138 len = 14 +
6139 strlen(clp->cl_rpcclient->cl_nodename) +
6140 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006141 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006142 1;
6143 rcu_read_unlock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006144
Trond Myklebust39d43d12020-10-07 18:24:18 -04006145 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006146 if (buflen)
6147 len += buflen + 1;
6148
Jeff Laytona3192682015-06-09 19:43:59 -04006149 if (len > NFS4_OPAQUE_LIMIT + 1)
6150 return -EINVAL;
6151
6152 /*
6153 * Since this string is allocated at mount time, and held until the
6154 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6155 * about a memory-reclaim deadlock.
6156 */
6157 str = kmalloc(len, GFP_KERNEL);
6158 if (!str)
6159 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006160
Chuck Levere984a552012-09-14 17:24:21 -04006161 rcu_read_lock();
Trond Myklebust1aee5512020-10-07 18:24:17 -04006162 if (buflen)
Chuck Lever025bb9f2018-06-04 10:53:34 -04006163 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Trond Myklebust1aee5512020-10-07 18:24:17 -04006164 clp->cl_rpcclient->cl_nodename, buf,
Chuck Lever848a4eb2018-06-04 10:53:29 -04006165 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006166 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006167 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006168 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006169 clp->cl_rpcclient->cl_nodename,
6170 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006171 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006172 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006173
Jeff Laytona3192682015-06-09 19:43:59 -04006174 clp->cl_owner_id = str;
6175 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006176}
6177
Jeff Layton873e3852015-06-09 19:44:00 -04006178static int
Jeff Layton873e3852015-06-09 19:44:00 -04006179nfs4_init_uniform_client_string(struct nfs_client *clp)
6180{
Trond Myklebust1aee5512020-10-07 18:24:17 -04006181 char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6182 size_t buflen;
Jeff Layton873e3852015-06-09 19:44:00 -04006183 size_t len;
6184 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006185
6186 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006187 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006188
Jeff Layton873e3852015-06-09 19:44:00 -04006189 len = 10 + 10 + 1 + 10 + 1 +
6190 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6191
Trond Myklebust39d43d12020-10-07 18:24:18 -04006192 buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
Trond Myklebust1aee5512020-10-07 18:24:17 -04006193 if (buflen)
6194 len += buflen + 1;
6195
Jeff Layton873e3852015-06-09 19:44:00 -04006196 if (len > NFS4_OPAQUE_LIMIT + 1)
6197 return -EINVAL;
6198
6199 /*
6200 * Since this string is allocated at mount time, and held until the
6201 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6202 * about a memory-reclaim deadlock.
6203 */
6204 str = kmalloc(len, GFP_KERNEL);
6205 if (!str)
6206 return -ENOMEM;
6207
Trond Myklebust1aee5512020-10-07 18:24:17 -04006208 if (buflen)
6209 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6210 clp->rpc_ops->version, clp->cl_minorversion,
6211 buf, clp->cl_rpcclient->cl_nodename);
6212 else
6213 scnprintf(str, len, "Linux NFSv%u.%u %s",
6214 clp->rpc_ops->version, clp->cl_minorversion,
6215 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006216 clp->cl_owner_id = str;
6217 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006218}
6219
Chuck Lever706cb8d2014-03-12 12:51:47 -04006220/*
6221 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6222 * services. Advertise one based on the address family of the
6223 * clientaddr.
6224 */
6225static unsigned int
6226nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6227{
6228 if (strchr(clp->cl_ipaddr, ':') != NULL)
6229 return scnprintf(buf, len, "tcp6");
6230 else
6231 return scnprintf(buf, len, "tcp");
6232}
6233
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006234static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6235{
6236 struct nfs4_setclientid *sc = calldata;
6237
6238 if (task->tk_status == 0)
6239 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6240}
6241
6242static const struct rpc_call_ops nfs4_setclientid_ops = {
6243 .rpc_call_done = nfs4_setclientid_done,
6244};
6245
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006246/**
6247 * nfs4_proc_setclientid - Negotiate client ID
6248 * @clp: state data structure
6249 * @program: RPC program for NFSv4 callback service
6250 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006251 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006252 * @res: where to place the result
6253 *
6254 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6255 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006256int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006257 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006258 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259{
6260 nfs4_verifier sc_verifier;
6261 struct nfs4_setclientid setclientid = {
6262 .sc_verifier = &sc_verifier,
6263 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006264 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 };
6266 struct rpc_message msg = {
6267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6268 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006269 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006270 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006272 struct rpc_task_setup task_setup_data = {
6273 .rpc_client = clp->cl_rpcclient,
6274 .rpc_message = &msg,
6275 .callback_ops = &nfs4_setclientid_ops,
6276 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006277 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006278 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006279 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006280 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
Chuck Leverde734832012-07-11 16:30:50 -04006282 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006283 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006284
6285 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6286 status = nfs4_init_uniform_client_string(clp);
6287 else
Jeff Laytona3192682015-06-09 19:43:59 -04006288 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006289
6290 if (status)
6291 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006292
Chuck Leverde734832012-07-11 16:30:50 -04006293 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006294 setclientid.sc_netid_len =
6295 nfs4_init_callback_netid(clp,
6296 setclientid.sc_netid,
6297 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006298 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006299 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 clp->cl_ipaddr, port >> 8, port & 255);
6301
Jeff Layton3a6bb732015-06-09 19:43:57 -04006302 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006303 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006304 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006305
6306 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006307 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006308 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006309 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6310 put_rpccred(setclientid.sc_cred);
6311 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006312
6313 if (status == 0)
6314 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006315out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006316 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006317 dprintk("NFS reply setclientid: %d\n", status);
6318 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319}
6320
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006321/**
6322 * nfs4_proc_setclientid_confirm - Confirm client ID
6323 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006324 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006325 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006326 *
6327 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6328 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006329int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006330 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006331 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 struct rpc_message msg = {
6334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006335 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006336 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 int status;
6339
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006340 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6341 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6342 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006343 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6344 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006345 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006346 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 return status;
6348}
6349
Trond Myklebustfe650402006-01-03 09:55:18 +01006350struct nfs4_delegreturndata {
6351 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006352 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006353 struct nfs_fh fh;
6354 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006355 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006356 struct {
6357 struct nfs4_layoutreturn_args arg;
6358 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006359 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006360 u32 roc_barrier;
6361 bool roc;
6362 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006363 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006364 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006365 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006366};
6367
Trond Myklebustfe650402006-01-03 09:55:18 +01006368static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6369{
6370 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006371 struct nfs4_exception exception = {
6372 .inode = data->inode,
6373 .stateid = &data->stateid,
6374 };
Andy Adamson938e1012009-04-01 09:22:28 -04006375
Trond Myklebust14516c32010-07-31 14:29:06 -04006376 if (!nfs4_sequence_done(task, &data->res.seq_res))
6377 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006378
Trond Myklebustca8acf82013-08-13 10:36:56 -04006379 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006380
6381 /* Handle Layoutreturn errors */
Trond Myklebust078000d2021-01-04 13:18:03 -05006382 if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6383 &data->res.lr_ret) == -EAGAIN)
Trond Myklebust287a9c52019-09-20 07:23:41 -04006384 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006385
Ricardo Labiaga79708862009-12-07 09:23:21 -05006386 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006387 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006388 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006389 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006390 case -NFS4ERR_ADMIN_REVOKED:
6391 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006392 case -NFS4ERR_EXPIRED:
6393 nfs4_free_revoked_stateid(data->res.server,
6394 data->args.stateid,
6395 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006396 fallthrough;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006397 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006398 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006399 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006400 task->tk_status = 0;
6401 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006402 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006403 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6404 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006405 if (data->args.bitmask) {
6406 data->args.bitmask = NULL;
6407 data->res.fattr = NULL;
6408 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006409 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006410 case -NFS4ERR_ACCESS:
6411 if (data->args.bitmask) {
6412 data->args.bitmask = NULL;
6413 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006414 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006415 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006416 fallthrough;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006417 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006418 task->tk_status = nfs4_async_handle_exception(task,
6419 data->res.server, task->tk_status,
6420 &exception);
6421 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006422 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006423 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006424 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006425 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006426 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006427out_restart:
6428 task->tk_status = 0;
6429 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006430}
6431
6432static void nfs4_delegreturn_release(void *calldata)
6433{
Peng Tao039b7562014-07-03 13:05:02 +08006434 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006435 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006436
Trond Myklebust078000d2021-01-04 13:18:03 -05006437 if (data->lr.roc)
6438 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6439 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006440 if (inode) {
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006441 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006442 nfs_iput_and_deactive(inode);
6443 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006444 kfree(calldata);
6445}
6446
Andy Adamson938e1012009-04-01 09:22:28 -04006447static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6448{
6449 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006450 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006451
6452 d_data = (struct nfs4_delegreturndata *)data;
6453
Trond Myklebust5326de92019-11-13 09:39:36 +01006454 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6455 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006456 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006457 }
Peng Tao500d7012015-09-22 11:35:22 +08006458
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006459 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6460 if (lo && !pnfs_layout_is_valid(lo)) {
6461 d_data->args.lr_args = NULL;
6462 d_data->res.lr_res = NULL;
6463 }
6464
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006465 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006466 &d_data->args.seq_args,
6467 &d_data->res.seq_res,
6468 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006469}
Andy Adamson938e1012009-04-01 09:22:28 -04006470
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006471static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006472 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006473 .rpc_call_done = nfs4_delegreturn_done,
6474 .rpc_release = nfs4_delegreturn_release,
6475};
6476
NeilBrowna52458b2018-12-03 11:30:31 +11006477static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006478{
6479 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006480 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006481 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006482 struct rpc_message msg = {
6483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6484 .rpc_cred = cred,
6485 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006486 struct rpc_task_setup task_setup_data = {
6487 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006488 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006489 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebustf304a802020-05-11 10:42:04 -04006490 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006491 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006492 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006493
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006494 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006495 if (data == NULL)
6496 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006497 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006498
6499 nfs4_state_protect(server->nfs_client,
6500 NFS_SP4_MACH_CRED_CLEANUP,
6501 &task_setup_data.rpc_client, &msg);
6502
Trond Myklebustfe650402006-01-03 09:55:18 +01006503 data->args.fhandle = &data->fh;
6504 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006505 data->args.bitmask = server->cache_consistency_bitmask;
Olga Kornievskaia76bd5c02020-09-14 17:05:08 -04006506 nfs4_bitmask_adjust(data->args.bitmask, inode, server, NULL);
Trond Myklebustfe650402006-01-03 09:55:18 +01006507 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006508 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006509 data->res.fattr = &data->fattr;
6510 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006511 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006512 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006513 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006514 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006515 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006516 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust078000d2021-01-04 13:18:03 -05006517 if (data->inode || issync) {
6518 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6519 cred);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006520 if (data->lr.roc) {
6521 data->args.lr_args = &data->lr.arg;
6522 data->res.lr_res = &data->lr.res;
6523 }
6524 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006525
Trond Myklebustc970aa82007-07-14 15:39:59 -04006526 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006527 msg.rpc_argp = &data->args;
6528 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006529 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006530 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006531 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006532 if (!issync)
6533 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006534 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006535 if (status != 0)
6536 goto out;
6537 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006538out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006539 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006540 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541}
6542
NeilBrowna52458b2018-12-03 11:30:31 +11006543int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544{
6545 struct nfs_server *server = NFS_SERVER(inode);
6546 struct nfs4_exception exception = { };
6547 int err;
6548 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006549 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006550 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 switch (err) {
6552 case -NFS4ERR_STALE_STATEID:
6553 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 case 0:
6555 return 0;
6556 }
6557 err = nfs4_handle_exception(server, err, &exception);
6558 } while (exception.retry);
6559 return err;
6560}
6561
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6563{
6564 struct inode *inode = state->inode;
6565 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006566 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006567 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006569 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006571 struct nfs_lockt_res res = {
6572 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 };
6574 struct rpc_message msg = {
6575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006576 .rpc_argp = &arg,
6577 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 .rpc_cred = state->owner->so_cred,
6579 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 struct nfs4_lock_state *lsp;
6581 int status;
6582
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006583 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006584 status = nfs4_set_lock_state(state, request);
6585 if (status != 0)
6586 goto out;
6587 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006588 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006589 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006590 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006591 switch (status) {
6592 case 0:
6593 request->fl_type = F_UNLCK;
6594 break;
6595 case -NFS4ERR_DENIED:
6596 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006598 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006599 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006600out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 return status;
6602}
6603
6604static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6605{
Trond Myklebust0688e642019-04-07 13:59:09 -04006606 struct nfs4_exception exception = {
6607 .interruptible = true,
6608 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 int err;
6610
6611 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006612 err = _nfs4_proc_getlk(state, cmd, request);
6613 trace_nfs4_get_lock(request, state, cmd, err);
6614 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 &exception);
6616 } while (exception.retry);
6617 return err;
6618}
6619
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006620/*
6621 * Update the seqid of a lock stateid after receiving
6622 * NFS4ERR_OLD_STATEID
6623 */
6624static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6625 struct nfs4_lock_state *lsp)
6626{
6627 struct nfs4_state *state = lsp->ls_state;
6628 bool ret = false;
6629
6630 spin_lock(&state->state_lock);
6631 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6632 goto out;
6633 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6634 nfs4_stateid_seqid_inc(dst);
6635 else
6636 dst->seqid = lsp->ls_stateid.seqid;
6637 ret = true;
6638out:
6639 spin_unlock(&state->state_lock);
6640 return ret;
6641}
6642
6643static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6644 struct nfs4_lock_state *lsp)
6645{
6646 struct nfs4_state *state = lsp->ls_state;
6647 bool ret;
6648
6649 spin_lock(&state->state_lock);
6650 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6651 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6652 spin_unlock(&state->state_lock);
6653 return ret;
6654}
6655
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006656struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006657 struct nfs_locku_args arg;
6658 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006659 struct nfs4_lock_state *lsp;
6660 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006661 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006662 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006663 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006664 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006665};
6666
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006667static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6668 struct nfs_open_context *ctx,
6669 struct nfs4_lock_state *lsp,
6670 struct nfs_seqid *seqid)
6671{
6672 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006673 struct nfs4_state *state = lsp->ls_state;
6674 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006675
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006676 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006677 if (p == NULL)
6678 return NULL;
6679 p->arg.fh = NFS_FH(inode);
6680 p->arg.fl = &p->fl;
6681 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006682 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006683 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006684 /* Ensure we don't close file until we're done freeing locks! */
6685 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006686 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006687 locks_init_lock(&p->fl);
6688 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006689 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006690 spin_lock(&state->state_lock);
6691 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6692 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006693 return p;
6694}
6695
Trond Myklebust06f814a2006-01-03 09:55:07 +01006696static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006697{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006698 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006699 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006700 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006701 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006702 put_nfs_open_context(calldata->ctx);
6703 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006704}
6705
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006706static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006707{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006708 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006709 struct nfs4_exception exception = {
6710 .inode = calldata->lsp->ls_state->inode,
6711 .stateid = &calldata->arg.stateid,
6712 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006713
Trond Myklebust14516c32010-07-31 14:29:06 -04006714 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6715 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006716 switch (task->tk_status) {
6717 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006718 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006719 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006720 if (nfs4_update_lock_stateid(calldata->lsp,
6721 &calldata->res.stateid))
6722 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006723 fallthrough;
Trond Myklebust26d36302016-09-22 13:39:05 -04006724 case -NFS4ERR_ADMIN_REVOKED:
6725 case -NFS4ERR_EXPIRED:
6726 nfs4_free_revoked_stateid(calldata->server,
6727 &calldata->arg.stateid,
6728 task->tk_msg.rpc_cred);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006729 fallthrough;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006730 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006731 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006732 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6733 calldata->lsp))
6734 rpc_restart_call_prepare(task);
6735 break;
6736 case -NFS4ERR_OLD_STATEID:
6737 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6738 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006739 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006740 break;
6741 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006742 task->tk_status = nfs4_async_handle_exception(task,
6743 calldata->server, task->tk_status,
6744 &exception);
6745 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006746 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006747 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006748 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006749}
6750
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006751static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006752{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006753 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006755 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6756 nfs_async_iocounter_wait(task, calldata->l_ctx))
6757 return;
6758
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006759 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006760 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006761 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006762 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006763 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006764 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006765 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006766 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006767 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006768 &calldata->res.seq_res,
6769 task) != 0)
6770 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006771 return;
6772out_no_action:
6773 task->tk_action = NULL;
6774out_wait:
6775 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776}
6777
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006778static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006779 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006780 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006781 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006782};
6783
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006784static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6785 struct nfs_open_context *ctx,
6786 struct nfs4_lock_state *lsp,
6787 struct nfs_seqid *seqid)
6788{
6789 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006790 struct rpc_message msg = {
6791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6792 .rpc_cred = ctx->cred,
6793 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006794 struct rpc_task_setup task_setup_data = {
6795 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006796 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006797 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006798 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006799 .flags = RPC_TASK_ASYNC,
6800 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006801
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006802 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6803 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6804
Frank Filz137d6ac2007-07-09 15:32:29 -07006805 /* Ensure this is an unlock - when canceling a lock, the
6806 * canceled lock is passed in, and it won't be an unlock.
6807 */
6808 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006809 if (fl->fl_flags & FL_CLOSE)
6810 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006811
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006812 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6813 if (data == NULL) {
6814 nfs_free_seqid(seqid);
6815 return ERR_PTR(-ENOMEM);
6816 }
6817
Anna Schumakerfba83f32018-05-04 16:22:50 -04006818 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006819 msg.rpc_argp = &data->arg;
6820 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006821 task_setup_data.callback_data = data;
6822 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006823}
6824
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6826{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006827 struct inode *inode = state->inode;
6828 struct nfs4_state_owner *sp = state->owner;
6829 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006830 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006831 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006832 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006833 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006834 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006835 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836
Trond Myklebust9b073572006-06-29 16:38:34 -04006837 status = nfs4_set_lock_state(state, request);
6838 /* Unlock _before_ we do the RPC call */
6839 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006840 /* Exclude nfs_delegation_claim_locks() */
6841 mutex_lock(&sp->so_delegreturn_mutex);
6842 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006843 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006844 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006845 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006846 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006847 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006848 }
6849 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006850 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006851 if (status != 0)
6852 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006853 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006854 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006855 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6856 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006857 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6858 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006859 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006860 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006861 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006862 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006863 status = PTR_ERR(task);
6864 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006865 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006866 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006867 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006868out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006869 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006870 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006871 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872}
6873
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006874struct nfs4_lockdata {
6875 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006876 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006877 struct nfs4_lock_state *lsp;
6878 struct nfs_open_context *ctx;
6879 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006880 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006881 int rpc_status;
6882 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006883 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006884};
6885
6886static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006887 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6888 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006889{
6890 struct nfs4_lockdata *p;
6891 struct inode *inode = lsp->ls_state->inode;
6892 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006893 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006894
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006895 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006896 if (p == NULL)
6897 return NULL;
6898
6899 p->arg.fh = NFS_FH(inode);
6900 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006901 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006902 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006903 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006904 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6905 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006906 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006907 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006908 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006909 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006910 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006911 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006912 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006913 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006914 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006915 locks_init_lock(&p->fl);
6916 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006917 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006918out_free_seqid:
6919 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006920out_free:
6921 kfree(p);
6922 return NULL;
6923}
6924
6925static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6926{
6927 struct nfs4_lockdata *data = calldata;
6928 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929
Harvey Harrison3110ff82008-05-02 13:42:44 -07006930 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006931 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006932 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006933 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006934 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006935 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006936 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006937 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006938 nfs4_stateid_copy(&data->arg.open_stateid,
6939 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006940 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006941 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006942 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006943 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006944 nfs4_stateid_copy(&data->arg.lock_stateid,
6945 &data->lsp->ls_stateid);
6946 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006947 if (!nfs4_valid_open_stateid(state)) {
6948 data->rpc_status = -EBADF;
6949 task->tk_action = NULL;
6950 goto out_release_open_seqid;
6951 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006952 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006953 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006954 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006955 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006956 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006957 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006958out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006959 nfs_release_seqid(data->arg.open_seqid);
6960out_release_lock_seqid:
6961 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006962out_wait:
6963 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006964 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006965}
6966
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006967static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6968{
6969 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006970 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006971
Harvey Harrison3110ff82008-05-02 13:42:44 -07006972 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006973
Trond Myklebust14516c32010-07-31 14:29:06 -04006974 if (!nfs4_sequence_done(task, &data->res.seq_res))
6975 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006976
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006977 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006978 switch (task->tk_status) {
6979 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006980 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006981 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006982 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006983 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006984 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006985 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006986 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006987 if (data->arg.new_lock_owner != 0) {
6988 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6989 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6990 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006991 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6992 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006993 break;
6994 case -NFS4ERR_BAD_STATEID:
6995 case -NFS4ERR_OLD_STATEID:
6996 case -NFS4ERR_STALE_STATEID:
6997 case -NFS4ERR_EXPIRED:
6998 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006999 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007000 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007001 goto out_restart;
7002 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05007003 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007004 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007005 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04007006out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07007007 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007008 return;
7009out_restart:
7010 if (!data->cancelled)
7011 rpc_restart_call_prepare(task);
7012 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007013}
7014
7015static void nfs4_lock_release(void *calldata)
7016{
7017 struct nfs4_lockdata *data = calldata;
7018
Harvey Harrison3110ff82008-05-02 13:42:44 -07007019 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05007020 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04007021 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007022 struct rpc_task *task;
7023 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7024 data->arg.lock_seqid);
7025 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08007026 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007027 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007028 } else
7029 nfs_free_seqid(data->arg.lock_seqid);
7030 nfs4_put_lock_state(data->lsp);
7031 put_nfs_open_context(data->ctx);
7032 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007033 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007034}
7035
7036static const struct rpc_call_ops nfs4_lock_ops = {
7037 .rpc_call_prepare = nfs4_lock_prepare,
7038 .rpc_call_done = nfs4_lock_done,
7039 .rpc_release = nfs4_lock_release,
7040};
7041
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007042static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7043{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007044 switch (error) {
7045 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04007046 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007047 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05007048 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007049 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04007050 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05007051 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007052 break;
7053 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05007054 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05007055 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08007056 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007057}
7058
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007059static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007060{
7061 struct nfs4_lockdata *data;
7062 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04007063 struct rpc_message msg = {
7064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7065 .rpc_cred = state->owner->so_cred,
7066 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04007067 struct rpc_task_setup task_setup_data = {
7068 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04007069 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007070 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05007071 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05007072 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04007073 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007074 int ret;
7075
Harvey Harrison3110ff82008-05-02 13:42:44 -07007076 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007077 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007078 fl->fl_u.nfs4_fl.owner,
7079 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007080 if (data == NULL)
7081 return -ENOMEM;
7082 if (IS_SETLKW(cmd))
7083 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007084 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7085 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05007086 msg.rpc_argp = &data->arg;
7087 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007088 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007089 if (recovery_type > NFS_LOCK_NEW) {
7090 if (recovery_type == NFS_LOCK_RECLAIM)
7091 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05007092 } else
7093 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04007094 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05007095 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007096 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05007097 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007098 if (ret == 0) {
7099 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007100 if (ret)
7101 nfs4_handle_setlk_error(data->server, data->lsp,
7102 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007103 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007104 data->cancelled = true;
Dave Wysochanski3d1a90a2020-12-11 05:12:51 -05007105 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007106 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007107 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007108 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109}
7110
7111static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7112{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007113 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007114 struct nfs4_exception exception = {
7115 .inode = state->inode,
7116 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007117 int err;
7118
7119 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007120 /* Cache the lock if possible... */
7121 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7122 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007123 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007124 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007125 break;
7126 nfs4_handle_exception(server, err, &exception);
7127 } while (exception.retry);
7128 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129}
7130
7131static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7132{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007133 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007134 struct nfs4_exception exception = {
7135 .inode = state->inode,
7136 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007137 int err;
7138
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007139 err = nfs4_set_lock_state(state, request);
7140 if (err != 0)
7141 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007142 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007143 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7144 return 0;
7145 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007146 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007147 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7148 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007149 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007150 switch (err) {
7151 default:
7152 goto out;
7153 case -NFS4ERR_GRACE:
7154 case -NFS4ERR_DELAY:
7155 nfs4_handle_exception(server, err, &exception);
7156 err = 0;
7157 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007158 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007159out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007160 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161}
7162
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007163#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007164static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7165{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007166 struct nfs4_lock_state *lsp;
7167 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007168
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007169 status = nfs4_set_lock_state(state, request);
7170 if (status != 0)
7171 return status;
7172 lsp = request->fl_u.nfs4_fl.owner;
7173 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7174 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7175 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007176 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007177}
7178#endif
7179
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7181{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007182 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007183 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007184 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007185 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186
Trond Myklebust01c3b862006-06-29 16:38:39 -04007187 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007188 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007189 if (status < 0)
7190 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007191 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007192 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007193 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007194 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007195 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007196 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007197 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007198 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007199 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007200 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007201 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007202 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007203 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007204 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007205out:
7206 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 return status;
7208}
7209
7210static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7211{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007212 struct nfs4_exception exception = {
7213 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007214 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007215 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007216 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 int err;
7218
7219 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007220 err = _nfs4_proc_setlk(state, cmd, request);
7221 if (err == -NFS4ERR_DENIED)
7222 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007224 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 } while (exception.retry);
7226 return err;
7227}
7228
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007229#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7230#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7231
7232static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007233nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7234 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007235{
7236 int status = -ERESTARTSYS;
7237 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7238
7239 while(!signalled()) {
7240 status = nfs4_proc_setlk(state, cmd, request);
7241 if ((status != -EAGAIN) || IS_SETLK(cmd))
7242 break;
7243 freezable_schedule_timeout_interruptible(timeout);
7244 timeout *= 2;
7245 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7246 status = -ERESTARTSYS;
7247 }
7248 return status;
7249}
7250
Jeff Laytona1d617d82016-09-17 18:17:39 -04007251#ifdef CONFIG_NFS_V4_1
7252struct nfs4_lock_waiter {
7253 struct task_struct *task;
7254 struct inode *inode;
7255 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007256};
7257
7258static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007259nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007260{
7261 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007262 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007263
Jeff Layton57174592018-03-18 08:37:03 -04007264 /* NULL key means to wake up everyone */
7265 if (key) {
7266 struct cb_notify_lock_args *cbnl = key;
7267 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7268 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007269
Jeff Layton57174592018-03-18 08:37:03 -04007270 /* Only wake if the callback was for the same owner. */
7271 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7272 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007273
Jeff Layton57174592018-03-18 08:37:03 -04007274 /* Make sure it's for the right inode */
7275 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7276 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007277 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007278
7279 /* override "private" so we can use default_wake_function */
7280 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007281 ret = woken_wake_function(wait, mode, flags, key);
7282 if (ret)
7283 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007284 wait->private = waiter;
7285 return ret;
7286}
7287
7288static int
7289nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7290{
7291 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007292 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7293 struct nfs_server *server = NFS_SERVER(state->inode);
7294 struct nfs_client *clp = server->nfs_client;
7295 wait_queue_head_t *q = &clp->cl_lock_waitq;
7296 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7297 .id = lsp->ls_seqid.owner_id,
7298 .s_dev = server->s_dev };
7299 struct nfs4_lock_waiter waiter = { .task = current,
7300 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007301 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007302 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007303
7304 /* Don't bother with waitqueue if we don't expect a callback */
7305 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7306 return nfs4_retry_setlk_simple(state, cmd, request);
7307
7308 init_wait(&wait);
7309 wait.private = &waiter;
7310 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007311
7312 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007313 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007314 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007315 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7316 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007317 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007318 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007319
7320 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007321 freezer_do_not_count();
7322 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7323 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007324 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007325 }
7326
Jeff Laytona1d617d82016-09-17 18:17:39 -04007327 return status;
7328}
7329#else /* !CONFIG_NFS_V4_1 */
7330static inline int
7331nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7332{
7333 return nfs4_retry_setlk_simple(state, cmd, request);
7334}
7335#endif
7336
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337static int
7338nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7339{
7340 struct nfs_open_context *ctx;
7341 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 int status;
7343
7344 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007345 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 state = ctx->state;
7347
Trond Myklebustd9531262009-07-21 19:22:38 -04007348 if (IS_GETLK(cmd)) {
7349 if (state != NULL)
7350 return nfs4_proc_getlk(state, F_GETLK, request);
7351 return 0;
7352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007353
7354 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7355 return -EINVAL;
7356
Trond Myklebustd9531262009-07-21 19:22:38 -04007357 if (request->fl_type == F_UNLCK) {
7358 if (state != NULL)
7359 return nfs4_proc_unlck(state, cmd, request);
7360 return 0;
7361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362
Trond Myklebustd9531262009-07-21 19:22:38 -04007363 if (state == NULL)
7364 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007365
7366 if ((request->fl_flags & FL_POSIX) &&
7367 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7368 return -ENOLCK;
7369
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007370 /*
7371 * Don't rely on the VFS having checked the file open mode,
7372 * since it won't do this for flock() locks.
7373 */
7374 switch (request->fl_type) {
7375 case F_RDLCK:
7376 if (!(filp->f_mode & FMODE_READ))
7377 return -EBADF;
7378 break;
7379 case F_WRLCK:
7380 if (!(filp->f_mode & FMODE_WRITE))
7381 return -EBADF;
7382 }
7383
Jeff Layton1ea67db2016-09-17 18:17:37 -04007384 status = nfs4_set_lock_state(state, request);
7385 if (status != 0)
7386 return status;
7387
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007388 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389}
7390
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007391int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007392{
7393 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007394 int err;
7395
7396 err = nfs4_set_lock_state(state, fl);
7397 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007398 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007399 do {
7400 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7401 if (err != -NFS4ERR_DELAY)
7402 break;
7403 ssleep(1);
7404 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007405 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007406}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007407
Trond Myklebustcf470c32012-03-07 13:49:12 -05007408struct nfs_release_lockowner_data {
7409 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007410 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007411 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007412 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007413 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007414};
7415
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007416static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7417{
7418 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007419 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007420 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7421 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007422 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007423 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007424}
7425
7426static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7427{
7428 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007429 struct nfs_server *server = data->server;
7430
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007431 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007432
7433 switch (task->tk_status) {
7434 case 0:
7435 renew_lease(server, data->timestamp);
7436 break;
7437 case -NFS4ERR_STALE_CLIENTID:
7438 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007439 nfs4_schedule_lease_recovery(server->nfs_client);
7440 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007441 case -NFS4ERR_LEASE_MOVED:
7442 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007443 if (nfs4_async_handle_error(task, server,
7444 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007445 rpc_restart_call_prepare(task);
7446 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007447}
7448
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007449static void nfs4_release_lockowner_release(void *calldata)
7450{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007451 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007452 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007453 kfree(calldata);
7454}
7455
Trond Myklebust17280172012-03-11 13:11:00 -04007456static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007457 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7458 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007459 .rpc_release = nfs4_release_lockowner_release,
7460};
7461
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007462static void
7463nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007464{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007465 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007466 struct rpc_message msg = {
7467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7468 };
7469
7470 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007471 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007472
Trond Myklebustcf470c32012-03-07 13:49:12 -05007473 data = kmalloc(sizeof(*data), GFP_NOFS);
7474 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007475 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007476 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007477 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007478 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7479 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7480 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007481
Trond Myklebustcf470c32012-03-07 13:49:12 -05007482 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007483 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007484 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007485 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007486}
7487
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007488#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7489
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007490static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007491 struct dentry *unused, struct inode *inode,
7492 const char *key, const void *buf,
7493 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007494{
Al Viro59301222016-05-27 10:19:30 -04007495 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007496}
7497
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007498static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007499 struct dentry *unused, struct inode *inode,
7500 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007501{
Al Virob2968212016-04-10 20:48:24 -04007502 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007503}
7504
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007505static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007506{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007507 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007508}
7509
David Quigleyc9bccef2013-05-22 12:50:45 -04007510#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007511
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007512static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007513 struct dentry *unused, struct inode *inode,
7514 const char *key, const void *buf,
7515 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007516{
7517 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007518 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007519
7520 return -EOPNOTSUPP;
7521}
7522
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007523static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007524 struct dentry *unused, struct inode *inode,
7525 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007526{
7527 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007528 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007529 return -EOPNOTSUPP;
7530}
7531
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007532static ssize_t
7533nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007534{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007535 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007536
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007537 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7538 len = security_inode_listsecurity(inode, list, list_len);
Chengguang Xu82c596e2020-06-17 09:09:39 +08007539 if (len >= 0 && list_len && len > list_len)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007540 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007541 }
7542 return len;
7543}
7544
7545static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7546 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007547 .get = nfs4_xattr_get_nfs4_label,
7548 .set = nfs4_xattr_set_nfs4_label,
7549};
David Quigleyc9bccef2013-05-22 12:50:45 -04007550
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007551#else
7552
7553static ssize_t
7554nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7555{
7556 return 0;
7557}
7558
7559#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007560
Frank van der Linden012a2112020-06-23 22:39:03 +00007561#ifdef CONFIG_NFS_V4_2
7562static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7563 struct dentry *unused, struct inode *inode,
7564 const char *key, const void *buf,
7565 size_t buflen, int flags)
7566{
7567 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007568 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007569
7570 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7571 return -EOPNOTSUPP;
7572
7573 /*
7574 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7575 * flags right now. Handling of xattr operations use the normal
7576 * file read/write permissions.
7577 *
7578 * Just in case the server has other ideas (which RFC 8276 allows),
7579 * do a cached access check for the XA* flags to possibly avoid
7580 * doing an RPC and getting EACCES back.
7581 */
7582 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7583 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7584 return -EACCES;
7585 }
7586
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007587 if (buf == NULL) {
7588 ret = nfs42_proc_removexattr(inode, key);
7589 if (!ret)
7590 nfs4_xattr_cache_remove(inode, key);
7591 } else {
7592 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7593 if (!ret)
7594 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7595 }
7596
7597 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007598}
7599
7600static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7601 struct dentry *unused, struct inode *inode,
7602 const char *key, void *buf, size_t buflen)
7603{
7604 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007605 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007606
7607 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7608 return -EOPNOTSUPP;
7609
7610 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7611 if (!(cache.mask & NFS_ACCESS_XAREAD))
7612 return -EACCES;
7613 }
7614
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007615 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7616 if (ret)
7617 return ret;
7618
7619 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7620 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7621 return ret;
7622
7623 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7624
7625 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007626}
7627
7628static ssize_t
7629nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7630{
7631 u64 cookie;
7632 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007633 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007634 char *buf;
7635 size_t buflen;
7636 struct nfs_access_entry cache;
7637
7638 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7639 return 0;
7640
7641 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7642 if (!(cache.mask & NFS_ACCESS_XALIST))
7643 return 0;
7644 }
7645
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007646 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7647 if (ret)
7648 return ret;
7649
7650 ret = nfs4_xattr_cache_list(inode, list, list_len);
7651 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7652 return ret;
7653
Frank van der Linden012a2112020-06-23 22:39:03 +00007654 cookie = 0;
7655 eof = false;
7656 buflen = list_len ? list_len : XATTR_LIST_MAX;
7657 buf = list_len ? list : NULL;
7658 size = 0;
7659
7660 while (!eof) {
7661 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7662 &cookie, &eof);
7663 if (ret < 0)
7664 return ret;
7665
7666 if (list_len) {
7667 buf += ret;
7668 buflen -= ret;
7669 }
7670 size += ret;
7671 }
7672
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007673 if (list_len)
7674 nfs4_xattr_cache_set_list(inode, list, size);
7675
Frank van der Linden012a2112020-06-23 22:39:03 +00007676 return size;
7677}
7678
7679#else
7680
7681static ssize_t
7682nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7683{
7684 return 0;
7685}
7686#endif /* CONFIG_NFS_V4_2 */
7687
Andy Adamson533eb462011-06-13 18:25:56 -04007688/*
7689 * nfs_fhget will use either the mounted_on_fileid or the fileid
7690 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007691static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7692{
Andy Adamson533eb462011-06-13 18:25:56 -04007693 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7694 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7695 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007696 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007697 return;
7698
7699 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007700 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007701 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7702 fattr->nlink = 2;
7703}
7704
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007705static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7706 const struct qstr *name,
7707 struct nfs4_fs_locations *fs_locations,
7708 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007709{
7710 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007711 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007712 struct nfs4_fs_locations_arg args = {
7713 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007714 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007715 .page = page,
7716 .bitmask = bitmask,
7717 };
Benny Halevy22958462009-04-01 09:22:02 -04007718 struct nfs4_fs_locations_res res = {
7719 .fs_locations = fs_locations,
7720 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007721 struct rpc_message msg = {
7722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7723 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007724 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007725 };
7726 int status;
7727
Harvey Harrison3110ff82008-05-02 13:42:44 -07007728 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007729
Chuck Leverc05cefc2017-11-05 15:45:22 -05007730 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7731 bitmask[1] = nfs4_fattr_bitmap[1];
7732
Andy Adamson533eb462011-06-13 18:25:56 -04007733 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7734 * is not supported */
7735 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007736 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007737 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007738 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007739
Trond Myklebustc228fd32007-01-13 02:28:11 -05007740 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007741 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007742 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007743 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007744 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007745 return status;
7746}
7747
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007748int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7749 const struct qstr *name,
7750 struct nfs4_fs_locations *fs_locations,
7751 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007752{
Trond Myklebust0688e642019-04-07 13:59:09 -04007753 struct nfs4_exception exception = {
7754 .interruptible = true,
7755 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007756 int err;
7757 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007758 err = _nfs4_proc_fs_locations(client, dir, name,
7759 fs_locations, page);
7760 trace_nfs4_get_fs_locations(dir, name, err);
7761 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007762 &exception);
7763 } while (exception.retry);
7764 return err;
7765}
7766
Chuck Leverb03d7352013-10-17 14:12:50 -04007767/*
7768 * This operation also signals the server that this client is
7769 * performing migration recovery. The server can stop returning
7770 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7771 * appended to this compound to identify the client ID which is
7772 * performing recovery.
7773 */
7774static int _nfs40_proc_get_locations(struct inode *inode,
7775 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007776 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007777{
7778 struct nfs_server *server = NFS_SERVER(inode);
7779 struct rpc_clnt *clnt = server->client;
7780 u32 bitmask[2] = {
7781 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7782 };
7783 struct nfs4_fs_locations_arg args = {
7784 .clientid = server->nfs_client->cl_clientid,
7785 .fh = NFS_FH(inode),
7786 .page = page,
7787 .bitmask = bitmask,
7788 .migration = 1, /* skip LOOKUP */
7789 .renew = 1, /* append RENEW */
7790 };
7791 struct nfs4_fs_locations_res res = {
7792 .fs_locations = locations,
7793 .migration = 1,
7794 .renew = 1,
7795 };
7796 struct rpc_message msg = {
7797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7798 .rpc_argp = &args,
7799 .rpc_resp = &res,
7800 .rpc_cred = cred,
7801 };
7802 unsigned long now = jiffies;
7803 int status;
7804
7805 nfs_fattr_init(&locations->fattr);
7806 locations->server = server;
7807 locations->nlocations = 0;
7808
Anna Schumakerfba83f32018-05-04 16:22:50 -04007809 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007810 status = nfs4_call_sync_sequence(clnt, server, &msg,
7811 &args.seq_args, &res.seq_res);
7812 if (status)
7813 return status;
7814
7815 renew_lease(server, now);
7816 return 0;
7817}
7818
7819#ifdef CONFIG_NFS_V4_1
7820
7821/*
7822 * This operation also signals the server that this client is
7823 * performing migration recovery. The server can stop asserting
7824 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7825 * performing this operation is identified in the SEQUENCE
7826 * operation in this compound.
7827 *
7828 * When the client supports GETATTR(fs_locations_info), it can
7829 * be plumbed in here.
7830 */
7831static int _nfs41_proc_get_locations(struct inode *inode,
7832 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007833 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007834{
7835 struct nfs_server *server = NFS_SERVER(inode);
7836 struct rpc_clnt *clnt = server->client;
7837 u32 bitmask[2] = {
7838 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7839 };
7840 struct nfs4_fs_locations_arg args = {
7841 .fh = NFS_FH(inode),
7842 .page = page,
7843 .bitmask = bitmask,
7844 .migration = 1, /* skip LOOKUP */
7845 };
7846 struct nfs4_fs_locations_res res = {
7847 .fs_locations = locations,
7848 .migration = 1,
7849 };
7850 struct rpc_message msg = {
7851 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7852 .rpc_argp = &args,
7853 .rpc_resp = &res,
7854 .rpc_cred = cred,
7855 };
7856 int status;
7857
7858 nfs_fattr_init(&locations->fattr);
7859 locations->server = server;
7860 locations->nlocations = 0;
7861
Anna Schumakerfba83f32018-05-04 16:22:50 -04007862 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007863 status = nfs4_call_sync_sequence(clnt, server, &msg,
7864 &args.seq_args, &res.seq_res);
7865 if (status == NFS4_OK &&
7866 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7867 status = -NFS4ERR_LEASE_MOVED;
7868 return status;
7869}
7870
7871#endif /* CONFIG_NFS_V4_1 */
7872
7873/**
7874 * nfs4_proc_get_locations - discover locations for a migrated FSID
7875 * @inode: inode on FSID that is migrating
7876 * @locations: result of query
7877 * @page: buffer
7878 * @cred: credential to use for this operation
7879 *
7880 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7881 * operation failed, or a negative errno if a local error occurred.
7882 *
7883 * On success, "locations" is filled in, but if the server has
7884 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7885 * asserted.
7886 *
7887 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7888 * from this client that require migration recovery.
7889 */
7890int nfs4_proc_get_locations(struct inode *inode,
7891 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007892 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007893{
7894 struct nfs_server *server = NFS_SERVER(inode);
7895 struct nfs_client *clp = server->nfs_client;
7896 const struct nfs4_mig_recovery_ops *ops =
7897 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007898 struct nfs4_exception exception = {
7899 .interruptible = true,
7900 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007901 int status;
7902
7903 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7904 (unsigned long long)server->fsid.major,
7905 (unsigned long long)server->fsid.minor,
7906 clp->cl_hostname);
7907 nfs_display_fhandle(NFS_FH(inode), __func__);
7908
7909 do {
7910 status = ops->get_locations(inode, locations, page, cred);
7911 if (status != -NFS4ERR_DELAY)
7912 break;
7913 nfs4_handle_exception(server, status, &exception);
7914 } while (exception.retry);
7915 return status;
7916}
7917
Chuck Lever44c99932013-10-17 14:13:30 -04007918/*
7919 * This operation also signals the server that this client is
7920 * performing "lease moved" recovery. The server can stop
7921 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7922 * is appended to this compound to identify the client ID which is
7923 * performing recovery.
7924 */
NeilBrowna52458b2018-12-03 11:30:31 +11007925static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007926{
7927 struct nfs_server *server = NFS_SERVER(inode);
7928 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7929 struct rpc_clnt *clnt = server->client;
7930 struct nfs4_fsid_present_arg args = {
7931 .fh = NFS_FH(inode),
7932 .clientid = clp->cl_clientid,
7933 .renew = 1, /* append RENEW */
7934 };
7935 struct nfs4_fsid_present_res res = {
7936 .renew = 1,
7937 };
7938 struct rpc_message msg = {
7939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7940 .rpc_argp = &args,
7941 .rpc_resp = &res,
7942 .rpc_cred = cred,
7943 };
7944 unsigned long now = jiffies;
7945 int status;
7946
7947 res.fh = nfs_alloc_fhandle();
7948 if (res.fh == NULL)
7949 return -ENOMEM;
7950
Anna Schumakerfba83f32018-05-04 16:22:50 -04007951 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007952 status = nfs4_call_sync_sequence(clnt, server, &msg,
7953 &args.seq_args, &res.seq_res);
7954 nfs_free_fhandle(res.fh);
7955 if (status)
7956 return status;
7957
7958 do_renew_lease(clp, now);
7959 return 0;
7960}
7961
7962#ifdef CONFIG_NFS_V4_1
7963
7964/*
7965 * This operation also signals the server that this client is
7966 * performing "lease moved" recovery. The server can stop asserting
7967 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7968 * this operation is identified in the SEQUENCE operation in this
7969 * compound.
7970 */
NeilBrowna52458b2018-12-03 11:30:31 +11007971static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007972{
7973 struct nfs_server *server = NFS_SERVER(inode);
7974 struct rpc_clnt *clnt = server->client;
7975 struct nfs4_fsid_present_arg args = {
7976 .fh = NFS_FH(inode),
7977 };
7978 struct nfs4_fsid_present_res res = {
7979 };
7980 struct rpc_message msg = {
7981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7982 .rpc_argp = &args,
7983 .rpc_resp = &res,
7984 .rpc_cred = cred,
7985 };
7986 int status;
7987
7988 res.fh = nfs_alloc_fhandle();
7989 if (res.fh == NULL)
7990 return -ENOMEM;
7991
Anna Schumakerfba83f32018-05-04 16:22:50 -04007992 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007993 status = nfs4_call_sync_sequence(clnt, server, &msg,
7994 &args.seq_args, &res.seq_res);
7995 nfs_free_fhandle(res.fh);
7996 if (status == NFS4_OK &&
7997 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7998 status = -NFS4ERR_LEASE_MOVED;
7999 return status;
8000}
8001
8002#endif /* CONFIG_NFS_V4_1 */
8003
8004/**
8005 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8006 * @inode: inode on FSID to check
8007 * @cred: credential to use for this operation
8008 *
8009 * Server indicates whether the FSID is present, moved, or not
8010 * recognized. This operation is necessary to clear a LEASE_MOVED
8011 * condition for this client ID.
8012 *
8013 * Returns NFS4_OK if the FSID is present on this server,
8014 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8015 * NFS4ERR code if some error occurred on the server, or a
8016 * negative errno if a local failure occurred.
8017 */
NeilBrowna52458b2018-12-03 11:30:31 +11008018int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04008019{
8020 struct nfs_server *server = NFS_SERVER(inode);
8021 struct nfs_client *clp = server->nfs_client;
8022 const struct nfs4_mig_recovery_ops *ops =
8023 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04008024 struct nfs4_exception exception = {
8025 .interruptible = true,
8026 };
Chuck Lever44c99932013-10-17 14:13:30 -04008027 int status;
8028
8029 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8030 (unsigned long long)server->fsid.major,
8031 (unsigned long long)server->fsid.minor,
8032 clp->cl_hostname);
8033 nfs_display_fhandle(NFS_FH(inode), __func__);
8034
8035 do {
8036 status = ops->fsid_present(inode, cred);
8037 if (status != -NFS4ERR_DELAY)
8038 break;
8039 nfs4_handle_exception(server, status, &exception);
8040 } while (exception.retry);
8041 return status;
8042}
8043
Trond Myklebust302fad72019-02-18 13:32:38 -05008044/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008045 * If 'use_integrity' is true and the state managment nfs_client
8046 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8047 * and the machine credential as per RFC3530bis and RFC5661 Security
8048 * Considerations sections. Otherwise, just use the user cred with the
8049 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04008050 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008051static 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 +00008052{
8053 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04008054 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8055 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008056 struct nfs4_secinfo_arg args = {
8057 .dir_fh = NFS_FH(dir),
8058 .name = name,
8059 };
8060 struct nfs4_secinfo_res res = {
8061 .flavors = flavors,
8062 };
8063 struct rpc_message msg = {
8064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8065 .rpc_argp = &args,
8066 .rpc_resp = &res,
8067 };
Anna Schumaker50493362019-08-14 15:30:16 -04008068 struct nfs4_call_sync_data data = {
8069 .seq_server = NFS_SERVER(dir),
8070 .seq_args = &args.seq_args,
8071 .seq_res = &res.seq_res,
8072 };
8073 struct rpc_task_setup task_setup = {
8074 .rpc_client = clnt,
8075 .rpc_message = &msg,
8076 .callback_ops = clp->cl_mvops->call_sync_ops,
8077 .callback_data = &data,
8078 .flags = RPC_TASK_NO_ROUND_ROBIN,
8079 };
NeilBrowna52458b2018-12-03 11:30:31 +11008080 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008081
8082 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04008083 clnt = clp->cl_rpcclient;
8084 task_setup.rpc_client = clnt;
8085
8086 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008087 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008088 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008089
8090 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008091
Anna Schumaker50493362019-08-14 15:30:16 -04008092 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8093 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8094 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008095
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008096 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008097
NeilBrowna52458b2018-12-03 11:30:31 +11008098 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008099 return status;
8100}
8101
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008102int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8103 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008104{
Trond Myklebust0688e642019-04-07 13:59:09 -04008105 struct nfs4_exception exception = {
8106 .interruptible = true,
8107 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008108 int err;
8109 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008110 err = -NFS4ERR_WRONGSEC;
8111
8112 /* try to use integrity protection with machine cred */
8113 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8114 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8115
8116 /*
8117 * if unable to use integrity protection, or SECINFO with
8118 * integrity protection returns NFS4ERR_WRONGSEC (which is
8119 * disallowed by spec, but exists in deployed servers) use
8120 * the current filesystem's rpc_client and the user cred.
8121 */
8122 if (err == -NFS4ERR_WRONGSEC)
8123 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8124
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008125 trace_nfs4_secinfo(dir, name, err);
8126 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008127 &exception);
8128 } while (exception.retry);
8129 return err;
8130}
8131
Andy Adamson557134a2009-04-01 09:21:53 -04008132#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008133/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008134 * Check the exchange flags returned by the server for invalid flags, having
8135 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8136 * DS flags set.
8137 */
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008138static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
Andy Adamson357f54d2010-12-14 10:11:57 -05008139{
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008140 if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8141 goto out_inval;
8142 else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
Andy Adamson357f54d2010-12-14 10:11:57 -05008143 goto out_inval;
8144 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8145 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8146 goto out_inval;
8147 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8148 goto out_inval;
8149 return NFS_OK;
8150out_inval:
8151 return -NFS4ERR_INVAL;
8152}
8153
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008154static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008155nfs41_same_server_scope(struct nfs41_server_scope *a,
8156 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008157{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008158 if (a->server_scope_sz != b->server_scope_sz)
8159 return false;
8160 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008161}
8162
Andy Adamson02a95de2016-02-05 16:08:37 -05008163static void
8164nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8165{
Trond Myklebust5c441542019-11-13 08:34:00 +01008166 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008167 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008168 struct nfs_client *clp = args->client;
8169
8170 switch (task->tk_status) {
8171 case -NFS4ERR_BADSESSION:
8172 case -NFS4ERR_DEADSESSION:
8173 nfs4_schedule_session_recovery(clp->cl_session,
8174 task->tk_status);
8175 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008176 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8177 res->dir != NFS4_CDFS4_BOTH) {
8178 rpc_task_close_connection(task);
8179 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8180 rpc_restart_call(task);
8181 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008182}
8183
8184static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008185 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008186};
8187
Andy Adamson357f54d2010-12-14 10:11:57 -05008188/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008189 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008190 *
8191 * The 4.1 client currently uses the same TCP connection for the
8192 * fore and backchannel.
8193 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008194static
8195int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8196 struct rpc_xprt *xprt,
8197 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008198 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008199{
8200 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008201 struct nfs41_bind_conn_to_session_args args = {
8202 .client = clp,
8203 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008204 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008205 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008206 struct nfs41_bind_conn_to_session_res res;
8207 struct rpc_message msg = {
8208 .rpc_proc =
8209 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008210 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008211 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008212 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008213 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008214 struct rpc_task_setup task_setup_data = {
8215 .rpc_client = clnt,
8216 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008217 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008218 .rpc_message = &msg,
8219 .flags = RPC_TASK_TIMEOUT,
8220 };
8221 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008222
Trond Myklebust71a097c2015-02-18 09:27:18 -08008223 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8224 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8225 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008226
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008227 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8228 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8229 args.dir = NFS4_CDFC4_FORE;
8230
8231 task = rpc_run_task(&task_setup_data);
8232 if (!IS_ERR(task)) {
8233 status = task->tk_status;
8234 rpc_put_task(task);
8235 } else
8236 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008237 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008238 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008239 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008240 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8241 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008242 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008243 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008244 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008245 dprintk("NFS: %s: Unexpected direction from server\n",
8246 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008247 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008248 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008249 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008250 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8251 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008252 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008253 }
8254 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008255
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008256 return status;
8257}
8258
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008259struct rpc_bind_conn_calldata {
8260 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008261 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008262};
8263
8264static int
8265nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8266 struct rpc_xprt *xprt,
8267 void *calldata)
8268{
8269 struct rpc_bind_conn_calldata *p = calldata;
8270
8271 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8272}
8273
NeilBrowna52458b2018-12-03 11:30:31 +11008274int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008275{
8276 struct rpc_bind_conn_calldata data = {
8277 .clp = clp,
8278 .cred = cred,
8279 };
8280 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8281 nfs4_proc_bind_conn_to_session_callback, &data);
8282}
8283
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008284/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008285 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8286 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008287 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008288static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8289 .how = SP4_MACH_CRED,
8290 .enforce.u.words = {
8291 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8292 1 << (OP_EXCHANGE_ID - 32) |
8293 1 << (OP_CREATE_SESSION - 32) |
8294 1 << (OP_DESTROY_SESSION - 32) |
8295 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008296 },
8297 .allow.u.words = {
8298 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008299 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008300 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008301 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008302 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008303 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008304 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008305 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008306 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008307 1 << (OP_FREE_STATEID - 32) |
8308 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008309 }
8310};
8311
8312/*
8313 * Select the state protection mode for client `clp' given the server results
8314 * from exchange_id in `sp'.
8315 *
8316 * Returns 0 on success, negative errno otherwise.
8317 */
8318static int nfs4_sp4_select_mode(struct nfs_client *clp,
8319 struct nfs41_state_protection *sp)
8320{
8321 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8322 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8323 1 << (OP_EXCHANGE_ID - 32) |
8324 1 << (OP_CREATE_SESSION - 32) |
8325 1 << (OP_DESTROY_SESSION - 32) |
8326 1 << (OP_DESTROY_CLIENTID - 32)
8327 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008328 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008329 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008330 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008331
8332 if (sp->how == SP4_MACH_CRED) {
8333 /* Print state protect result */
8334 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8335 for (i = 0; i <= LAST_NFS4_OP; i++) {
8336 if (test_bit(i, sp->enforce.u.longs))
8337 dfprintk(MOUNT, " enforce op %d\n", i);
8338 if (test_bit(i, sp->allow.u.longs))
8339 dfprintk(MOUNT, " allow op %d\n", i);
8340 }
8341
8342 /* make sure nothing is on enforce list that isn't supported */
8343 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8344 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8345 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008346 ret = -EINVAL;
8347 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008348 }
8349 }
8350
8351 /*
8352 * Minimal mode - state operations are allowed to use machine
8353 * credential. Note this already happens by default, so the
8354 * client doesn't have to do anything more than the negotiation.
8355 *
8356 * NOTE: we don't care if EXCHANGE_ID is in the list -
8357 * we're already using the machine cred for exchange_id
8358 * and will never use a different cred.
8359 */
8360 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8361 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8362 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8363 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8364 dfprintk(MOUNT, "sp4_mach_cred:\n");
8365 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008366 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008367 } else {
8368 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008369 ret = -EINVAL;
8370 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008371 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008372
8373 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008374 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8375 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008376 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8377 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008378 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008379 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008380
Andrew Elble99ade3c2015-12-02 09:39:51 -05008381 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8382 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008383 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008384 }
8385
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008386 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8387 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8388 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008389 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008390 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008391
8392 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8393 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8394 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008395 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008396 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008397
8398 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8399 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008400 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008401 }
8402
8403 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8404 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008405 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008406 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008407 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008408out:
8409 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008410 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008411}
8412
Andy Adamson8d89bd72016-09-09 09:22:18 -04008413struct nfs41_exchange_id_data {
8414 struct nfs41_exchange_id_res res;
8415 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008416};
8417
Andy Adamson8d89bd72016-09-09 09:22:18 -04008418static void nfs4_exchange_id_release(void *data)
8419{
8420 struct nfs41_exchange_id_data *cdata =
8421 (struct nfs41_exchange_id_data *)data;
8422
Olga Kornievskaia63513232017-03-13 10:36:19 -04008423 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008424 kfree(cdata->res.impl_id);
8425 kfree(cdata->res.server_scope);
8426 kfree(cdata->res.server_owner);
8427 kfree(cdata);
8428}
8429
8430static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008431 .rpc_release = nfs4_exchange_id_release,
8432};
8433
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008434/*
8435 * _nfs4_proc_exchange_id()
8436 *
8437 * Wrapper for EXCHANGE_ID operation.
8438 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008439static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008440nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008441 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008442{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008443 struct rpc_message msg = {
8444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008445 .rpc_cred = cred,
8446 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008447 struct rpc_task_setup task_setup_data = {
8448 .rpc_client = clp->cl_rpcclient,
8449 .callback_ops = &nfs4_exchange_id_call_ops,
8450 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008451 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008452 };
8453 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008454 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008455
Elena Reshetova212bf412017-10-20 12:53:38 +03008456 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008457 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008458
Trond Myklebust9c760d12017-07-31 18:38:50 -04008459 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008460 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008461 if (!calldata)
8462 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008463
Trond Myklebustfd405592017-08-01 16:02:47 -04008464 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008465
8466 status = nfs4_init_uniform_client_string(clp);
8467 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008468 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008469
Andy Adamson8d89bd72016-09-09 09:22:18 -04008470 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8471 GFP_NOFS);
8472 status = -ENOMEM;
8473 if (unlikely(calldata->res.server_owner == NULL))
8474 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008475
Andy Adamson8d89bd72016-09-09 09:22:18 -04008476 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008477 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008478 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008479 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008480
Andy Adamson8d89bd72016-09-09 09:22:18 -04008481 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8482 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008483 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008484
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008485 switch (sp4_how) {
8486 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008487 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008488 break;
8489
8490 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008491 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008492 break;
8493
8494 default:
8495 /* unsupported! */
8496 WARN_ON_ONCE(1);
8497 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008498 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008499 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008500 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008501 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008502 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008503 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8504 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008505 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008506 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008507 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8508 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008509#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008510 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008511#endif
8512 msg.rpc_argp = &calldata->args;
8513 msg.rpc_resp = &calldata->res;
8514 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008515
Trond Myklebust9c760d12017-07-31 18:38:50 -04008516 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008517
8518out_impl_id:
8519 kfree(calldata->res.impl_id);
8520out_server_scope:
8521 kfree(calldata->res.server_scope);
8522out_server_owner:
8523 kfree(calldata->res.server_owner);
8524out_calldata:
8525 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008526out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008527 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008528 return ERR_PTR(status);
8529}
8530
8531/*
8532 * _nfs4_proc_exchange_id()
8533 *
8534 * Wrapper for EXCHANGE_ID operation.
8535 */
NeilBrowna52458b2018-12-03 11:30:31 +11008536static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008537 u32 sp4_how)
8538{
8539 struct rpc_task *task;
8540 struct nfs41_exchange_id_args *argp;
8541 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008542 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008543 int status;
8544
8545 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8546 if (IS_ERR(task))
8547 return PTR_ERR(task);
8548
8549 argp = task->tk_msg.rpc_argp;
8550 resp = task->tk_msg.rpc_resp;
8551 status = task->tk_status;
8552 if (status != 0)
8553 goto out;
8554
Olga Kornievskaia8c390762020-10-16 09:25:45 -04008555 status = nfs4_check_cl_exchange_flags(resp->flags,
8556 clp->cl_mvops->minor_version);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008557 if (status != 0)
8558 goto out;
8559
8560 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8561 if (status != 0)
8562 goto out;
8563
Robert Milkowski7dc29932020-01-30 09:43:25 +00008564 do_renew_lease(clp, now);
8565
Trond Myklebust9c760d12017-07-31 18:38:50 -04008566 clp->cl_clientid = resp->clientid;
8567 clp->cl_exchange_flags = resp->flags;
8568 clp->cl_seqid = resp->seqid;
8569 /* Client ID is not confirmed */
8570 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8571 clear_bit(NFS4_SESSION_ESTABLISHED,
8572 &clp->cl_session->session_state);
8573
8574 if (clp->cl_serverscope != NULL &&
8575 !nfs41_same_server_scope(clp->cl_serverscope,
8576 resp->server_scope)) {
8577 dprintk("%s: server_scope mismatch detected\n",
8578 __func__);
8579 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8580 }
8581
8582 swap(clp->cl_serverowner, resp->server_owner);
8583 swap(clp->cl_serverscope, resp->server_scope);
8584 swap(clp->cl_implid, resp->impl_id);
8585
8586 /* Save the EXCHANGE_ID verifier session trunk tests */
8587 memcpy(clp->cl_confirm.data, argp->verifier.data,
8588 sizeof(clp->cl_confirm.data));
8589out:
8590 trace_nfs4_exchange_id(clp, status);
8591 rpc_put_task(task);
8592 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008593}
8594
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008595/*
8596 * nfs4_proc_exchange_id()
8597 *
8598 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8599 *
8600 * Since the clientid has expired, all compounds using sessions
8601 * associated with the stale clientid will be returning
8602 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8603 * be in some phase of session reset.
8604 *
8605 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8606 */
NeilBrowna52458b2018-12-03 11:30:31 +11008607int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008608{
8609 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8610 int status;
8611
8612 /* try SP4_MACH_CRED if krb5i/p */
8613 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8614 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008615 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008616 if (!status)
8617 return 0;
8618 }
8619
8620 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008621 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008622}
8623
Andy Adamson04fa2c62016-09-09 09:22:29 -04008624/**
8625 * nfs4_test_session_trunk
8626 *
8627 * This is an add_xprt_test() test function called from
8628 * rpc_clnt_setup_test_and_add_xprt.
8629 *
8630 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8631 * and is dereferrenced in nfs4_exchange_id_release
8632 *
8633 * Upon success, add the new transport to the rpc_clnt
8634 *
8635 * @clnt: struct rpc_clnt to get new transport
8636 * @xprt: the rpc_xprt to test
8637 * @data: call data for _nfs4_proc_exchange_id.
8638 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308639void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008640 void *data)
8641{
8642 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008643 struct rpc_task *task;
8644 int status;
8645
Andy Adamson04fa2c62016-09-09 09:22:29 -04008646 u32 sp4_how;
8647
8648 dprintk("--> %s try %s\n", __func__,
8649 xprt->address_strings[RPC_DISPLAY_ADDR]);
8650
8651 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8652
8653 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008654 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8655 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308656 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008657
8658 status = task->tk_status;
8659 if (status == 0)
8660 status = nfs4_detect_session_trunking(adata->clp,
8661 task->tk_msg.rpc_resp, xprt);
8662
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308663 if (status == 0)
8664 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8665
Trond Myklebust9c760d12017-07-31 18:38:50 -04008666 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008667}
8668EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8669
Trond Myklebust66245532012-05-25 17:18:09 -04008670static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008671 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008672{
8673 struct rpc_message msg = {
8674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8675 .rpc_argp = clp,
8676 .rpc_cred = cred,
8677 };
8678 int status;
8679
NeilBrown5a0c2572019-05-30 10:41:28 +10008680 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8681 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008682 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008683 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008684 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008685 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8686 return status;
8687}
8688
8689static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008690 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008691{
8692 unsigned int loop;
8693 int ret;
8694
8695 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8696 ret = _nfs4_proc_destroy_clientid(clp, cred);
8697 switch (ret) {
8698 case -NFS4ERR_DELAY:
8699 case -NFS4ERR_CLIENTID_BUSY:
8700 ssleep(1);
8701 break;
8702 default:
8703 return ret;
8704 }
8705 }
8706 return 0;
8707}
8708
8709int nfs4_destroy_clientid(struct nfs_client *clp)
8710{
NeilBrowna52458b2018-12-03 11:30:31 +11008711 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008712 int ret = 0;
8713
8714 if (clp->cl_mvops->minor_version < 1)
8715 goto out;
8716 if (clp->cl_exchange_flags == 0)
8717 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008718 if (clp->cl_preserve_clid)
8719 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008720 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008721 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008722 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008723 switch (ret) {
8724 case 0:
8725 case -NFS4ERR_STALE_CLIENTID:
8726 clp->cl_exchange_flags = 0;
8727 }
8728out:
8729 return ret;
8730}
8731
Donald Buczek0efb01b2019-07-07 21:26:08 +02008732#endif /* CONFIG_NFS_V4_1 */
8733
Andy Adamson2050f0c2009-04-01 09:22:30 -04008734struct nfs4_get_lease_time_data {
8735 struct nfs4_get_lease_time_args *args;
8736 struct nfs4_get_lease_time_res *res;
8737 struct nfs_client *clp;
8738};
8739
8740static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8741 void *calldata)
8742{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008743 struct nfs4_get_lease_time_data *data =
8744 (struct nfs4_get_lease_time_data *)calldata;
8745
8746 dprintk("--> %s\n", __func__);
8747 /* just setup sequence, do not trigger session recovery
8748 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008749 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008750 &data->args->la_seq_args,
8751 &data->res->lr_seq_res,
8752 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008753 dprintk("<-- %s\n", __func__);
8754}
8755
8756/*
8757 * Called from nfs4_state_manager thread for session setup, so don't recover
8758 * from sequence operation or clientid errors.
8759 */
8760static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8761{
8762 struct nfs4_get_lease_time_data *data =
8763 (struct nfs4_get_lease_time_data *)calldata;
8764
8765 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008766 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008767 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008768 switch (task->tk_status) {
8769 case -NFS4ERR_DELAY:
8770 case -NFS4ERR_GRACE:
8771 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8772 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8773 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05008774 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008775 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008776 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008777 return;
8778 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008779 dprintk("<-- %s\n", __func__);
8780}
8781
Trond Myklebust17280172012-03-11 13:11:00 -04008782static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008783 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8784 .rpc_call_done = nfs4_get_lease_time_done,
8785};
8786
8787int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8788{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008789 struct nfs4_get_lease_time_args args;
8790 struct nfs4_get_lease_time_res res = {
8791 .lr_fsinfo = fsinfo,
8792 };
8793 struct nfs4_get_lease_time_data data = {
8794 .args = &args,
8795 .res = &res,
8796 .clp = clp,
8797 };
8798 struct rpc_message msg = {
8799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8800 .rpc_argp = &args,
8801 .rpc_resp = &res,
8802 };
8803 struct rpc_task_setup task_setup = {
8804 .rpc_client = clp->cl_rpcclient,
8805 .rpc_message = &msg,
8806 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008807 .callback_data = &data,
8808 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008809 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008810
Anna Schumakerfba83f32018-05-04 16:22:50 -04008811 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008812 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008813}
8814
Donald Buczek0efb01b2019-07-07 21:26:08 +02008815#ifdef CONFIG_NFS_V4_1
8816
Andy Adamsonfc931582009-04-01 09:22:31 -04008817/*
8818 * Initialize the values to be used by the client in CREATE_SESSION
8819 * If nfs4_init_session set the fore channel request and response sizes,
8820 * use them.
8821 *
8822 * Set the back channel max_resp_sz_cached to zero to force the client to
8823 * always set csa_cachethis to FALSE because the current implementation
8824 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8825 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008826static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8827 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008828{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008829 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008830 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008831 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008832
Andy Adamson18aad3d2013-06-26 12:21:49 -04008833 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8834 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8835
Andy Adamsonfc931582009-04-01 09:22:31 -04008836 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008837 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8838 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008839 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008840 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008841
8842 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008843 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008844 __func__,
8845 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008846 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008847
8848 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008849 args->bc_attrs.max_rqst_sz = max_bc_payload;
8850 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008851 args->bc_attrs.max_resp_sz_cached = 0;
8852 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008853 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008854 if (args->bc_attrs.max_reqs > max_bc_slots)
8855 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008856
8857 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8858 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8859 __func__,
8860 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8861 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8862 args->bc_attrs.max_reqs);
8863}
8864
Trond Myklebust79969dd2015-02-18 11:30:18 -08008865static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8866 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008867{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008868 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008869 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008870
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008871 if (rcvd->max_resp_sz > sent->max_resp_sz)
8872 return -EINVAL;
8873 /*
8874 * Our requested max_ops is the minimum we need; we're not
8875 * prepared to break up compounds into smaller pieces than that.
8876 * So, no point even trying to continue if the server won't
8877 * cooperate:
8878 */
8879 if (rcvd->max_ops < sent->max_ops)
8880 return -EINVAL;
8881 if (rcvd->max_reqs == 0)
8882 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008883 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8884 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008885 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008886}
8887
Trond Myklebust79969dd2015-02-18 11:30:18 -08008888static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8889 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008890{
8891 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008892 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008893
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008894 if (!(res->flags & SESSION4_BACK_CHAN))
8895 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008896 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8897 return -EINVAL;
8898 if (rcvd->max_resp_sz < sent->max_resp_sz)
8899 return -EINVAL;
8900 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8901 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008902 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008903 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008904 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008905 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008906out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008907 return 0;
8908}
Andy Adamson8d353012009-04-01 09:22:32 -04008909
Andy Adamson8d353012009-04-01 09:22:32 -04008910static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008911 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008912{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008913 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008914
Trond Myklebust79969dd2015-02-18 11:30:18 -08008915 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008916 if (ret)
8917 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008918 return nfs4_verify_back_channel_attrs(args, res);
8919}
8920
8921static void nfs4_update_session(struct nfs4_session *session,
8922 struct nfs41_create_session_res *res)
8923{
8924 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008925 /* Mark client id and session as being confirmed */
8926 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8927 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008928 session->flags = res->flags;
8929 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008930 if (res->flags & SESSION4_BACK_CHAN)
8931 memcpy(&session->bc_attrs, &res->bc_attrs,
8932 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008933}
8934
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008935static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008936 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008937{
8938 struct nfs4_session *session = clp->cl_session;
8939 struct nfs41_create_session_args args = {
8940 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008941 .clientid = clp->cl_clientid,
8942 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008943 .cb_program = NFS4_CALLBACK,
8944 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008945 struct nfs41_create_session_res res;
8946
Andy Adamsonfc931582009-04-01 09:22:31 -04008947 struct rpc_message msg = {
8948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8949 .rpc_argp = &args,
8950 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008951 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008952 };
8953 int status;
8954
Chuck Lever6b26cc82016-05-02 14:40:40 -04008955 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008956 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008957
NeilBrown5a0c2572019-05-30 10:41:28 +10008958 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8959 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008960 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008961
Trond Myklebustb519d402016-09-11 14:50:01 -04008962 switch (status) {
8963 case -NFS4ERR_STALE_CLIENTID:
8964 case -NFS4ERR_DELAY:
8965 case -ETIMEDOUT:
8966 case -EACCES:
8967 case -EAGAIN:
8968 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08008969 }
Trond Myklebustb519d402016-09-11 14:50:01 -04008970
8971 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008972 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008973 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008974 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008975 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008976 if (status)
8977 goto out;
8978 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008979 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008980out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008981 return status;
8982}
8983
8984/*
8985 * Issues a CREATE_SESSION operation to the server.
8986 * It is the responsibility of the caller to verify the session is
8987 * expired before calling this routine.
8988 */
NeilBrowna52458b2018-12-03 11:30:31 +11008989int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008990{
8991 int status;
8992 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008993 struct nfs4_session *session = clp->cl_session;
8994
8995 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8996
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008997 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008998 if (status)
8999 goto out;
9000
Andy Adamsonaacd5532011-11-09 13:58:21 -05009001 /* Init or reset the session slot tables */
9002 status = nfs4_setup_session_slot_tables(session);
9003 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04009004 if (status)
9005 goto out;
9006
9007 ptr = (unsigned *)&session->sess_id.data[0];
9008 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9009 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04009010out:
9011 dprintk("<-- %s\n", __func__);
9012 return status;
9013}
9014
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009015/*
9016 * Issue the over-the-wire RPC DESTROY_SESSION.
9017 * The caller must serialize access to this routine.
9018 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009019int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11009020 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009021{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04009022 struct rpc_message msg = {
9023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9024 .rpc_argp = session,
9025 .rpc_cred = cred,
9026 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009027 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009028
9029 dprintk("--> nfs4_proc_destroy_session\n");
9030
9031 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05009032 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9033 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009034
NeilBrown5a0c2572019-05-30 10:41:28 +10009035 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9036 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009037 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009038
9039 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04009040 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04009041 "Session has been destroyed regardless...\n", status);
9042
9043 dprintk("<-- nfs4_proc_destroy_session\n");
9044 return status;
9045}
9046
Trond Myklebust7b38c362012-05-23 13:23:31 -04009047/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009048 * Renew the cl_session lease.
9049 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009050struct nfs4_sequence_data {
9051 struct nfs_client *clp;
9052 struct nfs4_sequence_args args;
9053 struct nfs4_sequence_res res;
9054};
9055
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009056static void nfs41_sequence_release(void *data)
9057{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009058 struct nfs4_sequence_data *calldata = data;
9059 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009060
Elena Reshetova212bf412017-10-20 12:53:38 +03009061 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009062 nfs4_schedule_state_renewal(clp);
9063 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009064 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009065}
9066
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009067static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9068{
9069 switch(task->tk_status) {
9070 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009071 rpc_delay(task, NFS4_POLL_RETRY_MAX);
9072 return -EAGAIN;
9073 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009074 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009075 }
9076 return 0;
9077}
9078
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009079static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009080{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009081 struct nfs4_sequence_data *calldata = data;
9082 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009083
Trond Myklebust14516c32010-07-31 14:29:06 -04009084 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9085 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009086
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009087 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009088 if (task->tk_status < 0) {
9089 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03009090 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08009091 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009092
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009093 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9094 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009095 return;
9096 }
9097 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009098 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08009099out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009100 dprintk("<-- %s\n", __func__);
9101}
9102
9103static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9104{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009105 struct nfs4_sequence_data *calldata = data;
9106 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009107 struct nfs4_sequence_args *args;
9108 struct nfs4_sequence_res *res;
9109
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009110 args = task->tk_msg.rpc_argp;
9111 res = task->tk_msg.rpc_resp;
9112
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009113 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009114}
9115
9116static const struct rpc_call_ops nfs41_sequence_ops = {
9117 .rpc_call_done = nfs41_sequence_call_done,
9118 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009119 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009120};
9121
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009122static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009123 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009124 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009125 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009126{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009127 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009128 struct rpc_message msg = {
9129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9130 .rpc_cred = cred,
9131 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009132 struct rpc_task_setup task_setup_data = {
9133 .rpc_client = clp->cl_rpcclient,
9134 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009135 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04009136 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009137 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009138 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009139
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009140 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009141 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009142 goto out_err;
9143
9144 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009145 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009146 if (calldata == NULL)
9147 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009148 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009149 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009150 msg.rpc_argp = &calldata->args;
9151 msg.rpc_resp = &calldata->res;
9152 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009153 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009154
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009155 ret = rpc_run_task(&task_setup_data);
9156 if (IS_ERR(ret))
9157 goto out_err;
9158 return ret;
9159out_put_clp:
9160 nfs_put_client(clp);
9161out_err:
9162 nfs41_release_slot(slot);
9163 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009164}
9165
NeilBrowna52458b2018-12-03 11:30:31 +11009166static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009167{
9168 struct rpc_task *task;
9169 int ret = 0;
9170
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009171 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009172 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009173 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009174 if (IS_ERR(task))
9175 ret = PTR_ERR(task);
9176 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009177 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009178 dprintk("<-- %s status=%d\n", __func__, ret);
9179 return ret;
9180}
9181
NeilBrowna52458b2018-12-03 11:30:31 +11009182static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009183{
9184 struct rpc_task *task;
9185 int ret;
9186
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009187 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009188 if (IS_ERR(task)) {
9189 ret = PTR_ERR(task);
9190 goto out;
9191 }
9192 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009193 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009194 ret = task->tk_status;
9195 rpc_put_task(task);
9196out:
9197 dprintk("<-- %s status=%d\n", __func__, ret);
9198 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009199}
9200
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009201struct nfs4_reclaim_complete_data {
9202 struct nfs_client *clp;
9203 struct nfs41_reclaim_complete_args arg;
9204 struct nfs41_reclaim_complete_res res;
9205};
9206
9207static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9208{
9209 struct nfs4_reclaim_complete_data *calldata = data;
9210
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009211 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009212 &calldata->arg.seq_args,
9213 &calldata->res.seq_res,
9214 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009215}
9216
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009217static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9218{
9219 switch(task->tk_status) {
9220 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009221 wake_up_all(&clp->cl_lock_waitq);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009222 fallthrough;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009223 case -NFS4ERR_COMPLETE_ALREADY:
9224 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9225 break;
9226 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009227 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009228 fallthrough;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009229 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009230 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009231 case -NFS4ERR_BADSESSION:
9232 case -NFS4ERR_DEADSESSION:
9233 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009234 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009235 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009236 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009237 }
9238 return 0;
9239}
9240
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009241static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9242{
9243 struct nfs4_reclaim_complete_data *calldata = data;
9244 struct nfs_client *clp = calldata->clp;
9245 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9246
9247 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04009248 if (!nfs41_sequence_done(task, res))
9249 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009250
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009251 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009252 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9253 rpc_restart_call_prepare(task);
9254 return;
9255 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009256 dprintk("<-- %s\n", __func__);
9257}
9258
9259static void nfs4_free_reclaim_complete_data(void *data)
9260{
9261 struct nfs4_reclaim_complete_data *calldata = data;
9262
9263 kfree(calldata);
9264}
9265
9266static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9267 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9268 .rpc_call_done = nfs4_reclaim_complete_done,
9269 .rpc_release = nfs4_free_reclaim_complete_data,
9270};
9271
9272/*
9273 * Issue a global reclaim complete.
9274 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009275static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009276 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009277{
9278 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009279 struct rpc_message msg = {
9280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009281 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009282 };
9283 struct rpc_task_setup task_setup_data = {
9284 .rpc_client = clp->cl_rpcclient,
9285 .rpc_message = &msg,
9286 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009287 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009288 };
9289 int status = -ENOMEM;
9290
9291 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009292 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009293 if (calldata == NULL)
9294 goto out;
9295 calldata->clp = clp;
9296 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009297
Anna Schumakerfba83f32018-05-04 16:22:50 -04009298 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009299 msg.rpc_argp = &calldata->arg;
9300 msg.rpc_resp = &calldata->res;
9301 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009302 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009303out:
9304 dprintk("<-- %s status=%d\n", __func__, status);
9305 return status;
9306}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009307
9308static void
9309nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9310{
9311 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009312 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009313
9314 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009315 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009316 &lgp->res.seq_res, task);
9317 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009318}
9319
9320static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9321{
9322 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009323
9324 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009325 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009326 dprintk("<-- %s\n", __func__);
9327}
9328
9329static int
9330nfs4_layoutget_handle_exception(struct rpc_task *task,
9331 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9332{
Trond Myklebustee314c22012-10-01 17:25:48 -07009333 struct inode *inode = lgp->args.inode;
9334 struct nfs_server *server = NFS_SERVER(inode);
9335 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009336 int nfs4err = task->tk_status;
9337 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009338 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009339
Boaz Harroshed7e5422014-01-22 20:34:54 +02009340 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009341
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009342 nfs4_sequence_free_slot(&lgp->res.seq_res);
9343
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009344 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009345 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009346 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009347
9348 /*
9349 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9350 * on the file. set tk_status to -ENODATA to tell upper layer to
9351 * retry go inband.
9352 */
9353 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009354 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009355 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009356 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009357 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9358 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9359 */
9360 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009361 status = -EOVERFLOW;
9362 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009363 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009364 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009365 * (or clients) writing to the same RAID stripe except when
9366 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009367 *
9368 * Treat it like we would RECALLCONFLICT -- we retry for a little
9369 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009370 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009371 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009372 if (lgp->args.minlength == 0) {
9373 status = -EOVERFLOW;
9374 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009375 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009376 status = -EBUSY;
9377 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009378 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009379 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009380 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009381 case -NFS4ERR_DELEG_REVOKED:
9382 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009383 case -NFS4ERR_EXPIRED:
9384 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009385 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009386 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009387 lo = NFS_I(inode)->layout;
9388 /* If the open stateid was bad, then recover it. */
9389 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009390 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009391 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009392 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009393 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009394 break;
9395 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009396
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009397 /*
9398 * Mark the bad layout state as invalid, then retry
9399 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009400 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009401 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009402 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009403 pnfs_free_lseg_list(&head);
9404 status = -EAGAIN;
9405 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009406 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009407
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009408 err = nfs4_handle_exception(server, nfs4err, exception);
9409 if (!status) {
9410 if (exception->retry)
9411 status = -EAGAIN;
9412 else
9413 status = err;
9414 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009415out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009416 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009417 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009418}
9419
Fred Isamandacb4522016-09-19 17:47:09 -04009420size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009421{
9422 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9423 return nfs_page_array_len(0, max_resp_sz);
9424}
9425
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009426static void nfs4_layoutget_release(void *calldata)
9427{
9428 struct nfs4_layoutget *lgp = calldata;
9429
9430 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009431 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009432 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009433 dprintk("<-- %s\n", __func__);
9434}
9435
9436static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9437 .rpc_call_prepare = nfs4_layoutget_prepare,
9438 .rpc_call_done = nfs4_layoutget_done,
9439 .rpc_release = nfs4_layoutget_release,
9440};
9441
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009442struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009443nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009444{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009445 struct inode *inode = lgp->args.inode;
9446 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009447 struct rpc_task *task;
9448 struct rpc_message msg = {
9449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9450 .rpc_argp = &lgp->args,
9451 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009452 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009453 };
9454 struct rpc_task_setup task_setup_data = {
9455 .rpc_client = server->client,
9456 .rpc_message = &msg,
9457 .callback_ops = &nfs4_layoutget_call_ops,
9458 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009459 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009460 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009461 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009462 struct nfs4_exception exception = {
9463 .inode = inode,
9464 .timeout = *timeout,
9465 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009466 int status = 0;
9467
9468 dprintk("--> %s\n", __func__);
9469
Peng Tao4bd5a982014-11-17 11:05:17 +08009470 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9471 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9472
Anna Schumakerfba83f32018-05-04 16:22:50 -04009473 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009474
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009475 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009476
Anna Schumaker820bf852017-01-11 15:01:43 -05009477 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009478 if (status != 0)
9479 goto out;
9480
Trond Myklebust18c07782019-02-13 07:55:31 -05009481 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009482 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9483 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009484 } else if (lgp->res.layoutp->len == 0) {
9485 status = -EAGAIN;
9486 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009487 } else
9488 lseg = pnfs_layout_process(lgp);
9489out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009490 trace_nfs4_layoutget(lgp->args.ctx,
9491 &lgp->args.range,
9492 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009493 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009494 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009495
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009496 rpc_put_task(task);
9497 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009498 if (status)
9499 return ERR_PTR(status);
9500 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009501}
9502
Benny Halevycbe82602011-05-22 19:52:37 +03009503static void
9504nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9505{
9506 struct nfs4_layoutreturn *lrp = calldata;
9507
9508 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009509 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009510 &lrp->args.seq_args,
9511 &lrp->res.seq_res,
9512 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009513 if (!pnfs_layout_is_valid(lrp->args.layout))
9514 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009515}
9516
9517static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9518{
9519 struct nfs4_layoutreturn *lrp = calldata;
9520 struct nfs_server *server;
9521
9522 dprintk("--> %s\n", __func__);
9523
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009524 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009525 return;
9526
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009527 /*
9528 * Was there an RPC level error? Assume the call succeeded,
9529 * and that we need to release the layout
9530 */
9531 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9532 lrp->res.lrs_present = 0;
9533 return;
9534 }
9535
Benny Halevycbe82602011-05-22 19:52:37 +03009536 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009537 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009538 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009539 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009540 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009541 lrp->args.inode))
9542 goto out_restart;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009543 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009544 default:
9545 task->tk_status = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05009546 fallthrough;
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009547 case 0:
9548 break;
9549 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009550 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009551 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009552 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009553 }
Benny Halevycbe82602011-05-22 19:52:37 +03009554 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009555 return;
9556out_restart:
9557 task->tk_status = 0;
9558 nfs4_sequence_free_slot(&lrp->res.seq_res);
9559 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009560}
9561
9562static void nfs4_layoutreturn_release(void *calldata)
9563{
9564 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009565 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009566
9567 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009568 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009569 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009570 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009571 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9572 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009573 pnfs_put_layout_hdr(lrp->args.layout);
9574 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009575 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009576 kfree(calldata);
9577 dprintk("<-- %s\n", __func__);
9578}
9579
9580static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9581 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9582 .rpc_call_done = nfs4_layoutreturn_done,
9583 .rpc_release = nfs4_layoutreturn_release,
9584};
9585
Peng Tao6c166052014-11-17 09:30:40 +08009586int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009587{
9588 struct rpc_task *task;
9589 struct rpc_message msg = {
9590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9591 .rpc_argp = &lrp->args,
9592 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009593 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009594 };
9595 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009596 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009597 .rpc_message = &msg,
9598 .callback_ops = &nfs4_layoutreturn_call_ops,
9599 .callback_data = lrp,
9600 };
Peng Tao6c166052014-11-17 09:30:40 +08009601 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009602
Andrew Elble99ade3c2015-12-02 09:39:51 -05009603 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9604 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9605 &task_setup_data.rpc_client, &msg);
9606
Benny Halevycbe82602011-05-22 19:52:37 +03009607 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009608 if (!sync) {
9609 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9610 if (!lrp->inode) {
9611 nfs4_layoutreturn_release(lrp);
9612 return -EAGAIN;
9613 }
9614 task_setup_data.flags |= RPC_TASK_ASYNC;
9615 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009616 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009617 task = rpc_run_task(&task_setup_data);
9618 if (IS_ERR(task))
9619 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009620 if (sync)
9621 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009622 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009623 dprintk("<-- %s status=%d\n", __func__, status);
9624 rpc_put_task(task);
9625 return status;
9626}
9627
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009628static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009629_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9630 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009631 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009632{
9633 struct nfs4_getdeviceinfo_args args = {
9634 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009635 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9636 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009637 };
9638 struct nfs4_getdeviceinfo_res res = {
9639 .pdev = pdev,
9640 };
9641 struct rpc_message msg = {
9642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9643 .rpc_argp = &args,
9644 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009645 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009646 };
9647 int status;
9648
9649 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009650 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009651 if (res.notification & ~args.notify_types)
9652 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009653 if (res.notification != args.notify_types)
9654 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009655
Trond Myklebustcac1d3a2020-12-16 16:31:26 -05009656 trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
9657
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009658 dprintk("<-- %s status=%d\n", __func__, status);
9659
9660 return status;
9661}
9662
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009663int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9664 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009665 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009666{
9667 struct nfs4_exception exception = { };
9668 int err;
9669
9670 do {
9671 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009672 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009673 &exception);
9674 } while (exception.retry);
9675 return err;
9676}
9677EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9678
Andy Adamson863a3c62011-03-23 13:27:54 +00009679static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9680{
9681 struct nfs4_layoutcommit_data *data = calldata;
9682 struct nfs_server *server = NFS_SERVER(data->args.inode);
9683
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009684 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009685 &data->args.seq_args,
9686 &data->res.seq_res,
9687 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009688}
9689
9690static void
9691nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9692{
9693 struct nfs4_layoutcommit_data *data = calldata;
9694 struct nfs_server *server = NFS_SERVER(data->args.inode);
9695
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009696 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009697 return;
9698
9699 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009700 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9701 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9702 case -NFS4ERR_BADLAYOUT: /* no layout */
9703 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009704 task->tk_status = 0;
Gustavo A. R. Silvaffb81712020-11-20 12:26:46 -06009705 break;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009706 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009707 break;
9708 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009709 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009710 rpc_restart_call_prepare(task);
9711 return;
9712 }
9713 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009714}
9715
9716static void nfs4_layoutcommit_release(void *calldata)
9717{
9718 struct nfs4_layoutcommit_data *data = calldata;
9719
Andy Adamsondb29c082011-07-30 20:52:38 -04009720 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009721 nfs_post_op_update_inode_force_wcc(data->args.inode,
9722 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009723 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009724 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009725 kfree(data);
9726}
9727
9728static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9729 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9730 .rpc_call_done = nfs4_layoutcommit_done,
9731 .rpc_release = nfs4_layoutcommit_release,
9732};
9733
9734int
Andy Adamsonef311532011-03-12 02:58:10 -05009735nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009736{
9737 struct rpc_message msg = {
9738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9739 .rpc_argp = &data->args,
9740 .rpc_resp = &data->res,
9741 .rpc_cred = data->cred,
9742 };
9743 struct rpc_task_setup task_setup_data = {
9744 .task = &data->task,
9745 .rpc_client = NFS_CLIENT(data->args.inode),
9746 .rpc_message = &msg,
9747 .callback_ops = &nfs4_layoutcommit_ops,
9748 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009749 };
9750 struct rpc_task *task;
9751 int status = 0;
9752
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009753 dprintk("NFS: initiating layoutcommit call. sync %d "
9754 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009755 data->args.lastbytewritten,
9756 data->args.inode->i_ino);
9757
Trond Myklebust472e2592015-02-05 16:50:30 -05009758 if (!sync) {
9759 data->inode = nfs_igrab_and_active(data->args.inode);
9760 if (data->inode == NULL) {
9761 nfs4_layoutcommit_release(data);
9762 return -EAGAIN;
9763 }
9764 task_setup_data.flags = RPC_TASK_ASYNC;
9765 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009766 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009767 task = rpc_run_task(&task_setup_data);
9768 if (IS_ERR(task))
9769 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009770 if (sync)
9771 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009772 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009773 dprintk("%s: status %d\n", __func__, status);
9774 rpc_put_task(task);
9775 return status;
9776}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009777
Trond Myklebust302fad72019-02-18 13:32:38 -05009778/*
Andy Adamson97431202013-08-08 10:57:56 -04009779 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9780 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9781 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009782static int
9783_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009784 struct nfs_fsinfo *info,
9785 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009786{
9787 struct nfs41_secinfo_no_name_args args = {
9788 .style = SECINFO_STYLE_CURRENT_FH,
9789 };
9790 struct nfs4_secinfo_res res = {
9791 .flavors = flavors,
9792 };
9793 struct rpc_message msg = {
9794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9795 .rpc_argp = &args,
9796 .rpc_resp = &res,
9797 };
Anna Schumakercc15e242019-08-14 16:22:31 -04009798 struct nfs4_call_sync_data data = {
9799 .seq_server = server,
9800 .seq_args = &args.seq_args,
9801 .seq_res = &res.seq_res,
9802 };
9803 struct rpc_task_setup task_setup = {
9804 .rpc_client = server->client,
9805 .rpc_message = &msg,
9806 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9807 .callback_data = &data,
9808 .flags = RPC_TASK_NO_ROUND_ROBIN,
9809 };
NeilBrowna52458b2018-12-03 11:30:31 +11009810 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009811 int status;
9812
9813 if (use_integrity) {
Colin Ian King48bb6ec2020-06-10 00:22:57 +01009814 task_setup.rpc_client = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009815
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009816 cred = nfs4_get_clid_cred(server->nfs_client);
9817 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009818 }
9819
9820 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009821 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9822 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009823 dprintk("<-- %s status=%d\n", __func__, status);
9824
NeilBrowna52458b2018-12-03 11:30:31 +11009825 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009826
9827 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009828}
9829
9830static int
9831nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9832 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9833{
Trond Myklebust0688e642019-04-07 13:59:09 -04009834 struct nfs4_exception exception = {
9835 .interruptible = true,
9836 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009837 int err;
9838 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009839 /* first try using integrity protection */
9840 err = -NFS4ERR_WRONGSEC;
9841
9842 /* try to use integrity protection with machine cred */
9843 if (_nfs4_is_integrity_protected(server->nfs_client))
9844 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9845 flavors, true);
9846
9847 /*
9848 * if unable to use integrity protection, or SECINFO with
9849 * integrity protection returns NFS4ERR_WRONGSEC (which is
9850 * disallowed by spec, but exists in deployed servers) use
9851 * the current filesystem's rpc_client and the user cred.
9852 */
9853 if (err == -NFS4ERR_WRONGSEC)
9854 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9855 flavors, false);
9856
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009857 switch (err) {
9858 case 0:
9859 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009860 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009861 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009862 default:
9863 err = nfs4_handle_exception(server, err, &exception);
9864 }
9865 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009866out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009867 return err;
9868}
9869
9870static int
9871nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9872 struct nfs_fsinfo *info)
9873{
9874 int err;
9875 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009876 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009877 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009878 struct nfs4_secinfo4 *secinfo;
9879 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009880
9881 page = alloc_page(GFP_KERNEL);
9882 if (!page) {
9883 err = -ENOMEM;
9884 goto out;
9885 }
9886
9887 flavors = page_address(page);
9888 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9889
9890 /*
9891 * Fall back on "guess and check" method if
9892 * the server doesn't support SECINFO_NO_NAME
9893 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009894 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009895 err = nfs4_find_root_sec(server, fhandle, info);
9896 goto out_freepage;
9897 }
9898 if (err)
9899 goto out_freepage;
9900
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009901 for (i = 0; i < flavors->num_flavors; i++) {
9902 secinfo = &flavors->flavors[i];
9903
9904 switch (secinfo->flavor) {
9905 case RPC_AUTH_NULL:
9906 case RPC_AUTH_UNIX:
9907 case RPC_AUTH_GSS:
9908 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9909 &secinfo->flavor_info);
9910 break;
9911 default:
9912 flavor = RPC_AUTH_MAXFLAVOR;
9913 break;
9914 }
9915
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009916 if (!nfs_auth_info_match(&server->auth_info, flavor))
9917 flavor = RPC_AUTH_MAXFLAVOR;
9918
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009919 if (flavor != RPC_AUTH_MAXFLAVOR) {
9920 err = nfs4_lookup_root_sec(server, fhandle,
9921 info, flavor);
9922 if (!err)
9923 break;
9924 }
9925 }
9926
9927 if (flavor == RPC_AUTH_MAXFLAVOR)
9928 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009929
9930out_freepage:
9931 put_page(page);
9932 if (err == -EACCES)
9933 return -EPERM;
9934out:
9935 return err;
9936}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009937
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009938static int _nfs41_test_stateid(struct nfs_server *server,
9939 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009940 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009941{
9942 int status;
9943 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009944 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009945 };
9946 struct nfs41_test_stateid_res res;
9947 struct rpc_message msg = {
9948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9949 .rpc_argp = &args,
9950 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009951 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009952 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009953 struct rpc_clnt *rpc_client = server->client;
9954
9955 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9956 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009957
Chuck Lever38527b12012-07-11 16:30:23 -04009958 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009959 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009960 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009961 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009962 if (status != NFS_OK) {
9963 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009964 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009965 }
9966 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009967 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009968}
9969
Trond Myklebust43912bb2016-09-22 13:38:56 -04009970static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9971 int err, struct nfs4_exception *exception)
9972{
9973 exception->retry = 0;
9974 switch(err) {
9975 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009976 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009977 nfs4_handle_exception(server, err, exception);
9978 break;
9979 case -NFS4ERR_BADSESSION:
9980 case -NFS4ERR_BADSLOT:
9981 case -NFS4ERR_BAD_HIGH_SLOT:
9982 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9983 case -NFS4ERR_DEADSESSION:
9984 nfs4_do_handle_exception(server, err, exception);
9985 }
9986}
9987
Chuck Lever38527b12012-07-11 16:30:23 -04009988/**
9989 * nfs41_test_stateid - perform a TEST_STATEID operation
9990 *
9991 * @server: server / transport on which to perform the operation
9992 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009993 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009994 *
9995 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9996 * Otherwise a negative NFS4ERR value is returned if the operation
9997 * failed or the state ID is not currently valid.
9998 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009999static int nfs41_test_stateid(struct nfs_server *server,
10000 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010001 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -040010002{
Trond Myklebust0688e642019-04-07 13:59:09 -040010003 struct nfs4_exception exception = {
10004 .interruptible = true,
10005 };
Bryan Schumaker7d974792011-06-02 14:59:08 -040010006 int err;
10007 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010008 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -040010009 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -040010010 } while (exception.retry);
10011 return err;
10012}
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010013
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010014struct nfs_free_stateid_data {
10015 struct nfs_server *server;
10016 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010017 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010018};
10019
10020static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10021{
10022 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -050010023 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010024 &data->args.seq_args,
10025 &data->res.seq_res,
10026 task);
10027}
10028
10029static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10030{
10031 struct nfs_free_stateid_data *data = calldata;
10032
10033 nfs41_sequence_done(task, &data->res.seq_res);
10034
10035 switch (task->tk_status) {
10036 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +100010037 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010038 rpc_restart_call_prepare(task);
10039 }
10040}
10041
10042static void nfs41_free_stateid_release(void *calldata)
10043{
10044 kfree(calldata);
10045}
10046
Trond Myklebust17f26b12013-08-21 15:48:42 -040010047static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010048 .rpc_call_prepare = nfs41_free_stateid_prepare,
10049 .rpc_call_done = nfs41_free_stateid_done,
10050 .rpc_release = nfs41_free_stateid_release,
10051};
10052
Anna Schumaker2f261022018-05-15 13:03:39 -040010053/**
10054 * nfs41_free_stateid - perform a FREE_STATEID operation
10055 *
10056 * @server: server / transport on which to perform the operation
10057 * @stateid: state ID to release
10058 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -050010059 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -040010060 *
10061 * Note: this function is always asynchronous.
10062 */
10063static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010064 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +110010065 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010066 bool privileged)
10067{
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010068 struct rpc_message msg = {
10069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040010070 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010071 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010072 struct rpc_task_setup task_setup = {
10073 .rpc_client = server->client,
10074 .rpc_message = &msg,
10075 .callback_ops = &nfs41_free_stateid_ops,
10076 .flags = RPC_TASK_ASYNC,
10077 };
10078 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -040010079 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010080
Weston Andros Adamson3787d502013-08-13 16:37:36 -040010081 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10082 &task_setup.rpc_client, &msg);
10083
Chuck Lever38527b12012-07-11 16:30:23 -040010084 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010085 data = kmalloc(sizeof(*data), GFP_NOFS);
10086 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -040010087 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010088 data->server = server;
10089 nfs4_stateid_copy(&data->args.stateid, stateid);
10090
10091 task_setup.callback_data = data;
10092
10093 msg.rpc_argp = &data->args;
10094 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -040010095 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -040010096 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010097 if (IS_ERR(task))
10098 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -040010099 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010100 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -040010101}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010102
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010103static void
10104nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010105{
NeilBrowna52458b2018-12-03 11:30:31 +110010106 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010107
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010108 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010109 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010110}
10111
Trond Myklebust36281ca2012-03-04 18:13:56 -050010112static bool nfs41_match_stateid(const nfs4_stateid *s1,
10113 const nfs4_stateid *s2)
10114{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010115 if (s1->type != s2->type)
10116 return false;
10117
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010118 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010119 return false;
10120
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010121 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010122 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010123
Anna Schumaker045c5512017-01-11 16:59:48 -050010124 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010125}
10126
Andy Adamson557134a2009-04-01 09:21:53 -040010127#endif /* CONFIG_NFS_V4_1 */
10128
Trond Myklebust36281ca2012-03-04 18:13:56 -050010129static bool nfs4_match_stateid(const nfs4_stateid *s1,
10130 const nfs4_stateid *s2)
10131{
Trond Myklebustf597c532012-03-04 18:13:56 -050010132 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010133}
10134
10135
Trond Myklebust17280172012-03-11 13:11:00 -040010136static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010137 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010138 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139 .recover_open = nfs4_open_reclaim,
10140 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010141 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010142 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010143};
10144
Andy Adamson591d71c2009-04-01 09:22:47 -040010145#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010146static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010147 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10148 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10149 .recover_open = nfs4_open_reclaim,
10150 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010151 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010152 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010153 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010154};
10155#endif /* CONFIG_NFS_V4_1 */
10156
Trond Myklebust17280172012-03-11 13:11:00 -040010157static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010158 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010159 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010160 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010161 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010162 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010163};
10164
Andy Adamson591d71c2009-04-01 09:22:47 -040010165#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010166static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010167 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10168 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010169 .recover_open = nfs41_open_expired,
10170 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010171 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010172};
10173#endif /* CONFIG_NFS_V4_1 */
10174
Trond Myklebust17280172012-03-11 13:11:00 -040010175static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010176 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010177 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010178 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010179};
10180
10181#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010182static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010183 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010184 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010185 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010186};
10187#endif
10188
Chuck Leverec011fe2013-10-17 14:12:39 -040010189static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010190 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010191 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010192};
10193
10194#if defined(CONFIG_NFS_V4_1)
10195static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010196 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010197 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010198};
10199#endif /* CONFIG_NFS_V4_1 */
10200
Trond Myklebust97dc1352010-06-16 09:52:26 -040010201static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10202 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010203 .init_caps = NFS_CAP_READDIRPLUS
10204 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010205 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010206 .init_client = nfs40_init_client,
10207 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010208 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010209 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010210 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010211 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010212 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010213 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010214 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10215 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10216 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010217 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010218};
10219
10220#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010221static struct nfs_seqid *
10222nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10223{
10224 return NULL;
10225}
10226
Trond Myklebust97dc1352010-06-16 09:52:26 -040010227static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10228 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010229 .init_caps = NFS_CAP_READDIRPLUS
10230 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010231 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010232 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010233 | NFS_CAP_ATOMIC_OPEN_V1
10234 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010235 .init_client = nfs41_init_client,
10236 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010237 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010238 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010239 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010240 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010241 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010242 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010243 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010244 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10245 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10246 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010247 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010248};
10249#endif
10250
Steve Dickson42c2c422013-05-22 12:50:38 -040010251#if defined(CONFIG_NFS_V4_2)
10252static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10253 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010254 .init_caps = NFS_CAP_READDIRPLUS
10255 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010256 | NFS_CAP_POSIX_LOCK
10257 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010258 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010259 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010260 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010261 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010262 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010263 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010264 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010265 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010266 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010267 | NFS_CAP_CLONE
Anna Schumakerc5675522014-05-28 13:41:22 -040010268 | NFS_CAP_LAYOUTERROR
10269 | NFS_CAP_READ_PLUS,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010270 .init_client = nfs41_init_client,
10271 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010272 .match_stateid = nfs41_match_stateid,
10273 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010274 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010275 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010276 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010277 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010278 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010279 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10280 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10281 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010282 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010283};
10284#endif
10285
Trond Myklebust97dc1352010-06-16 09:52:26 -040010286const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10287 [0] = &nfs_v4_0_minor_ops,
10288#if defined(CONFIG_NFS_V4_1)
10289 [1] = &nfs_v4_1_minor_ops,
10290#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010291#if defined(CONFIG_NFS_V4_2)
10292 [2] = &nfs_v4_2_minor_ops,
10293#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010294};
10295
Trond Myklebust13997822016-07-24 17:10:52 -040010296static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010297{
Frank van der Linden012a2112020-06-23 22:39:03 +000010298 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010299
10300 error = generic_listxattr(dentry, list, size);
10301 if (error < 0)
10302 return error;
10303 if (list) {
10304 list += error;
10305 size -= error;
10306 }
10307
10308 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10309 if (error2 < 0)
10310 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010311
10312 if (list) {
10313 list += error2;
10314 size -= error2;
10315 }
10316
10317 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10318 if (error3 < 0)
10319 return error3;
10320
10321 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010322}
10323
Trond Myklebust17f26b12013-08-21 15:48:42 -040010324static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010325 .create = nfs_create,
10326 .lookup = nfs_lookup,
10327 .atomic_open = nfs_atomic_open,
10328 .link = nfs_link,
10329 .unlink = nfs_unlink,
10330 .symlink = nfs_symlink,
10331 .mkdir = nfs_mkdir,
10332 .rmdir = nfs_rmdir,
10333 .mknod = nfs_mknod,
10334 .rename = nfs_rename,
10335 .permission = nfs_permission,
10336 .getattr = nfs_getattr,
10337 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010338 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010339};
10340
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010341static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010342 .permission = nfs_permission,
10343 .getattr = nfs_getattr,
10344 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010345 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010346};
10347
David Howells509de812006-08-22 20:06:11 -040010348const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010349 .version = 4, /* protocol version */
10350 .dentry_ops = &nfs4_dentry_operations,
10351 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010352 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010353 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010354 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010355 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010356 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010357 .getattr = nfs4_proc_getattr,
10358 .setattr = nfs4_proc_setattr,
10359 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010360 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010361 .access = nfs4_proc_access,
10362 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010363 .create = nfs4_proc_create,
10364 .remove = nfs4_proc_remove,
10365 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010366 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010367 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010368 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010369 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010370 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010371 .link = nfs4_proc_link,
10372 .symlink = nfs4_proc_symlink,
10373 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010374 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010375 .readdir = nfs4_proc_readdir,
10376 .mknod = nfs4_proc_mknod,
10377 .statfs = nfs4_proc_statfs,
10378 .fsinfo = nfs4_proc_fsinfo,
10379 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010380 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010381 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010382 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010383 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010384 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010385 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010386 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010387 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010388 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010389 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010390 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010391 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010392 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010393 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010394 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010395 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010396 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010397 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010398 .create_server = nfs4_create_server,
10399 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010400};
10401
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010402static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010403 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010404 .list = nfs4_xattr_list_nfs4_acl,
10405 .get = nfs4_xattr_get_nfs4_acl,
10406 .set = nfs4_xattr_set_nfs4_acl,
10407};
10408
Frank van der Linden012a2112020-06-23 22:39:03 +000010409#ifdef CONFIG_NFS_V4_2
10410static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10411 .prefix = XATTR_USER_PREFIX,
10412 .get = nfs4_xattr_get_nfs4_user,
10413 .set = nfs4_xattr_set_nfs4_user,
10414};
10415#endif
10416
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010417const struct xattr_handler *nfs4_xattr_handlers[] = {
10418 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010419#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10420 &nfs4_xattr_nfs4_label_handler,
10421#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010422#ifdef CONFIG_NFS_V4_2
10423 &nfs4_xattr_nfs4_user_handler,
10424#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010425 NULL
10426};
10427
Linus Torvalds1da177e2005-04-16 15:20:36 -070010428/*
10429 * Local variables:
10430 * c-basic-offset: 8
10431 * End:
10432 */