blob: 512afb1c786773761691ed6820b0f2268113d4e2 [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"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust30846df2018-04-07 13:44:28 -040074#define NFS4_BITMASK_SZ 3
75
Trond Myklebust2066fe82006-09-15 08:30:46 -040076#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define NFS4_POLL_RETRY_MAX (15*HZ)
78
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020079/* file attributes which can be mapped to nfs attributes */
80#define NFS4_VALID_ATTRS (ATTR_MODE \
81 | ATTR_UID \
82 | ATTR_GID \
83 | ATTR_SIZE \
84 | ATTR_ATIME \
85 | ATTR_MTIME \
86 | ATTR_CTIME \
87 | ATTR_ATIME_SET \
88 | ATTR_MTIME_SET)
89
Trond Myklebustcdd4e682006-01-03 09:55:12 +010090struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080091static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050093static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040094static 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 +110095static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040096 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110097 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040098 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400100static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100101 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400102 struct nfs4_slot *slot,
103 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400104static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100105 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400106static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100107 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400108#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400109
110#ifdef CONFIG_NFS_V4_SECURITY_LABEL
111static inline struct nfs4_label *
112nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
113 struct iattr *sattr, struct nfs4_label *label)
114{
115 int err;
116
117 if (label == NULL)
118 return NULL;
119
120 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
121 return NULL;
122
David Quigleyaa9c2662013-05-22 12:50:44 -0400123 err = security_dentry_init_security(dentry, sattr->ia_mode,
124 &dentry->d_name, (void **)&label->label, &label->len);
125 if (err == 0)
126 return label;
127
128 return NULL;
129}
130static inline void
131nfs4_label_release_security(struct nfs4_label *label)
132{
133 if (label)
134 security_release_secctx(label->label, label->len);
135}
136static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
137{
138 if (label)
139 return server->attr_bitmask;
140
141 return server->attr_bitmask_nl;
142}
143#else
144static inline struct nfs4_label *
145nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
146 struct iattr *sattr, struct nfs4_label *l)
147{ return NULL; }
148static inline void
149nfs4_label_release_security(struct nfs4_label *label)
150{ return; }
151static inline u32 *
152nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
153{ return server->attr_bitmask; }
154#endif
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500157static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Trond Myklebust52567b02009-10-23 14:46:42 -0400159 if (err >= -1000)
160 return err;
161 switch (err) {
162 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500163 case -NFS4ERR_LAYOUTTRYLATER:
164 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000166 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400167 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000168 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800169 case -NFS4ERR_BADOWNER:
170 case -NFS4ERR_BADNAME:
171 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400172 case -NFS4ERR_SHARE_DENIED:
173 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400174 case -NFS4ERR_MINOR_VERS_MISMATCH:
175 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400176 case -NFS4ERR_FILE_OPEN:
177 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700180 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400183 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/*
187 * This is our standard bitmap for GETATTR requests.
188 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400189const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 FATTR4_WORD0_TYPE
191 | FATTR4_WORD0_CHANGE
192 | FATTR4_WORD0_SIZE
193 | FATTR4_WORD0_FSID
194 | FATTR4_WORD0_FILEID,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400205#ifdef CONFIG_NFS_V4_SECURITY_LABEL
206 FATTR4_WORD2_SECURITY_LABEL
207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Trond Myklebust1549210f2012-06-05 09:16:47 -0400210static const u32 nfs4_pnfs_open_bitmap[3] = {
211 FATTR4_WORD0_TYPE
212 | FATTR4_WORD0_CHANGE
213 | FATTR4_WORD0_SIZE
214 | FATTR4_WORD0_FSID
215 | FATTR4_WORD0_FILEID,
216 FATTR4_WORD1_MODE
217 | FATTR4_WORD1_NUMLINKS
218 | FATTR4_WORD1_OWNER
219 | FATTR4_WORD1_OWNER_GROUP
220 | FATTR4_WORD1_RAWDEV
221 | FATTR4_WORD1_SPACE_USED
222 | FATTR4_WORD1_TIME_ACCESS
223 | FATTR4_WORD1_TIME_METADATA
224 | FATTR4_WORD1_TIME_MODIFY,
225 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500226#ifdef CONFIG_NFS_V4_SECURITY_LABEL
227 | FATTR4_WORD2_SECURITY_LABEL
228#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400229};
230
Andy Adamsone23008e2012-10-02 21:07:32 -0400231static const u32 nfs4_open_noattr_bitmap[3] = {
232 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400233 | FATTR4_WORD0_FILEID,
234};
235
David Quigleya09df2c2013-05-22 12:50:41 -0400236const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 FATTR4_WORD0_FILES_AVAIL
238 | FATTR4_WORD0_FILES_FREE
239 | FATTR4_WORD0_FILES_TOTAL,
240 FATTR4_WORD1_SPACE_AVAIL
241 | FATTR4_WORD1_SPACE_FREE
242 | FATTR4_WORD1_SPACE_TOTAL
243};
244
David Quigleya09df2c2013-05-22 12:50:41 -0400245const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 FATTR4_WORD0_MAXLINK
247 | FATTR4_WORD0_MAXNAME,
248 0
249};
250
Fred Isamandae100c2011-07-30 20:52:37 -0400251const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 | FATTR4_WORD0_MAXREAD
253 | FATTR4_WORD0_MAXWRITE
254 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700255 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400256 | FATTR4_WORD1_FS_LAYOUT_TYPES,
257 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800258 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
David Quigleya09df2c2013-05-22 12:50:41 -0400261const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500262 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400263 | FATTR4_WORD0_SIZE
264 | FATTR4_WORD0_FSID
265 | FATTR4_WORD0_FILEID
266 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500267 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Trond Myklebust30846df2018-04-07 13:44:28 -0400277static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
278 struct inode *inode)
279{
280 unsigned long cache_validity;
281
282 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
283 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
284 return;
285
286 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
287 if (!(cache_validity & NFS_INO_REVAL_FORCED))
288 cache_validity &= ~(NFS_INO_INVALID_CHANGE
289 | NFS_INO_INVALID_SIZE);
290
291 if (!(cache_validity & NFS_INO_INVALID_SIZE))
292 dst[0] &= ~FATTR4_WORD0_SIZE;
293
294 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
295 dst[0] &= ~FATTR4_WORD0_CHANGE;
296}
297
298static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
299 const __u32 *src, struct inode *inode)
300{
301 nfs4_bitmap_copy_adjust(dst, src, inode);
302}
303
Al Virobc4785c2006-10-19 23:28:51 -0700304static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct nfs4_readdir_arg *readdir)
306{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400307 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700308 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000311 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
313 return;
314 }
315
316 readdir->cookie = 0;
317 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
318 if (cookie == 2)
319 return;
320
321 /*
322 * NFSv4 servers do not return entries for '.' and '..'
323 * Therefore, we fake these entries here. We let '.'
324 * have cookie 0 and '..' have cookie 1. Note that
325 * when talking to the server, we always send cookie 0
326 * instead of 1 or 2.
327 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (cookie == 0) {
331 *p++ = xdr_one; /* next */
332 *p++ = xdr_zero; /* cookie, first word */
333 *p++ = xdr_one; /* cookie, second word */
334 *p++ = xdr_one; /* entry len */
335 memcpy(p, ".\0\0\0", 4); /* entry */
336 p++;
337 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400338 *p++ = htonl(attrs); /* bitmap */
339 *p++ = htonl(12); /* attribute buffer length */
340 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000341 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
344 *p++ = xdr_one; /* next */
345 *p++ = xdr_zero; /* cookie, first word */
346 *p++ = xdr_two; /* cookie, second word */
347 *p++ = xdr_two; /* entry len */
348 memcpy(p, "..\0\0", 4); /* entry */
349 p++;
350 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400351 *p++ = htonl(attrs); /* bitmap */
352 *p++ = htonl(12); /* attribute buffer length */
353 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000354 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 readdir->pgbase = (char *)p - (char *)start;
357 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800358 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Trond Myklebust26d36302016-09-22 13:39:05 -0400361static void nfs4_test_and_free_stateid(struct nfs_server *server,
362 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100363 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400364{
365 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
366
367 ops->test_and_free_expired(server, stateid, cred);
368}
369
370static void __nfs4_free_revoked_stateid(struct nfs_server *server,
371 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100372 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400373{
374 stateid->type = NFS4_REVOKED_STATEID_TYPE;
375 nfs4_test_and_free_stateid(server, stateid, cred);
376}
377
378static void nfs4_free_revoked_stateid(struct nfs_server *server,
379 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100380 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400381{
382 nfs4_stateid tmp;
383
384 nfs4_stateid_copy(&tmp, stateid);
385 __nfs4_free_revoked_stateid(server, &tmp, cred);
386}
387
NeilBrown8478eaa2014-09-18 16:09:27 +1000388static long nfs4_update_delay(long *timeout)
389{
390 long ret;
391 if (!timeout)
392 return NFS4_POLL_RETRY_MAX;
393 if (*timeout <= 0)
394 *timeout = NFS4_POLL_RETRY_MIN;
395 if (*timeout > NFS4_POLL_RETRY_MAX)
396 *timeout = NFS4_POLL_RETRY_MAX;
397 ret = *timeout;
398 *timeout <<= 1;
399 return ret;
400}
401
Trond Myklebust0688e642019-04-07 13:59:09 -0400402static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500403{
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 might_sleep();
405
NeilBrown8478eaa2014-09-18 16:09:27 +1000406 freezable_schedule_timeout_killable_unsafe(
407 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400408 if (!__fatal_signal_pending(current))
409 return 0;
410 return -EINTR;
411}
412
413static int nfs4_delay_interruptible(long *timeout)
414{
415 might_sleep();
416
417 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
418 if (!signal_pending(current))
419 return 0;
420 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
421}
422
423static int nfs4_delay(long *timeout, bool interruptible)
424{
425 if (interruptible)
426 return nfs4_delay_interruptible(timeout);
427 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500428}
429
Trond Myklebust50c80002019-07-11 19:02:18 -0400430static const nfs4_stateid *
431nfs4_recoverable_stateid(const nfs4_stateid *stateid)
432{
433 if (!stateid)
434 return NULL;
435 switch (stateid->type) {
436 case NFS4_OPEN_STATEID_TYPE:
437 case NFS4_LOCK_STATEID_TYPE:
438 case NFS4_DELEGATION_STATEID_TYPE:
439 return stateid;
440 default:
441 break;
442 }
443 return NULL;
444}
445
Trond Myklebust65de8722008-12-23 15:21:44 -0500446/* This is the error handling routine for processes that are allowed
447 * to sleep.
448 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400449static int nfs4_do_handle_exception(struct nfs_server *server,
450 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500451{
452 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500453 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400454 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500455 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500456 int ret = errorcode;
457
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400458 exception->delay = 0;
459 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500460 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400461
Trond Myklebust50c80002019-07-11 19:02:18 -0400462 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400463 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400464 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400465
Trond Myklebust65de8722008-12-23 15:21:44 -0500466 switch(errorcode) {
467 case 0:
468 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400469 case -NFS4ERR_BADHANDLE:
470 case -ESTALE:
471 if (inode != NULL && S_ISREG(inode->i_mode))
472 pnfs_destroy_layout(NFS_I(inode));
473 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400474 case -NFS4ERR_DELEG_REVOKED:
475 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400476 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400477 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400478 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400479 if (inode != NULL && stateid != NULL) {
480 nfs_inode_find_state_and_recover(inode,
481 stateid);
482 goto wait_on_recovery;
483 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500484 /* Fall through */
Trond Myklebust272289a2016-09-22 13:39:15 -0400485 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400486 if (inode) {
487 int err;
488
489 err = nfs_async_inode_return_delegation(inode,
490 stateid);
491 if (err == 0)
492 goto wait_on_recovery;
493 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
494 exception->retry = 1;
495 break;
496 }
497 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500498 if (state == NULL)
499 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400500 ret = nfs4_schedule_stateid_recovery(server, state);
501 if (ret < 0)
502 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500503 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500504 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500505 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500506 nfs4_schedule_lease_recovery(clp);
507 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400508 case -NFS4ERR_MOVED:
509 ret = nfs4_schedule_migration_recovery(server);
510 if (ret < 0)
511 break;
512 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400513 case -NFS4ERR_LEASE_MOVED:
514 nfs4_schedule_lease_moved_recovery(clp);
515 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500516#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400517 case -NFS4ERR_BADSESSION:
518 case -NFS4ERR_BADSLOT:
519 case -NFS4ERR_BAD_HIGH_SLOT:
520 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
521 case -NFS4ERR_DEADSESSION:
522 case -NFS4ERR_SEQ_FALSE_RETRY:
523 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100524 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400525 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500526#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500527 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500528 if (exception->timeout > HZ) {
529 /* We have retried a decent amount, time to
530 * fail
531 */
532 ret = -EBUSY;
533 break;
534 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500535 /* Fall through */
Trond Myklebust65de8722008-12-23 15:21:44 -0500536 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400537 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500538 /* Fall through */
Trond Myklebust2598ed32015-09-20 16:10:18 -0400539 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400540 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400541 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400542 exception->delay = 1;
543 return 0;
544
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400545 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500546 case -NFS4ERR_OLD_STATEID:
547 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800548 break;
549 case -NFS4ERR_BADOWNER:
550 /* The following works around a Linux server bug! */
551 case -NFS4ERR_BADNAME:
552 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
553 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
554 exception->retry = 1;
555 printk(KERN_WARNING "NFS: v4 server %s "
556 "does not accept raw "
557 "uid/gids. "
558 "Reenabling the idmapper.\n",
559 server->nfs_client->cl_hostname);
560 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500561 }
562 /* We failed to handle the error */
563 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500564wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400565 exception->recovering = 1;
566 return 0;
567}
568
569/* This is the error handling routine for processes that are allowed
570 * to sleep.
571 */
572int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
573{
574 struct nfs_client *clp = server->nfs_client;
575 int ret;
576
577 ret = nfs4_do_handle_exception(server, errorcode, exception);
578 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400579 ret = nfs4_delay(&exception->timeout,
580 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400581 goto out_retry;
582 }
583 if (exception->recovering) {
584 ret = nfs4_wait_clnt_recover(clp);
585 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
586 return -EIO;
587 goto out_retry;
588 }
589 return ret;
590out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500591 if (ret == 0)
592 exception->retry = 1;
593 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500594}
595
Trond Myklebust037fc982015-09-20 15:51:00 -0400596static int
597nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
598 int errorcode, struct nfs4_exception *exception)
599{
600 struct nfs_client *clp = server->nfs_client;
601 int ret;
602
603 ret = nfs4_do_handle_exception(server, errorcode, exception);
604 if (exception->delay) {
605 rpc_delay(task, nfs4_update_delay(&exception->timeout));
606 goto out_retry;
607 }
608 if (exception->recovering) {
609 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
610 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
611 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
612 goto out_retry;
613 }
614 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
615 ret = -EIO;
616 return ret;
617out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500618 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400619 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500620 /*
621 * For NFS4ERR_MOVED, the client transport will need to
622 * be recomputed after migration recovery has completed.
623 */
624 if (errorcode == -NFS4ERR_MOVED)
625 rpc_task_release_transport(task);
626 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400627 return ret;
628}
629
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400630int
Trond Myklebust037fc982015-09-20 15:51:00 -0400631nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
632 struct nfs4_state *state, long *timeout)
633{
634 struct nfs4_exception exception = {
635 .state = state,
636 };
637
638 if (task->tk_status >= 0)
639 return 0;
640 if (timeout)
641 exception.timeout = *timeout;
642 task->tk_status = nfs4_async_handle_exception(task, server,
643 task->tk_status,
644 &exception);
645 if (exception.delay && timeout)
646 *timeout = exception.timeout;
647 if (exception.retry)
648 return -EAGAIN;
649 return 0;
650}
651
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400652/*
653 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
654 * or 'false' otherwise.
655 */
656static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
657{
658 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500659 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400660}
Trond Myklebust65de8722008-12-23 15:21:44 -0500661
Trond Myklebust452e9352010-07-31 14:29:06 -0400662static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 spin_lock(&clp->cl_lock);
665 if (time_before(clp->cl_last_renewal,timestamp))
666 clp->cl_last_renewal = timestamp;
667 spin_unlock(&clp->cl_lock);
668}
669
Trond Myklebust452e9352010-07-31 14:29:06 -0400670static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
671{
Trond Myklebustbe824162015-07-05 14:50:46 -0400672 struct nfs_client *clp = server->nfs_client;
673
674 if (!nfs4_has_session(clp))
675 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400676}
677
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400678struct nfs4_call_sync_data {
679 const struct nfs_server *seq_server;
680 struct nfs4_sequence_args *seq_args;
681 struct nfs4_sequence_res *seq_res;
682};
683
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800684void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400685 struct nfs4_sequence_res *res, int cache_reply,
686 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400687{
688 args->sa_slot = NULL;
689 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400690 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400691
692 res->sr_slot = NULL;
693}
694
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400695static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400696{
697 struct nfs4_slot *slot = res->sr_slot;
698 struct nfs4_slot_table *tbl;
699
Chuck Lever3bd23842013-08-09 12:49:19 -0400700 tbl = slot->table;
701 spin_lock(&tbl->slot_tbl_lock);
702 if (!nfs41_wake_and_assign_slot(tbl, slot))
703 nfs4_free_slot(tbl, slot);
704 spin_unlock(&tbl->slot_tbl_lock);
705
706 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400707}
708
709static int nfs40_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
711{
712 if (res->sr_slot != NULL)
713 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400714 return 1;
715}
716
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400717#if defined(CONFIG_NFS_V4_1)
718
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400719static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400720{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500721 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400722 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500723 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400724
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400725 if (!slot)
726 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500727 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500728 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500729
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400730 /* Bump the slot sequence number */
731 if (slot->seq_done)
732 slot->seq_nr++;
733 slot->seq_done = 0;
734
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500735 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500736 /* Be nice to the server: try to ensure that the last transmitted
737 * value for highest_user_slotid <= target_highest_slotid
738 */
739 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
740 send_new_highest_used_slotid = true;
741
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500742 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500743 send_new_highest_used_slotid = false;
744 goto out_unlock;
745 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500746 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500747
748 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
749 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500750out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500751 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500752 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400753 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400754 if (waitqueue_active(&tbl->slot_waitq))
755 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400756}
757
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400758static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
759{
760 nfs41_release_slot(res->sr_slot);
761 res->sr_slot = NULL;
762}
763
Trond Myklebust3453d572018-06-20 17:53:34 -0400764static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
765 u32 seqnr)
766{
767 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
768 slot->seq_nr_highest_sent = seqnr;
769}
770static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
771 u32 seqnr)
772{
773 slot->seq_nr_highest_sent = seqnr;
774 slot->seq_nr_last_acked = seqnr;
775}
776
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400777static int nfs41_sequence_process(struct rpc_task *task,
778 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400779{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500780 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500781 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400782 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100783 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500784 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400785
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500786 if (slot == NULL)
787 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400788 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500789 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400790 goto out;
791
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500792 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500793
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400794 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100795
796 status = res->sr_status;
797 if (task->tk_status == -NFS4ERR_DEADSESSION)
798 status = -NFS4ERR_DEADSESSION;
799
Andy Adamson691daf32009-12-04 15:55:39 -0500800 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100801 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400802 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400803 /* Mark this sequence number as having been acked */
804 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400805 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400806 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500807 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500808 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500809 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400810 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
811 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500812 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400813 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500814 case 1:
815 /*
816 * sr_status remains 1 if an RPC level error occurred.
817 * The server may or may not have processed the sequence
818 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500819 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400820 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
821 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500822 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 case -NFS4ERR_DELAY:
824 /* The server detected a resend of the RPC call and
825 * returned NFS4ERR_DELAY as per Section 2.10.6.2
826 * of RFC5661.
827 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500828 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400829 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500830 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500831 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400832 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400833 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400834 case -NFS4ERR_RETRY_UNCACHED_REP:
835 case -NFS4ERR_SEQ_FALSE_RETRY:
836 /*
837 * The server thinks we tried to replay a request.
838 * Retry the call after bumping the sequence ID.
839 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400840 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400841 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500842 case -NFS4ERR_BADSLOT:
843 /*
844 * The slot id we used was probably retired. Try again
845 * using a different slot id.
846 */
Trond Myklebust99589102018-06-09 12:50:50 -0400847 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400848 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500849 goto retry_nowait;
850 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400851 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500852 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400853 * Were one or more calls using this slot interrupted?
854 * If the server never received the request, then our
855 * transmitted slot sequence number may be too high.
Trond Myklebustac20d162012-12-15 15:36:07 -0500856 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400857 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
858 slot->seq_nr--;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500859 goto retry_nowait;
860 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400861 /*
862 * RFC5661:
863 * A retry might be sent while the original request is
864 * still in progress on the replier. The replier SHOULD
865 * deal with the issue by returning NFS4ERR_DELAY as the
866 * reply to SEQUENCE or CB_SEQUENCE operation, but
867 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
868 *
869 * Restart the search after a delay.
870 */
871 slot->seq_nr = slot->seq_nr_highest_sent;
872 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100873 case -NFS4ERR_BADSESSION:
874 case -NFS4ERR_DEADSESSION:
875 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
876 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400877 default:
878 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400879 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400880 }
881out:
882 /* The session may be reset by one of the error handlers. */
883 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500884out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500885 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400886session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100887 nfs4_schedule_session_recovery(session, status);
888 dprintk("%s ERROR: %d Reset session\n", __func__, status);
889 nfs41_sequence_free_slot(res);
890 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400891retry_new_seq:
892 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500893retry_nowait:
894 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400895 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500896 task->tk_status = 0;
897 ret = 0;
898 }
899 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400900out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400901 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400902 goto out;
903 rpc_delay(task, NFS4_POLL_RETRY_MAX);
904 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400905}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400906
907int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
908{
909 if (!nfs41_sequence_process(task, res))
910 return 0;
911 if (res->sr_slot != NULL)
912 nfs41_sequence_free_slot(res);
913 return 1;
914
915}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500916EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400917
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400918static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
919{
920 if (res->sr_slot == NULL)
921 return 1;
922 if (res->sr_slot->table->session != NULL)
923 return nfs41_sequence_process(task, res);
924 return nfs40_sequence_done(task, res);
925}
926
927static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
928{
929 if (res->sr_slot != NULL) {
930 if (res->sr_slot->table->session != NULL)
931 nfs41_sequence_free_slot(res);
932 else
933 nfs40_sequence_free_slot(res);
934 }
935}
936
Peng Tao2c4b1312014-06-11 05:24:16 +0800937int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400938{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500939 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400940 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400941 if (!res->sr_slot->table->session)
942 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400943 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400944}
Peng Tao2c4b1312014-06-11 05:24:16 +0800945EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400946
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
948{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400949 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950
Trond Myklebust035168ab2010-06-16 09:52:26 -0400951 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
952
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500953 nfs4_setup_sequence(data->seq_server->nfs_client,
954 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955}
956
Andy Adamson69ab40c2009-04-01 09:22:19 -0400957static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
958{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400959 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400960
Trond Myklebust14516c32010-07-31 14:29:06 -0400961 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400962}
963
Trond Myklebust17280172012-03-11 13:11:00 -0400964static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400965 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400966 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400967};
968
Chuck Lever3bd23842013-08-09 12:49:19 -0400969#else /* !CONFIG_NFS_V4_1 */
970
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400971static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
972{
973 return nfs40_sequence_done(task, res);
974}
975
976static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
977{
978 if (res->sr_slot != NULL)
979 nfs40_sequence_free_slot(res);
980}
981
Peng Tao2c4b1312014-06-11 05:24:16 +0800982int nfs4_sequence_done(struct rpc_task *task,
983 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400984{
Chuck Lever3bd23842013-08-09 12:49:19 -0400985 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400986}
Peng Tao2c4b1312014-06-11 05:24:16 +0800987EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400988
989#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400990
Trond Myklebustc1dffe02019-03-01 12:13:34 -0500991static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
992{
993 res->sr_timestamp = jiffies;
994 res->sr_status_flags = 0;
995 res->sr_status = 1;
996}
997
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400998static
999void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1000 struct nfs4_sequence_res *res,
1001 struct nfs4_slot *slot)
1002{
1003 if (!slot)
1004 return;
1005 slot->privileged = args->sa_privileged ? 1 : 0;
1006 args->sa_slot = slot;
1007
1008 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001009}
1010
1011int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001012 struct nfs4_sequence_args *args,
1013 struct nfs4_sequence_res *res,
1014 struct rpc_task *task)
1015{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001016 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001017 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001018 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001019
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001020 /* slot already allocated? */
1021 if (res->sr_slot != NULL)
1022 goto out_start;
1023
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001024 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001025 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001026
Trond Myklebust3453d572018-06-20 17:53:34 -04001027 spin_lock(&tbl->slot_tbl_lock);
1028 /* The state manager will wait until the slot table is empty */
1029 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1030 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001031
Trond Myklebust3453d572018-06-20 17:53:34 -04001032 slot = nfs4_alloc_slot(tbl);
1033 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001034 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001035 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001036 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001037 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001038 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001039
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001040 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001041
Anna Schumakerad05cc02017-01-11 13:37:06 -05001042 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001043out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001044 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001045 rpc_call_start(task);
1046 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001047out_sleep_timeout:
1048 /* Try again in 1/4 second */
1049 if (args->sa_privileged)
1050 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1051 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1052 else
1053 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1054 NULL, jiffies + (HZ >> 2));
1055 spin_unlock(&tbl->slot_tbl_lock);
1056 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001057out_sleep:
1058 if (args->sa_privileged)
1059 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001060 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001061 else
1062 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1063 spin_unlock(&tbl->slot_tbl_lock);
1064 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001065}
1066EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1067
Chuck Lever9915ea72013-08-09 12:48:27 -04001068static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1069{
1070 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001071 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001072 data->seq_args, data->seq_res, task);
1073}
1074
1075static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1076{
1077 struct nfs4_call_sync_data *data = calldata;
1078 nfs4_sequence_done(task, data->seq_res);
1079}
1080
1081static const struct rpc_call_ops nfs40_call_sync_ops = {
1082 .rpc_call_prepare = nfs40_call_sync_prepare,
1083 .rpc_call_done = nfs40_call_sync_done,
1084};
1085
Anna Schumaker48c05852019-08-14 15:27:00 -04001086static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1087{
1088 int ret;
1089 struct rpc_task *task;
1090
1091 task = rpc_run_task(task_setup);
1092 if (IS_ERR(task))
1093 return PTR_ERR(task);
1094
1095 ret = task->tk_status;
1096 rpc_put_task(task);
1097 return ret;
1098}
1099
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001100static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1101 struct nfs_server *server,
1102 struct rpc_message *msg,
1103 struct nfs4_sequence_args *args,
1104 struct nfs4_sequence_res *res,
1105 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001106{
Chuck Lever9915ea72013-08-09 12:48:27 -04001107 struct nfs_client *clp = server->nfs_client;
1108 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001109 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001110 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001111 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112 };
1113 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001114 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001115 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001116 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001117 .callback_data = &data,
1118 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001119 };
1120
Anna Schumaker48c05852019-08-14 15:27:00 -04001121 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001122}
1123
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001124static int nfs4_call_sync_sequence(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{
1130 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1131}
1132
1133
Bryan Schumaker7c513052011-03-24 17:12:24 +00001134int nfs4_call_sync(struct rpc_clnt *clnt,
1135 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001136 struct rpc_message *msg,
1137 struct nfs4_sequence_args *args,
1138 struct nfs4_sequence_res *res,
1139 int cache_reply)
1140{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001141 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001142 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Trond Myklebust3c591172018-07-31 15:54:10 -04001145static void
1146nfs4_inc_nlink_locked(struct inode *inode)
1147{
1148 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1149 inc_nlink(inode);
1150}
1151
1152static void
1153nfs4_dec_nlink_locked(struct inode *inode)
1154{
1155 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1156 drop_nlink(inode);
1157}
1158
1159static void
1160update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001161 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 struct nfs_inode *nfsi = NFS_I(dir);
1164
Trond Myklebust16e14372018-03-20 16:53:31 -04001165 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1166 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001167 | NFS_INO_INVALID_DATA
1168 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001169 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001170 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1171 nfsi->attrtimeo_timestamp = jiffies;
1172 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001174 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001175 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1176 NFS_INO_INVALID_ACL;
1177 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001178 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001179 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001180 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001181 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001182 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001183}
1184
1185static void
1186update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001187 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001188{
1189 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001190 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 spin_unlock(&dir->i_lock);
1192}
1193
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001194struct nfs4_open_createattrs {
1195 struct nfs4_label *label;
1196 struct iattr *sattr;
1197 const __u32 verf[2];
1198};
1199
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001200static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1201 int err, struct nfs4_exception *exception)
1202{
1203 if (err != -EINVAL)
1204 return false;
1205 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1206 return false;
1207 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1208 exception->retry = 1;
1209 return true;
1210}
1211
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001212static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1213{
1214 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1215}
1216
1217static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1218{
1219 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1220
1221 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1222}
1223
Trond Myklebust6ae37332015-01-30 14:21:14 -05001224static u32
1225nfs4_map_atomic_open_share(struct nfs_server *server,
1226 fmode_t fmode, int openflags)
1227{
1228 u32 res = 0;
1229
1230 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1231 case FMODE_READ:
1232 res = NFS4_SHARE_ACCESS_READ;
1233 break;
1234 case FMODE_WRITE:
1235 res = NFS4_SHARE_ACCESS_WRITE;
1236 break;
1237 case FMODE_READ|FMODE_WRITE:
1238 res = NFS4_SHARE_ACCESS_BOTH;
1239 }
1240 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1241 goto out;
1242 /* Want no delegation if we're using O_DIRECT */
1243 if (openflags & O_DIRECT)
1244 res |= NFS4_SHARE_WANT_NO_DELEG;
1245out:
1246 return res;
1247}
1248
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001249static enum open_claim_type4
1250nfs4_map_atomic_open_claim(struct nfs_server *server,
1251 enum open_claim_type4 claim)
1252{
1253 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1254 return claim;
1255 switch (claim) {
1256 default:
1257 return claim;
1258 case NFS4_OPEN_CLAIM_FH:
1259 return NFS4_OPEN_CLAIM_NULL;
1260 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1261 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1262 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1263 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1264 }
1265}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268{
1269 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001270 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 p->o_res.seqid = p->o_arg.seqid;
1272 p->c_res.seqid = p->c_arg.seqid;
1273 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001274 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001275 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001276 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001277}
1278
Al Viro82a2c1b2011-06-22 18:30:55 -04001279static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001280 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001281 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001282 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001283 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001284{
Al Viro82a2c1b2011-06-22 18:30:55 -04001285 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001286 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001287 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001288 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001289 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290 struct nfs4_opendata *p;
1291
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001292 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001293 if (p == NULL)
1294 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001295
1296 p->f_label = nfs4_label_alloc(server, gfp_mask);
1297 if (IS_ERR(p->f_label))
1298 goto err_free_p;
1299
Kinglong Meea49c2692015-07-27 15:31:38 +08001300 p->a_label = nfs4_label_alloc(server, gfp_mask);
1301 if (IS_ERR(p->a_label))
1302 goto err_free_f;
1303
Trond Myklebust63f5f792015-01-23 19:19:25 -05001304 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1305 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001306 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001307 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001308 nfs_sb_active(dentry->d_sb);
1309 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001310 p->dir = parent;
1311 p->owner = sp;
1312 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001313 p->o_arg.open_flags = flags;
1314 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001315 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001316 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1317 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001318 if (flags & O_CREAT) {
1319 p->o_arg.umask = current_umask();
1320 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1321 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1322 p->o_arg.u.attrs = &p->attrs;
1323 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1324
1325 memcpy(p->o_arg.u.verifier.data, c->verf,
1326 sizeof(p->o_arg.u.verifier.data));
1327 }
1328 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001329 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1330 * will return permission denied for all bits until close */
1331 if (!(flags & O_EXCL)) {
1332 /* ask server to check for all possible rights as results
1333 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001334 switch (p->o_arg.claim) {
1335 default:
1336 break;
1337 case NFS4_OPEN_CLAIM_NULL:
1338 case NFS4_OPEN_CLAIM_FH:
1339 p->o_arg.access = NFS4_ACCESS_READ |
1340 NFS4_ACCESS_MODIFY |
1341 NFS4_ACCESS_EXTEND |
1342 NFS4_ACCESS_EXECUTE;
1343 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001344 }
David Howells7539bba2006-08-22 20:06:09 -04001345 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001346 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1347 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001348 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001349 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001350 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001351 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001352 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001353 case NFS4_OPEN_CLAIM_NULL:
1354 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1355 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1356 p->o_arg.fh = NFS_FH(dir);
1357 break;
1358 case NFS4_OPEN_CLAIM_PREVIOUS:
1359 case NFS4_OPEN_CLAIM_FH:
1360 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1361 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001362 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001363 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001364 p->c_arg.fh = &p->o_res.fh;
1365 p->c_arg.stateid = &p->o_res.stateid;
1366 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001367 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001368 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001369 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001370
1371err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001372 nfs4_label_free(p->a_label);
1373err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001374 nfs4_label_free(p->f_label);
1375err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001376 kfree(p);
1377err:
1378 dput(parent);
1379 return NULL;
1380}
1381
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001382static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001383{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001384 struct nfs4_opendata *p = container_of(kref,
1385 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001386 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001387
Fred Isaman30ae2412016-10-18 13:39:51 -04001388 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001389 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001390 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001391 if (p->state != NULL)
1392 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001393 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001394
Kinglong Meea49c2692015-07-27 15:31:38 +08001395 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001396 nfs4_label_free(p->f_label);
1397
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001398 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001399 dput(p->dentry);
1400 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001401 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001402 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001403 kfree(p);
1404}
1405
1406static void nfs4_opendata_put(struct nfs4_opendata *p)
1407{
1408 if (p != NULL)
1409 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001410}
1411
Trond Myklebust24311f82015-09-20 10:50:17 -04001412static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1413 fmode_t fmode)
1414{
1415 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1416 case FMODE_READ|FMODE_WRITE:
1417 return state->n_rdwr != 0;
1418 case FMODE_WRITE:
1419 return state->n_wronly != 0;
1420 case FMODE_READ:
1421 return state->n_rdonly != 0;
1422 }
1423 WARN_ON_ONCE(1);
1424 return false;
1425}
1426
Trond Myklebustbe189f72018-09-27 17:12:33 -04001427static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1428 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001429{
1430 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001431
Trond Myklebust536e43d2012-01-17 22:04:26 -05001432 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001433 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001434 switch (claim) {
1435 case NFS4_OPEN_CLAIM_NULL:
1436 case NFS4_OPEN_CLAIM_FH:
1437 goto out;
1438 default:
1439 break;
1440 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001442 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001443 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1444 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001445 break;
1446 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001447 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1448 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001449 break;
1450 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001451 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1452 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001453 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001454out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001455 return ret;
1456}
1457
Trond Myklebust2a606182015-08-19 22:30:00 -05001458static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1459 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001460{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001461 if (delegation == NULL)
1462 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001463 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001464 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001465 switch (claim) {
1466 case NFS4_OPEN_CLAIM_NULL:
1467 case NFS4_OPEN_CLAIM_FH:
1468 break;
1469 case NFS4_OPEN_CLAIM_PREVIOUS:
1470 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1471 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001472 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001473 default:
1474 return 0;
1475 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001476 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001477 return 1;
1478}
1479
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001480static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001481{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001482 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001483 case FMODE_WRITE:
1484 state->n_wronly++;
1485 break;
1486 case FMODE_READ:
1487 state->n_rdonly++;
1488 break;
1489 case FMODE_READ|FMODE_WRITE:
1490 state->n_rdwr++;
1491 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001492 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001493}
1494
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001495#ifdef CONFIG_NFS_V4_1
1496static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1497{
1498 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1499 return true;
1500 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1501 return true;
1502 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1503 return true;
1504 return false;
1505}
1506#endif /* CONFIG_NFS_V4_1 */
1507
Trond Myklebustc9399f22017-11-06 15:28:01 -05001508static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1509{
1510 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1511 wake_up_all(&state->waitq);
1512}
1513
1514static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1515 const nfs4_stateid *stateid)
1516{
1517 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1518 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1519
1520 if (stateid_seqid == state_seqid + 1U ||
1521 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1522 nfs_state_log_update_open_stateid(state);
1523 else
1524 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1525}
1526
Trond Myklebust4f14c192014-02-12 19:15:06 -05001527static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001528{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001529 struct nfs_client *clp = state->owner->so_server->nfs_client;
1530 bool need_recover = false;
1531
1532 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1533 need_recover = true;
1534 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1535 need_recover = true;
1536 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1537 need_recover = true;
1538 if (need_recover)
1539 nfs4_state_mark_reclaim_nograce(clp, state);
1540}
1541
Trond Myklebustc9399f22017-11-06 15:28:01 -05001542/*
1543 * Check for whether or not the caller may update the open stateid
1544 * to the value passed in by stateid.
1545 *
1546 * Note: This function relies heavily on the server implementing
1547 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1548 * correctly.
1549 * i.e. The stateid seqids have to be initialised to 1, and
1550 * are then incremented on every state transition.
1551 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001552static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001553 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001554{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001555 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1556 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1557 if (stateid->seqid == cpu_to_be32(1))
1558 nfs_state_log_update_open_stateid(state);
1559 else
1560 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001561 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001562 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001563
1564 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1565 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001566 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001567 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001568 return false;
1569}
1570
Trond Myklebustf95549c2015-01-23 18:06:09 -05001571static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1572{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001573 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1574 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001575 if (state->n_wronly)
1576 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1577 if (state->n_rdonly)
1578 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1579 if (state->n_rdwr)
1580 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001581 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001582}
1583
Trond Myklebust226056c2014-02-11 10:41:07 -05001584static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1585 nfs4_stateid *stateid, fmode_t fmode)
1586{
1587 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1588 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1589 case FMODE_WRITE:
1590 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1591 break;
1592 case FMODE_READ:
1593 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1594 break;
1595 case 0:
1596 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1597 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1598 clear_bit(NFS_OPEN_STATE, &state->flags);
1599 }
1600 if (stateid == NULL)
1601 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001602 /* Handle OPEN+OPEN_DOWNGRADE races */
1603 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1604 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001605 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001606 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001607 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001608 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001609 nfs4_stateid_copy(&state->stateid, stateid);
1610 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001611 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001612out:
1613 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001614}
1615
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001616static void nfs_clear_open_stateid(struct nfs4_state *state,
1617 nfs4_stateid *arg_stateid,
1618 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001619{
1620 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001621 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1622 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1623 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001624 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001625 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1626 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001627}
1628
Trond Myklebust1393d962016-09-22 13:39:13 -04001629static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001630 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001631 __must_hold(&state->owner->so_lock)
1632 __must_hold(&state->seqlock)
1633 __must_hold(RCU)
1634
Trond Myklebust003707c2007-07-05 18:07:55 -04001635{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001636 DEFINE_WAIT(wait);
1637 int status = 0;
1638 for (;;) {
1639
1640 if (!nfs_need_update_open_stateid(state, stateid))
1641 return;
1642 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001643 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001644 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001645 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001646 /* Rely on seqids for serialisation with NFSv4.0 */
1647 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1648 break;
1649
1650 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1651 /*
1652 * Ensure we process the state changes in the same order
1653 * in which the server processed them by delaying the
1654 * update of the stateid until we are in sequence.
1655 */
1656 write_sequnlock(&state->seqlock);
1657 spin_unlock(&state->owner->so_lock);
1658 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001659 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001660 if (!signal_pending(current)) {
1661 if (schedule_timeout(5*HZ) == 0)
1662 status = -EAGAIN;
1663 else
1664 status = 0;
1665 } else
1666 status = -EINTR;
1667 finish_wait(&state->waitq, &wait);
1668 rcu_read_lock();
1669 spin_lock(&state->owner->so_lock);
1670 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001671 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001672
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001673 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1674 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001675 nfs4_stateid_copy(freeme, &state->open_stateid);
1676 nfs_test_and_clear_all_open_stateid(state);
1677 }
1678
Trond Myklebuste999e802014-02-10 18:20:47 -05001679 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1680 nfs4_stateid_copy(&state->stateid, stateid);
1681 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001682 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001683 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001684}
1685
Trond Myklebustc9399f22017-11-06 15:28:01 -05001686static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001687 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001688 fmode_t fmode,
1689 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001691 /*
1692 * Protect the call to nfs4_state_set_mode_locked and
1693 * serialise the stateid update
1694 */
1695 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001696 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1697 switch (fmode) {
1698 case FMODE_READ:
1699 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1700 break;
1701 case FMODE_WRITE:
1702 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1703 break;
1704 case FMODE_READ|FMODE_WRITE:
1705 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001706 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001707 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001708 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001709}
1710
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001711static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1712{
1713 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1714 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1715 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1716 clear_bit(NFS_OPEN_STATE, &state->flags);
1717}
1718
Trond Myklebustc9399f22017-11-06 15:28:01 -05001719static void nfs_state_set_delegation(struct nfs4_state *state,
1720 const nfs4_stateid *deleg_stateid,
1721 fmode_t fmode)
1722{
1723 /*
1724 * Protect the call to nfs4_state_set_mode_locked and
1725 * serialise the stateid update
1726 */
1727 write_seqlock(&state->seqlock);
1728 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1729 set_bit(NFS_DELEGATED_STATE, &state->flags);
1730 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001733static void nfs_state_clear_delegation(struct nfs4_state *state)
1734{
1735 write_seqlock(&state->seqlock);
1736 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1737 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1738 write_sequnlock(&state->seqlock);
1739}
1740
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001741int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001742 const nfs4_stateid *open_stateid,
1743 const nfs4_stateid *delegation,
1744 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001745{
Trond Myklebust1393d962016-09-22 13:39:13 -04001746 struct nfs_server *server = NFS_SERVER(state->inode);
1747 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001748 struct nfs_inode *nfsi = NFS_I(state->inode);
1749 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001750 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001751 int ret = 0;
1752
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001753 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001754
1755 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001756 spin_lock(&state->owner->so_lock);
1757 if (open_stateid != NULL) {
1758 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1759 ret = 1;
1760 }
1761
Trond Myklebust333ac782019-10-22 12:12:17 -04001762 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001763 if (deleg_cur == NULL)
1764 goto no_delegation;
1765
1766 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001767 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001768 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001769 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001770 goto no_delegation_unlock;
1771
1772 if (delegation == NULL)
1773 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001774 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001775 goto no_delegation_unlock;
1776
Trond Myklebustb7391f42008-12-23 15:21:52 -05001777 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001778 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001779 ret = 1;
1780no_delegation_unlock:
1781 spin_unlock(&deleg_cur->lock);
1782no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001783 if (ret)
1784 update_open_stateflags(state, fmode);
1785 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001786 rcu_read_unlock();
1787
Trond Myklebust4f14c192014-02-12 19:15:06 -05001788 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001789 nfs4_schedule_state_manager(clp);
1790 if (freeme.type != 0)
1791 nfs4_test_and_free_stateid(server, &freeme,
1792 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001793
1794 return ret;
1795}
1796
Trond Myklebust39071e62015-01-24 15:07:56 -05001797static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1798 const nfs4_stateid *stateid)
1799{
1800 struct nfs4_state *state = lsp->ls_state;
1801 bool ret = false;
1802
1803 spin_lock(&state->state_lock);
1804 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1805 goto out_noupdate;
1806 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1807 goto out_noupdate;
1808 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1809 ret = true;
1810out_noupdate:
1811 spin_unlock(&state->state_lock);
1812 return ret;
1813}
Trond Myklebust34310432008-12-23 15:21:38 -05001814
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001815static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001816{
1817 struct nfs_delegation *delegation;
1818
Trond Myklebustf5086242018-03-20 16:43:13 -04001819 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001820 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001821 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001822 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001823 rcu_read_unlock();
1824 return;
1825 }
1826 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001827 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001828}
1829
Trond Myklebust6ee41262007-07-08 14:11:36 -04001830static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001831{
1832 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001833 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001834 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001835 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001836 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001837 nfs4_stateid stateid;
1838 int ret = -EAGAIN;
1839
Trond Myklebustaac00a82007-07-05 19:02:21 -04001840 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001841 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001842 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001843 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001844 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001845 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001846 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001847 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001848 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001849 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001850 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001851 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001852 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001853 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001854 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001855 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001857 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001858 if (!opendata->is_recover) {
1859 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1860 if (ret != 0)
1861 goto out;
1862 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001863 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001864
1865 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001866 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001867 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001868 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001869out:
1870 return ERR_PTR(ret);
1871out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001872 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001873 return state;
1874}
1875
Andy Adamsone23008e2012-10-02 21:07:32 -04001876static void
1877nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1878{
1879 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1880 struct nfs_delegation *delegation;
1881 int delegation_flags = 0;
1882
1883 rcu_read_lock();
1884 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1885 if (delegation)
1886 delegation_flags = delegation->flags;
1887 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001888 switch (data->o_arg.claim) {
1889 default:
1890 break;
1891 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1892 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001893 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1894 "returning a delegation for "
1895 "OPEN(CLAIM_DELEGATE_CUR)\n",
1896 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001897 return;
1898 }
1899 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001900 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001901 data->owner->so_cred,
1902 data->o_res.delegation_type,
1903 &data->o_res.delegation,
1904 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001905 else
1906 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001907 data->owner->so_cred,
1908 data->o_res.delegation_type,
1909 &data->o_res.delegation,
1910 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001911
1912 if (data->o_res.do_recall)
1913 nfs_async_inode_return_delegation(state->inode,
1914 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001915}
1916
1917/*
1918 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1919 * and update the nfs4_state.
1920 */
1921static struct nfs4_state *
1922_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1923{
1924 struct inode *inode = data->state->inode;
1925 struct nfs4_state *state = data->state;
1926 int ret;
1927
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001928 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001929 if (data->rpc_status)
1930 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001931 /* cached opens have already been processed */
1932 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001933 }
1934
Andy Adamsone23008e2012-10-02 21:07:32 -04001935 ret = nfs_refresh_inode(inode, &data->f_attr);
1936 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001937 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001938
1939 if (data->o_res.delegation_type != 0)
1940 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001941update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001942 if (!update_open_stateid(state, &data->o_res.stateid,
1943 NULL, data->o_arg.fmode))
1944 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001945 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001946
1947 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001948}
1949
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001950static struct inode *
1951nfs4_opendata_get_inode(struct nfs4_opendata *data)
1952{
1953 struct inode *inode;
1954
1955 switch (data->o_arg.claim) {
1956 case NFS4_OPEN_CLAIM_NULL:
1957 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1958 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1959 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1960 return ERR_PTR(-EAGAIN);
1961 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1962 &data->f_attr, data->f_label);
1963 break;
1964 default:
1965 inode = d_inode(data->dentry);
1966 ihold(inode);
1967 nfs_refresh_inode(inode, &data->f_attr);
1968 }
1969 return inode;
1970}
1971
Andy Adamsone23008e2012-10-02 21:07:32 -04001972static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001973nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1974{
1975 struct nfs4_state *state;
1976 struct inode *inode;
1977
1978 inode = nfs4_opendata_get_inode(data);
1979 if (IS_ERR(inode))
1980 return ERR_CAST(inode);
1981 if (data->state != NULL && data->state->inode == inode) {
1982 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001983 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04001984 } else
1985 state = nfs4_get_open_state(inode, data->owner);
1986 iput(inode);
1987 if (state == NULL)
1988 state = ERR_PTR(-ENOMEM);
1989 return state;
1990}
1991
Andy Adamsone23008e2012-10-02 21:07:32 -04001992static struct nfs4_state *
1993_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001994{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001995 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001996
Trond Myklebustaac00a82007-07-05 19:02:21 -04001997 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001998 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001999 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002000 goto out;
2001 }
2002
Trond Myklebust75e8c482017-08-08 10:38:07 -04002003 state = nfs4_opendata_find_nfs4_state(data);
2004 if (IS_ERR(state))
2005 goto out;
2006
Andy Adamsone23008e2012-10-02 21:07:32 -04002007 if (data->o_res.delegation_type != 0)
2008 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002009 if (!update_open_stateid(state, &data->o_res.stateid,
2010 NULL, data->o_arg.fmode)) {
2011 nfs4_put_open_state(state);
2012 state = ERR_PTR(-EAGAIN);
2013 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002014out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002015 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002016 return state;
2017}
2018
Andy Adamsone23008e2012-10-02 21:07:32 -04002019static struct nfs4_state *
2020nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2021{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002022 struct nfs4_state *ret;
2023
Andy Adamsone23008e2012-10-02 21:07:32 -04002024 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002025 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2026 else
2027 ret = _nfs4_opendata_to_nfs4_state(data);
2028 nfs4_sequence_free_slot(&data->o_res.seq_res);
2029 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002030}
2031
Trond Myklebust0de43972018-09-02 15:57:01 -04002032static struct nfs_open_context *
2033nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002034{
2035 struct nfs_inode *nfsi = NFS_I(state->inode);
2036 struct nfs_open_context *ctx;
2037
Trond Myklebust0de43972018-09-02 15:57:01 -04002038 rcu_read_lock();
2039 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002040 if (ctx->state != state)
2041 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002042 if ((ctx->mode & mode) != mode)
2043 continue;
2044 if (!get_nfs_open_context(ctx))
2045 continue;
2046 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002047 return ctx;
2048 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002049 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002050 return ERR_PTR(-ENOENT);
2051}
2052
Trond Myklebust0de43972018-09-02 15:57:01 -04002053static struct nfs_open_context *
2054nfs4_state_find_open_context(struct nfs4_state *state)
2055{
2056 struct nfs_open_context *ctx;
2057
2058 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2059 if (!IS_ERR(ctx))
2060 return ctx;
2061 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2062 if (!IS_ERR(ctx))
2063 return ctx;
2064 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2065}
2066
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002067static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2068 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002069{
2070 struct nfs4_opendata *opendata;
2071
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002072 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002073 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002074 if (opendata == NULL)
2075 return ERR_PTR(-ENOMEM);
2076 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002077 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002078 return opendata;
2079}
2080
Trond Myklebust24311f82015-09-20 10:50:17 -04002081static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2082 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002083{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002084 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002085 int ret;
2086
Trond Myklebust24311f82015-09-20 10:50:17 -04002087 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002088 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002089 opendata->o_arg.open_flags = 0;
2090 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002091 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2092 NFS_SB(opendata->dentry->d_sb),
2093 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002094 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2095 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2096 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002097 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002098 if (ret != 0)
2099 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002100 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002101 if (IS_ERR(newstate))
2102 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002103 if (newstate != opendata->state)
2104 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002105 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002106 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002107}
2108
2109static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2110{
Trond Myklebust864472e2006-01-03 09:55:15 +01002111 int ret;
2112
2113 /* memory barrier prior to reading state->n_* */
2114 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002115 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2116 if (ret != 0)
2117 return ret;
2118 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2119 if (ret != 0)
2120 return ret;
2121 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2122 if (ret != 0)
2123 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002124 /*
2125 * We may have performed cached opens for all three recoveries.
2126 * Check if we need to update the current stateid.
2127 */
2128 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002129 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002130 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002131 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002132 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002133 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002134 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002135 return 0;
2136}
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138/*
2139 * OPEN_RECLAIM:
2140 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002142static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002144 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002145 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002146 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int status;
2148
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002149 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2150 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002151 if (IS_ERR(opendata))
2152 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002153 rcu_read_lock();
2154 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002155 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002156 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002157 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002158 opendata->o_arg.u.delegation_type = delegation_type;
2159 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002160 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return status;
2162}
2163
Trond Myklebust539cd032007-06-05 11:46:42 -04002164static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 struct nfs_server *server = NFS_SERVER(state->inode);
2167 struct nfs4_exception exception = { };
2168 int err;
2169 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002170 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002171 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002172 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2173 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002174 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002175 break;
2176 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 } while (exception.retry);
2178 return err;
2179}
2180
Trond Myklebust864472e2006-01-03 09:55:15 +01002181static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2182{
2183 struct nfs_open_context *ctx;
2184 int ret;
2185
2186 ctx = nfs4_state_find_open_context(state);
2187 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002188 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002189 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2190 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002191 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002192 put_nfs_open_context(ctx);
2193 return ret;
2194}
2195
NeilBrowndce26302017-12-13 09:57:09 +11002196static 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 -07002197{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002198 switch (err) {
2199 default:
2200 printk(KERN_ERR "NFS: %s: unhandled error "
2201 "%d.\n", __func__, err);
2202 case 0:
2203 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002204 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002205 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002206 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002207 break;
2208 case -NFS4ERR_BADSESSION:
2209 case -NFS4ERR_BADSLOT:
2210 case -NFS4ERR_BAD_HIGH_SLOT:
2211 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2212 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002213 return -EAGAIN;
2214 case -NFS4ERR_STALE_CLIENTID:
2215 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002216 /* Don't recall a delegation if it was lost */
2217 nfs4_schedule_lease_recovery(server->nfs_client);
2218 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002219 case -NFS4ERR_MOVED:
2220 nfs4_schedule_migration_recovery(server);
2221 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002222 case -NFS4ERR_LEASE_MOVED:
2223 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2224 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002225 case -NFS4ERR_DELEG_REVOKED:
2226 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002227 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002228 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002229 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002230 nfs_inode_find_state_and_recover(state->inode,
2231 stateid);
2232 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002233 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002234 case -NFS4ERR_DELAY:
2235 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002236 ssleep(1);
2237 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002238 case -ENOMEM:
2239 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002240 if (fl) {
2241 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2242 if (lsp)
2243 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2244 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002245 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return err;
2248}
2249
Trond Myklebust24311f82015-09-20 10:50:17 -04002250int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002251 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002252{
2253 struct nfs_server *server = NFS_SERVER(state->inode);
2254 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002255 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002256
2257 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2258 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2259 if (IS_ERR(opendata))
2260 return PTR_ERR(opendata);
2261 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002262 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002263 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2264 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002265 goto out;
2266 }
2267 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002268 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2269 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002270 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002271 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002272 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2273 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2274 if (err)
2275 goto out;
2276 }
2277 nfs_state_clear_delegation(state);
2278out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002279 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002280 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002281}
2282
Chuck Leverbe05c862013-08-09 12:49:47 -04002283static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2284{
2285 struct nfs4_opendata *data = calldata;
2286
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002287 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2288 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002289}
2290
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002291static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2292{
2293 struct nfs4_opendata *data = calldata;
2294
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002295 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002296
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002297 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002298 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002299 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002300 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002301 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002302 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002303 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002304}
2305
2306static void nfs4_open_confirm_release(void *calldata)
2307{
2308 struct nfs4_opendata *data = calldata;
2309 struct nfs4_state *state = NULL;
2310
2311 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002312 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002313 goto out_free;
2314 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002315 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002316 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002317 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002318 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002319 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002320out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002321 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002322}
2323
2324static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002325 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002326 .rpc_call_done = nfs4_open_confirm_done,
2327 .rpc_release = nfs4_open_confirm_release,
2328};
2329
2330/*
2331 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2332 */
2333static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2334{
David Howells2b0143b2015-03-17 22:25:59 +00002335 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002336 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002337 struct rpc_message msg = {
2338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2339 .rpc_argp = &data->c_arg,
2340 .rpc_resp = &data->c_res,
2341 .rpc_cred = data->owner->so_cred,
2342 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002343 struct rpc_task_setup task_setup_data = {
2344 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002345 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002346 .callback_ops = &nfs4_open_confirm_ops,
2347 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002348 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002349 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002350 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int status;
2352
Anna Schumakerfba83f32018-05-04 16:22:50 -04002353 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2354 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002355 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002356 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002357 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002358 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002359 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002360 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002361 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002362 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002363 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002364 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002365 smp_wmb();
2366 } else
2367 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002368 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return status;
2370}
2371
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002372static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002374 struct nfs4_opendata *data = calldata;
2375 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002376 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002377 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002378
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002379 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002380 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002381 /*
2382 * Check if we still need to send an OPEN call, or if we can use
2383 * a delegation instead.
2384 */
2385 if (data->state != NULL) {
2386 struct nfs_delegation *delegation;
2387
Trond Myklebustbe189f72018-09-27 17:12:33 -04002388 if (can_open_cached(data->state, data->o_arg.fmode,
2389 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002390 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002391 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002392 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002393 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002394 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002395 rcu_read_unlock();
2396 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002397 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002398 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002399 switch (claim) {
2400 default:
2401 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002402 case NFS4_OPEN_CLAIM_PREVIOUS:
2403 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2404 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002405 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002406 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002407 case NFS4_OPEN_CLAIM_FH:
2408 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002409 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002410 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002411 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002412 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002413 &data->o_res.seq_res,
2414 task) != 0)
2415 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002416
2417 /* Set the create mode (note dependency on the session type) */
2418 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2419 if (data->o_arg.open_flags & O_EXCL) {
2420 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2421 if (nfs4_has_persistent_session(clp))
2422 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2423 else if (clp->cl_mvops->minor_version > 0)
2424 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2425 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002426 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002427unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002428 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002429 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002430out_no_action:
2431 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002432out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002433 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002434}
2435
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002436static void nfs4_open_done(struct rpc_task *task, void *calldata)
2437{
2438 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002440 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002441
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002442 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002443 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002444
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002445 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002446 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2447 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002448 case S_IFREG:
2449 break;
2450 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002451 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002452 break;
2453 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002454 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002455 break;
2456 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002457 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002458 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002459 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002460 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002461 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2462 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002463 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002464 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002465}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002466
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002467static void nfs4_open_release(void *calldata)
2468{
2469 struct nfs4_opendata *data = calldata;
2470 struct nfs4_state *state = NULL;
2471
2472 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002473 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002474 goto out_free;
2475 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002476 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002477 goto out_free;
2478 /* In case we need an open_confirm, no cleanup! */
2479 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2480 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002481 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002482 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002483 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002484out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002485 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002486}
2487
2488static const struct rpc_call_ops nfs4_open_ops = {
2489 .rpc_call_prepare = nfs4_open_prepare,
2490 .rpc_call_done = nfs4_open_done,
2491 .rpc_release = nfs4_open_release,
2492};
2493
Fred Isaman3b65a302016-09-19 10:06:49 -04002494static int nfs4_run_open_task(struct nfs4_opendata *data,
2495 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002496{
David Howells2b0143b2015-03-17 22:25:59 +00002497 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002498 struct nfs_server *server = NFS_SERVER(dir);
2499 struct nfs_openargs *o_arg = &data->o_arg;
2500 struct nfs_openres *o_res = &data->o_res;
2501 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002502 struct rpc_message msg = {
2503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2504 .rpc_argp = o_arg,
2505 .rpc_resp = o_res,
2506 .rpc_cred = data->owner->so_cred,
2507 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002508 struct rpc_task_setup task_setup_data = {
2509 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002510 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002511 .callback_ops = &nfs4_open_ops,
2512 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002513 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002514 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002515 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002516 int status;
2517
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002518 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002519 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002520 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002521 data->cancelled = false;
2522 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002523 if (!ctx) {
2524 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002525 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002526 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002527 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002528 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002529 pnfs_lgopen_prepare(data, ctx);
2530 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002531 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002532 if (IS_ERR(task))
2533 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002534 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002535 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002536 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002537 smp_wmb();
2538 } else
2539 status = data->rpc_status;
2540 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002541
2542 return status;
2543}
2544
2545static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2546{
David Howells2b0143b2015-03-17 22:25:59 +00002547 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002548 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002549 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002550
Fred Isaman3b65a302016-09-19 10:06:49 -04002551 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002552 if (status != 0 || !data->rpc_done)
2553 return status;
2554
Trond Myklebust6926afd12012-01-07 13:22:46 -05002555 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2556
Anna Schumakerd7e98252017-01-11 16:13:29 -05002557 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002558 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002559
2560 return status;
2561}
2562
Trond Myklebustf3792d62014-07-10 08:54:32 -04002563/*
2564 * Additional permission checks in order to distinguish between an
2565 * open for read, and an open for execute. This works around the
2566 * fact that NFSv4 OPEN treats read and execute permissions as being
2567 * the same.
2568 * Note that in the non-execute case, we want to turn off permission
2569 * checking if we just created a new file (POSIX open() semantics).
2570 */
NeilBrowna52458b2018-12-03 11:30:31 +11002571static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002572 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002573 struct nfs4_state *state, fmode_t fmode,
2574 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002575{
2576 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002577 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002578
2579 /* access call failed or for some reason the server doesn't
2580 * support any access modes -- defer access call until later */
2581 if (opendata->o_res.access_supported == 0)
2582 return 0;
2583
2584 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002585 /*
2586 * Use openflags to check for exec, because fmode won't
2587 * always have FMODE_EXEC set when file open for exec.
2588 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002589 if (openflags & __FMODE_EXEC) {
2590 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002591 if (S_ISDIR(state->inode->i_mode))
2592 mask = NFS4_ACCESS_LOOKUP;
2593 else
2594 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002595 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002596 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002597
2598 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002599 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2600 nfs_access_add_cache(state->inode, &cache);
2601
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002602 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2603 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002604 return 0;
2605
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002606 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002607}
2608
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002609/*
2610 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2611 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002612static int _nfs4_proc_open(struct nfs4_opendata *data,
2613 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002614{
David Howells2b0143b2015-03-17 22:25:59 +00002615 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002616 struct nfs_server *server = NFS_SERVER(dir);
2617 struct nfs_openargs *o_arg = &data->o_arg;
2618 struct nfs_openres *o_res = &data->o_res;
2619 int status;
2620
Fred Isaman3b65a302016-09-19 10:06:49 -04002621 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002622 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002623 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002624 if (status != 0) {
2625 if (status == -NFS4ERR_BADNAME &&
2626 !(o_arg->open_flags & O_CREAT))
2627 return -ENOENT;
2628 return status;
2629 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002630
Trond Myklebust6926afd12012-01-07 13:22:46 -05002631 nfs_fattr_map_and_free_names(server, &data->f_attr);
2632
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002633 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002634 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002635 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002636 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002637 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002638 if (data->file_created ||
2639 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002640 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002641 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002642 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002643 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2644 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002646 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002648 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002650 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2651 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002652 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2653 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002654 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658/*
2659 * OPEN_EXPIRED:
2660 * reclaim state on the server after a network partition.
2661 * Assumes caller holds the appropriate lock
2662 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002663static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002665 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002666 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002668 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002669 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002670 if (IS_ERR(opendata))
2671 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002672 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002673 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002674 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002675 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002679static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002680{
Trond Myklebust539cd032007-06-05 11:46:42 -04002681 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002682 struct nfs4_exception exception = { };
2683 int err;
2684
2685 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002686 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002687 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002688 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2689 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002690 switch (err) {
2691 default:
2692 goto out;
2693 case -NFS4ERR_GRACE:
2694 case -NFS4ERR_DELAY:
2695 nfs4_handle_exception(server, err, &exception);
2696 err = 0;
2697 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002698 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002699out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002700 return err;
2701}
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002706 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Trond Myklebust864472e2006-01-03 09:55:15 +01002708 ctx = nfs4_state_find_open_context(state);
2709 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002710 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002711 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002712 put_nfs_open_context(ctx);
2713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Trond Myklebust41020b62016-09-22 13:38:58 -04002716static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2717 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002718{
Trond Myklebust41020b62016-09-22 13:38:58 -04002719 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002720 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002721}
2722
2723static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2724{
2725 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002726 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002727}
2728
2729static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2730{
2731 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2732 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002733 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002734 return nfs4_open_expired(sp, state);
2735}
2736
Trond Myklebust45870d62016-09-22 13:38:59 -04002737static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2738 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002739 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002740{
2741 return -NFS4ERR_BAD_STATEID;
2742}
2743
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002744#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002745static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2746 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002747 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002748{
2749 int status;
2750
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002751 switch (stateid->type) {
2752 default:
2753 break;
2754 case NFS4_INVALID_STATEID_TYPE:
2755 case NFS4_SPECIAL_STATEID_TYPE:
2756 return -NFS4ERR_BAD_STATEID;
2757 case NFS4_REVOKED_STATEID_TYPE:
2758 goto out_free;
2759 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002760
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002761 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002762 switch (status) {
2763 case -NFS4ERR_EXPIRED:
2764 case -NFS4ERR_ADMIN_REVOKED:
2765 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002766 break;
2767 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002768 return status;
2769 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002770out_free:
2771 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002772 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002773 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002774}
2775
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002776static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002777{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002778 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002779 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002780 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002781 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002782 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002783
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002784 /* Get the delegation credential for use by test/free_stateid */
2785 rcu_read_lock();
2786 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002787 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002788 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002789 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002790 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002791 }
2792
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002793 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002794 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002795
Trond Myklebust994b15b2018-09-05 14:07:14 -04002796 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2797 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002798 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002799 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002800 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002801 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002802
NeilBrowna52458b2018-12-03 11:30:31 +11002803 if (delegation->cred)
2804 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002805 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002806 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002807 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002808 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002809 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002810 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002811 else
2812 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002813
Trond Myklebust8c39a392019-07-19 13:48:44 -04002814 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002815 return ret;
2816}
2817
2818static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2819{
2820 nfs4_stateid tmp;
2821
2822 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2823 nfs4_copy_delegation_stateid(state->inode, state->state,
2824 &tmp, NULL) &&
2825 nfs4_stateid_match_other(&state->stateid, &tmp))
2826 nfs_state_set_delegation(state, &tmp, state->state);
2827 else
2828 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002829}
2830
2831/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002832 * nfs41_check_expired_locks - possibly free a lock stateid
2833 *
2834 * @state: NFSv4 state for an inode
2835 *
2836 * Returns NFS_OK if recovery for this stateid is now finished.
2837 * Otherwise a negative NFS4ERR value is returned.
2838 */
2839static int nfs41_check_expired_locks(struct nfs4_state *state)
2840{
2841 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002842 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002843 struct nfs_server *server = NFS_SERVER(state->inode);
2844
2845 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2846 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002847
2848 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002849 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2850 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002851 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002852
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002853 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002854 spin_unlock(&state->state_lock);
2855
2856 nfs4_put_lock_state(prev);
2857 prev = lsp;
2858
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002859 status = nfs41_test_and_free_expired_stateid(server,
2860 &lsp->ls_stateid,
2861 cred);
2862 trace_nfs4_test_lock_stateid(state, lsp, status);
2863 if (status == -NFS4ERR_EXPIRED ||
2864 status == -NFS4ERR_BAD_STATEID) {
2865 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002866 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002867 if (!recover_lost_locks)
2868 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2869 } else if (status != NFS_OK) {
2870 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002871 nfs4_put_lock_state(prev);
2872 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002873 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002874 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002875 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002876 }
2877 spin_unlock(&state->state_lock);
2878 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002879out:
2880 return ret;
2881}
2882
2883/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002884 * nfs41_check_open_stateid - possibly free an open stateid
2885 *
2886 * @state: NFSv4 state for an inode
2887 *
2888 * Returns NFS_OK if recovery for this stateid is now finished.
2889 * Otherwise a negative NFS4ERR value is returned.
2890 */
2891static int nfs41_check_open_stateid(struct nfs4_state *state)
2892{
2893 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002894 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002895 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002896 int status;
2897
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002898 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002899 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002900 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002901 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002902 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002903 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002904 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002905 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002906 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002907 if (nfs_open_stateid_recover_openmode(state))
2908 return -NFS4ERR_OPENMODE;
2909 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002910}
2911
2912static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2913{
Chuck Levereb64cf92012-07-11 16:30:05 -04002914 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002915
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002916 status = nfs41_check_delegation_stateid(state);
2917 if (status != NFS_OK)
2918 return status;
2919 nfs41_delegation_recover_stateid(state);
2920
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002921 status = nfs41_check_expired_locks(state);
2922 if (status != NFS_OK)
2923 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002924 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002925 if (status != NFS_OK)
2926 status = nfs4_open_expired(sp, state);
2927 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002928}
2929#endif
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002932 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2933 * fields corresponding to attributes that were used to store the verifier.
2934 * Make sure we clobber those fields in the later setattr call
2935 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002936static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002937 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002938{
Trond Myklebust609339c2018-03-28 16:18:17 -04002939 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2940 __u32 attrset[3];
2941 unsigned ret;
2942 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002943
Trond Myklebust609339c2018-03-28 16:18:17 -04002944 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2945 attrset[i] = opendata->o_res.attrset[i];
2946 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2947 attrset[i] &= ~bitmask[i];
2948 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002949
Trond Myklebust609339c2018-03-28 16:18:17 -04002950 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2951 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002952
Trond Myklebust609339c2018-03-28 16:18:17 -04002953 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2954 if (sattr->ia_valid & ATTR_ATIME_SET)
2955 ret |= ATTR_ATIME_SET;
2956 else
2957 ret |= ATTR_ATIME;
2958 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002959
Trond Myklebust609339c2018-03-28 16:18:17 -04002960 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2961 if (sattr->ia_valid & ATTR_MTIME_SET)
2962 ret |= ATTR_MTIME_SET;
2963 else
2964 ret |= ATTR_MTIME;
2965 }
2966
2967 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002968 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002969 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002970}
2971
Trond Myklebustc21443c2013-02-07 14:26:21 -05002972static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002973 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002974{
2975 struct nfs4_state_owner *sp = opendata->owner;
2976 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002977 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002978 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002979 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05002980 struct inode *dir = d_inode(opendata->dir);
2981 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002982 unsigned int seq;
2983 int ret;
2984
2985 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05002986 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002987
Fred Isaman3b65a302016-09-19 10:06:49 -04002988 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002989 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002990 goto out;
2991
Trond Myklebustae55e592018-05-22 11:17:16 -04002992 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002993 ret = PTR_ERR(state);
2994 if (IS_ERR(state))
2995 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002996 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002997 if (server->caps & NFS_CAP_POSIX_LOCK)
2998 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002999 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3000 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003001
Trond Myklebust275bb302013-05-29 13:11:28 -04003002 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003003 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003004 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003005 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003006 alias = d_exact_alias(dentry, state->inode);
3007 if (!alias)
3008 alias = d_splice_alias(igrab(state->inode), dentry);
3009 /* d_splice_alias() can't fail here - it's a non-directory */
3010 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003011 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003012 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003013 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003014 }
3015
3016 switch(opendata->o_arg.claim) {
3017 default:
3018 break;
3019 case NFS4_OPEN_CLAIM_NULL:
3020 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3021 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3022 if (!opendata->rpc_done)
3023 break;
3024 if (opendata->o_res.delegation_type != 0)
3025 dir_verifier = nfs_save_change_attribute(dir);
3026 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003027 }
3028
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003029 /* Parse layoutget results before we check for access */
3030 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3031
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003032 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3033 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003034 if (ret != 0)
3035 goto out;
3036
David Howells2b0143b2015-03-17 22:25:59 +00003037 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003038 nfs_inode_attach_open_context(ctx);
3039 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3040 nfs4_schedule_stateid_recovery(server, state);
3041 }
Fred Isaman2409a972016-10-06 12:11:21 -04003042
Trond Myklebustc21443c2013-02-07 14:26:21 -05003043out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003044 if (!opendata->cancelled)
3045 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003046 return ret;
3047}
3048
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003049/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003050 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 */
Andy Adamson82be4172012-05-23 05:02:35 -04003052static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003053 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003054 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003055 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003056 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 struct nfs4_state_owner *sp;
3059 struct nfs4_state *state = NULL;
3060 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003061 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003062 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003063 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003064 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003065 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003066 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003067 struct iattr *sattr = c->sattr;
3068 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003069 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003070 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003074 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3075 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3077 goto out_err;
3078 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003079 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003080 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003081 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003082 if (d_really_is_positive(dentry))
3083 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003084 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003085 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003086 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003087 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3088 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003089 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003090 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
David Quigley14c43f72013-05-22 12:50:43 -04003092 if (label) {
3093 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3094 if (IS_ERR(olabel)) {
3095 status = PTR_ERR(olabel);
3096 goto err_opendata_put;
3097 }
3098 }
3099
Trond Myklebuste911b812014-03-26 13:24:37 -07003100 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3101 if (!opendata->f_attr.mdsthreshold) {
3102 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3103 if (!opendata->f_attr.mdsthreshold)
3104 goto err_free_label;
3105 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003106 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003107 }
David Howells2b0143b2015-03-17 22:25:59 +00003108 if (d_really_is_positive(dentry))
3109 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003110
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003111 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003112 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003113 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003114 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003115
NeilBrownefcbc042015-07-30 13:00:56 +10003116 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003117 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003118 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003119 /*
3120 * send create attributes which was not set by open
3121 * with an extra setattr.
3122 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003123 if (attrs || label) {
3124 unsigned ia_old = sattr->ia_valid;
3125
3126 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003127 nfs_fattr_init(opendata->o_res.f_attr);
3128 status = nfs4_do_setattr(state->inode, cred,
3129 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003130 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003131 if (status == 0) {
3132 nfs_setattr_update_inode(state->inode, sattr,
3133 opendata->o_res.f_attr);
3134 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3135 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003136 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003137 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003138 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003139 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003140 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003141
Trond Myklebuste911b812014-03-26 13:24:37 -07003142 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003143 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003144 opendata->f_attr.mdsthreshold = NULL;
3145 }
Andy Adamson82be4172012-05-23 05:02:35 -04003146
David Quigley14c43f72013-05-22 12:50:43 -04003147 nfs4_label_free(olabel);
3148
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003149 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003152err_free_label:
3153 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003154err_opendata_put:
3155 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003156err_put_state_owner:
3157 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return status;
3160}
3161
3162
Andy Adamson82be4172012-05-23 05:02:35 -04003163static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003164 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003165 int flags,
3166 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003167 struct nfs4_label *label,
3168 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003170 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003171 struct nfs4_exception exception = {
3172 .interruptible = true,
3173 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003175 struct nfs4_open_createattrs c = {
3176 .label = label,
3177 .sattr = sattr,
3178 .verf = {
3179 [0] = (__u32)jiffies,
3180 [1] = (__u32)current->pid,
3181 },
3182 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 int status;
3184
3185 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003186 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003187 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003188 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (status == 0)
3190 break;
3191 /* NOTE: BAD_SEQID means the server and client disagree about the
3192 * book-keeping w.r.t. state-changing operations
3193 * (OPEN/CLOSE/LOCK/LOCKU...)
3194 * It is actually a sign of a bug on the client or on the server.
3195 *
3196 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003197 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 * have unhashed the old state_owner for us, and that we can
3199 * therefore safely retry using a new one. We should still warn
3200 * the user though...
3201 */
3202 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003203 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003204 " returned a bad sequence-id error!\n",
3205 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 exception.retry = 1;
3207 continue;
3208 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003209 /*
3210 * BAD_STATEID on OPEN means that the server cancelled our
3211 * state before it received the OPEN_CONFIRM.
3212 * Recover by retrying the request as per the discussion
3213 * on Page 181 of RFC3530.
3214 */
3215 if (status == -NFS4ERR_BAD_STATEID) {
3216 exception.retry = 1;
3217 continue;
3218 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003219 if (status == -NFS4ERR_EXPIRED) {
3220 nfs4_schedule_lease_recovery(server->nfs_client);
3221 exception.retry = 1;
3222 continue;
3223 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003224 if (status == -EAGAIN) {
3225 /* We must have found a delegation */
3226 exception.retry = 1;
3227 continue;
3228 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003229 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3230 continue;
3231 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 status, &exception));
3233 } while (exception.retry);
3234 return res;
3235}
3236
Trond Myklebust8487c472016-06-26 08:44:35 -04003237static int _nfs4_do_setattr(struct inode *inode,
3238 struct nfs_setattrargs *arg,
3239 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003240 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003241 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003242{
3243 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003244 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3246 .rpc_argp = arg,
3247 .rpc_resp = res,
3248 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003249 };
NeilBrowna52458b2018-12-03 11:30:31 +11003250 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003251 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003252 bool truncate;
3253 int status;
3254
3255 nfs_fattr_init(res->fattr);
3256
3257 /* Servers should only apply open mode checks for file size changes */
3258 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003259 if (!truncate)
3260 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003261
Trond Myklebust991eedb2018-04-09 11:15:30 -04003262 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003263 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003264 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003265 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003266 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003267 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003268 l_ctx = nfs_get_lock_context(ctx);
3269 if (IS_ERR(l_ctx))
3270 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003271 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3272 &arg->stateid, &delegation_cred);
3273 nfs_put_lock_context(l_ctx);
3274 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003275 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003276 else if (status == -EAGAIN)
3277 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003278 } else {
3279zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003280 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003281 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003282 if (delegation_cred)
3283 msg.rpc_cred = delegation_cred;
3284
3285 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3286
NeilBrowna52458b2018-12-03 11:30:31 +11003287 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003288 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003289 renew_lease(server, timestamp);
3290 trace_nfs4_setattr(inode, &arg->stateid, status);
3291 return status;
3292}
3293
NeilBrowna52458b2018-12-03 11:30:31 +11003294static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003295 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003296 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003297 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003299 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003300 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003301 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003302 struct nfs_setattrargs arg = {
3303 .fh = NFS_FH(inode),
3304 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003306 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003307 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003308 };
3309 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003311 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003313 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003314 struct nfs4_exception exception = {
3315 .state = state,
3316 .inode = inode,
3317 .stateid = &arg.stateid,
3318 };
3319 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003322 nfs4_bitmap_copy_adjust_setattr(bitmask,
3323 nfs4_bitmask(server, olabel),
3324 inode);
3325
NeilBrown29b59f92016-10-13 15:26:47 +11003326 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003327 switch (err) {
3328 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003329 if (!(sattr->ia_valid & ATTR_SIZE)) {
3330 pr_warn_once("NFSv4: server %s is incorrectly "
3331 "applying open mode checks to "
3332 "a SETATTR that is not "
3333 "changing file size.\n",
3334 server->nfs_client->cl_hostname);
3335 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003336 if (state && !(state->state & FMODE_WRITE)) {
3337 err = -EBADF;
3338 if (sattr->ia_valid & ATTR_OPEN)
3339 err = -EACCES;
3340 goto out;
3341 }
3342 }
3343 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003345out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return err;
3347}
3348
Peng Tao500d7012015-09-22 11:35:22 +08003349static bool
3350nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3351{
3352 if (inode == NULL || !nfs_have_layout(inode))
3353 return false;
3354
3355 return pnfs_wait_on_layoutreturn(inode, task);
3356}
3357
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003358/*
3359 * Update the seqid of an open stateid
3360 */
3361static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3362 struct nfs4_state *state)
3363{
3364 __be32 seqid_open;
3365 u32 dst_seqid;
3366 int seq;
3367
3368 for (;;) {
3369 if (!nfs4_valid_open_stateid(state))
3370 break;
3371 seq = read_seqbegin(&state->seqlock);
3372 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3373 nfs4_stateid_copy(dst, &state->open_stateid);
3374 if (read_seqretry(&state->seqlock, seq))
3375 continue;
3376 break;
3377 }
3378 seqid_open = state->open_stateid.seqid;
3379 if (read_seqretry(&state->seqlock, seq))
3380 continue;
3381
3382 dst_seqid = be32_to_cpu(dst->seqid);
3383 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3384 dst->seqid = seqid_open;
3385 break;
3386 }
3387}
3388
3389/*
3390 * Update the seqid of an open stateid after receiving
3391 * NFS4ERR_OLD_STATEID
3392 */
3393static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3394 struct nfs4_state *state)
3395{
3396 __be32 seqid_open;
3397 u32 dst_seqid;
3398 bool ret;
3399 int seq;
3400
3401 for (;;) {
3402 ret = false;
3403 if (!nfs4_valid_open_stateid(state))
3404 break;
3405 seq = read_seqbegin(&state->seqlock);
3406 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3407 if (read_seqretry(&state->seqlock, seq))
3408 continue;
3409 break;
3410 }
3411 seqid_open = state->open_stateid.seqid;
3412 if (read_seqretry(&state->seqlock, seq))
3413 continue;
3414
3415 dst_seqid = be32_to_cpu(dst->seqid);
3416 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3417 dst->seqid = cpu_to_be32(dst_seqid + 1);
3418 else
3419 dst->seqid = seqid_open;
3420 ret = true;
3421 break;
3422 }
3423
3424 return ret;
3425}
3426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427struct nfs4_closedata {
3428 struct inode *inode;
3429 struct nfs4_state *state;
3430 struct nfs_closeargs arg;
3431 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003432 struct {
3433 struct nfs4_layoutreturn_args arg;
3434 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003435 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003436 u32 roc_barrier;
3437 bool roc;
3438 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003439 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003440 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441};
3442
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003443static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003444{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003445 struct nfs4_closedata *calldata = data;
3446 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003447 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003448
Trond Myklebustcf805162016-11-15 14:56:07 -05003449 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003450 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3451 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003452 nfs4_put_open_state(calldata->state);
3453 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003454 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003455 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003456 kfree(calldata);
3457}
3458
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003459static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003461 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003464 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003465 struct nfs4_exception exception = {
3466 .state = state,
3467 .inode = calldata->inode,
3468 .stateid = &calldata->arg.stateid,
3469 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Chuck Levera3ca5652012-03-01 17:00:40 -05003471 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003472 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3473 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003474 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003475
3476 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04003477 if (pnfs_roc_done(task, calldata->inode,
3478 &calldata->arg.lr_args,
3479 &calldata->res.lr_res,
3480 &calldata->res.lr_ret) == -EAGAIN)
3481 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003482
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003483 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 * the state_owner. we keep this around to process errors
3485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 switch (task->tk_status) {
3487 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003488 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003489 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003490 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003491 case -NFS4ERR_ACCESS:
3492 if (calldata->arg.bitmask != NULL) {
3493 calldata->arg.bitmask = NULL;
3494 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003495 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003496
3497 }
3498 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003499 case -NFS4ERR_OLD_STATEID:
3500 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003501 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003502 state))
3503 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003504 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003505 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003507 case -NFS4ERR_EXPIRED:
3508 nfs4_free_revoked_stateid(server,
3509 &calldata->arg.stateid,
3510 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003511 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003512 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003513 if (calldata->arg.fmode == 0)
3514 break;
3515 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003517 task->tk_status = nfs4_async_handle_exception(task,
3518 server, task->tk_status, &exception);
3519 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003520 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003522 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3523 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003524out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003525 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003526 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003527 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003528 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003529 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003530out_restart:
3531 task->tk_status = 0;
3532 rpc_restart_call_prepare(task);
3533 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003536static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003538 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003539 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003540 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003541 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003542 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003543 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003544
Chuck Levera3ca5652012-03-01 17:00:40 -05003545 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003546 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003547 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003548
Trond Myklebust88069f72009-12-08 08:33:16 -05003549 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003550 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003551 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3552 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3553 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003554 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003555 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003556 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003557 if (state->n_rdonly == 0)
3558 call_close |= is_rdonly;
3559 else if (is_rdonly)
3560 calldata->arg.fmode |= FMODE_READ;
3561 if (state->n_wronly == 0)
3562 call_close |= is_wronly;
3563 else if (is_wronly)
3564 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003565 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3566 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003567 } else if (is_rdwr)
3568 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3569
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003570 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3571 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003572 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003573 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003574
3575 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003576 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003577 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003578 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003579
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003580 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003581 nfs_release_seqid(calldata->arg.seqid);
3582 goto out_wait;
3583 }
3584
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003585 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3586 if (lo && !pnfs_layout_is_valid(lo)) {
3587 calldata->arg.lr_args = NULL;
3588 calldata->res.lr_res = NULL;
3589 }
3590
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003591 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003592 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003593
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003594 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003595 /* Close-to-open cache consistency revalidation */
3596 if (!nfs4_have_delegation(inode, FMODE_READ))
3597 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3598 else
3599 calldata->arg.bitmask = NULL;
3600 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003601
Trond Myklebust6ae37332015-01-30 14:21:14 -05003602 calldata->arg.share_access =
3603 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3604 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003605
Trond Myklebustd8d84982016-12-19 12:14:44 -05003606 if (calldata->res.fattr == NULL)
3607 calldata->arg.bitmask = NULL;
3608 else if (calldata->arg.bitmask == NULL)
3609 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003610 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003611 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003612 &calldata->arg.seq_args,
3613 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003614 task) != 0)
3615 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003616 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003617 return;
3618out_no_action:
3619 task->tk_action = NULL;
3620out_wait:
3621 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003624static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003625 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003626 .rpc_call_done = nfs4_close_done,
3627 .rpc_release = nfs4_free_closedata,
3628};
3629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630/*
3631 * It is possible for data to be read/written from a mem-mapped file
3632 * after the sys_close call (which hits the vfs layer as a flush).
3633 * This means that we can't safely call nfsv4 close on a file until
3634 * the inode is cleared. This in turn means that we are not good
3635 * NFSv4 citizens - we do not indicate to the server to update the file's
3636 * share state even when we are done with one of the three share
3637 * stateid's in the inode.
3638 *
3639 * NOTE: Caller must be holding the sp->so_owner semaphore!
3640 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003641int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003643 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003644 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003646 struct nfs4_state_owner *sp = state->owner;
3647 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003648 struct rpc_message msg = {
3649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3650 .rpc_cred = state->owner->so_cred,
3651 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003652 struct rpc_task_setup task_setup_data = {
3653 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003654 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003655 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003656 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003657 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003658 };
Trond Myklebust95121352005-10-18 14:20:12 -07003659 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003661 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3662 &task_setup_data.rpc_client, &msg);
3663
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003664 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003666 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003667 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003668 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003670 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003671 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3672 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003674 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3675 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003676 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003677 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003678 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003679 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003680 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003681 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003682 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003683 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003684 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003685 calldata->lr.roc = pnfs_roc(state->inode,
3686 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3687 if (calldata->lr.roc) {
3688 calldata->arg.lr_args = &calldata->lr.arg;
3689 calldata->res.lr_res = &calldata->lr.res;
3690 }
Al Viro643168c2011-06-22 18:20:23 -04003691 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003692
Trond Myklebust1174dd12010-12-21 10:52:24 -05003693 msg.rpc_argp = &calldata->arg;
3694 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003695 task_setup_data.callback_data = calldata;
3696 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003697 if (IS_ERR(task))
3698 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003699 status = 0;
3700 if (wait)
3701 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003702 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003703 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003704out_free_calldata:
3705 kfree(calldata);
3706out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003707 nfs4_put_open_state(state);
3708 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003709 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710}
3711
Trond Myklebust2b484292010-09-17 10:56:51 -04003712static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003713nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3714 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003717 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3718
3719 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Trond Myklebust565277f2007-10-15 18:17:53 -04003721 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003722 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003723
3724 nfs4_label_release_security(label);
3725
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003726 if (IS_ERR(state))
3727 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003728 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729}
3730
Trond Myklebust1185a552009-12-03 15:54:02 -05003731static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003732{
3733 if (ctx->state == NULL)
3734 return;
3735 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003736 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003737 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003738 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003739}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Trond Myklebustb944dba2013-11-04 15:20:20 -05003741#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3742#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003743#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3746{
Kinglong Mee8c612822015-08-26 21:12:58 +08003747 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003748 struct nfs4_server_caps_arg args = {
3749 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003750 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003751 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 struct nfs4_server_caps_res res = {};
3753 struct rpc_message msg = {
3754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003755 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 .rpc_resp = &res,
3757 };
3758 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003759 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Kinglong Mee8c612822015-08-26 21:12:58 +08003761 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3762 FATTR4_WORD0_FH_EXPIRE_TYPE |
3763 FATTR4_WORD0_LINK_SUPPORT |
3764 FATTR4_WORD0_SYMLINK_SUPPORT |
3765 FATTR4_WORD0_ACLSUPPORT;
3766 if (minorversion)
3767 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3768
Bryan Schumaker7c513052011-03-24 17:12:24 +00003769 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003771 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003772 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003773 case 0:
3774 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3775 res.attr_bitmask[2] = 0;
3776 break;
3777 case 1:
3778 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3779 break;
3780 case 2:
3781 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003784 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3785 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3786 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3787 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003788 NFS_CAP_CTIME|NFS_CAP_MTIME|
3789 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003790 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3791 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 server->caps |= NFS_CAP_ACLS;
3793 if (res.has_links != 0)
3794 server->caps |= NFS_CAP_HARDLINKS;
3795 if (res.has_symlinks != 0)
3796 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003797 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3798 server->caps |= NFS_CAP_FILEID;
3799 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3800 server->caps |= NFS_CAP_MODE;
3801 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3802 server->caps |= NFS_CAP_NLINK;
3803 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3804 server->caps |= NFS_CAP_OWNER;
3805 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3806 server->caps |= NFS_CAP_OWNER_GROUP;
3807 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3808 server->caps |= NFS_CAP_ATIME;
3809 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3810 server->caps |= NFS_CAP_CTIME;
3811 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3812 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003813#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3814 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3815 server->caps |= NFS_CAP_SECURITY_LABEL;
3816#endif
3817 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3818 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003819 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003820
Trond Myklebusta65318b2009-03-11 14:10:28 -04003821 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3822 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3823 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003824 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003825
3826 /* Avoid a regression due to buggy server */
3827 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3828 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003829 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3830 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003833 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 return status;
3837}
3838
Trond Myklebust55a97592006-06-09 09:34:19 -04003839int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840{
Trond Myklebust0688e642019-04-07 13:59:09 -04003841 struct nfs4_exception exception = {
3842 .interruptible = true,
3843 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 int err;
3845 do {
3846 err = nfs4_handle_exception(server,
3847 _nfs4_server_capabilities(server, fhandle),
3848 &exception);
3849 } while (exception.retry);
3850 return err;
3851}
3852
3853static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3854 struct nfs_fsinfo *info)
3855{
David Quigleyaa9c2662013-05-22 12:50:44 -04003856 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003858 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 };
3860 struct nfs4_lookup_res res = {
3861 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003862 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 .fh = fhandle,
3864 };
3865 struct rpc_message msg = {
3866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3867 .rpc_argp = &args,
3868 .rpc_resp = &res,
3869 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003870
David Quigleyaa9c2662013-05-22 12:50:44 -04003871 bitmask[0] = nfs4_fattr_bitmap[0];
3872 bitmask[1] = nfs4_fattr_bitmap[1];
3873 /*
3874 * Process the label in the upcoming getfattr
3875 */
3876 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3877
Trond Myklebust0e574af2005-10-27 22:12:38 -04003878 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003879 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880}
3881
3882static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3883 struct nfs_fsinfo *info)
3884{
Trond Myklebust0688e642019-04-07 13:59:09 -04003885 struct nfs4_exception exception = {
3886 .interruptible = true,
3887 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 int err;
3889 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003890 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003891 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003892 switch (err) {
3893 case 0:
3894 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003895 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003896 default:
3897 err = nfs4_handle_exception(server, err, &exception);
3898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003900out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return err;
3902}
3903
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003904static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3905 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3906{
Trond Myklebustc2190662013-08-26 19:23:04 -04003907 struct rpc_auth_create_args auth_args = {
3908 .pseudoflavor = flavor,
3909 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003910 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003911
Trond Myklebustc2190662013-08-26 19:23:04 -04003912 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003913 if (IS_ERR(auth))
3914 return -EACCES;
3915 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003916}
3917
Chuck Lever9a744ba2013-03-16 15:56:02 -04003918/*
3919 * Retry pseudoroot lookup with various security flavors. We do this when:
3920 *
3921 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3922 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3923 *
3924 * Returns zero on success, or a negative NFS4ERR value, or a
3925 * negative errno value.
3926 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003927static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003928 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003930 /* Per 3530bis 15.33.5 */
3931 static const rpc_authflavor_t flav_array[] = {
3932 RPC_AUTH_GSS_KRB5P,
3933 RPC_AUTH_GSS_KRB5I,
3934 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003935 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003936 RPC_AUTH_NULL,
3937 };
3938 int status = -EPERM;
3939 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003941 if (server->auth_info.flavor_len > 0) {
3942 /* try each flavor specified by user */
3943 for (i = 0; i < server->auth_info.flavor_len; i++) {
3944 status = nfs4_lookup_root_sec(server, fhandle, info,
3945 server->auth_info.flavors[i]);
3946 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3947 continue;
3948 break;
3949 }
3950 } else {
3951 /* no flavors specified by user, try default list */
3952 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3953 status = nfs4_lookup_root_sec(server, fhandle, info,
3954 flav_array[i]);
3955 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3956 continue;
3957 break;
3958 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003959 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003960
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003961 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003962 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003963 * to access the mount. It could also mean that we tried to mount
3964 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3965 * existing mount programs don't handle -EACCES very well so it should
3966 * be mapped to -EPERM instead.
3967 */
3968 if (status == -EACCES)
3969 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003970 return status;
3971}
3972
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003973/**
3974 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3975 * @server: initialized nfs_server handle
3976 * @fhandle: we fill in the pseudo-fs root file handle
3977 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003978 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003979 *
3980 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003981 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003982int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003983 struct nfs_fsinfo *info,
3984 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003985{
Andre Przywarac7757072015-04-23 17:17:40 +01003986 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003987
Andre Przywarac7757072015-04-23 17:17:40 +01003988 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003989 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003990
3991 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003992 status = server->nfs_client->cl_mvops->find_root_sec(server,
3993 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003994
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 if (status == 0)
3996 status = nfs4_server_capabilities(server, fhandle);
3997 if (status == 0)
3998 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003999
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004000 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
Bryan Schumakerbae36242012-05-10 15:07:31 -04004003static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4004 struct nfs_fsinfo *info)
4005{
4006 int error;
4007 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004008 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004009
4010 error = nfs4_server_capabilities(server, mntfh);
4011 if (error < 0) {
4012 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4013 return error;
4014 }
4015
Trond Myklebusta841b542018-04-07 13:50:59 -04004016 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004017 if (error < 0) {
4018 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004019 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004020 }
4021
4022 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4023 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4024 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4025
Scott Mayhew779df6a2020-03-03 17:58:37 -05004026out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004027 return error;
4028}
4029
Manoj Naik6b97fd32006-06-09 09:34:29 -04004030/*
4031 * Get locations and (maybe) other attributes of a referral.
4032 * Note that we'll actually follow the referral later when
4033 * we detect fsid mismatch in inode revalidation
4034 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004035static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4036 const struct qstr *name, struct nfs_fattr *fattr,
4037 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004038{
4039 int status = -ENOMEM;
4040 struct page *page = NULL;
4041 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004042
4043 page = alloc_page(GFP_KERNEL);
4044 if (page == NULL)
4045 goto out;
4046 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4047 if (locations == NULL)
4048 goto out;
4049
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004050 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004051 if (status != 0)
4052 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004053
4054 /*
4055 * If the fsid didn't change, this is a migration event, not a
4056 * referral. Cause us to drop into the exception handler, which
4057 * will kick off migration recovery.
4058 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004059 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004060 dprintk("%s: server did not return a different fsid for"
4061 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004062 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004063 goto out;
4064 }
Andy Adamson533eb462011-06-13 18:25:56 -04004065 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4066 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004067
Andy Adamson533eb462011-06-13 18:25:56 -04004068 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004069 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004070 memset(fhandle, 0, sizeof(struct nfs_fh));
4071out:
4072 if (page)
4073 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004074 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004075 return status;
4076}
4077
David Quigley1775fd32013-05-22 12:50:42 -04004078static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004079 struct nfs_fattr *fattr, struct nfs4_label *label,
4080 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081{
Trond Myklebust771734f2018-04-07 13:54:23 -04004082 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 struct nfs4_getattr_arg args = {
4084 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004085 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 };
4087 struct nfs4_getattr_res res = {
4088 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004089 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 .server = server,
4091 };
4092 struct rpc_message msg = {
4093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4094 .rpc_argp = &args,
4095 .rpc_resp = &res,
4096 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004097 unsigned short task_flags = 0;
4098
4099 /* Is this is an attribute revalidation, subject to softreval? */
4100 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4101 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004102
Trond Myklebust771734f2018-04-07 13:54:23 -04004103 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004104
Trond Myklebust0e574af2005-10-27 22:12:38 -04004105 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004106 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4107 return nfs4_do_call_sync(server->client, server, &msg,
4108 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109}
4110
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004111int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004112 struct nfs_fattr *fattr, struct nfs4_label *label,
4113 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
Trond Myklebust0688e642019-04-07 13:59:09 -04004115 struct nfs4_exception exception = {
4116 .interruptible = true,
4117 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 int err;
4119 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004120 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004121 trace_nfs4_getattr(server, fhandle, fattr, err);
4122 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 &exception);
4124 } while (exception.retry);
4125 return err;
4126}
4127
4128/*
4129 * The file is not closed if it is opened due to the a request to change
4130 * the size of the file. The open call will not be needed once the
4131 * VFS layer lookup-intents are implemented.
4132 *
4133 * Close is called when the inode is destroyed.
4134 * If we haven't opened the file for O_WRONLY, we
4135 * need to in the size_change case to obtain a stateid.
4136 *
4137 * Got race?
4138 * Because OPEN is always done by name in nfsv4, it is
4139 * possible that we opened a different file by the same
4140 * name. We can recognize this race condition, but we
4141 * can't do anything about it besides returning an error.
4142 *
4143 * This will be fixed with VFS changes (lookup-intent).
4144 */
4145static int
4146nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4147 struct iattr *sattr)
4148{
David Howells2b0143b2015-03-17 22:25:59 +00004149 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004150 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004151 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004152 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 int status;
4154
Peng Tao88ac8152014-09-12 11:04:10 +08004155 if (pnfs_ld_layoutret_on_setattr(inode) &&
4156 sattr->ia_valid & ATTR_SIZE &&
4157 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004158 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004159
Trond Myklebust0e574af2005-10-27 22:12:38 -04004160 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Andy Adamson26699402012-05-30 16:12:24 -04004162 /* Deal with open(O_TRUNC) */
4163 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004164 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004165
4166 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004167 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004168 return 0;
4169
Trond Myklebustd5308382005-11-04 15:33:38 -05004170 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004171 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004172
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004173 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004174 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004175 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004176 }
4177
David Quigley14c43f72013-05-22 12:50:43 -04004178 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4179 if (IS_ERR(label))
4180 return PTR_ERR(label);
4181
Trond Myklebust199366f2018-03-20 16:43:18 -04004182 /* Return any delegations if we're going to change ACLs */
4183 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004184 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004185
NeilBrown29b59f92016-10-13 15:26:47 +11004186 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004187 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004188 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004189 nfs_setsecurity(inode, fattr, label);
4190 }
David Quigley14c43f72013-05-22 12:50:43 -04004191 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return status;
4193}
4194
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004195static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004196 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004197 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004198{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004199 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004200 int status;
4201 struct nfs4_lookup_arg args = {
4202 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004203 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004204 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004205 };
4206 struct nfs4_lookup_res res = {
4207 .server = server,
4208 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004209 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004210 .fh = fhandle,
4211 };
4212 struct rpc_message msg = {
4213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4214 .rpc_argp = &args,
4215 .rpc_resp = &res,
4216 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004217 unsigned short task_flags = 0;
4218
4219 /* Is this is an attribute revalidation, subject to softreval? */
4220 if (nfs_lookup_is_soft_revalidate(dentry))
4221 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004222
David Quigleyaa9c2662013-05-22 12:50:44 -04004223 args.bitmask = nfs4_bitmask(server, label);
4224
David Howells2b3de442006-08-22 20:06:09 -04004225 nfs_fattr_init(fattr);
4226
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004227 dprintk("NFS call lookup %pd2\n", dentry);
4228 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4229 status = nfs4_do_call_sync(clnt, server, &msg,
4230 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 dprintk("NFS reply lookup: %d\n", status);
4232 return status;
4233}
4234
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004235static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004236{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004237 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004238 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004239 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4240 fattr->nlink = 2;
4241}
4242
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004243static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004244 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004245 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004246{
Trond Myklebust0688e642019-04-07 13:59:09 -04004247 struct nfs4_exception exception = {
4248 .interruptible = true,
4249 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004250 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004251 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004252 int err;
4253 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004254 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004255 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004256 switch (err) {
4257 case -NFS4ERR_BADNAME:
4258 err = -ENOENT;
4259 goto out;
4260 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004261 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004262 if (err == -NFS4ERR_MOVED)
4263 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004264 goto out;
4265 case -NFS4ERR_WRONGSEC:
4266 err = -EPERM;
4267 if (client != *clnt)
4268 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004269 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004270 if (IS_ERR(client))
4271 return PTR_ERR(client);
4272
4273 exception.retry = 1;
4274 break;
4275 default:
4276 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4277 }
4278 } while (exception.retry);
4279
4280out:
4281 if (err == 0)
4282 *clnt = client;
4283 else if (client != *clnt)
4284 rpc_shutdown_client(client);
4285
4286 return err;
4287}
4288
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004289static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004290 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4291 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004293 int status;
4294 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004295
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004296 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004297 if (client != NFS_CLIENT(dir)) {
4298 rpc_shutdown_client(client);
4299 nfs_fixup_secinfo_attributes(fattr);
4300 }
4301 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302}
4303
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004304struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004305nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004306 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4307{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004308 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004309 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004310
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004311 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004312 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004313 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004314 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004315}
4316
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004317static int _nfs4_proc_lookupp(struct inode *inode,
4318 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4319 struct nfs4_label *label)
4320{
4321 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4322 struct nfs_server *server = NFS_SERVER(inode);
4323 int status;
4324 struct nfs4_lookupp_arg args = {
4325 .bitmask = server->attr_bitmask,
4326 .fh = NFS_FH(inode),
4327 };
4328 struct nfs4_lookupp_res res = {
4329 .server = server,
4330 .fattr = fattr,
4331 .label = label,
4332 .fh = fhandle,
4333 };
4334 struct rpc_message msg = {
4335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4336 .rpc_argp = &args,
4337 .rpc_resp = &res,
4338 };
4339
4340 args.bitmask = nfs4_bitmask(server, label);
4341
4342 nfs_fattr_init(fattr);
4343
4344 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4345 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4346 &res.seq_res, 0);
4347 dprintk("NFS reply lookupp: %d\n", status);
4348 return status;
4349}
4350
4351static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4352 struct nfs_fattr *fattr, struct nfs4_label *label)
4353{
Trond Myklebust0688e642019-04-07 13:59:09 -04004354 struct nfs4_exception exception = {
4355 .interruptible = true,
4356 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004357 int err;
4358 do {
4359 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4360 trace_nfs4_lookupp(inode, err);
4361 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4362 &exception);
4363 } while (exception.retry);
4364 return err;
4365}
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4368{
Trond Myklebust76b32992007-08-10 17:45:11 -04004369 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 struct nfs4_accessargs args = {
4371 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004372 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004374 struct nfs4_accessres res = {
4375 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004376 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 struct rpc_message msg = {
4378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4379 .rpc_argp = &args,
4380 .rpc_resp = &res,
4381 .rpc_cred = entry->cred,
4382 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004383 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Trond Myklebust7c672652018-06-04 15:00:53 -04004385 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004386 res.fattr = nfs_alloc_fattr();
4387 if (res.fattr == NULL)
4388 return -ENOMEM;
4389 args.bitmask = server->cache_consistency_bitmask;
4390 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004391 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004393 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004394 if (res.fattr)
4395 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004397 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return status;
4399}
4400
4401static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4402{
Trond Myklebust0688e642019-04-07 13:59:09 -04004403 struct nfs4_exception exception = {
4404 .interruptible = true,
4405 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 int err;
4407 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004408 err = _nfs4_proc_access(inode, entry);
4409 trace_nfs4_access(inode, err);
4410 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 &exception);
4412 } while (exception.retry);
4413 return err;
4414}
4415
4416/*
4417 * TODO: For the time being, we don't try to get any attributes
4418 * along with any of the zero-copy operations READ, READDIR,
4419 * READLINK, WRITE.
4420 *
4421 * In the case of the first three, we want to put the GETATTR
4422 * after the read-type operation -- this is because it is hard
4423 * to predict the length of a GETATTR response in v4, and thus
4424 * align the READ data correctly. This means that the GETATTR
4425 * may end up partially falling into the page cache, and we should
4426 * shift it into the 'tail' of the xdr_buf before processing.
4427 * To do this efficiently, we need to know the total length
4428 * of data received, which doesn't seem to be available outside
4429 * of the RPC layer.
4430 *
4431 * In the case of WRITE, we also want to put the GETATTR after
4432 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004433 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 *
4435 * Both of these changes to the XDR layer would in fact be quite
4436 * minor, but I decided to leave them for a subsequent patch.
4437 */
4438static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4439 unsigned int pgbase, unsigned int pglen)
4440{
4441 struct nfs4_readlink args = {
4442 .fh = NFS_FH(inode),
4443 .pgbase = pgbase,
4444 .pglen = pglen,
4445 .pages = &page,
4446 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004447 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 struct rpc_message msg = {
4449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4450 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004451 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 };
4453
Bryan Schumaker7c513052011-03-24 17:12:24 +00004454 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 -07004455}
4456
4457static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4458 unsigned int pgbase, unsigned int pglen)
4459{
Trond Myklebust0688e642019-04-07 13:59:09 -04004460 struct nfs4_exception exception = {
4461 .interruptible = true,
4462 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 int err;
4464 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004465 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4466 trace_nfs4_readlink(inode, err);
4467 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 &exception);
4469 } while (exception.retry);
4470 return err;
4471}
4472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004474 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476static int
4477nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004478 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004480 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004481 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004482 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 int status = 0;
4485
NeilBrown532d4de2016-10-13 15:26:47 +11004486 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004487 if (IS_ERR(ctx))
4488 return PTR_ERR(ctx);
4489
David Quigleyaa9c2662013-05-22 12:50:44 -04004490 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4491
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004492 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4493 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004494 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 if (IS_ERR(state)) {
4496 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004497 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004500 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004501 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 return status;
4503}
4504
Trond Myklebust3c591172018-07-31 15:54:10 -04004505static int
4506_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
Trond Myklebust16e42952005-10-27 22:12:44 -04004508 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004509 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004511 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004513 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004514 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004515 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4518 .rpc_argp = &args,
4519 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004521 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004522 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004523
Bryan Schumaker7c513052011-03-24 17:12:24 +00004524 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004525 if (status == 0) {
4526 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004527 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004528 /* Removing a directory decrements nlink in the parent */
4529 if (ftype == NF4DIR && dir->i_nlink > 2)
4530 nfs4_dec_nlink_locked(dir);
4531 spin_unlock(&dir->i_lock);
4532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 return status;
4534}
4535
Trond Myklebust912678d2018-03-20 16:43:15 -04004536static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4537{
Trond Myklebust0688e642019-04-07 13:59:09 -04004538 struct nfs4_exception exception = {
4539 .interruptible = true,
4540 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004541 struct inode *inode = d_inode(dentry);
4542 int err;
4543
Trond Myklebustc01d3642018-03-20 16:43:20 -04004544 if (inode) {
4545 if (inode->i_nlink == 1)
4546 nfs4_inode_return_delegation(inode);
4547 else
4548 nfs4_inode_make_writeable(inode);
4549 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004550 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004551 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004552 trace_nfs4_remove(dir, &dentry->d_name, err);
4553 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4554 &exception);
4555 } while (exception.retry);
4556 return err;
4557}
4558
4559static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560{
Trond Myklebust0688e642019-04-07 13:59:09 -04004561 struct nfs4_exception exception = {
4562 .interruptible = true,
4563 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004567 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004568 trace_nfs4_remove(dir, name, err);
4569 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 &exception);
4571 } while (exception.retry);
4572 return err;
4573}
4574
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004575static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4576 struct dentry *dentry,
4577 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004579 struct nfs_removeargs *args = msg->rpc_argp;
4580 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Trond Myklebust977fcc22018-03-20 16:43:17 -04004582 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004584 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004585
4586 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004587
4588 if (inode)
4589 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590}
4591
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004592static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4593{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004594 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004595 &data->args.seq_args,
4596 &data->res.seq_res,
4597 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598}
4599
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004600static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004602 struct nfs_unlinkdata *data = task->tk_calldata;
4603 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004604
Trond Myklebust14516c32010-07-31 14:29:06 -04004605 if (!nfs4_sequence_done(task, &res->seq_res))
4606 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004607 if (nfs4_async_handle_error(task, res->server, NULL,
4608 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004609 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004610 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004611 update_changeattr(dir, &res->cinfo,
4612 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004613 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614}
4615
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004616static void nfs4_proc_rename_setup(struct rpc_message *msg,
4617 struct dentry *old_dentry,
4618 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004619{
Jeff Laytond3d41522010-09-17 17:31:57 -04004620 struct nfs_renameargs *arg = msg->rpc_argp;
4621 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004622 struct inode *old_inode = d_inode(old_dentry);
4623 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004624
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004625 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004626 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004627 if (new_inode)
4628 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004629 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004630 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004631 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004632}
4633
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004634static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4635{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004636 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004637 &data->args.seq_args,
4638 &data->res.seq_res,
4639 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004640}
4641
4642static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4643 struct inode *new_dir)
4644{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004645 struct nfs_renamedata *data = task->tk_calldata;
4646 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004647
4648 if (!nfs4_sequence_done(task, &res->seq_res))
4649 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004650 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004651 return 0;
4652
Trond Myklebustc733c492017-01-11 12:32:26 -05004653 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004654 if (new_dir != old_dir) {
4655 /* Note: If we moved a directory, nlink will change */
4656 update_changeattr(old_dir, &res->old_cinfo,
4657 res->old_fattr->time_start,
4658 NFS_INO_INVALID_OTHER);
4659 update_changeattr(new_dir, &res->new_cinfo,
4660 res->new_fattr->time_start,
4661 NFS_INO_INVALID_OTHER);
4662 } else
4663 update_changeattr(old_dir, &res->old_cinfo,
4664 res->old_fattr->time_start,
4665 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004666 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004667 return 1;
4668}
4669
Al Virobeffb8f2016-07-20 16:34:42 -04004670static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004672 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004673 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 struct nfs4_link_arg arg = {
4675 .fh = NFS_FH(inode),
4676 .dir_fh = NFS_FH(dir),
4677 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004678 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004680 struct nfs4_link_res res = {
4681 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004682 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004683 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 struct rpc_message msg = {
4685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4686 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004687 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004689 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Trond Myklebust136f2622010-04-16 16:22:49 -04004691 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004692 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004693 goto out;
4694
David Quigley14c43f72013-05-22 12:50:43 -04004695 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4696 if (IS_ERR(res.label)) {
4697 status = PTR_ERR(res.label);
4698 goto out;
4699 }
4700
Trond Myklebustc01d3642018-03-20 16:43:20 -04004701 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004702 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004703
Bryan Schumaker7c513052011-03-24 17:12:24 +00004704 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004705 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004706 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004707 status = nfs_post_op_update_inode(inode, res.fattr);
4708 if (!status)
4709 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004710 }
David Quigley14c43f72013-05-22 12:50:43 -04004711
4712
4713 nfs4_label_free(res.label);
4714
Trond Myklebust136f2622010-04-16 16:22:49 -04004715out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004716 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 return status;
4718}
4719
Al Virobeffb8f2016-07-20 16:34:42 -04004720static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721{
Trond Myklebust0688e642019-04-07 13:59:09 -04004722 struct nfs4_exception exception = {
4723 .interruptible = true,
4724 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 int err;
4726 do {
4727 err = nfs4_handle_exception(NFS_SERVER(inode),
4728 _nfs4_proc_link(inode, dir, name),
4729 &exception);
4730 } while (exception.retry);
4731 return err;
4732}
4733
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004734struct nfs4_createdata {
4735 struct rpc_message msg;
4736 struct nfs4_create_arg arg;
4737 struct nfs4_create_res res;
4738 struct nfs_fh fh;
4739 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004740 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004741};
4742
4743static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004744 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004745{
4746 struct nfs4_createdata *data;
4747
4748 data = kzalloc(sizeof(*data), GFP_KERNEL);
4749 if (data != NULL) {
4750 struct nfs_server *server = NFS_SERVER(dir);
4751
David Quigley14c43f72013-05-22 12:50:43 -04004752 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4753 if (IS_ERR(data->label))
4754 goto out_free;
4755
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004756 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4757 data->msg.rpc_argp = &data->arg;
4758 data->msg.rpc_resp = &data->res;
4759 data->arg.dir_fh = NFS_FH(dir);
4760 data->arg.server = server;
4761 data->arg.name = name;
4762 data->arg.attrs = sattr;
4763 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004764 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004765 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004766 data->res.server = server;
4767 data->res.fh = &data->fh;
4768 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004769 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004770 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004771 }
4772 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004773out_free:
4774 kfree(data);
4775 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004776}
4777
4778static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4779{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004780 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004781 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004782 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004783 spin_lock(&dir->i_lock);
4784 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004785 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004786 /* Creating a directory bumps nlink in the parent */
4787 if (data->arg.ftype == NF4DIR)
4788 nfs4_inc_nlink_locked(dir);
4789 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004790 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004791 }
4792 return status;
4793}
4794
4795static void nfs4_free_createdata(struct nfs4_createdata *data)
4796{
David Quigley14c43f72013-05-22 12:50:43 -04004797 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004798 kfree(data);
4799}
4800
Chuck Lever4f390c12006-08-22 20:06:22 -04004801static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004802 struct page *page, unsigned int len, struct iattr *sattr,
4803 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004805 struct nfs4_createdata *data;
4806 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Chuck Lever94a6d752006-08-22 20:06:23 -04004808 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004809 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004810
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004811 status = -ENOMEM;
4812 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4813 if (data == NULL)
4814 goto out;
4815
4816 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4817 data->arg.u.symlink.pages = &page;
4818 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004819 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004821 status = nfs4_do_create(dir, dentry, data);
4822
4823 nfs4_free_createdata(data);
4824out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 return status;
4826}
4827
Chuck Lever4f390c12006-08-22 20:06:22 -04004828static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004829 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830{
Trond Myklebust0688e642019-04-07 13:59:09 -04004831 struct nfs4_exception exception = {
4832 .interruptible = true,
4833 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004834 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004836
4837 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004840 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4841 trace_nfs4_symlink(dir, &dentry->d_name, err);
4842 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 &exception);
4844 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004845
4846 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 return err;
4848}
4849
4850static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004851 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004853 struct nfs4_createdata *data;
4854 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004856 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4857 if (data == NULL)
4858 goto out;
4859
David Quigley1775fd32013-05-22 12:50:42 -04004860 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004861 status = nfs4_do_create(dir, dentry, data);
4862
4863 nfs4_free_createdata(data);
4864out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 return status;
4866}
4867
4868static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4869 struct iattr *sattr)
4870{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004871 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004872 struct nfs4_exception exception = {
4873 .interruptible = true,
4874 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004875 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004877
David Quigleyaa9c2662013-05-22 12:50:44 -04004878 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4879
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004880 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4881 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004883 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4884 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4885 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 &exception);
4887 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004888 nfs4_label_release_security(label);
4889
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 return err;
4891}
4892
NeilBrown684f39b2018-12-03 11:30:30 +11004893static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004894 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895{
David Howells2b0143b2015-03-17 22:25:59 +00004896 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 struct nfs4_readdir_arg args = {
4898 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004899 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 .pgbase = 0,
4901 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004902 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004903 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 };
4905 struct nfs4_readdir_res res;
4906 struct rpc_message msg = {
4907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4908 .rpc_argp = &args,
4909 .rpc_resp = &res,
4910 .rpc_cred = cred,
4911 };
4912 int status;
4913
Al Viro6de14722013-09-16 10:53:17 -04004914 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4915 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004916 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004917 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004919 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05004920 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004921 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004922 status += args.pgbase;
4923 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004924
4925 nfs_invalidate_atime(dir);
4926
Harvey Harrison3110ff82008-05-02 13:42:44 -07004927 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 return status;
4929}
4930
NeilBrown684f39b2018-12-03 11:30:30 +11004931static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004932 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933{
Trond Myklebust0688e642019-04-07 13:59:09 -04004934 struct nfs4_exception exception = {
4935 .interruptible = true,
4936 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 int err;
4938 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004939 err = _nfs4_proc_readdir(dentry, cred, cookie,
4940 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004941 trace_nfs4_readdir(d_inode(dentry), err);
4942 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 &exception);
4944 } while (exception.retry);
4945 return err;
4946}
4947
4948static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004949 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004951 struct nfs4_createdata *data;
4952 int mode = sattr->ia_mode;
4953 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004955 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4956 if (data == NULL)
4957 goto out;
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004960 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004962 data->arg.ftype = NF4BLK;
4963 data->arg.u.device.specdata1 = MAJOR(rdev);
4964 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 }
4966 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004967 data->arg.ftype = NF4CHR;
4968 data->arg.u.device.specdata1 = MAJOR(rdev);
4969 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004970 } else if (!S_ISSOCK(mode)) {
4971 status = -EINVAL;
4972 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 }
David Quigley1775fd32013-05-22 12:50:42 -04004974
David Quigleyaa9c2662013-05-22 12:50:44 -04004975 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004976 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004977out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004978 nfs4_free_createdata(data);
4979out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 return status;
4981}
4982
4983static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4984 struct iattr *sattr, dev_t rdev)
4985{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004986 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004987 struct nfs4_exception exception = {
4988 .interruptible = true,
4989 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004990 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004992
David Quigleyaa9c2662013-05-22 12:50:44 -04004993 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4994
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004995 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4996 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004998 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4999 trace_nfs4_mknod(dir, &dentry->d_name, err);
5000 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 &exception);
5002 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005003
5004 nfs4_label_release_security(label);
5005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 return err;
5007}
5008
5009static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5010 struct nfs_fsstat *fsstat)
5011{
5012 struct nfs4_statfs_arg args = {
5013 .fh = fhandle,
5014 .bitmask = server->attr_bitmask,
5015 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005016 struct nfs4_statfs_res res = {
5017 .fsstat = fsstat,
5018 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 struct rpc_message msg = {
5020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5021 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005022 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 };
5024
Trond Myklebust0e574af2005-10-27 22:12:38 -04005025 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005026 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027}
5028
5029static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5030{
Trond Myklebust0688e642019-04-07 13:59:09 -04005031 struct nfs4_exception exception = {
5032 .interruptible = true,
5033 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 int err;
5035 do {
5036 err = nfs4_handle_exception(server,
5037 _nfs4_proc_statfs(server, fhandle, fsstat),
5038 &exception);
5039 } while (exception.retry);
5040 return err;
5041}
5042
5043static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5044 struct nfs_fsinfo *fsinfo)
5045{
5046 struct nfs4_fsinfo_arg args = {
5047 .fh = fhandle,
5048 .bitmask = server->attr_bitmask,
5049 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005050 struct nfs4_fsinfo_res res = {
5051 .fsinfo = fsinfo,
5052 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 struct rpc_message msg = {
5054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5055 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005056 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 };
5058
Bryan Schumaker7c513052011-03-24 17:12:24 +00005059 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060}
5061
5062static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5063{
Trond Myklebust0688e642019-04-07 13:59:09 -04005064 struct nfs4_exception exception = {
5065 .interruptible = true,
5066 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 int err;
5068
5069 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005070 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005071 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005072 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005073 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005074 break;
5075 }
5076 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 } while (exception.retry);
5078 return err;
5079}
5080
5081static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5082{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005083 int error;
5084
Trond Myklebust0e574af2005-10-27 22:12:38 -04005085 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005086 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005087 if (error == 0) {
5088 /* block layout checks this! */
5089 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005090 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005091 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005092
5093 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094}
5095
5096static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5097 struct nfs_pathconf *pathconf)
5098{
5099 struct nfs4_pathconf_arg args = {
5100 .fh = fhandle,
5101 .bitmask = server->attr_bitmask,
5102 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005103 struct nfs4_pathconf_res res = {
5104 .pathconf = pathconf,
5105 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 struct rpc_message msg = {
5107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5108 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005109 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 };
5111
5112 /* None of the pathconf attributes are mandatory to implement */
5113 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5114 memset(pathconf, 0, sizeof(*pathconf));
5115 return 0;
5116 }
5117
Trond Myklebust0e574af2005-10-27 22:12:38 -04005118 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005119 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120}
5121
5122static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5123 struct nfs_pathconf *pathconf)
5124{
Trond Myklebust0688e642019-04-07 13:59:09 -04005125 struct nfs4_exception exception = {
5126 .interruptible = true,
5127 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 int err;
5129
5130 do {
5131 err = nfs4_handle_exception(server,
5132 _nfs4_proc_pathconf(server, fhandle, pathconf),
5133 &exception);
5134 } while (exception.retry);
5135 return err;
5136}
5137
Trond Myklebust5521abf2013-03-16 20:54:34 -04005138int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005139 const struct nfs_open_context *ctx,
5140 const struct nfs_lock_context *l_ctx,
5141 fmode_t fmode)
5142{
NeilBrown17393472016-10-13 15:26:47 +11005143 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005144}
5145EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5146
Trond Myklebust5521abf2013-03-16 20:54:34 -04005147static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5148 const struct nfs_open_context *ctx,
5149 const struct nfs_lock_context *l_ctx,
5150 fmode_t fmode)
5151{
Ben Dooksd49dd112019-10-16 17:28:21 +01005152 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005153
Trond Myklebuste1253be2014-03-05 08:44:23 -05005154 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005155 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005156 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005157 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005158}
5159
5160static bool nfs4_error_stateid_expired(int err)
5161{
5162 switch (err) {
5163 case -NFS4ERR_DELEG_REVOKED:
5164 case -NFS4ERR_ADMIN_REVOKED:
5165 case -NFS4ERR_BAD_STATEID:
5166 case -NFS4ERR_STALE_STATEID:
5167 case -NFS4ERR_OLD_STATEID:
5168 case -NFS4ERR_OPENMODE:
5169 case -NFS4ERR_EXPIRED:
5170 return true;
5171 }
5172 return false;
5173}
5174
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005175static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005177 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005179 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005180 if (task->tk_status < 0) {
5181 struct nfs4_exception exception = {
5182 .inode = hdr->inode,
5183 .state = hdr->args.context->state,
5184 .stateid = &hdr->args.stateid,
5185 };
5186 task->tk_status = nfs4_async_handle_exception(task,
5187 server, task->tk_status, &exception);
5188 if (exception.retry) {
5189 rpc_restart_call_prepare(task);
5190 return -EAGAIN;
5191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005193
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005195 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197}
5198
Trond Myklebust5521abf2013-03-16 20:54:34 -04005199static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005200 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005201{
5202
5203 if (!nfs4_error_stateid_expired(task->tk_status) ||
5204 nfs4_stateid_is_current(&args->stateid,
5205 args->context,
5206 args->lock_context,
5207 FMODE_READ))
5208 return false;
5209 rpc_restart_call_prepare(task);
5210 return true;
5211}
5212
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005213static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005214{
5215
5216 dprintk("--> %s\n", __func__);
5217
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005218 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005219 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005220 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005221 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005222 if (task->tk_status > 0)
5223 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005224 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5225 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005226}
5227
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005228static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5229 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005231 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005232 if (!hdr->pgio_done_cb)
5233 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005234 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005235 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236}
5237
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005238static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5239 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005240{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005241 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005242 &hdr->args.seq_args,
5243 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005244 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005245 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005246 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5247 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005248 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005249 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005250 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005251 return -EIO;
5252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253}
5254
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005255static int nfs4_write_done_cb(struct rpc_task *task,
5256 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005258 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005259
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005260 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005261 if (task->tk_status < 0) {
5262 struct nfs4_exception exception = {
5263 .inode = hdr->inode,
5264 .state = hdr->args.context->state,
5265 .stateid = &hdr->args.stateid,
5266 };
5267 task->tk_status = nfs4_async_handle_exception(task,
5268 NFS_SERVER(inode), task->tk_status,
5269 &exception);
5270 if (exception.retry) {
5271 rpc_restart_call_prepare(task);
5272 return -EAGAIN;
5273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005275 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005276 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005277 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005278 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280}
5281
Trond Myklebust5521abf2013-03-16 20:54:34 -04005282static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005283 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005284{
5285
5286 if (!nfs4_error_stateid_expired(task->tk_status) ||
5287 nfs4_stateid_is_current(&args->stateid,
5288 args->context,
5289 args->lock_context,
5290 FMODE_WRITE))
5291 return false;
5292 rpc_restart_call_prepare(task);
5293 return true;
5294}
5295
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005296static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005297{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005298 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005299 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005300 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005301 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005302 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5303 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005304}
5305
Trond Myklebust5a37f852012-04-28 14:55:16 -04005306static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005307bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005308{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005309 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005310 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005311 return false;
5312 /* Otherwise, request attributes if and only if we don't hold
5313 * a delegation
5314 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005315 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005316}
Fred Isamana69aef12011-03-03 15:13:47 +00005317
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005318static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005319 struct rpc_message *msg,
5320 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005322 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005323
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005324 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5325 hdr->args.bitmask = NULL;
5326 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005327 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005328 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005329
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005330 if (!hdr->pgio_done_cb)
5331 hdr->pgio_done_cb = nfs4_write_done_cb;
5332 hdr->res.server = server;
5333 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005335 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005336 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005337 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338}
5339
Fred Isaman0b7c0152012-04-20 14:47:39 -04005340static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5341{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005342 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005343 &data->args.seq_args,
5344 &data->res.seq_res,
5345 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346}
5347
Fred Isaman0b7c0152012-04-20 14:47:39 -04005348static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005351
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005352 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005353 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5354 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005355 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005356 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359}
5360
Fred Isaman0b7c0152012-04-20 14:47:39 -04005361static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005362{
5363 if (!nfs4_sequence_done(task, &data->res.seq_res))
5364 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005365 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005366}
5367
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005368static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5369 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005371 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005372
Fred Isaman0b7c0152012-04-20 14:47:39 -04005373 if (data->commit_done_cb == NULL)
5374 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005375 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005376 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005377 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005378 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379}
5380
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005381static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5382 struct nfs_commitres *res)
5383{
5384 struct inode *dst_inode = file_inode(dst);
5385 struct nfs_server *server = NFS_SERVER(dst_inode);
5386 struct rpc_message msg = {
5387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5388 .rpc_argp = args,
5389 .rpc_resp = res,
5390 };
5391
5392 args->fh = NFS_FH(dst_inode);
5393 return nfs4_call_sync(server->client, server, &msg,
5394 &args->seq_args, &res->seq_res, 1);
5395}
5396
5397int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5398{
5399 struct nfs_commitargs args = {
5400 .offset = offset,
5401 .count = count,
5402 };
5403 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5404 struct nfs4_exception exception = { };
5405 int status;
5406
5407 do {
5408 status = _nfs4_proc_commit(dst, &args, res);
5409 status = nfs4_handle_exception(dst_server, status, &exception);
5410 } while (exception.retry);
5411
5412 return status;
5413}
5414
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005415struct nfs4_renewdata {
5416 struct nfs_client *client;
5417 unsigned long timestamp;
5418};
5419
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420/*
5421 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5422 * standalone procedure for queueing an asynchronous RENEW.
5423 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005424static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005425{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005426 struct nfs4_renewdata *data = calldata;
5427 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005428
Elena Reshetova212bf412017-10-20 12:53:38 +03005429 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005430 nfs4_schedule_state_renewal(clp);
5431 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005432 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005433}
5434
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005435static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005437 struct nfs4_renewdata *data = calldata;
5438 struct nfs_client *clp = data->client;
5439 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005441 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005442 switch (task->tk_status) {
5443 case 0:
5444 break;
5445 case -NFS4ERR_LEASE_MOVED:
5446 nfs4_schedule_lease_moved_recovery(clp);
5447 break;
5448 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005449 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005450 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5451 return;
5452 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005453 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005454 return;
5455 }
5456 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005458 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459}
5460
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005461static const struct rpc_call_ops nfs4_renew_ops = {
5462 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005463 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005464};
5465
NeilBrowna52458b2018-12-03 11:30:31 +11005466static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467{
5468 struct rpc_message msg = {
5469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5470 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005471 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005473 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005475 if (renew_flags == 0)
5476 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005477 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005478 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005479 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005480 if (data == NULL) {
5481 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005482 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005483 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005484 data->client = clp;
5485 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005486 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005487 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488}
5489
NeilBrowna52458b2018-12-03 11:30:31 +11005490static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491{
5492 struct rpc_message msg = {
5493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5494 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005495 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 };
5497 unsigned long now = jiffies;
5498 int status;
5499
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005500 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 if (status < 0)
5502 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005503 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 return 0;
5505}
5506
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005507static inline int nfs4_server_supports_acls(struct nfs_server *server)
5508{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005509 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005510}
5511
Trond Myklebust21f498c2012-08-24 10:59:25 -04005512/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5513 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005514 * the stack.
5515 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005516#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005517
Neil Hormane9e3d722011-03-04 19:26:03 -05005518static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005519 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005520{
5521 struct page *newpage, **spages;
5522 int rc = 0;
5523 size_t len;
5524 spages = pages;
5525
5526 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005527 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005528 newpage = alloc_page(GFP_KERNEL);
5529
5530 if (newpage == NULL)
5531 goto unwind;
5532 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005533 buf += len;
5534 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005535 *pages++ = newpage;
5536 rc++;
5537 } while (buflen != 0);
5538
5539 return rc;
5540
5541unwind:
5542 for(; rc > 0; rc--)
5543 __free_page(spages[rc-1]);
5544 return -ENOMEM;
5545}
5546
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005547struct nfs4_cached_acl {
5548 int cached;
5549 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005550 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005551};
5552
5553static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005554{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005555 struct nfs_inode *nfsi = NFS_I(inode);
5556
5557 spin_lock(&inode->i_lock);
5558 kfree(nfsi->nfs4_acl);
5559 nfsi->nfs4_acl = acl;
5560 spin_unlock(&inode->i_lock);
5561}
5562
5563static void nfs4_zap_acl_attr(struct inode *inode)
5564{
5565 nfs4_set_cached_acl(inode, NULL);
5566}
5567
5568static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5569{
5570 struct nfs_inode *nfsi = NFS_I(inode);
5571 struct nfs4_cached_acl *acl;
5572 int ret = -ENOENT;
5573
5574 spin_lock(&inode->i_lock);
5575 acl = nfsi->nfs4_acl;
5576 if (acl == NULL)
5577 goto out;
5578 if (buf == NULL) /* user is just asking for length */
5579 goto out_len;
5580 if (acl->cached == 0)
5581 goto out;
5582 ret = -ERANGE; /* see getxattr(2) man page */
5583 if (acl->len > buflen)
5584 goto out;
5585 memcpy(buf, acl->data, acl->len);
5586out_len:
5587 ret = acl->len;
5588out:
5589 spin_unlock(&inode->i_lock);
5590 return ret;
5591}
5592
Sachin Prabhu5794d212012-04-17 14:36:40 +01005593static 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 +00005594{
5595 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005596 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005597
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005598 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005599 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005600 if (acl == NULL)
5601 goto out;
5602 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005603 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005604 } else {
5605 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5606 if (acl == NULL)
5607 goto out;
5608 acl->cached = 0;
5609 }
5610 acl->len = acl_len;
5611out:
5612 nfs4_set_cached_acl(inode, acl);
5613}
5614
Andy Adamsonbf118a32011-12-07 11:55:27 -05005615/*
5616 * The getxattr API returns the required buffer length when called with a
5617 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5618 * the required buf. On a NULL buf, we send a page of data to the server
5619 * guessing that the ACL request can be serviced by a page. If so, we cache
5620 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5621 * the cache. If not so, we throw away the page, and cache the required
5622 * length. The next getxattr call will then produce another round trip to
5623 * the server, this time with the input buf of the required size.
5624 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005625static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005626{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005627 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005628 struct nfs_getaclargs args = {
5629 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005630 .acl_len = buflen,
5631 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005632 struct nfs_getaclres res = {
5633 .acl_len = buflen,
5634 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005635 struct rpc_message msg = {
5636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5637 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005638 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005639 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005640 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005641 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005642 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005643
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005644 if (buflen == 0)
5645 buflen = server->rsize;
5646
5647 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5648 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5649 if (!pages)
5650 return -ENOMEM;
5651
5652 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005653
Andy Adamsonbf118a32011-12-07 11:55:27 -05005654 for (i = 0; i < npages; i++) {
5655 pages[i] = alloc_page(GFP_KERNEL);
5656 if (!pages[i])
5657 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005658 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005659
5660 /* for decoding across pages */
5661 res.acl_scratch = alloc_page(GFP_KERNEL);
5662 if (!res.acl_scratch)
5663 goto out_free;
5664
Andy Adamsonbf118a32011-12-07 11:55:27 -05005665 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005666
Peng Taode040be2012-01-10 22:42:47 +08005667 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005668 __func__, buf, buflen, npages, args.acl_len);
5669 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5670 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005671 if (ret)
5672 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005673
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005674 /* Handle the case where the passed-in buffer is too short */
5675 if (res.acl_flags & NFS4_ACL_TRUNC) {
5676 /* Did the user only issue a request for the acl length? */
5677 if (buf == NULL)
5678 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005679 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005680 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005681 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005682 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005683 if (buf) {
5684 if (res.acl_len > buflen) {
5685 ret = -ERANGE;
5686 goto out_free;
5687 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005688 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005689 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005690out_ok:
5691 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005692out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005693 for (i = 0; i < npages; i++)
5694 if (pages[i])
5695 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005696 if (res.acl_scratch)
5697 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005698 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005699 return ret;
5700}
5701
Trond Myklebust16b42892006-08-24 12:27:15 -04005702static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5703{
Trond Myklebust0688e642019-04-07 13:59:09 -04005704 struct nfs4_exception exception = {
5705 .interruptible = true,
5706 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005707 ssize_t ret;
5708 do {
5709 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005710 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005711 if (ret >= 0)
5712 break;
5713 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5714 } while (exception.retry);
5715 return ret;
5716}
5717
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005718static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5719{
5720 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005721 int ret;
5722
5723 if (!nfs4_server_supports_acls(server))
5724 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005725 ret = nfs_revalidate_inode(server, inode);
5726 if (ret < 0)
5727 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005728 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5729 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005730 ret = nfs4_read_cached_acl(inode, buf, buflen);
5731 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005732 /* -ENOENT is returned if there is no ACL or if there is an ACL
5733 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005734 return ret;
5735 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005736}
5737
Trond Myklebust16b42892006-08-24 12:27:15 -04005738static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005739{
5740 struct nfs_server *server = NFS_SERVER(inode);
5741 struct page *pages[NFS4ACL_MAXPAGES];
5742 struct nfs_setaclargs arg = {
5743 .fh = NFS_FH(inode),
5744 .acl_pages = pages,
5745 .acl_len = buflen,
5746 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005747 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005748 struct rpc_message msg = {
5749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5750 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005751 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005752 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005753 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005754 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005755
5756 if (!nfs4_server_supports_acls(server))
5757 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005758 if (npages > ARRAY_SIZE(pages))
5759 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005760 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005761 if (i < 0)
5762 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005763 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005764 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005765
5766 /*
5767 * Free each page after tx, so the only ref left is
5768 * held by the network stack
5769 */
5770 for (; i > 0; i--)
5771 put_page(pages[i-1]);
5772
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005773 /*
5774 * Acl update can result in inode attribute update.
5775 * so mark the attribute cache invalid.
5776 */
5777 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005778 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005779 | NFS_INO_INVALID_CTIME
5780 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005781 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005782 nfs_access_zap_cache(inode);
5783 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005784 return ret;
5785}
5786
Trond Myklebust16b42892006-08-24 12:27:15 -04005787static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5788{
5789 struct nfs4_exception exception = { };
5790 int err;
5791 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005792 err = __nfs4_proc_set_acl(inode, buf, buflen);
5793 trace_nfs4_set_acl(inode, err);
5794 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005795 &exception);
5796 } while (exception.retry);
5797 return err;
5798}
5799
David Quigleyaa9c2662013-05-22 12:50:44 -04005800#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5801static int _nfs4_get_security_label(struct inode *inode, void *buf,
5802 size_t buflen)
5803{
5804 struct nfs_server *server = NFS_SERVER(inode);
5805 struct nfs_fattr fattr;
5806 struct nfs4_label label = {0, 0, buflen, buf};
5807
5808 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005809 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005810 .fh = NFS_FH(inode),
5811 .bitmask = bitmask,
5812 };
5813 struct nfs4_getattr_res res = {
5814 .fattr = &fattr,
5815 .label = &label,
5816 .server = server,
5817 };
5818 struct rpc_message msg = {
5819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005820 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005821 .rpc_resp = &res,
5822 };
5823 int ret;
5824
5825 nfs_fattr_init(&fattr);
5826
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005827 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005828 if (ret)
5829 return ret;
5830 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5831 return -ENOENT;
5832 if (buflen < label.len)
5833 return -ERANGE;
5834 return 0;
5835}
5836
5837static int nfs4_get_security_label(struct inode *inode, void *buf,
5838 size_t buflen)
5839{
Trond Myklebust0688e642019-04-07 13:59:09 -04005840 struct nfs4_exception exception = {
5841 .interruptible = true,
5842 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005843 int err;
5844
5845 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5846 return -EOPNOTSUPP;
5847
5848 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005849 err = _nfs4_get_security_label(inode, buf, buflen);
5850 trace_nfs4_get_security_label(inode, err);
5851 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005852 &exception);
5853 } while (exception.retry);
5854 return err;
5855}
5856
5857static int _nfs4_do_set_security_label(struct inode *inode,
5858 struct nfs4_label *ilabel,
5859 struct nfs_fattr *fattr,
5860 struct nfs4_label *olabel)
5861{
5862
5863 struct iattr sattr = {0};
5864 struct nfs_server *server = NFS_SERVER(inode);
5865 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005866 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005867 .fh = NFS_FH(inode),
5868 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005869 .server = server,
5870 .bitmask = bitmask,
5871 .label = ilabel,
5872 };
5873 struct nfs_setattrres res = {
5874 .fattr = fattr,
5875 .label = olabel,
5876 .server = server,
5877 };
5878 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5880 .rpc_argp = &arg,
5881 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005882 };
5883 int status;
5884
Jeff Layton12207f62013-11-01 10:49:32 -04005885 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005886
Jeff Layton12207f62013-11-01 10:49:32 -04005887 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005888 if (status)
5889 dprintk("%s failed: %d\n", __func__, status);
5890
5891 return status;
5892}
5893
5894static int nfs4_do_set_security_label(struct inode *inode,
5895 struct nfs4_label *ilabel,
5896 struct nfs_fattr *fattr,
5897 struct nfs4_label *olabel)
5898{
5899 struct nfs4_exception exception = { };
5900 int err;
5901
5902 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005903 err = _nfs4_do_set_security_label(inode, ilabel,
5904 fattr, olabel);
5905 trace_nfs4_set_security_label(inode, err);
5906 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005907 &exception);
5908 } while (exception.retry);
5909 return err;
5910}
5911
5912static int
Al Viro59301222016-05-27 10:19:30 -04005913nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005914{
5915 struct nfs4_label ilabel, *olabel = NULL;
5916 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005917 int status;
5918
5919 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5920 return -EOPNOTSUPP;
5921
5922 nfs_fattr_init(&fattr);
5923
5924 ilabel.pi = 0;
5925 ilabel.lfs = 0;
5926 ilabel.label = (char *)buf;
5927 ilabel.len = buflen;
5928
David Quigleyaa9c2662013-05-22 12:50:44 -04005929 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5930 if (IS_ERR(olabel)) {
5931 status = -PTR_ERR(olabel);
5932 goto out;
5933 }
5934
5935 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5936 if (status == 0)
5937 nfs_setsecurity(inode, &fattr, olabel);
5938
5939 nfs4_label_free(olabel);
5940out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005941 return status;
5942}
5943#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5944
5945
Chuck Leverf0920752012-05-21 22:45:41 -04005946static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5947 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005948{
5949 __be32 verf[2];
5950
Chuck Lever2c820d92012-05-21 22:45:33 -04005951 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5952 /* An impossible timestamp guarantees this value
5953 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005954 verf[0] = cpu_to_be32(U32_MAX);
5955 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005956 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005957 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005958 u64 ns = ktime_to_ns(nn->boot_time);
5959
5960 verf[0] = cpu_to_be32(ns >> 32);
5961 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005962 }
Chuck Levercd937102012-03-02 17:14:31 -05005963 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5964}
5965
Jeff Laytona3192682015-06-09 19:43:59 -04005966static int
5967nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005968{
Jeff Laytona3192682015-06-09 19:43:59 -04005969 size_t len;
5970 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005971
Trond Myklebustceb3a162015-01-03 15:16:04 -05005972 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005973 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005974
Jeff Laytona3192682015-06-09 19:43:59 -04005975 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005976 len = 14 +
5977 strlen(clp->cl_rpcclient->cl_nodename) +
5978 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005979 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005980 1;
5981 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005982 if (nfs4_client_id_uniquifier[0] != '\0')
5983 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005984 if (len > NFS4_OPAQUE_LIMIT + 1)
5985 return -EINVAL;
5986
5987 /*
5988 * Since this string is allocated at mount time, and held until the
5989 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5990 * about a memory-reclaim deadlock.
5991 */
5992 str = kmalloc(len, GFP_KERNEL);
5993 if (!str)
5994 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005995
Chuck Levere984a552012-09-14 17:24:21 -04005996 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005997 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005998 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005999 clp->cl_rpcclient->cl_nodename,
6000 nfs4_client_id_uniquifier,
6001 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006002 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006003 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006004 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006005 clp->cl_rpcclient->cl_nodename,
6006 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006007 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006008 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006009
Jeff Laytona3192682015-06-09 19:43:59 -04006010 clp->cl_owner_id = str;
6011 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006012}
6013
Jeff Layton873e3852015-06-09 19:44:00 -04006014static int
6015nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006016{
Jeff Layton873e3852015-06-09 19:44:00 -04006017 size_t len;
6018 char *str;
6019
6020 len = 10 + 10 + 1 + 10 + 1 +
6021 strlen(nfs4_client_id_uniquifier) + 1 +
6022 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6023
6024 if (len > NFS4_OPAQUE_LIMIT + 1)
6025 return -EINVAL;
6026
6027 /*
6028 * Since this string is allocated at mount time, and held until the
6029 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6030 * about a memory-reclaim deadlock.
6031 */
6032 str = kmalloc(len, GFP_KERNEL);
6033 if (!str)
6034 return -ENOMEM;
6035
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006036 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04006037 clp->rpc_ops->version, clp->cl_minorversion,
6038 nfs4_client_id_uniquifier,
6039 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006040 clp->cl_owner_id = str;
6041 return 0;
6042}
6043
6044static int
6045nfs4_init_uniform_client_string(struct nfs_client *clp)
6046{
Jeff Layton873e3852015-06-09 19:44:00 -04006047 size_t len;
6048 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006049
6050 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006051 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006052
6053 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04006054 return nfs4_init_uniquifier_client_string(clp);
6055
6056 len = 10 + 10 + 1 + 10 + 1 +
6057 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6058
6059 if (len > NFS4_OPAQUE_LIMIT + 1)
6060 return -EINVAL;
6061
6062 /*
6063 * Since this string is allocated at mount time, and held until the
6064 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6065 * about a memory-reclaim deadlock.
6066 */
6067 str = kmalloc(len, GFP_KERNEL);
6068 if (!str)
6069 return -ENOMEM;
6070
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006071 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04006072 clp->rpc_ops->version, clp->cl_minorversion,
6073 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006074 clp->cl_owner_id = str;
6075 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006076}
6077
Chuck Lever706cb8d2014-03-12 12:51:47 -04006078/*
6079 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6080 * services. Advertise one based on the address family of the
6081 * clientaddr.
6082 */
6083static unsigned int
6084nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6085{
6086 if (strchr(clp->cl_ipaddr, ':') != NULL)
6087 return scnprintf(buf, len, "tcp6");
6088 else
6089 return scnprintf(buf, len, "tcp");
6090}
6091
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006092static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6093{
6094 struct nfs4_setclientid *sc = calldata;
6095
6096 if (task->tk_status == 0)
6097 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6098}
6099
6100static const struct rpc_call_ops nfs4_setclientid_ops = {
6101 .rpc_call_done = nfs4_setclientid_done,
6102};
6103
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006104/**
6105 * nfs4_proc_setclientid - Negotiate client ID
6106 * @clp: state data structure
6107 * @program: RPC program for NFSv4 callback service
6108 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006109 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006110 * @res: where to place the result
6111 *
6112 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6113 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006114int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006115 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006116 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117{
6118 nfs4_verifier sc_verifier;
6119 struct nfs4_setclientid setclientid = {
6120 .sc_verifier = &sc_verifier,
6121 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006122 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 };
6124 struct rpc_message msg = {
6125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6126 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006127 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006128 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006130 struct rpc_task_setup task_setup_data = {
6131 .rpc_client = clp->cl_rpcclient,
6132 .rpc_message = &msg,
6133 .callback_ops = &nfs4_setclientid_ops,
6134 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006135 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006136 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006137 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006138 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
Chuck Leverde734832012-07-11 16:30:50 -04006140 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006141 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006142
6143 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6144 status = nfs4_init_uniform_client_string(clp);
6145 else
Jeff Laytona3192682015-06-09 19:43:59 -04006146 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006147
6148 if (status)
6149 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006150
Chuck Leverde734832012-07-11 16:30:50 -04006151 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006152 setclientid.sc_netid_len =
6153 nfs4_init_callback_netid(clp,
6154 setclientid.sc_netid,
6155 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006156 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006157 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 clp->cl_ipaddr, port >> 8, port & 255);
6159
Jeff Layton3a6bb732015-06-09 19:43:57 -04006160 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006161 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006162 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006163
6164 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006165 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006166 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006167 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6168 put_rpccred(setclientid.sc_cred);
6169 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006170
6171 if (status == 0)
6172 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006173out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006174 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006175 dprintk("NFS reply setclientid: %d\n", status);
6176 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177}
6178
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006179/**
6180 * nfs4_proc_setclientid_confirm - Confirm client ID
6181 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006182 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006183 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006184 *
6185 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6186 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006187int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006188 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006189 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 struct rpc_message msg = {
6192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006193 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006194 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 int status;
6197
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006198 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6199 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6200 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006201 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6202 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006203 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006204 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 return status;
6206}
6207
Trond Myklebustfe650402006-01-03 09:55:18 +01006208struct nfs4_delegreturndata {
6209 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006210 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006211 struct nfs_fh fh;
6212 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006213 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006214 struct {
6215 struct nfs4_layoutreturn_args arg;
6216 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006217 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006218 u32 roc_barrier;
6219 bool roc;
6220 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006221 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006222 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006223 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006224};
6225
Trond Myklebustfe650402006-01-03 09:55:18 +01006226static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6227{
6228 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006229 struct nfs4_exception exception = {
6230 .inode = data->inode,
6231 .stateid = &data->stateid,
6232 };
Andy Adamson938e1012009-04-01 09:22:28 -04006233
Trond Myklebust14516c32010-07-31 14:29:06 -04006234 if (!nfs4_sequence_done(task, &data->res.seq_res))
6235 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006236
Trond Myklebustca8acf82013-08-13 10:36:56 -04006237 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006238
6239 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04006240 if (pnfs_roc_done(task, data->inode,
6241 &data->args.lr_args,
6242 &data->res.lr_res,
6243 &data->res.lr_ret) == -EAGAIN)
6244 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006245
Ricardo Labiaga79708862009-12-07 09:23:21 -05006246 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006247 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006248 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006249 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006250 case -NFS4ERR_ADMIN_REVOKED:
6251 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006252 case -NFS4ERR_EXPIRED:
6253 nfs4_free_revoked_stateid(data->res.server,
6254 data->args.stateid,
6255 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006256 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006257 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006258 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006259 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006260 task->tk_status = 0;
6261 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006262 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006263 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6264 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006265 if (data->args.bitmask) {
6266 data->args.bitmask = NULL;
6267 data->res.fattr = NULL;
6268 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006269 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006270 case -NFS4ERR_ACCESS:
6271 if (data->args.bitmask) {
6272 data->args.bitmask = NULL;
6273 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006274 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006275 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006276 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006277 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006278 task->tk_status = nfs4_async_handle_exception(task,
6279 data->res.server, task->tk_status,
6280 &exception);
6281 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006282 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006283 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006284 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006285 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006286 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006287out_restart:
6288 task->tk_status = 0;
6289 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006290}
6291
6292static void nfs4_delegreturn_release(void *calldata)
6293{
Peng Tao039b7562014-07-03 13:05:02 +08006294 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006295 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006296
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006297 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006298 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006299 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6300 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006301 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006302 nfs_iput_and_deactive(inode);
6303 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006304 kfree(calldata);
6305}
6306
Andy Adamson938e1012009-04-01 09:22:28 -04006307static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6308{
6309 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006310 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006311
6312 d_data = (struct nfs4_delegreturndata *)data;
6313
Trond Myklebust5326de92019-11-13 09:39:36 +01006314 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6315 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006316 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006317 }
Peng Tao500d7012015-09-22 11:35:22 +08006318
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006319 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6320 if (lo && !pnfs_layout_is_valid(lo)) {
6321 d_data->args.lr_args = NULL;
6322 d_data->res.lr_res = NULL;
6323 }
6324
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006325 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006326 &d_data->args.seq_args,
6327 &d_data->res.seq_res,
6328 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006329}
Andy Adamson938e1012009-04-01 09:22:28 -04006330
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006331static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006332 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006333 .rpc_call_done = nfs4_delegreturn_done,
6334 .rpc_release = nfs4_delegreturn_release,
6335};
6336
NeilBrowna52458b2018-12-03 11:30:31 +11006337static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006338{
6339 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006340 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006341 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006342 struct rpc_message msg = {
6343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6344 .rpc_cred = cred,
6345 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006346 struct rpc_task_setup task_setup_data = {
6347 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006348 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006349 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebust61296502020-02-07 19:38:12 -05006350 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006351 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006352 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006353
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006354 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006355 if (data == NULL)
6356 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006357 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006358
6359 nfs4_state_protect(server->nfs_client,
6360 NFS_SP4_MACH_CRED_CLEANUP,
6361 &task_setup_data.rpc_client, &msg);
6362
Trond Myklebustfe650402006-01-03 09:55:18 +01006363 data->args.fhandle = &data->fh;
6364 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006365 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006366 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006367 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006368 data->res.fattr = &data->fattr;
6369 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006370 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006371 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006372 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006373 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006374 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006375 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006376 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006377 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006378 if (data->lr.roc) {
6379 data->args.lr_args = &data->lr.arg;
6380 data->res.lr_res = &data->lr.res;
6381 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006382 } else if (data->lr.roc) {
6383 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6384 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006385 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006386
Trond Myklebustc970aa82007-07-14 15:39:59 -04006387 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006388 msg.rpc_argp = &data->args;
6389 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006390 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006391 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006392 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006393 if (!issync)
6394 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006395 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006396 if (status != 0)
6397 goto out;
6398 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006399out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006400 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006401 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402}
6403
NeilBrowna52458b2018-12-03 11:30:31 +11006404int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405{
6406 struct nfs_server *server = NFS_SERVER(inode);
6407 struct nfs4_exception exception = { };
6408 int err;
6409 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006410 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006411 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 switch (err) {
6413 case -NFS4ERR_STALE_STATEID:
6414 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 case 0:
6416 return 0;
6417 }
6418 err = nfs4_handle_exception(server, err, &exception);
6419 } while (exception.retry);
6420 return err;
6421}
6422
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6424{
6425 struct inode *inode = state->inode;
6426 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006427 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006428 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006430 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006432 struct nfs_lockt_res res = {
6433 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 };
6435 struct rpc_message msg = {
6436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006437 .rpc_argp = &arg,
6438 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 .rpc_cred = state->owner->so_cred,
6440 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 struct nfs4_lock_state *lsp;
6442 int status;
6443
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006444 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006445 status = nfs4_set_lock_state(state, request);
6446 if (status != 0)
6447 goto out;
6448 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006449 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006450 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006451 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006452 switch (status) {
6453 case 0:
6454 request->fl_type = F_UNLCK;
6455 break;
6456 case -NFS4ERR_DENIED:
6457 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006459 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006460 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006461out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 return status;
6463}
6464
6465static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6466{
Trond Myklebust0688e642019-04-07 13:59:09 -04006467 struct nfs4_exception exception = {
6468 .interruptible = true,
6469 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 int err;
6471
6472 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006473 err = _nfs4_proc_getlk(state, cmd, request);
6474 trace_nfs4_get_lock(request, state, cmd, err);
6475 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 &exception);
6477 } while (exception.retry);
6478 return err;
6479}
6480
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006481/*
6482 * Update the seqid of a lock stateid after receiving
6483 * NFS4ERR_OLD_STATEID
6484 */
6485static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6486 struct nfs4_lock_state *lsp)
6487{
6488 struct nfs4_state *state = lsp->ls_state;
6489 bool ret = false;
6490
6491 spin_lock(&state->state_lock);
6492 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6493 goto out;
6494 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6495 nfs4_stateid_seqid_inc(dst);
6496 else
6497 dst->seqid = lsp->ls_stateid.seqid;
6498 ret = true;
6499out:
6500 spin_unlock(&state->state_lock);
6501 return ret;
6502}
6503
6504static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6505 struct nfs4_lock_state *lsp)
6506{
6507 struct nfs4_state *state = lsp->ls_state;
6508 bool ret;
6509
6510 spin_lock(&state->state_lock);
6511 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6512 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6513 spin_unlock(&state->state_lock);
6514 return ret;
6515}
6516
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006517struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006518 struct nfs_locku_args arg;
6519 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006520 struct nfs4_lock_state *lsp;
6521 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006522 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006523 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006524 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006525 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006526};
6527
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006528static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6529 struct nfs_open_context *ctx,
6530 struct nfs4_lock_state *lsp,
6531 struct nfs_seqid *seqid)
6532{
6533 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006534 struct nfs4_state *state = lsp->ls_state;
6535 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006536
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006537 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006538 if (p == NULL)
6539 return NULL;
6540 p->arg.fh = NFS_FH(inode);
6541 p->arg.fl = &p->fl;
6542 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006543 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006544 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006545 /* Ensure we don't close file until we're done freeing locks! */
6546 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006547 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006548 locks_init_lock(&p->fl);
6549 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006550 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006551 spin_lock(&state->state_lock);
6552 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6553 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006554 return p;
6555}
6556
Trond Myklebust06f814a2006-01-03 09:55:07 +01006557static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006558{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006559 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006560 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006561 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006562 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006563 put_nfs_open_context(calldata->ctx);
6564 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006565}
6566
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006567static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006568{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006569 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006570 struct nfs4_exception exception = {
6571 .inode = calldata->lsp->ls_state->inode,
6572 .stateid = &calldata->arg.stateid,
6573 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006574
Trond Myklebust14516c32010-07-31 14:29:06 -04006575 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6576 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006577 switch (task->tk_status) {
6578 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006579 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006580 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006581 if (nfs4_update_lock_stateid(calldata->lsp,
6582 &calldata->res.stateid))
6583 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006584 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006585 case -NFS4ERR_ADMIN_REVOKED:
6586 case -NFS4ERR_EXPIRED:
6587 nfs4_free_revoked_stateid(calldata->server,
6588 &calldata->arg.stateid,
6589 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006590 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006591 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006592 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006593 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6594 calldata->lsp))
6595 rpc_restart_call_prepare(task);
6596 break;
6597 case -NFS4ERR_OLD_STATEID:
6598 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6599 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006600 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006601 break;
6602 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006603 task->tk_status = nfs4_async_handle_exception(task,
6604 calldata->server, task->tk_status,
6605 &exception);
6606 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006607 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006608 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006609 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006610}
6611
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006612static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006613{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006614 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006616 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6617 nfs_async_iocounter_wait(task, calldata->l_ctx))
6618 return;
6619
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006620 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006621 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006622 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006623 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006624 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006625 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006626 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006627 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006628 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006629 &calldata->res.seq_res,
6630 task) != 0)
6631 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006632 return;
6633out_no_action:
6634 task->tk_action = NULL;
6635out_wait:
6636 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637}
6638
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006639static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006640 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006641 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006642 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006643};
6644
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006645static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6646 struct nfs_open_context *ctx,
6647 struct nfs4_lock_state *lsp,
6648 struct nfs_seqid *seqid)
6649{
6650 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006651 struct rpc_message msg = {
6652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6653 .rpc_cred = ctx->cred,
6654 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006655 struct rpc_task_setup task_setup_data = {
6656 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006657 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006658 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006659 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006660 .flags = RPC_TASK_ASYNC,
6661 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006662
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006663 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6664 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6665
Frank Filz137d6ac2007-07-09 15:32:29 -07006666 /* Ensure this is an unlock - when canceling a lock, the
6667 * canceled lock is passed in, and it won't be an unlock.
6668 */
6669 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006670 if (fl->fl_flags & FL_CLOSE)
6671 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006672
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006673 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6674 if (data == NULL) {
6675 nfs_free_seqid(seqid);
6676 return ERR_PTR(-ENOMEM);
6677 }
6678
Anna Schumakerfba83f32018-05-04 16:22:50 -04006679 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006680 msg.rpc_argp = &data->arg;
6681 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006682 task_setup_data.callback_data = data;
6683 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006684}
6685
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6687{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006688 struct inode *inode = state->inode;
6689 struct nfs4_state_owner *sp = state->owner;
6690 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006691 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006692 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006693 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006694 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006695 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006696 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697
Trond Myklebust9b073572006-06-29 16:38:34 -04006698 status = nfs4_set_lock_state(state, request);
6699 /* Unlock _before_ we do the RPC call */
6700 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006701 /* Exclude nfs_delegation_claim_locks() */
6702 mutex_lock(&sp->so_delegreturn_mutex);
6703 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006704 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006705 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006706 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006707 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006708 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006709 }
6710 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006711 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006712 if (status != 0)
6713 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006714 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006715 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006716 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6717 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006718 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6719 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006720 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006721 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006722 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006723 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006724 status = PTR_ERR(task);
6725 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006726 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006727 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006728 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006729out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006730 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006731 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006732 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733}
6734
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006735struct nfs4_lockdata {
6736 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006737 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006738 struct nfs4_lock_state *lsp;
6739 struct nfs_open_context *ctx;
6740 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006741 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006742 int rpc_status;
6743 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006744 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006745};
6746
6747static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006748 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6749 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006750{
6751 struct nfs4_lockdata *p;
6752 struct inode *inode = lsp->ls_state->inode;
6753 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006754 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006755
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006756 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006757 if (p == NULL)
6758 return NULL;
6759
6760 p->arg.fh = NFS_FH(inode);
6761 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006762 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006763 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006764 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006765 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6766 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006767 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006768 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006769 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006770 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006771 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006772 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006773 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006774 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006775 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006776 locks_init_lock(&p->fl);
6777 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006778 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006779out_free_seqid:
6780 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006781out_free:
6782 kfree(p);
6783 return NULL;
6784}
6785
6786static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6787{
6788 struct nfs4_lockdata *data = calldata;
6789 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
Harvey Harrison3110ff82008-05-02 13:42:44 -07006791 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006792 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006793 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006794 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006795 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006796 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006797 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006798 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006799 nfs4_stateid_copy(&data->arg.open_stateid,
6800 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006801 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006802 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006803 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006804 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006805 nfs4_stateid_copy(&data->arg.lock_stateid,
6806 &data->lsp->ls_stateid);
6807 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006808 if (!nfs4_valid_open_stateid(state)) {
6809 data->rpc_status = -EBADF;
6810 task->tk_action = NULL;
6811 goto out_release_open_seqid;
6812 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006813 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006814 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006815 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006816 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006817 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006818 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006819out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006820 nfs_release_seqid(data->arg.open_seqid);
6821out_release_lock_seqid:
6822 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006823out_wait:
6824 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006825 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006826}
6827
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006828static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6829{
6830 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006831 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006832
Harvey Harrison3110ff82008-05-02 13:42:44 -07006833 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006834
Trond Myklebust14516c32010-07-31 14:29:06 -04006835 if (!nfs4_sequence_done(task, &data->res.seq_res))
6836 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006837
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006838 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006839 switch (task->tk_status) {
6840 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006841 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006842 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006843 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006844 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006845 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006846 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006847 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006848 if (data->arg.new_lock_owner != 0) {
6849 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6850 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6851 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006852 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6853 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006854 break;
6855 case -NFS4ERR_BAD_STATEID:
6856 case -NFS4ERR_OLD_STATEID:
6857 case -NFS4ERR_STALE_STATEID:
6858 case -NFS4ERR_EXPIRED:
6859 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006860 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006861 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006862 goto out_restart;
6863 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006864 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006865 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006866 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006867out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006868 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006869 return;
6870out_restart:
6871 if (!data->cancelled)
6872 rpc_restart_call_prepare(task);
6873 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006874}
6875
6876static void nfs4_lock_release(void *calldata)
6877{
6878 struct nfs4_lockdata *data = calldata;
6879
Harvey Harrison3110ff82008-05-02 13:42:44 -07006880 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006881 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006882 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006883 struct rpc_task *task;
6884 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6885 data->arg.lock_seqid);
6886 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006887 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006888 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006889 } else
6890 nfs_free_seqid(data->arg.lock_seqid);
6891 nfs4_put_lock_state(data->lsp);
6892 put_nfs_open_context(data->ctx);
6893 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006894 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006895}
6896
6897static const struct rpc_call_ops nfs4_lock_ops = {
6898 .rpc_call_prepare = nfs4_lock_prepare,
6899 .rpc_call_done = nfs4_lock_done,
6900 .rpc_release = nfs4_lock_release,
6901};
6902
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006903static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6904{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006905 switch (error) {
6906 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006907 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006908 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006909 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006910 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006911 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006912 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006913 break;
6914 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006915 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006916 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08006917 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006918}
6919
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006920static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006921{
6922 struct nfs4_lockdata *data;
6923 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006924 struct rpc_message msg = {
6925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6926 .rpc_cred = state->owner->so_cred,
6927 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006928 struct rpc_task_setup task_setup_data = {
6929 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006930 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006931 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006932 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05006933 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006934 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006935 int ret;
6936
Harvey Harrison3110ff82008-05-02 13:42:44 -07006937 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006938 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006939 fl->fl_u.nfs4_fl.owner,
6940 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006941 if (data == NULL)
6942 return -ENOMEM;
6943 if (IS_SETLKW(cmd))
6944 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006945 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6946 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006947 msg.rpc_argp = &data->arg;
6948 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006949 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006950 if (recovery_type > NFS_LOCK_NEW) {
6951 if (recovery_type == NFS_LOCK_RECLAIM)
6952 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006953 } else
6954 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006955 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006956 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006957 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006958 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006959 if (ret == 0) {
6960 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006961 if (ret)
6962 nfs4_handle_setlk_error(data->server, data->lsp,
6963 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006964 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006965 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006966 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006967 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006968 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006969 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970}
6971
6972static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6973{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006974 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006975 struct nfs4_exception exception = {
6976 .inode = state->inode,
6977 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006978 int err;
6979
6980 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006981 /* Cache the lock if possible... */
6982 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6983 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006984 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006985 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006986 break;
6987 nfs4_handle_exception(server, err, &exception);
6988 } while (exception.retry);
6989 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990}
6991
6992static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6993{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006994 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006995 struct nfs4_exception exception = {
6996 .inode = state->inode,
6997 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006998 int err;
6999
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007000 err = nfs4_set_lock_state(state, request);
7001 if (err != 0)
7002 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007003 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007004 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7005 return 0;
7006 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007007 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007008 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7009 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007010 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007011 switch (err) {
7012 default:
7013 goto out;
7014 case -NFS4ERR_GRACE:
7015 case -NFS4ERR_DELAY:
7016 nfs4_handle_exception(server, err, &exception);
7017 err = 0;
7018 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007019 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007020out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007021 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022}
7023
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007024#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007025static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7026{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007027 struct nfs4_lock_state *lsp;
7028 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007029
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007030 status = nfs4_set_lock_state(state, request);
7031 if (status != 0)
7032 return status;
7033 lsp = request->fl_u.nfs4_fl.owner;
7034 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7035 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7036 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007037 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007038}
7039#endif
7040
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7042{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007043 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007044 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007045 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007046 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047
Trond Myklebust01c3b862006-06-29 16:38:39 -04007048 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007049 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007050 if (status < 0)
7051 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007052 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007053 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007054 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007055 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007056 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007057 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007058 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007059 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007060 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007061 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007062 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007063 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007064 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007065 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007066out:
7067 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068 return status;
7069}
7070
7071static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7072{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007073 struct nfs4_exception exception = {
7074 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007075 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007076 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007077 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078 int err;
7079
7080 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007081 err = _nfs4_proc_setlk(state, cmd, request);
7082 if (err == -NFS4ERR_DENIED)
7083 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007085 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 } while (exception.retry);
7087 return err;
7088}
7089
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007090#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7091#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7092
7093static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007094nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7095 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007096{
7097 int status = -ERESTARTSYS;
7098 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7099
7100 while(!signalled()) {
7101 status = nfs4_proc_setlk(state, cmd, request);
7102 if ((status != -EAGAIN) || IS_SETLK(cmd))
7103 break;
7104 freezable_schedule_timeout_interruptible(timeout);
7105 timeout *= 2;
7106 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7107 status = -ERESTARTSYS;
7108 }
7109 return status;
7110}
7111
Jeff Laytona1d617d82016-09-17 18:17:39 -04007112#ifdef CONFIG_NFS_V4_1
7113struct nfs4_lock_waiter {
7114 struct task_struct *task;
7115 struct inode *inode;
7116 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007117};
7118
7119static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007120nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007121{
7122 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007123 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007124
Jeff Layton57174592018-03-18 08:37:03 -04007125 /* NULL key means to wake up everyone */
7126 if (key) {
7127 struct cb_notify_lock_args *cbnl = key;
7128 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7129 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007130
Jeff Layton57174592018-03-18 08:37:03 -04007131 /* Only wake if the callback was for the same owner. */
7132 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7133 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007134
Jeff Layton57174592018-03-18 08:37:03 -04007135 /* Make sure it's for the right inode */
7136 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7137 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007138 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007139
7140 /* override "private" so we can use default_wake_function */
7141 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007142 ret = woken_wake_function(wait, mode, flags, key);
7143 if (ret)
7144 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007145 wait->private = waiter;
7146 return ret;
7147}
7148
7149static int
7150nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7151{
7152 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007153 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7154 struct nfs_server *server = NFS_SERVER(state->inode);
7155 struct nfs_client *clp = server->nfs_client;
7156 wait_queue_head_t *q = &clp->cl_lock_waitq;
7157 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7158 .id = lsp->ls_seqid.owner_id,
7159 .s_dev = server->s_dev };
7160 struct nfs4_lock_waiter waiter = { .task = current,
7161 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007162 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007163 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007164
7165 /* Don't bother with waitqueue if we don't expect a callback */
7166 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7167 return nfs4_retry_setlk_simple(state, cmd, request);
7168
7169 init_wait(&wait);
7170 wait.private = &waiter;
7171 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007172
7173 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007174 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007175 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007176 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7177 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007178 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007179 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007180
7181 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007182 freezer_do_not_count();
7183 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7184 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007185 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007186 }
7187
Jeff Laytona1d617d82016-09-17 18:17:39 -04007188 return status;
7189}
7190#else /* !CONFIG_NFS_V4_1 */
7191static inline int
7192nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7193{
7194 return nfs4_retry_setlk_simple(state, cmd, request);
7195}
7196#endif
7197
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198static int
7199nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7200{
7201 struct nfs_open_context *ctx;
7202 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 int status;
7204
7205 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007206 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 state = ctx->state;
7208
Trond Myklebustd9531262009-07-21 19:22:38 -04007209 if (IS_GETLK(cmd)) {
7210 if (state != NULL)
7211 return nfs4_proc_getlk(state, F_GETLK, request);
7212 return 0;
7213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214
7215 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7216 return -EINVAL;
7217
Trond Myklebustd9531262009-07-21 19:22:38 -04007218 if (request->fl_type == F_UNLCK) {
7219 if (state != NULL)
7220 return nfs4_proc_unlck(state, cmd, request);
7221 return 0;
7222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223
Trond Myklebustd9531262009-07-21 19:22:38 -04007224 if (state == NULL)
7225 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007226
7227 if ((request->fl_flags & FL_POSIX) &&
7228 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7229 return -ENOLCK;
7230
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007231 /*
7232 * Don't rely on the VFS having checked the file open mode,
7233 * since it won't do this for flock() locks.
7234 */
7235 switch (request->fl_type) {
7236 case F_RDLCK:
7237 if (!(filp->f_mode & FMODE_READ))
7238 return -EBADF;
7239 break;
7240 case F_WRLCK:
7241 if (!(filp->f_mode & FMODE_WRITE))
7242 return -EBADF;
7243 }
7244
Jeff Layton1ea67db2016-09-17 18:17:37 -04007245 status = nfs4_set_lock_state(state, request);
7246 if (status != 0)
7247 return status;
7248
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007249 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250}
7251
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007252int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007253{
7254 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007255 int err;
7256
7257 err = nfs4_set_lock_state(state, fl);
7258 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007259 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007260 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007261 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007262}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007263
Trond Myklebustcf470c32012-03-07 13:49:12 -05007264struct nfs_release_lockowner_data {
7265 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007266 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007267 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007268 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007269 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007270};
7271
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007272static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7273{
7274 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007275 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007276 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7277 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007278 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007279 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007280}
7281
7282static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7283{
7284 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007285 struct nfs_server *server = data->server;
7286
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007287 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007288
7289 switch (task->tk_status) {
7290 case 0:
7291 renew_lease(server, data->timestamp);
7292 break;
7293 case -NFS4ERR_STALE_CLIENTID:
7294 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007295 nfs4_schedule_lease_recovery(server->nfs_client);
7296 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007297 case -NFS4ERR_LEASE_MOVED:
7298 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007299 if (nfs4_async_handle_error(task, server,
7300 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007301 rpc_restart_call_prepare(task);
7302 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007303}
7304
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007305static void nfs4_release_lockowner_release(void *calldata)
7306{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007307 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007308 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007309 kfree(calldata);
7310}
7311
Trond Myklebust17280172012-03-11 13:11:00 -04007312static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007313 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7314 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007315 .rpc_release = nfs4_release_lockowner_release,
7316};
7317
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007318static void
7319nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007320{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007321 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007322 struct rpc_message msg = {
7323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7324 };
7325
7326 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007327 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007328
Trond Myklebustcf470c32012-03-07 13:49:12 -05007329 data = kmalloc(sizeof(*data), GFP_NOFS);
7330 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007331 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007332 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007333 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007334 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7335 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7336 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007337
Trond Myklebustcf470c32012-03-07 13:49:12 -05007338 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007339 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007340 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007341 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007342}
7343
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007344#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7345
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007346static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007347 struct dentry *unused, struct inode *inode,
7348 const char *key, const void *buf,
7349 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007350{
Al Viro59301222016-05-27 10:19:30 -04007351 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007352}
7353
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007354static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007355 struct dentry *unused, struct inode *inode,
7356 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007357{
Al Virob2968212016-04-10 20:48:24 -04007358 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007359}
7360
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007361static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007362{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007363 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007364}
7365
David Quigleyc9bccef2013-05-22 12:50:45 -04007366#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007367
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007368static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007369 struct dentry *unused, struct inode *inode,
7370 const char *key, const void *buf,
7371 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007372{
7373 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007374 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007375
7376 return -EOPNOTSUPP;
7377}
7378
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007379static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007380 struct dentry *unused, struct inode *inode,
7381 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007382{
7383 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007384 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007385 return -EOPNOTSUPP;
7386}
7387
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007388static ssize_t
7389nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007390{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007391 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007392
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007393 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7394 len = security_inode_listsecurity(inode, list, list_len);
7395 if (list_len && len > list_len)
7396 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007397 }
7398 return len;
7399}
7400
7401static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7402 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007403 .get = nfs4_xattr_get_nfs4_label,
7404 .set = nfs4_xattr_set_nfs4_label,
7405};
David Quigleyc9bccef2013-05-22 12:50:45 -04007406
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007407#else
7408
7409static ssize_t
7410nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7411{
7412 return 0;
7413}
7414
7415#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007416
Andy Adamson533eb462011-06-13 18:25:56 -04007417/*
7418 * nfs_fhget will use either the mounted_on_fileid or the fileid
7419 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007420static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7421{
Andy Adamson533eb462011-06-13 18:25:56 -04007422 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7423 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7424 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007425 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007426 return;
7427
7428 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007429 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007430 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7431 fattr->nlink = 2;
7432}
7433
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007434static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7435 const struct qstr *name,
7436 struct nfs4_fs_locations *fs_locations,
7437 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007438{
7439 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007440 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007441 struct nfs4_fs_locations_arg args = {
7442 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007443 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007444 .page = page,
7445 .bitmask = bitmask,
7446 };
Benny Halevy22958462009-04-01 09:22:02 -04007447 struct nfs4_fs_locations_res res = {
7448 .fs_locations = fs_locations,
7449 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007450 struct rpc_message msg = {
7451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7452 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007453 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007454 };
7455 int status;
7456
Harvey Harrison3110ff82008-05-02 13:42:44 -07007457 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007458
Chuck Leverc05cefc2017-11-05 15:45:22 -05007459 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7460 bitmask[1] = nfs4_fattr_bitmap[1];
7461
Andy Adamson533eb462011-06-13 18:25:56 -04007462 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7463 * is not supported */
7464 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007465 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007466 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007467 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007468
Trond Myklebustc228fd32007-01-13 02:28:11 -05007469 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007470 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007471 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007472 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007473 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007474 return status;
7475}
7476
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007477int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7478 const struct qstr *name,
7479 struct nfs4_fs_locations *fs_locations,
7480 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007481{
Trond Myklebust0688e642019-04-07 13:59:09 -04007482 struct nfs4_exception exception = {
7483 .interruptible = true,
7484 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007485 int err;
7486 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007487 err = _nfs4_proc_fs_locations(client, dir, name,
7488 fs_locations, page);
7489 trace_nfs4_get_fs_locations(dir, name, err);
7490 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007491 &exception);
7492 } while (exception.retry);
7493 return err;
7494}
7495
Chuck Leverb03d7352013-10-17 14:12:50 -04007496/*
7497 * This operation also signals the server that this client is
7498 * performing migration recovery. The server can stop returning
7499 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7500 * appended to this compound to identify the client ID which is
7501 * performing recovery.
7502 */
7503static int _nfs40_proc_get_locations(struct inode *inode,
7504 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007505 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007506{
7507 struct nfs_server *server = NFS_SERVER(inode);
7508 struct rpc_clnt *clnt = server->client;
7509 u32 bitmask[2] = {
7510 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7511 };
7512 struct nfs4_fs_locations_arg args = {
7513 .clientid = server->nfs_client->cl_clientid,
7514 .fh = NFS_FH(inode),
7515 .page = page,
7516 .bitmask = bitmask,
7517 .migration = 1, /* skip LOOKUP */
7518 .renew = 1, /* append RENEW */
7519 };
7520 struct nfs4_fs_locations_res res = {
7521 .fs_locations = locations,
7522 .migration = 1,
7523 .renew = 1,
7524 };
7525 struct rpc_message msg = {
7526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7527 .rpc_argp = &args,
7528 .rpc_resp = &res,
7529 .rpc_cred = cred,
7530 };
7531 unsigned long now = jiffies;
7532 int status;
7533
7534 nfs_fattr_init(&locations->fattr);
7535 locations->server = server;
7536 locations->nlocations = 0;
7537
Anna Schumakerfba83f32018-05-04 16:22:50 -04007538 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007539 status = nfs4_call_sync_sequence(clnt, server, &msg,
7540 &args.seq_args, &res.seq_res);
7541 if (status)
7542 return status;
7543
7544 renew_lease(server, now);
7545 return 0;
7546}
7547
7548#ifdef CONFIG_NFS_V4_1
7549
7550/*
7551 * This operation also signals the server that this client is
7552 * performing migration recovery. The server can stop asserting
7553 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7554 * performing this operation is identified in the SEQUENCE
7555 * operation in this compound.
7556 *
7557 * When the client supports GETATTR(fs_locations_info), it can
7558 * be plumbed in here.
7559 */
7560static int _nfs41_proc_get_locations(struct inode *inode,
7561 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007562 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007563{
7564 struct nfs_server *server = NFS_SERVER(inode);
7565 struct rpc_clnt *clnt = server->client;
7566 u32 bitmask[2] = {
7567 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7568 };
7569 struct nfs4_fs_locations_arg args = {
7570 .fh = NFS_FH(inode),
7571 .page = page,
7572 .bitmask = bitmask,
7573 .migration = 1, /* skip LOOKUP */
7574 };
7575 struct nfs4_fs_locations_res res = {
7576 .fs_locations = locations,
7577 .migration = 1,
7578 };
7579 struct rpc_message msg = {
7580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7581 .rpc_argp = &args,
7582 .rpc_resp = &res,
7583 .rpc_cred = cred,
7584 };
7585 int status;
7586
7587 nfs_fattr_init(&locations->fattr);
7588 locations->server = server;
7589 locations->nlocations = 0;
7590
Anna Schumakerfba83f32018-05-04 16:22:50 -04007591 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007592 status = nfs4_call_sync_sequence(clnt, server, &msg,
7593 &args.seq_args, &res.seq_res);
7594 if (status == NFS4_OK &&
7595 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7596 status = -NFS4ERR_LEASE_MOVED;
7597 return status;
7598}
7599
7600#endif /* CONFIG_NFS_V4_1 */
7601
7602/**
7603 * nfs4_proc_get_locations - discover locations for a migrated FSID
7604 * @inode: inode on FSID that is migrating
7605 * @locations: result of query
7606 * @page: buffer
7607 * @cred: credential to use for this operation
7608 *
7609 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7610 * operation failed, or a negative errno if a local error occurred.
7611 *
7612 * On success, "locations" is filled in, but if the server has
7613 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7614 * asserted.
7615 *
7616 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7617 * from this client that require migration recovery.
7618 */
7619int nfs4_proc_get_locations(struct inode *inode,
7620 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007621 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007622{
7623 struct nfs_server *server = NFS_SERVER(inode);
7624 struct nfs_client *clp = server->nfs_client;
7625 const struct nfs4_mig_recovery_ops *ops =
7626 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007627 struct nfs4_exception exception = {
7628 .interruptible = true,
7629 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007630 int status;
7631
7632 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7633 (unsigned long long)server->fsid.major,
7634 (unsigned long long)server->fsid.minor,
7635 clp->cl_hostname);
7636 nfs_display_fhandle(NFS_FH(inode), __func__);
7637
7638 do {
7639 status = ops->get_locations(inode, locations, page, cred);
7640 if (status != -NFS4ERR_DELAY)
7641 break;
7642 nfs4_handle_exception(server, status, &exception);
7643 } while (exception.retry);
7644 return status;
7645}
7646
Chuck Lever44c99932013-10-17 14:13:30 -04007647/*
7648 * This operation also signals the server that this client is
7649 * performing "lease moved" recovery. The server can stop
7650 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7651 * is appended to this compound to identify the client ID which is
7652 * performing recovery.
7653 */
NeilBrowna52458b2018-12-03 11:30:31 +11007654static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007655{
7656 struct nfs_server *server = NFS_SERVER(inode);
7657 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7658 struct rpc_clnt *clnt = server->client;
7659 struct nfs4_fsid_present_arg args = {
7660 .fh = NFS_FH(inode),
7661 .clientid = clp->cl_clientid,
7662 .renew = 1, /* append RENEW */
7663 };
7664 struct nfs4_fsid_present_res res = {
7665 .renew = 1,
7666 };
7667 struct rpc_message msg = {
7668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7669 .rpc_argp = &args,
7670 .rpc_resp = &res,
7671 .rpc_cred = cred,
7672 };
7673 unsigned long now = jiffies;
7674 int status;
7675
7676 res.fh = nfs_alloc_fhandle();
7677 if (res.fh == NULL)
7678 return -ENOMEM;
7679
Anna Schumakerfba83f32018-05-04 16:22:50 -04007680 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007681 status = nfs4_call_sync_sequence(clnt, server, &msg,
7682 &args.seq_args, &res.seq_res);
7683 nfs_free_fhandle(res.fh);
7684 if (status)
7685 return status;
7686
7687 do_renew_lease(clp, now);
7688 return 0;
7689}
7690
7691#ifdef CONFIG_NFS_V4_1
7692
7693/*
7694 * This operation also signals the server that this client is
7695 * performing "lease moved" recovery. The server can stop asserting
7696 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7697 * this operation is identified in the SEQUENCE operation in this
7698 * compound.
7699 */
NeilBrowna52458b2018-12-03 11:30:31 +11007700static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007701{
7702 struct nfs_server *server = NFS_SERVER(inode);
7703 struct rpc_clnt *clnt = server->client;
7704 struct nfs4_fsid_present_arg args = {
7705 .fh = NFS_FH(inode),
7706 };
7707 struct nfs4_fsid_present_res res = {
7708 };
7709 struct rpc_message msg = {
7710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7711 .rpc_argp = &args,
7712 .rpc_resp = &res,
7713 .rpc_cred = cred,
7714 };
7715 int status;
7716
7717 res.fh = nfs_alloc_fhandle();
7718 if (res.fh == NULL)
7719 return -ENOMEM;
7720
Anna Schumakerfba83f32018-05-04 16:22:50 -04007721 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007722 status = nfs4_call_sync_sequence(clnt, server, &msg,
7723 &args.seq_args, &res.seq_res);
7724 nfs_free_fhandle(res.fh);
7725 if (status == NFS4_OK &&
7726 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7727 status = -NFS4ERR_LEASE_MOVED;
7728 return status;
7729}
7730
7731#endif /* CONFIG_NFS_V4_1 */
7732
7733/**
7734 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7735 * @inode: inode on FSID to check
7736 * @cred: credential to use for this operation
7737 *
7738 * Server indicates whether the FSID is present, moved, or not
7739 * recognized. This operation is necessary to clear a LEASE_MOVED
7740 * condition for this client ID.
7741 *
7742 * Returns NFS4_OK if the FSID is present on this server,
7743 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7744 * NFS4ERR code if some error occurred on the server, or a
7745 * negative errno if a local failure occurred.
7746 */
NeilBrowna52458b2018-12-03 11:30:31 +11007747int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007748{
7749 struct nfs_server *server = NFS_SERVER(inode);
7750 struct nfs_client *clp = server->nfs_client;
7751 const struct nfs4_mig_recovery_ops *ops =
7752 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007753 struct nfs4_exception exception = {
7754 .interruptible = true,
7755 };
Chuck Lever44c99932013-10-17 14:13:30 -04007756 int status;
7757
7758 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7759 (unsigned long long)server->fsid.major,
7760 (unsigned long long)server->fsid.minor,
7761 clp->cl_hostname);
7762 nfs_display_fhandle(NFS_FH(inode), __func__);
7763
7764 do {
7765 status = ops->fsid_present(inode, cred);
7766 if (status != -NFS4ERR_DELAY)
7767 break;
7768 nfs4_handle_exception(server, status, &exception);
7769 } while (exception.retry);
7770 return status;
7771}
7772
Trond Myklebust302fad72019-02-18 13:32:38 -05007773/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007774 * If 'use_integrity' is true and the state managment nfs_client
7775 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7776 * and the machine credential as per RFC3530bis and RFC5661 Security
7777 * Considerations sections. Otherwise, just use the user cred with the
7778 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007779 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007780static 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 +00007781{
7782 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04007783 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7784 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007785 struct nfs4_secinfo_arg args = {
7786 .dir_fh = NFS_FH(dir),
7787 .name = name,
7788 };
7789 struct nfs4_secinfo_res res = {
7790 .flavors = flavors,
7791 };
7792 struct rpc_message msg = {
7793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7794 .rpc_argp = &args,
7795 .rpc_resp = &res,
7796 };
Anna Schumaker50493362019-08-14 15:30:16 -04007797 struct nfs4_call_sync_data data = {
7798 .seq_server = NFS_SERVER(dir),
7799 .seq_args = &args.seq_args,
7800 .seq_res = &res.seq_res,
7801 };
7802 struct rpc_task_setup task_setup = {
7803 .rpc_client = clnt,
7804 .rpc_message = &msg,
7805 .callback_ops = clp->cl_mvops->call_sync_ops,
7806 .callback_data = &data,
7807 .flags = RPC_TASK_NO_ROUND_ROBIN,
7808 };
NeilBrowna52458b2018-12-03 11:30:31 +11007809 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007810
7811 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04007812 clnt = clp->cl_rpcclient;
7813 task_setup.rpc_client = clnt;
7814
7815 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007816 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007817 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007818
7819 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007820
Anna Schumaker50493362019-08-14 15:30:16 -04007821 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7822 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7823 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007824
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007825 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007826
NeilBrowna52458b2018-12-03 11:30:31 +11007827 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007828 return status;
7829}
7830
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007831int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7832 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007833{
Trond Myklebust0688e642019-04-07 13:59:09 -04007834 struct nfs4_exception exception = {
7835 .interruptible = true,
7836 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007837 int err;
7838 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007839 err = -NFS4ERR_WRONGSEC;
7840
7841 /* try to use integrity protection with machine cred */
7842 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7843 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7844
7845 /*
7846 * if unable to use integrity protection, or SECINFO with
7847 * integrity protection returns NFS4ERR_WRONGSEC (which is
7848 * disallowed by spec, but exists in deployed servers) use
7849 * the current filesystem's rpc_client and the user cred.
7850 */
7851 if (err == -NFS4ERR_WRONGSEC)
7852 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7853
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007854 trace_nfs4_secinfo(dir, name, err);
7855 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007856 &exception);
7857 } while (exception.retry);
7858 return err;
7859}
7860
Andy Adamson557134a2009-04-01 09:21:53 -04007861#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007862/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007863 * Check the exchange flags returned by the server for invalid flags, having
7864 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7865 * DS flags set.
7866 */
7867static int nfs4_check_cl_exchange_flags(u32 flags)
7868{
7869 if (flags & ~EXCHGID4_FLAG_MASK_R)
7870 goto out_inval;
7871 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7872 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7873 goto out_inval;
7874 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7875 goto out_inval;
7876 return NFS_OK;
7877out_inval:
7878 return -NFS4ERR_INVAL;
7879}
7880
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007881static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007882nfs41_same_server_scope(struct nfs41_server_scope *a,
7883 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007884{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007885 if (a->server_scope_sz != b->server_scope_sz)
7886 return false;
7887 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007888}
7889
Andy Adamson02a95de2016-02-05 16:08:37 -05007890static void
7891nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7892{
Trond Myklebust5c441542019-11-13 08:34:00 +01007893 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
7894 struct nfs_client *clp = args->client;
7895
7896 switch (task->tk_status) {
7897 case -NFS4ERR_BADSESSION:
7898 case -NFS4ERR_DEADSESSION:
7899 nfs4_schedule_session_recovery(clp->cl_session,
7900 task->tk_status);
7901 }
Andy Adamson02a95de2016-02-05 16:08:37 -05007902}
7903
7904static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7905 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7906};
7907
Andy Adamson357f54d2010-12-14 10:11:57 -05007908/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007909 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007910 *
7911 * The 4.1 client currently uses the same TCP connection for the
7912 * fore and backchannel.
7913 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007914static
7915int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7916 struct rpc_xprt *xprt,
7917 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007918 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007919{
7920 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007921 struct nfs41_bind_conn_to_session_args args = {
7922 .client = clp,
7923 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7924 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007925 struct nfs41_bind_conn_to_session_res res;
7926 struct rpc_message msg = {
7927 .rpc_proc =
7928 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007929 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007930 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007931 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007932 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007933 struct rpc_task_setup task_setup_data = {
7934 .rpc_client = clnt,
7935 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007936 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007937 .rpc_message = &msg,
7938 .flags = RPC_TASK_TIMEOUT,
7939 };
7940 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007941
Trond Myklebust71a097c2015-02-18 09:27:18 -08007942 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7943 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7944 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007945
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007946 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7947 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7948 args.dir = NFS4_CDFC4_FORE;
7949
7950 task = rpc_run_task(&task_setup_data);
7951 if (!IS_ERR(task)) {
7952 status = task->tk_status;
7953 rpc_put_task(task);
7954 } else
7955 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007956 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007957 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007958 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007959 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7960 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007961 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007962 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007963 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007964 dprintk("NFS: %s: Unexpected direction from server\n",
7965 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007966 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007967 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007968 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007969 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7970 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007971 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007972 }
7973 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007974
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007975 return status;
7976}
7977
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007978struct rpc_bind_conn_calldata {
7979 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007980 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007981};
7982
7983static int
7984nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7985 struct rpc_xprt *xprt,
7986 void *calldata)
7987{
7988 struct rpc_bind_conn_calldata *p = calldata;
7989
7990 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7991}
7992
NeilBrowna52458b2018-12-03 11:30:31 +11007993int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007994{
7995 struct rpc_bind_conn_calldata data = {
7996 .clp = clp,
7997 .cred = cred,
7998 };
7999 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8000 nfs4_proc_bind_conn_to_session_callback, &data);
8001}
8002
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008003/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008004 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8005 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008006 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008007static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8008 .how = SP4_MACH_CRED,
8009 .enforce.u.words = {
8010 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8011 1 << (OP_EXCHANGE_ID - 32) |
8012 1 << (OP_CREATE_SESSION - 32) |
8013 1 << (OP_DESTROY_SESSION - 32) |
8014 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008015 },
8016 .allow.u.words = {
8017 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008018 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008019 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008020 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008021 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008022 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008023 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008024 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008025 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008026 1 << (OP_FREE_STATEID - 32) |
8027 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008028 }
8029};
8030
8031/*
8032 * Select the state protection mode for client `clp' given the server results
8033 * from exchange_id in `sp'.
8034 *
8035 * Returns 0 on success, negative errno otherwise.
8036 */
8037static int nfs4_sp4_select_mode(struct nfs_client *clp,
8038 struct nfs41_state_protection *sp)
8039{
8040 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8041 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8042 1 << (OP_EXCHANGE_ID - 32) |
8043 1 << (OP_CREATE_SESSION - 32) |
8044 1 << (OP_DESTROY_SESSION - 32) |
8045 1 << (OP_DESTROY_CLIENTID - 32)
8046 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008047 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008048 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008049 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008050
8051 if (sp->how == SP4_MACH_CRED) {
8052 /* Print state protect result */
8053 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8054 for (i = 0; i <= LAST_NFS4_OP; i++) {
8055 if (test_bit(i, sp->enforce.u.longs))
8056 dfprintk(MOUNT, " enforce op %d\n", i);
8057 if (test_bit(i, sp->allow.u.longs))
8058 dfprintk(MOUNT, " allow op %d\n", i);
8059 }
8060
8061 /* make sure nothing is on enforce list that isn't supported */
8062 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8063 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8064 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008065 ret = -EINVAL;
8066 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008067 }
8068 }
8069
8070 /*
8071 * Minimal mode - state operations are allowed to use machine
8072 * credential. Note this already happens by default, so the
8073 * client doesn't have to do anything more than the negotiation.
8074 *
8075 * NOTE: we don't care if EXCHANGE_ID is in the list -
8076 * we're already using the machine cred for exchange_id
8077 * and will never use a different cred.
8078 */
8079 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8080 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8081 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8082 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8083 dfprintk(MOUNT, "sp4_mach_cred:\n");
8084 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008085 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008086 } else {
8087 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008088 ret = -EINVAL;
8089 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008090 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008091
8092 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008093 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8094 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008095 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8096 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008097 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008098 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008099
Andrew Elble99ade3c2015-12-02 09:39:51 -05008100 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8101 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008102 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008103 }
8104
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008105 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8106 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8107 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008108 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008109 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008110
8111 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8112 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8113 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008114 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008115 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008116
8117 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8118 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008119 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008120 }
8121
8122 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8123 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008124 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008125 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008126 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008127out:
8128 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008129 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008130}
8131
Andy Adamson8d89bd72016-09-09 09:22:18 -04008132struct nfs41_exchange_id_data {
8133 struct nfs41_exchange_id_res res;
8134 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008135};
8136
Andy Adamson8d89bd72016-09-09 09:22:18 -04008137static void nfs4_exchange_id_release(void *data)
8138{
8139 struct nfs41_exchange_id_data *cdata =
8140 (struct nfs41_exchange_id_data *)data;
8141
Olga Kornievskaia63513232017-03-13 10:36:19 -04008142 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008143 kfree(cdata->res.impl_id);
8144 kfree(cdata->res.server_scope);
8145 kfree(cdata->res.server_owner);
8146 kfree(cdata);
8147}
8148
8149static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008150 .rpc_release = nfs4_exchange_id_release,
8151};
8152
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008153/*
8154 * _nfs4_proc_exchange_id()
8155 *
8156 * Wrapper for EXCHANGE_ID operation.
8157 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008158static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008159nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008160 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008161{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008162 struct rpc_message msg = {
8163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008164 .rpc_cred = cred,
8165 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008166 struct rpc_task_setup task_setup_data = {
8167 .rpc_client = clp->cl_rpcclient,
8168 .callback_ops = &nfs4_exchange_id_call_ops,
8169 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008170 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008171 };
8172 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008173 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008174
Elena Reshetova212bf412017-10-20 12:53:38 +03008175 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008176 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008177
Trond Myklebust9c760d12017-07-31 18:38:50 -04008178 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008179 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008180 if (!calldata)
8181 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008182
Trond Myklebustfd405592017-08-01 16:02:47 -04008183 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008184
8185 status = nfs4_init_uniform_client_string(clp);
8186 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008187 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008188
Andy Adamson8d89bd72016-09-09 09:22:18 -04008189 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8190 GFP_NOFS);
8191 status = -ENOMEM;
8192 if (unlikely(calldata->res.server_owner == NULL))
8193 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008194
Andy Adamson8d89bd72016-09-09 09:22:18 -04008195 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008196 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008197 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008198 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008199
Andy Adamson8d89bd72016-09-09 09:22:18 -04008200 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8201 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008202 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008203
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008204 switch (sp4_how) {
8205 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008206 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008207 break;
8208
8209 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008210 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008211 break;
8212
8213 default:
8214 /* unsupported! */
8215 WARN_ON_ONCE(1);
8216 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008217 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008218 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008219 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008220 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008221 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008222 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8223 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008224 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008225 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008226 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8227 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008228#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008229 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008230#endif
8231 msg.rpc_argp = &calldata->args;
8232 msg.rpc_resp = &calldata->res;
8233 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008234
Trond Myklebust9c760d12017-07-31 18:38:50 -04008235 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008236
8237out_impl_id:
8238 kfree(calldata->res.impl_id);
8239out_server_scope:
8240 kfree(calldata->res.server_scope);
8241out_server_owner:
8242 kfree(calldata->res.server_owner);
8243out_calldata:
8244 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008245out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008246 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008247 return ERR_PTR(status);
8248}
8249
8250/*
8251 * _nfs4_proc_exchange_id()
8252 *
8253 * Wrapper for EXCHANGE_ID operation.
8254 */
NeilBrowna52458b2018-12-03 11:30:31 +11008255static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008256 u32 sp4_how)
8257{
8258 struct rpc_task *task;
8259 struct nfs41_exchange_id_args *argp;
8260 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008261 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008262 int status;
8263
8264 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8265 if (IS_ERR(task))
8266 return PTR_ERR(task);
8267
8268 argp = task->tk_msg.rpc_argp;
8269 resp = task->tk_msg.rpc_resp;
8270 status = task->tk_status;
8271 if (status != 0)
8272 goto out;
8273
8274 status = nfs4_check_cl_exchange_flags(resp->flags);
8275 if (status != 0)
8276 goto out;
8277
8278 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8279 if (status != 0)
8280 goto out;
8281
Robert Milkowski7dc29932020-01-30 09:43:25 +00008282 do_renew_lease(clp, now);
8283
Trond Myklebust9c760d12017-07-31 18:38:50 -04008284 clp->cl_clientid = resp->clientid;
8285 clp->cl_exchange_flags = resp->flags;
8286 clp->cl_seqid = resp->seqid;
8287 /* Client ID is not confirmed */
8288 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8289 clear_bit(NFS4_SESSION_ESTABLISHED,
8290 &clp->cl_session->session_state);
8291
8292 if (clp->cl_serverscope != NULL &&
8293 !nfs41_same_server_scope(clp->cl_serverscope,
8294 resp->server_scope)) {
8295 dprintk("%s: server_scope mismatch detected\n",
8296 __func__);
8297 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8298 }
8299
8300 swap(clp->cl_serverowner, resp->server_owner);
8301 swap(clp->cl_serverscope, resp->server_scope);
8302 swap(clp->cl_implid, resp->impl_id);
8303
8304 /* Save the EXCHANGE_ID verifier session trunk tests */
8305 memcpy(clp->cl_confirm.data, argp->verifier.data,
8306 sizeof(clp->cl_confirm.data));
8307out:
8308 trace_nfs4_exchange_id(clp, status);
8309 rpc_put_task(task);
8310 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008311}
8312
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008313/*
8314 * nfs4_proc_exchange_id()
8315 *
8316 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8317 *
8318 * Since the clientid has expired, all compounds using sessions
8319 * associated with the stale clientid will be returning
8320 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8321 * be in some phase of session reset.
8322 *
8323 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8324 */
NeilBrowna52458b2018-12-03 11:30:31 +11008325int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008326{
8327 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8328 int status;
8329
8330 /* try SP4_MACH_CRED if krb5i/p */
8331 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8332 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008333 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008334 if (!status)
8335 return 0;
8336 }
8337
8338 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008339 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008340}
8341
Andy Adamson04fa2c62016-09-09 09:22:29 -04008342/**
8343 * nfs4_test_session_trunk
8344 *
8345 * This is an add_xprt_test() test function called from
8346 * rpc_clnt_setup_test_and_add_xprt.
8347 *
8348 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8349 * and is dereferrenced in nfs4_exchange_id_release
8350 *
8351 * Upon success, add the new transport to the rpc_clnt
8352 *
8353 * @clnt: struct rpc_clnt to get new transport
8354 * @xprt: the rpc_xprt to test
8355 * @data: call data for _nfs4_proc_exchange_id.
8356 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308357void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008358 void *data)
8359{
8360 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008361 struct rpc_task *task;
8362 int status;
8363
Andy Adamson04fa2c62016-09-09 09:22:29 -04008364 u32 sp4_how;
8365
8366 dprintk("--> %s try %s\n", __func__,
8367 xprt->address_strings[RPC_DISPLAY_ADDR]);
8368
8369 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8370
8371 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008372 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8373 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308374 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008375
8376 status = task->tk_status;
8377 if (status == 0)
8378 status = nfs4_detect_session_trunking(adata->clp,
8379 task->tk_msg.rpc_resp, xprt);
8380
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308381 if (status == 0)
8382 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8383
Trond Myklebust9c760d12017-07-31 18:38:50 -04008384 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008385}
8386EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8387
Trond Myklebust66245532012-05-25 17:18:09 -04008388static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008389 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008390{
8391 struct rpc_message msg = {
8392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8393 .rpc_argp = clp,
8394 .rpc_cred = cred,
8395 };
8396 int status;
8397
NeilBrown5a0c2572019-05-30 10:41:28 +10008398 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8399 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008400 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008401 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008402 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008403 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8404 return status;
8405}
8406
8407static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008408 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008409{
8410 unsigned int loop;
8411 int ret;
8412
8413 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8414 ret = _nfs4_proc_destroy_clientid(clp, cred);
8415 switch (ret) {
8416 case -NFS4ERR_DELAY:
8417 case -NFS4ERR_CLIENTID_BUSY:
8418 ssleep(1);
8419 break;
8420 default:
8421 return ret;
8422 }
8423 }
8424 return 0;
8425}
8426
8427int nfs4_destroy_clientid(struct nfs_client *clp)
8428{
NeilBrowna52458b2018-12-03 11:30:31 +11008429 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008430 int ret = 0;
8431
8432 if (clp->cl_mvops->minor_version < 1)
8433 goto out;
8434 if (clp->cl_exchange_flags == 0)
8435 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008436 if (clp->cl_preserve_clid)
8437 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008438 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008439 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008440 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008441 switch (ret) {
8442 case 0:
8443 case -NFS4ERR_STALE_CLIENTID:
8444 clp->cl_exchange_flags = 0;
8445 }
8446out:
8447 return ret;
8448}
8449
Donald Buczek0efb01b2019-07-07 21:26:08 +02008450#endif /* CONFIG_NFS_V4_1 */
8451
Andy Adamson2050f0c2009-04-01 09:22:30 -04008452struct nfs4_get_lease_time_data {
8453 struct nfs4_get_lease_time_args *args;
8454 struct nfs4_get_lease_time_res *res;
8455 struct nfs_client *clp;
8456};
8457
8458static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8459 void *calldata)
8460{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008461 struct nfs4_get_lease_time_data *data =
8462 (struct nfs4_get_lease_time_data *)calldata;
8463
8464 dprintk("--> %s\n", __func__);
8465 /* just setup sequence, do not trigger session recovery
8466 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008467 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008468 &data->args->la_seq_args,
8469 &data->res->lr_seq_res,
8470 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008471 dprintk("<-- %s\n", __func__);
8472}
8473
8474/*
8475 * Called from nfs4_state_manager thread for session setup, so don't recover
8476 * from sequence operation or clientid errors.
8477 */
8478static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8479{
8480 struct nfs4_get_lease_time_data *data =
8481 (struct nfs4_get_lease_time_data *)calldata;
8482
8483 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008484 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008485 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008486 switch (task->tk_status) {
8487 case -NFS4ERR_DELAY:
8488 case -NFS4ERR_GRACE:
8489 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8490 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8491 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008492 /* fall through */
8493 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008494 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008495 return;
8496 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008497 dprintk("<-- %s\n", __func__);
8498}
8499
Trond Myklebust17280172012-03-11 13:11:00 -04008500static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008501 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8502 .rpc_call_done = nfs4_get_lease_time_done,
8503};
8504
8505int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8506{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008507 struct nfs4_get_lease_time_args args;
8508 struct nfs4_get_lease_time_res res = {
8509 .lr_fsinfo = fsinfo,
8510 };
8511 struct nfs4_get_lease_time_data data = {
8512 .args = &args,
8513 .res = &res,
8514 .clp = clp,
8515 };
8516 struct rpc_message msg = {
8517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8518 .rpc_argp = &args,
8519 .rpc_resp = &res,
8520 };
8521 struct rpc_task_setup task_setup = {
8522 .rpc_client = clp->cl_rpcclient,
8523 .rpc_message = &msg,
8524 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008525 .callback_data = &data,
8526 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008527 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008528
Anna Schumakerfba83f32018-05-04 16:22:50 -04008529 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008530 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008531}
8532
Donald Buczek0efb01b2019-07-07 21:26:08 +02008533#ifdef CONFIG_NFS_V4_1
8534
Andy Adamsonfc931582009-04-01 09:22:31 -04008535/*
8536 * Initialize the values to be used by the client in CREATE_SESSION
8537 * If nfs4_init_session set the fore channel request and response sizes,
8538 * use them.
8539 *
8540 * Set the back channel max_resp_sz_cached to zero to force the client to
8541 * always set csa_cachethis to FALSE because the current implementation
8542 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8543 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008544static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8545 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008546{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008547 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008548 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008549 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008550
Andy Adamson18aad3d2013-06-26 12:21:49 -04008551 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8552 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8553
Andy Adamsonfc931582009-04-01 09:22:31 -04008554 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008555 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8556 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008557 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008558 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008559
8560 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008561 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008562 __func__,
8563 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008564 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008565
8566 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008567 args->bc_attrs.max_rqst_sz = max_bc_payload;
8568 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008569 args->bc_attrs.max_resp_sz_cached = 0;
8570 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008571 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008572 if (args->bc_attrs.max_reqs > max_bc_slots)
8573 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008574
8575 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8576 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8577 __func__,
8578 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8579 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8580 args->bc_attrs.max_reqs);
8581}
8582
Trond Myklebust79969dd12015-02-18 11:30:18 -08008583static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8584 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008585{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008586 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008587 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008588
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008589 if (rcvd->max_resp_sz > sent->max_resp_sz)
8590 return -EINVAL;
8591 /*
8592 * Our requested max_ops is the minimum we need; we're not
8593 * prepared to break up compounds into smaller pieces than that.
8594 * So, no point even trying to continue if the server won't
8595 * cooperate:
8596 */
8597 if (rcvd->max_ops < sent->max_ops)
8598 return -EINVAL;
8599 if (rcvd->max_reqs == 0)
8600 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008601 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8602 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008603 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008604}
8605
Trond Myklebust79969dd12015-02-18 11:30:18 -08008606static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8607 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008608{
8609 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008610 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008611
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008612 if (!(res->flags & SESSION4_BACK_CHAN))
8613 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008614 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8615 return -EINVAL;
8616 if (rcvd->max_resp_sz < sent->max_resp_sz)
8617 return -EINVAL;
8618 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8619 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008620 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008621 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008622 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008623 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008624out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008625 return 0;
8626}
Andy Adamson8d353012009-04-01 09:22:32 -04008627
Andy Adamson8d353012009-04-01 09:22:32 -04008628static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008629 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008630{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008631 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008632
Trond Myklebust79969dd12015-02-18 11:30:18 -08008633 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008634 if (ret)
8635 return ret;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008636 return nfs4_verify_back_channel_attrs(args, res);
8637}
8638
8639static void nfs4_update_session(struct nfs4_session *session,
8640 struct nfs41_create_session_res *res)
8641{
8642 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008643 /* Mark client id and session as being confirmed */
8644 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8645 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd12015-02-18 11:30:18 -08008646 session->flags = res->flags;
8647 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008648 if (res->flags & SESSION4_BACK_CHAN)
8649 memcpy(&session->bc_attrs, &res->bc_attrs,
8650 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008651}
8652
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008653static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008654 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008655{
8656 struct nfs4_session *session = clp->cl_session;
8657 struct nfs41_create_session_args args = {
8658 .client = clp,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008659 .clientid = clp->cl_clientid,
8660 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008661 .cb_program = NFS4_CALLBACK,
8662 };
Trond Myklebust79969dd12015-02-18 11:30:18 -08008663 struct nfs41_create_session_res res;
8664
Andy Adamsonfc931582009-04-01 09:22:31 -04008665 struct rpc_message msg = {
8666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8667 .rpc_argp = &args,
8668 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008669 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008670 };
8671 int status;
8672
Chuck Lever6b26cc82016-05-02 14:40:40 -04008673 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008674 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008675
NeilBrown5a0c2572019-05-30 10:41:28 +10008676 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8677 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008678 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008679
Trond Myklebustb519d402016-09-11 14:50:01 -04008680 switch (status) {
8681 case -NFS4ERR_STALE_CLIENTID:
8682 case -NFS4ERR_DELAY:
8683 case -ETIMEDOUT:
8684 case -EACCES:
8685 case -EAGAIN:
8686 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08008687 }
Trond Myklebustb519d402016-09-11 14:50:01 -04008688
8689 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008690 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008691 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008692 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008693 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008694 if (status)
8695 goto out;
8696 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008697 }
Trond Myklebust79969dd12015-02-18 11:30:18 -08008698out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008699 return status;
8700}
8701
8702/*
8703 * Issues a CREATE_SESSION operation to the server.
8704 * It is the responsibility of the caller to verify the session is
8705 * expired before calling this routine.
8706 */
NeilBrowna52458b2018-12-03 11:30:31 +11008707int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008708{
8709 int status;
8710 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008711 struct nfs4_session *session = clp->cl_session;
8712
8713 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8714
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008715 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008716 if (status)
8717 goto out;
8718
Andy Adamsonaacd5532011-11-09 13:58:21 -05008719 /* Init or reset the session slot tables */
8720 status = nfs4_setup_session_slot_tables(session);
8721 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008722 if (status)
8723 goto out;
8724
8725 ptr = (unsigned *)&session->sess_id.data[0];
8726 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8727 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008728out:
8729 dprintk("<-- %s\n", __func__);
8730 return status;
8731}
8732
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008733/*
8734 * Issue the over-the-wire RPC DESTROY_SESSION.
8735 * The caller must serialize access to this routine.
8736 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008737int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008738 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008739{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008740 struct rpc_message msg = {
8741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8742 .rpc_argp = session,
8743 .rpc_cred = cred,
8744 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008745 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008746
8747 dprintk("--> nfs4_proc_destroy_session\n");
8748
8749 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008750 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8751 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008752
NeilBrown5a0c2572019-05-30 10:41:28 +10008753 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8754 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008755 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008756
8757 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008758 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008759 "Session has been destroyed regardless...\n", status);
8760
8761 dprintk("<-- nfs4_proc_destroy_session\n");
8762 return status;
8763}
8764
Trond Myklebust7b38c362012-05-23 13:23:31 -04008765/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008766 * Renew the cl_session lease.
8767 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008768struct nfs4_sequence_data {
8769 struct nfs_client *clp;
8770 struct nfs4_sequence_args args;
8771 struct nfs4_sequence_res res;
8772};
8773
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008774static void nfs41_sequence_release(void *data)
8775{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008776 struct nfs4_sequence_data *calldata = data;
8777 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008778
Elena Reshetova212bf412017-10-20 12:53:38 +03008779 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008780 nfs4_schedule_state_renewal(clp);
8781 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008782 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008783}
8784
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008785static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8786{
8787 switch(task->tk_status) {
8788 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008789 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8790 return -EAGAIN;
8791 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008792 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008793 }
8794 return 0;
8795}
8796
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008797static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008798{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008799 struct nfs4_sequence_data *calldata = data;
8800 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008801
Trond Myklebust14516c32010-07-31 14:29:06 -04008802 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8803 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008804
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008805 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008806 if (task->tk_status < 0) {
8807 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008808 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008809 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008810
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008811 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8812 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008813 return;
8814 }
8815 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008816 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008817out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008818 dprintk("<-- %s\n", __func__);
8819}
8820
8821static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8822{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008823 struct nfs4_sequence_data *calldata = data;
8824 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008825 struct nfs4_sequence_args *args;
8826 struct nfs4_sequence_res *res;
8827
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008828 args = task->tk_msg.rpc_argp;
8829 res = task->tk_msg.rpc_resp;
8830
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008831 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008832}
8833
8834static const struct rpc_call_ops nfs41_sequence_ops = {
8835 .rpc_call_done = nfs41_sequence_call_done,
8836 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008837 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008838};
8839
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008840static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008841 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008842 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008843 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008844{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008845 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008846 struct rpc_message msg = {
8847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8848 .rpc_cred = cred,
8849 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008850 struct rpc_task_setup task_setup_data = {
8851 .rpc_client = clp->cl_rpcclient,
8852 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008853 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008854 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008855 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008856 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008857
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008858 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008859 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008860 goto out_err;
8861
8862 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008863 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008864 if (calldata == NULL)
8865 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008866 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008867 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008868 msg.rpc_argp = &calldata->args;
8869 msg.rpc_resp = &calldata->res;
8870 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008871 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008872
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008873 ret = rpc_run_task(&task_setup_data);
8874 if (IS_ERR(ret))
8875 goto out_err;
8876 return ret;
8877out_put_clp:
8878 nfs_put_client(clp);
8879out_err:
8880 nfs41_release_slot(slot);
8881 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008882}
8883
NeilBrowna52458b2018-12-03 11:30:31 +11008884static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008885{
8886 struct rpc_task *task;
8887 int ret = 0;
8888
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008889 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008890 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008891 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008892 if (IS_ERR(task))
8893 ret = PTR_ERR(task);
8894 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008895 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008896 dprintk("<-- %s status=%d\n", __func__, ret);
8897 return ret;
8898}
8899
NeilBrowna52458b2018-12-03 11:30:31 +11008900static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008901{
8902 struct rpc_task *task;
8903 int ret;
8904
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008905 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008906 if (IS_ERR(task)) {
8907 ret = PTR_ERR(task);
8908 goto out;
8909 }
8910 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008911 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008912 ret = task->tk_status;
8913 rpc_put_task(task);
8914out:
8915 dprintk("<-- %s status=%d\n", __func__, ret);
8916 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008917}
8918
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008919struct nfs4_reclaim_complete_data {
8920 struct nfs_client *clp;
8921 struct nfs41_reclaim_complete_args arg;
8922 struct nfs41_reclaim_complete_res res;
8923};
8924
8925static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8926{
8927 struct nfs4_reclaim_complete_data *calldata = data;
8928
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008929 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008930 &calldata->arg.seq_args,
8931 &calldata->res.seq_res,
8932 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008933}
8934
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008935static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8936{
8937 switch(task->tk_status) {
8938 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008939 wake_up_all(&clp->cl_lock_waitq);
8940 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008941 case -NFS4ERR_COMPLETE_ALREADY:
8942 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8943 break;
8944 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008945 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008946 /* fall through */
8947 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008948 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008949 case -NFS4ERR_BADSESSION:
8950 case -NFS4ERR_DEADSESSION:
8951 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008952 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008953 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008954 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008955 }
8956 return 0;
8957}
8958
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008959static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8960{
8961 struct nfs4_reclaim_complete_data *calldata = data;
8962 struct nfs_client *clp = calldata->clp;
8963 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8964
8965 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008966 if (!nfs41_sequence_done(task, res))
8967 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008968
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008969 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008970 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8971 rpc_restart_call_prepare(task);
8972 return;
8973 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008974 dprintk("<-- %s\n", __func__);
8975}
8976
8977static void nfs4_free_reclaim_complete_data(void *data)
8978{
8979 struct nfs4_reclaim_complete_data *calldata = data;
8980
8981 kfree(calldata);
8982}
8983
8984static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8985 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8986 .rpc_call_done = nfs4_reclaim_complete_done,
8987 .rpc_release = nfs4_free_reclaim_complete_data,
8988};
8989
8990/*
8991 * Issue a global reclaim complete.
8992 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008993static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008994 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008995{
8996 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008997 struct rpc_message msg = {
8998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008999 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009000 };
9001 struct rpc_task_setup task_setup_data = {
9002 .rpc_client = clp->cl_rpcclient,
9003 .rpc_message = &msg,
9004 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009005 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009006 };
9007 int status = -ENOMEM;
9008
9009 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009010 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009011 if (calldata == NULL)
9012 goto out;
9013 calldata->clp = clp;
9014 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009015
Anna Schumakerfba83f32018-05-04 16:22:50 -04009016 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009017 msg.rpc_argp = &calldata->arg;
9018 msg.rpc_resp = &calldata->res;
9019 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009020 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009021out:
9022 dprintk("<-- %s status=%d\n", __func__, status);
9023 return status;
9024}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009025
9026static void
9027nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9028{
9029 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009030 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009031
9032 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009033 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009034 &lgp->res.seq_res, task);
9035 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009036}
9037
9038static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9039{
9040 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009041
9042 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009043 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009044 dprintk("<-- %s\n", __func__);
9045}
9046
9047static int
9048nfs4_layoutget_handle_exception(struct rpc_task *task,
9049 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9050{
Trond Myklebustee314c22012-10-01 17:25:48 -07009051 struct inode *inode = lgp->args.inode;
9052 struct nfs_server *server = NFS_SERVER(inode);
9053 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009054 int nfs4err = task->tk_status;
9055 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009056 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009057
Boaz Harroshed7e5422014-01-22 20:34:54 +02009058 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009059
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009060 nfs4_sequence_free_slot(&lgp->res.seq_res);
9061
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009062 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009063 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009064 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009065
9066 /*
9067 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9068 * on the file. set tk_status to -ENODATA to tell upper layer to
9069 * retry go inband.
9070 */
9071 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009072 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009073 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009074 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009075 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9076 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9077 */
9078 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009079 status = -EOVERFLOW;
9080 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009081 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009082 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009083 * (or clients) writing to the same RAID stripe except when
9084 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009085 *
9086 * Treat it like we would RECALLCONFLICT -- we retry for a little
9087 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009088 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009089 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009090 if (lgp->args.minlength == 0) {
9091 status = -EOVERFLOW;
9092 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009093 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009094 status = -EBUSY;
9095 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009096 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009097 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009098 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009099 case -NFS4ERR_DELEG_REVOKED:
9100 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009101 case -NFS4ERR_EXPIRED:
9102 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009103 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009104 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009105 lo = NFS_I(inode)->layout;
9106 /* If the open stateid was bad, then recover it. */
9107 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009108 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009109 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009110 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009111 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009112 break;
9113 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009114
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009115 /*
9116 * Mark the bad layout state as invalid, then retry
9117 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009118 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009119 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009120 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009121 pnfs_free_lseg_list(&head);
9122 status = -EAGAIN;
9123 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009124 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009125
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009126 err = nfs4_handle_exception(server, nfs4err, exception);
9127 if (!status) {
9128 if (exception->retry)
9129 status = -EAGAIN;
9130 else
9131 status = err;
9132 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009133out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009134 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009135 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009136}
9137
Fred Isamandacb4522016-09-19 17:47:09 -04009138size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009139{
9140 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9141 return nfs_page_array_len(0, max_resp_sz);
9142}
9143
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009144static void nfs4_layoutget_release(void *calldata)
9145{
9146 struct nfs4_layoutget *lgp = calldata;
9147
9148 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009149 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009150 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009151 dprintk("<-- %s\n", __func__);
9152}
9153
9154static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9155 .rpc_call_prepare = nfs4_layoutget_prepare,
9156 .rpc_call_done = nfs4_layoutget_done,
9157 .rpc_release = nfs4_layoutget_release,
9158};
9159
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009160struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009161nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009162{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009163 struct inode *inode = lgp->args.inode;
9164 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009165 struct rpc_task *task;
9166 struct rpc_message msg = {
9167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9168 .rpc_argp = &lgp->args,
9169 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009170 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009171 };
9172 struct rpc_task_setup task_setup_data = {
9173 .rpc_client = server->client,
9174 .rpc_message = &msg,
9175 .callback_ops = &nfs4_layoutget_call_ops,
9176 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009177 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009178 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009179 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009180 struct nfs4_exception exception = {
9181 .inode = inode,
9182 .timeout = *timeout,
9183 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009184 int status = 0;
9185
9186 dprintk("--> %s\n", __func__);
9187
Peng Tao4bd5a982014-11-17 11:05:17 +08009188 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9189 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9190
Anna Schumakerfba83f32018-05-04 16:22:50 -04009191 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009192
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009193 task = rpc_run_task(&task_setup_data);
9194 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009195 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05009196 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009197 if (status != 0)
9198 goto out;
9199
Trond Myklebust18c07782019-02-13 07:55:31 -05009200 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009201 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9202 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009203 } else if (lgp->res.layoutp->len == 0) {
9204 status = -EAGAIN;
9205 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009206 } else
9207 lseg = pnfs_layout_process(lgp);
9208out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009209 trace_nfs4_layoutget(lgp->args.ctx,
9210 &lgp->args.range,
9211 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009212 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009213 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009214
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009215 rpc_put_task(task);
9216 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009217 if (status)
9218 return ERR_PTR(status);
9219 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009220}
9221
Benny Halevycbe82602011-05-22 19:52:37 +03009222static void
9223nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9224{
9225 struct nfs4_layoutreturn *lrp = calldata;
9226
9227 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009228 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009229 &lrp->args.seq_args,
9230 &lrp->res.seq_res,
9231 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009232 if (!pnfs_layout_is_valid(lrp->args.layout))
9233 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009234}
9235
9236static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9237{
9238 struct nfs4_layoutreturn *lrp = calldata;
9239 struct nfs_server *server;
9240
9241 dprintk("--> %s\n", __func__);
9242
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009243 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009244 return;
9245
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009246 /*
9247 * Was there an RPC level error? Assume the call succeeded,
9248 * and that we need to release the layout
9249 */
9250 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9251 lrp->res.lrs_present = 0;
9252 return;
9253 }
9254
Benny Halevycbe82602011-05-22 19:52:37 +03009255 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009256 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009257 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009258 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009259 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009260 lrp->args.inode))
9261 goto out_restart;
9262 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009263 default:
9264 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05009265 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009266 case 0:
9267 break;
9268 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009269 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009270 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009271 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009272 }
Benny Halevycbe82602011-05-22 19:52:37 +03009273 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009274 return;
9275out_restart:
9276 task->tk_status = 0;
9277 nfs4_sequence_free_slot(&lrp->res.seq_res);
9278 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009279}
9280
9281static void nfs4_layoutreturn_release(void *calldata)
9282{
9283 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009284 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009285
9286 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009287 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009288 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009289 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009290 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9291 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009292 pnfs_put_layout_hdr(lrp->args.layout);
9293 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009294 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009295 kfree(calldata);
9296 dprintk("<-- %s\n", __func__);
9297}
9298
9299static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9300 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9301 .rpc_call_done = nfs4_layoutreturn_done,
9302 .rpc_release = nfs4_layoutreturn_release,
9303};
9304
Peng Tao6c166052014-11-17 09:30:40 +08009305int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009306{
9307 struct rpc_task *task;
9308 struct rpc_message msg = {
9309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9310 .rpc_argp = &lrp->args,
9311 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009312 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009313 };
9314 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c5772013-07-22 12:42:05 -04009315 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009316 .rpc_message = &msg,
9317 .callback_ops = &nfs4_layoutreturn_call_ops,
9318 .callback_data = lrp,
9319 };
Peng Tao6c166052014-11-17 09:30:40 +08009320 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009321
Andrew Elble99ade3c2015-12-02 09:39:51 -05009322 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9323 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9324 &task_setup_data.rpc_client, &msg);
9325
Benny Halevycbe82602011-05-22 19:52:37 +03009326 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009327 if (!sync) {
9328 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9329 if (!lrp->inode) {
9330 nfs4_layoutreturn_release(lrp);
9331 return -EAGAIN;
9332 }
9333 task_setup_data.flags |= RPC_TASK_ASYNC;
9334 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009335 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009336 task = rpc_run_task(&task_setup_data);
9337 if (IS_ERR(task))
9338 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009339 if (sync)
9340 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009341 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009342 dprintk("<-- %s status=%d\n", __func__, status);
9343 rpc_put_task(task);
9344 return status;
9345}
9346
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009347static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009348_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9349 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009350 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009351{
9352 struct nfs4_getdeviceinfo_args args = {
9353 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009354 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9355 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009356 };
9357 struct nfs4_getdeviceinfo_res res = {
9358 .pdev = pdev,
9359 };
9360 struct rpc_message msg = {
9361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9362 .rpc_argp = &args,
9363 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009364 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009365 };
9366 int status;
9367
9368 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009369 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009370 if (res.notification & ~args.notify_types)
9371 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009372 if (res.notification != args.notify_types)
9373 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009374
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009375 dprintk("<-- %s status=%d\n", __func__, status);
9376
9377 return status;
9378}
9379
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009380int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9381 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009382 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009383{
9384 struct nfs4_exception exception = { };
9385 int err;
9386
9387 do {
9388 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009389 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009390 &exception);
9391 } while (exception.retry);
9392 return err;
9393}
9394EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9395
Andy Adamson863a3c62011-03-23 13:27:54 +00009396static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9397{
9398 struct nfs4_layoutcommit_data *data = calldata;
9399 struct nfs_server *server = NFS_SERVER(data->args.inode);
9400
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009401 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009402 &data->args.seq_args,
9403 &data->res.seq_res,
9404 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009405}
9406
9407static void
9408nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9409{
9410 struct nfs4_layoutcommit_data *data = calldata;
9411 struct nfs_server *server = NFS_SERVER(data->args.inode);
9412
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009413 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009414 return;
9415
9416 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009417 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9418 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9419 case -NFS4ERR_BADLAYOUT: /* no layout */
9420 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009421 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009422 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009423 break;
9424 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009425 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009426 rpc_restart_call_prepare(task);
9427 return;
9428 }
9429 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009430}
9431
9432static void nfs4_layoutcommit_release(void *calldata)
9433{
9434 struct nfs4_layoutcommit_data *data = calldata;
9435
Andy Adamsondb29c082011-07-30 20:52:38 -04009436 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009437 nfs_post_op_update_inode_force_wcc(data->args.inode,
9438 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009439 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009440 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009441 kfree(data);
9442}
9443
9444static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9445 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9446 .rpc_call_done = nfs4_layoutcommit_done,
9447 .rpc_release = nfs4_layoutcommit_release,
9448};
9449
9450int
Andy Adamsonef311532011-03-12 02:58:10 -05009451nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009452{
9453 struct rpc_message msg = {
9454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9455 .rpc_argp = &data->args,
9456 .rpc_resp = &data->res,
9457 .rpc_cred = data->cred,
9458 };
9459 struct rpc_task_setup task_setup_data = {
9460 .task = &data->task,
9461 .rpc_client = NFS_CLIENT(data->args.inode),
9462 .rpc_message = &msg,
9463 .callback_ops = &nfs4_layoutcommit_ops,
9464 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009465 };
9466 struct rpc_task *task;
9467 int status = 0;
9468
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009469 dprintk("NFS: initiating layoutcommit call. sync %d "
9470 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009471 data->args.lastbytewritten,
9472 data->args.inode->i_ino);
9473
Trond Myklebust472e2592015-02-05 16:50:30 -05009474 if (!sync) {
9475 data->inode = nfs_igrab_and_active(data->args.inode);
9476 if (data->inode == NULL) {
9477 nfs4_layoutcommit_release(data);
9478 return -EAGAIN;
9479 }
9480 task_setup_data.flags = RPC_TASK_ASYNC;
9481 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009482 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009483 task = rpc_run_task(&task_setup_data);
9484 if (IS_ERR(task))
9485 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009486 if (sync)
9487 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009488 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009489 dprintk("%s: status %d\n", __func__, status);
9490 rpc_put_task(task);
9491 return status;
9492}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009493
Trond Myklebust302fad72019-02-18 13:32:38 -05009494/*
Andy Adamson97431202013-08-08 10:57:56 -04009495 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9496 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9497 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009498static int
9499_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009500 struct nfs_fsinfo *info,
9501 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009502{
9503 struct nfs41_secinfo_no_name_args args = {
9504 .style = SECINFO_STYLE_CURRENT_FH,
9505 };
9506 struct nfs4_secinfo_res res = {
9507 .flavors = flavors,
9508 };
9509 struct rpc_message msg = {
9510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9511 .rpc_argp = &args,
9512 .rpc_resp = &res,
9513 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009514 struct rpc_clnt *clnt = server->client;
Anna Schumakercc15e242019-08-14 16:22:31 -04009515 struct nfs4_call_sync_data data = {
9516 .seq_server = server,
9517 .seq_args = &args.seq_args,
9518 .seq_res = &res.seq_res,
9519 };
9520 struct rpc_task_setup task_setup = {
9521 .rpc_client = server->client,
9522 .rpc_message = &msg,
9523 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9524 .callback_data = &data,
9525 .flags = RPC_TASK_NO_ROUND_ROBIN,
9526 };
NeilBrowna52458b2018-12-03 11:30:31 +11009527 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009528 int status;
9529
9530 if (use_integrity) {
9531 clnt = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009532 task_setup.rpc_client = clnt;
9533
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009534 cred = nfs4_get_clid_cred(server->nfs_client);
9535 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009536 }
9537
9538 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009539 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9540 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009541 dprintk("<-- %s status=%d\n", __func__, status);
9542
NeilBrowna52458b2018-12-03 11:30:31 +11009543 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009544
9545 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009546}
9547
9548static int
9549nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9550 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9551{
Trond Myklebust0688e642019-04-07 13:59:09 -04009552 struct nfs4_exception exception = {
9553 .interruptible = true,
9554 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009555 int err;
9556 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009557 /* first try using integrity protection */
9558 err = -NFS4ERR_WRONGSEC;
9559
9560 /* try to use integrity protection with machine cred */
9561 if (_nfs4_is_integrity_protected(server->nfs_client))
9562 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9563 flavors, true);
9564
9565 /*
9566 * if unable to use integrity protection, or SECINFO with
9567 * integrity protection returns NFS4ERR_WRONGSEC (which is
9568 * disallowed by spec, but exists in deployed servers) use
9569 * the current filesystem's rpc_client and the user cred.
9570 */
9571 if (err == -NFS4ERR_WRONGSEC)
9572 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9573 flavors, false);
9574
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009575 switch (err) {
9576 case 0:
9577 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009578 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009579 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009580 default:
9581 err = nfs4_handle_exception(server, err, &exception);
9582 }
9583 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009584out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009585 return err;
9586}
9587
9588static int
9589nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9590 struct nfs_fsinfo *info)
9591{
9592 int err;
9593 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009594 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009595 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009596 struct nfs4_secinfo4 *secinfo;
9597 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009598
9599 page = alloc_page(GFP_KERNEL);
9600 if (!page) {
9601 err = -ENOMEM;
9602 goto out;
9603 }
9604
9605 flavors = page_address(page);
9606 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9607
9608 /*
9609 * Fall back on "guess and check" method if
9610 * the server doesn't support SECINFO_NO_NAME
9611 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009612 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009613 err = nfs4_find_root_sec(server, fhandle, info);
9614 goto out_freepage;
9615 }
9616 if (err)
9617 goto out_freepage;
9618
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009619 for (i = 0; i < flavors->num_flavors; i++) {
9620 secinfo = &flavors->flavors[i];
9621
9622 switch (secinfo->flavor) {
9623 case RPC_AUTH_NULL:
9624 case RPC_AUTH_UNIX:
9625 case RPC_AUTH_GSS:
9626 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9627 &secinfo->flavor_info);
9628 break;
9629 default:
9630 flavor = RPC_AUTH_MAXFLAVOR;
9631 break;
9632 }
9633
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009634 if (!nfs_auth_info_match(&server->auth_info, flavor))
9635 flavor = RPC_AUTH_MAXFLAVOR;
9636
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009637 if (flavor != RPC_AUTH_MAXFLAVOR) {
9638 err = nfs4_lookup_root_sec(server, fhandle,
9639 info, flavor);
9640 if (!err)
9641 break;
9642 }
9643 }
9644
9645 if (flavor == RPC_AUTH_MAXFLAVOR)
9646 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009647
9648out_freepage:
9649 put_page(page);
9650 if (err == -EACCES)
9651 return -EPERM;
9652out:
9653 return err;
9654}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009655
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009656static int _nfs41_test_stateid(struct nfs_server *server,
9657 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009658 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009659{
9660 int status;
9661 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009662 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009663 };
9664 struct nfs41_test_stateid_res res;
9665 struct rpc_message msg = {
9666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9667 .rpc_argp = &args,
9668 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009669 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009670 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009671 struct rpc_clnt *rpc_client = server->client;
9672
9673 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9674 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009675
Chuck Lever38527b12012-07-11 16:30:23 -04009676 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009677 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009678 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009679 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009680 if (status != NFS_OK) {
9681 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009682 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009683 }
9684 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009685 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009686}
9687
Trond Myklebust43912bb2016-09-22 13:38:56 -04009688static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9689 int err, struct nfs4_exception *exception)
9690{
9691 exception->retry = 0;
9692 switch(err) {
9693 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009694 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009695 nfs4_handle_exception(server, err, exception);
9696 break;
9697 case -NFS4ERR_BADSESSION:
9698 case -NFS4ERR_BADSLOT:
9699 case -NFS4ERR_BAD_HIGH_SLOT:
9700 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9701 case -NFS4ERR_DEADSESSION:
9702 nfs4_do_handle_exception(server, err, exception);
9703 }
9704}
9705
Chuck Lever38527b12012-07-11 16:30:23 -04009706/**
9707 * nfs41_test_stateid - perform a TEST_STATEID operation
9708 *
9709 * @server: server / transport on which to perform the operation
9710 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009711 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009712 *
9713 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9714 * Otherwise a negative NFS4ERR value is returned if the operation
9715 * failed or the state ID is not currently valid.
9716 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009717static int nfs41_test_stateid(struct nfs_server *server,
9718 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009719 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009720{
Trond Myklebust0688e642019-04-07 13:59:09 -04009721 struct nfs4_exception exception = {
9722 .interruptible = true,
9723 };
Bryan Schumaker7d974792011-06-02 14:59:08 -04009724 int err;
9725 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009726 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009727 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009728 } while (exception.retry);
9729 return err;
9730}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009731
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009732struct nfs_free_stateid_data {
9733 struct nfs_server *server;
9734 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009735 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009736};
9737
9738static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9739{
9740 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009741 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009742 &data->args.seq_args,
9743 &data->res.seq_res,
9744 task);
9745}
9746
9747static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9748{
9749 struct nfs_free_stateid_data *data = calldata;
9750
9751 nfs41_sequence_done(task, &data->res.seq_res);
9752
9753 switch (task->tk_status) {
9754 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009755 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009756 rpc_restart_call_prepare(task);
9757 }
9758}
9759
9760static void nfs41_free_stateid_release(void *calldata)
9761{
9762 kfree(calldata);
9763}
9764
Trond Myklebust17f26b12013-08-21 15:48:42 -04009765static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009766 .rpc_call_prepare = nfs41_free_stateid_prepare,
9767 .rpc_call_done = nfs41_free_stateid_done,
9768 .rpc_release = nfs41_free_stateid_release,
9769};
9770
Anna Schumaker2f261022018-05-15 13:03:39 -04009771/**
9772 * nfs41_free_stateid - perform a FREE_STATEID operation
9773 *
9774 * @server: server / transport on which to perform the operation
9775 * @stateid: state ID to release
9776 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009777 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009778 *
9779 * Note: this function is always asynchronous.
9780 */
9781static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009782 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009783 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009784 bool privileged)
9785{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009786 struct rpc_message msg = {
9787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009788 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009789 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009790 struct rpc_task_setup task_setup = {
9791 .rpc_client = server->client,
9792 .rpc_message = &msg,
9793 .callback_ops = &nfs41_free_stateid_ops,
9794 .flags = RPC_TASK_ASYNC,
9795 };
9796 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009797 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009798
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009799 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9800 &task_setup.rpc_client, &msg);
9801
Chuck Lever38527b12012-07-11 16:30:23 -04009802 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009803 data = kmalloc(sizeof(*data), GFP_NOFS);
9804 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009805 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009806 data->server = server;
9807 nfs4_stateid_copy(&data->args.stateid, stateid);
9808
9809 task_setup.callback_data = data;
9810
9811 msg.rpc_argp = &data->args;
9812 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009813 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009814 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009815 if (IS_ERR(task))
9816 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009817 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009818 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009819}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009820
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009821static void
9822nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009823{
NeilBrowna52458b2018-12-03 11:30:31 +11009824 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009825
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009826 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009827 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009828}
9829
Trond Myklebust36281ca2012-03-04 18:13:56 -05009830static bool nfs41_match_stateid(const nfs4_stateid *s1,
9831 const nfs4_stateid *s2)
9832{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009833 if (s1->type != s2->type)
9834 return false;
9835
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009836 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009837 return false;
9838
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009839 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009840 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009841
Anna Schumaker045c5512017-01-11 16:59:48 -05009842 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009843}
9844
Andy Adamson557134a2009-04-01 09:21:53 -04009845#endif /* CONFIG_NFS_V4_1 */
9846
Trond Myklebust36281ca2012-03-04 18:13:56 -05009847static bool nfs4_match_stateid(const nfs4_stateid *s1,
9848 const nfs4_stateid *s2)
9849{
Trond Myklebustf597c532012-03-04 18:13:56 -05009850 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009851}
9852
9853
Trond Myklebust17280172012-03-11 13:11:00 -04009854static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009855 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009856 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009857 .recover_open = nfs4_open_reclaim,
9858 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009859 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009860 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009861};
9862
Andy Adamson591d71c2009-04-01 09:22:47 -04009863#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009864static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009865 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9866 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9867 .recover_open = nfs4_open_reclaim,
9868 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009869 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009870 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009871 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009872};
9873#endif /* CONFIG_NFS_V4_1 */
9874
Trond Myklebust17280172012-03-11 13:11:00 -04009875static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009876 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009877 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009878 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009879 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009880 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009881};
9882
Andy Adamson591d71c2009-04-01 09:22:47 -04009883#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009884static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009885 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9886 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009887 .recover_open = nfs41_open_expired,
9888 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009889 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009890};
9891#endif /* CONFIG_NFS_V4_1 */
9892
Trond Myklebust17280172012-03-11 13:11:00 -04009893static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009894 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009895 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009896 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009897};
9898
9899#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009900static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009901 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009902 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009903 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009904};
9905#endif
9906
Chuck Leverec011fe2013-10-17 14:12:39 -04009907static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009908 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009909 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009910};
9911
9912#if defined(CONFIG_NFS_V4_1)
9913static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009914 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009915 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009916};
9917#endif /* CONFIG_NFS_V4_1 */
9918
Trond Myklebust97dc1352010-06-16 09:52:26 -04009919static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9920 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009921 .init_caps = NFS_CAP_READDIRPLUS
9922 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009923 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009924 .init_client = nfs40_init_client,
9925 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009926 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009927 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009928 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009929 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009930 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009931 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009932 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9933 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9934 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009935 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009936};
9937
9938#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009939static struct nfs_seqid *
9940nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9941{
9942 return NULL;
9943}
9944
Trond Myklebust97dc1352010-06-16 09:52:26 -04009945static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9946 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009947 .init_caps = NFS_CAP_READDIRPLUS
9948 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009949 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009950 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009951 | NFS_CAP_ATOMIC_OPEN_V1
9952 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009953 .init_client = nfs41_init_client,
9954 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009955 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009956 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009957 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009958 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009959 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009960 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009961 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009962 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9963 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9964 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009965 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009966};
9967#endif
9968
Steve Dickson42c2c422013-05-22 12:50:38 -04009969#if defined(CONFIG_NFS_V4_2)
9970static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9971 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009972 .init_caps = NFS_CAP_READDIRPLUS
9973 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009974 | NFS_CAP_POSIX_LOCK
9975 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009976 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009977 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009978 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009979 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009980 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -04009981 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009982 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009983 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009984 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -05009985 | NFS_CAP_CLONE
9986 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009987 .init_client = nfs41_init_client,
9988 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009989 .match_stateid = nfs41_match_stateid,
9990 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009991 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009992 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009993 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009994 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009995 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009996 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9997 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9998 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009999 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010000};
10001#endif
10002
Trond Myklebust97dc1352010-06-16 09:52:26 -040010003const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10004 [0] = &nfs_v4_0_minor_ops,
10005#if defined(CONFIG_NFS_V4_1)
10006 [1] = &nfs_v4_1_minor_ops,
10007#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010008#if defined(CONFIG_NFS_V4_2)
10009 [2] = &nfs_v4_2_minor_ops,
10010#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010011};
10012
Trond Myklebust13997822016-07-24 17:10:52 -040010013static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010014{
10015 ssize_t error, error2;
10016
10017 error = generic_listxattr(dentry, list, size);
10018 if (error < 0)
10019 return error;
10020 if (list) {
10021 list += error;
10022 size -= error;
10023 }
10024
10025 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10026 if (error2 < 0)
10027 return error2;
10028 return error + error2;
10029}
10030
Trond Myklebust17f26b12013-08-21 15:48:42 -040010031static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010032 .create = nfs_create,
10033 .lookup = nfs_lookup,
10034 .atomic_open = nfs_atomic_open,
10035 .link = nfs_link,
10036 .unlink = nfs_unlink,
10037 .symlink = nfs_symlink,
10038 .mkdir = nfs_mkdir,
10039 .rmdir = nfs_rmdir,
10040 .mknod = nfs_mknod,
10041 .rename = nfs_rename,
10042 .permission = nfs_permission,
10043 .getattr = nfs_getattr,
10044 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010045 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010046};
10047
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010048static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010049 .permission = nfs_permission,
10050 .getattr = nfs_getattr,
10051 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010052 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010053};
10054
David Howells509de812006-08-22 20:06:11 -040010055const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010056 .version = 4, /* protocol version */
10057 .dentry_ops = &nfs4_dentry_operations,
10058 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010059 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010060 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010061 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010062 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010063 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010064 .getattr = nfs4_proc_getattr,
10065 .setattr = nfs4_proc_setattr,
10066 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010067 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010068 .access = nfs4_proc_access,
10069 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070 .create = nfs4_proc_create,
10071 .remove = nfs4_proc_remove,
10072 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010073 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010074 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010075 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010076 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010077 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010078 .link = nfs4_proc_link,
10079 .symlink = nfs4_proc_symlink,
10080 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010081 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010082 .readdir = nfs4_proc_readdir,
10083 .mknod = nfs4_proc_mknod,
10084 .statfs = nfs4_proc_statfs,
10085 .fsinfo = nfs4_proc_fsinfo,
10086 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010087 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010088 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010089 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010090 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010091 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010092 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010093 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010094 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010095 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010096 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010098 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010099 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010100 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010101 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010102 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010103 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -040010104 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010105 .create_server = nfs4_create_server,
10106 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010107};
10108
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010109static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010110 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010111 .list = nfs4_xattr_list_nfs4_acl,
10112 .get = nfs4_xattr_get_nfs4_acl,
10113 .set = nfs4_xattr_set_nfs4_acl,
10114};
10115
10116const struct xattr_handler *nfs4_xattr_handlers[] = {
10117 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010118#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10119 &nfs4_xattr_nfs4_label_handler,
10120#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010121 NULL
10122};
10123
Linus Torvalds1da177e2005-04-16 15:20:36 -070010124/*
10125 * Local variables:
10126 * c-basic-offset: 8
10127 * End:
10128 */