blob: 8963062da57ec1e895cf6f746c48101558e7c43b [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
He Zhe59679d92020-07-06 17:52:24 +0800417 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400418 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
Anna Schumaker913fadc2020-07-08 10:33:40 -0400777static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
778 struct nfs4_slot *slot)
779{
780 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
781 if (!IS_ERR(task))
782 rpc_put_task_async(task);
783}
784
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400785static int nfs41_sequence_process(struct rpc_task *task,
786 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400787{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500788 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500789 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400790 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100791 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500792 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400793
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500794 if (slot == NULL)
795 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400796 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500797 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400798 goto out;
799
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500800 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400801 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500802
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400803 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100804
805 status = res->sr_status;
806 if (task->tk_status == -NFS4ERR_DEADSESSION)
807 status = -NFS4ERR_DEADSESSION;
808
Andy Adamson691daf32009-12-04 15:55:39 -0500809 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100810 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400811 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400812 /* Mark this sequence number as having been acked */
813 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400814 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400815 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500816 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500817 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400818 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
819 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500820 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400821 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500822 case 1:
823 /*
824 * sr_status remains 1 if an RPC level error occurred.
825 * The server may or may not have processed the sequence
826 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500827 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400828 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
829 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500830 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400831 case -NFS4ERR_DELAY:
832 /* The server detected a resend of the RPC call and
833 * returned NFS4ERR_DELAY as per Section 2.10.6.2
834 * of RFC5661.
835 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500836 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400837 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500838 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500839 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400840 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400842 case -NFS4ERR_RETRY_UNCACHED_REP:
843 case -NFS4ERR_SEQ_FALSE_RETRY:
844 /*
845 * The server thinks we tried to replay a request.
846 * Retry the call after bumping the sequence ID.
847 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400848 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400849 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500850 case -NFS4ERR_BADSLOT:
851 /*
852 * The slot id we used was probably retired. Try again
853 * using a different slot id.
854 */
Trond Myklebust99589102018-06-09 12:50:50 -0400855 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400856 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500857 goto retry_nowait;
858 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400859 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500860 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400861 * Were one or more calls using this slot interrupted?
862 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400863 * transmitted slot sequence number may be too high. However,
864 * if the server did receive the request then it might
865 * accidentally give us a reply with a mismatched operation.
866 * We can sort this out by sending a lone sequence operation
867 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500868 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400869 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
870 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400871 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
872 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
873 res->sr_slot = NULL;
874 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500875 goto retry_nowait;
876 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400877 /*
878 * RFC5661:
879 * A retry might be sent while the original request is
880 * still in progress on the replier. The replier SHOULD
881 * deal with the issue by returning NFS4ERR_DELAY as the
882 * reply to SEQUENCE or CB_SEQUENCE operation, but
883 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
884 *
885 * Restart the search after a delay.
886 */
887 slot->seq_nr = slot->seq_nr_highest_sent;
888 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100889 case -NFS4ERR_BADSESSION:
890 case -NFS4ERR_DEADSESSION:
891 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
892 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400893 default:
894 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400895 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400896 }
897out:
898 /* The session may be reset by one of the error handlers. */
899 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500900out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500901 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400902session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100903 nfs4_schedule_session_recovery(session, status);
904 dprintk("%s ERROR: %d Reset session\n", __func__, status);
905 nfs41_sequence_free_slot(res);
906 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400907retry_new_seq:
908 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500909retry_nowait:
910 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400911 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500912 task->tk_status = 0;
913 ret = 0;
914 }
915 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400916out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400917 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400918 goto out;
919 rpc_delay(task, NFS4_POLL_RETRY_MAX);
920 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400921}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400922
923int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
924{
925 if (!nfs41_sequence_process(task, res))
926 return 0;
927 if (res->sr_slot != NULL)
928 nfs41_sequence_free_slot(res);
929 return 1;
930
931}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500932EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400933
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400934static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
935{
936 if (res->sr_slot == NULL)
937 return 1;
938 if (res->sr_slot->table->session != NULL)
939 return nfs41_sequence_process(task, res);
940 return nfs40_sequence_done(task, res);
941}
942
943static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
944{
945 if (res->sr_slot != NULL) {
946 if (res->sr_slot->table->session != NULL)
947 nfs41_sequence_free_slot(res);
948 else
949 nfs40_sequence_free_slot(res);
950 }
951}
952
Peng Tao2c4b1312014-06-11 05:24:16 +0800953int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400954{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500955 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400956 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400957 if (!res->sr_slot->table->session)
958 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400959 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400960}
Peng Tao2c4b1312014-06-11 05:24:16 +0800961EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400962
Andy Adamsonce5039c2009-04-01 09:22:13 -0400963static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
964{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400965 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400966
Trond Myklebust035168ab2010-06-16 09:52:26 -0400967 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
968
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500969 nfs4_setup_sequence(data->seq_server->nfs_client,
970 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400971}
972
Andy Adamson69ab40c2009-04-01 09:22:19 -0400973static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400976
Trond Myklebust14516c32010-07-31 14:29:06 -0400977 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400978}
979
Trond Myklebust17280172012-03-11 13:11:00 -0400980static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400982 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400983};
984
Chuck Lever3bd23842013-08-09 12:49:19 -0400985#else /* !CONFIG_NFS_V4_1 */
986
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400987static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
988{
989 return nfs40_sequence_done(task, res);
990}
991
992static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
993{
994 if (res->sr_slot != NULL)
995 nfs40_sequence_free_slot(res);
996}
997
Peng Tao2c4b1312014-06-11 05:24:16 +0800998int nfs4_sequence_done(struct rpc_task *task,
999 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001000{
Chuck Lever3bd23842013-08-09 12:49:19 -04001001 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001002}
Peng Tao2c4b1312014-06-11 05:24:16 +08001003EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001004
1005#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001006
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001007static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1008{
1009 res->sr_timestamp = jiffies;
1010 res->sr_status_flags = 0;
1011 res->sr_status = 1;
1012}
1013
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001014static
1015void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1016 struct nfs4_sequence_res *res,
1017 struct nfs4_slot *slot)
1018{
1019 if (!slot)
1020 return;
1021 slot->privileged = args->sa_privileged ? 1 : 0;
1022 args->sa_slot = slot;
1023
1024 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001025}
1026
1027int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001028 struct nfs4_sequence_args *args,
1029 struct nfs4_sequence_res *res,
1030 struct rpc_task *task)
1031{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001032 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001033 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001034 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001035
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001036 /* slot already allocated? */
1037 if (res->sr_slot != NULL)
1038 goto out_start;
1039
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001040 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001041 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001042
Trond Myklebust3453d572018-06-20 17:53:34 -04001043 spin_lock(&tbl->slot_tbl_lock);
1044 /* The state manager will wait until the slot table is empty */
1045 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1046 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001047
Trond Myklebust3453d572018-06-20 17:53:34 -04001048 slot = nfs4_alloc_slot(tbl);
1049 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001050 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001051 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001052 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001053 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001054 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001055
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001056 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001057
Anna Schumakerad05cc02017-01-11 13:37:06 -05001058 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001059out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001060 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001061 rpc_call_start(task);
1062 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001063out_sleep_timeout:
1064 /* Try again in 1/4 second */
1065 if (args->sa_privileged)
1066 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1067 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1068 else
1069 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1070 NULL, jiffies + (HZ >> 2));
1071 spin_unlock(&tbl->slot_tbl_lock);
1072 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001073out_sleep:
1074 if (args->sa_privileged)
1075 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001076 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001077 else
1078 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1079 spin_unlock(&tbl->slot_tbl_lock);
1080 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001081}
1082EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1083
Chuck Lever9915ea72013-08-09 12:48:27 -04001084static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1085{
1086 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001087 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001088 data->seq_args, data->seq_res, task);
1089}
1090
1091static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1092{
1093 struct nfs4_call_sync_data *data = calldata;
1094 nfs4_sequence_done(task, data->seq_res);
1095}
1096
1097static const struct rpc_call_ops nfs40_call_sync_ops = {
1098 .rpc_call_prepare = nfs40_call_sync_prepare,
1099 .rpc_call_done = nfs40_call_sync_done,
1100};
1101
Anna Schumaker48c05852019-08-14 15:27:00 -04001102static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1103{
1104 int ret;
1105 struct rpc_task *task;
1106
1107 task = rpc_run_task(task_setup);
1108 if (IS_ERR(task))
1109 return PTR_ERR(task);
1110
1111 ret = task->tk_status;
1112 rpc_put_task(task);
1113 return ret;
1114}
1115
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001116static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1117 struct nfs_server *server,
1118 struct rpc_message *msg,
1119 struct nfs4_sequence_args *args,
1120 struct nfs4_sequence_res *res,
1121 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001122{
Chuck Lever9915ea72013-08-09 12:48:27 -04001123 struct nfs_client *clp = server->nfs_client;
1124 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001125 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001126 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001127 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001128 };
1129 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001130 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001131 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001132 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001133 .callback_data = &data,
1134 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001135 };
1136
Anna Schumaker48c05852019-08-14 15:27:00 -04001137 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001138}
1139
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001140static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1141 struct nfs_server *server,
1142 struct rpc_message *msg,
1143 struct nfs4_sequence_args *args,
1144 struct nfs4_sequence_res *res)
1145{
1146 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1147}
1148
1149
Bryan Schumaker7c513052011-03-24 17:12:24 +00001150int nfs4_call_sync(struct rpc_clnt *clnt,
1151 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001152 struct rpc_message *msg,
1153 struct nfs4_sequence_args *args,
1154 struct nfs4_sequence_res *res,
1155 int cache_reply)
1156{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001157 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001158 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001159}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Trond Myklebust3c591172018-07-31 15:54:10 -04001161static void
1162nfs4_inc_nlink_locked(struct inode *inode)
1163{
1164 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1165 inc_nlink(inode);
1166}
1167
1168static void
1169nfs4_dec_nlink_locked(struct inode *inode)
1170{
1171 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1172 drop_nlink(inode);
1173}
1174
1175static void
1176update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001177 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
1179 struct nfs_inode *nfsi = NFS_I(dir);
1180
Trond Myklebust16e14372018-03-20 16:53:31 -04001181 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1182 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001183 | NFS_INO_INVALID_DATA
1184 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001185 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001186 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1187 nfsi->attrtimeo_timestamp = jiffies;
1188 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001190 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001191 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1192 NFS_INO_INVALID_ACL;
1193 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001194 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001195 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001196 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001197 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001198 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001199}
1200
1201static void
1202update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001203 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001204{
1205 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001206 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 spin_unlock(&dir->i_lock);
1208}
1209
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001210struct nfs4_open_createattrs {
1211 struct nfs4_label *label;
1212 struct iattr *sattr;
1213 const __u32 verf[2];
1214};
1215
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001216static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1217 int err, struct nfs4_exception *exception)
1218{
1219 if (err != -EINVAL)
1220 return false;
1221 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1222 return false;
1223 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1224 exception->retry = 1;
1225 return true;
1226}
1227
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001228static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1229{
1230 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1231}
1232
1233static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1234{
1235 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1236
1237 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1238}
1239
Trond Myklebust6ae37332015-01-30 14:21:14 -05001240static u32
1241nfs4_map_atomic_open_share(struct nfs_server *server,
1242 fmode_t fmode, int openflags)
1243{
1244 u32 res = 0;
1245
1246 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1247 case FMODE_READ:
1248 res = NFS4_SHARE_ACCESS_READ;
1249 break;
1250 case FMODE_WRITE:
1251 res = NFS4_SHARE_ACCESS_WRITE;
1252 break;
1253 case FMODE_READ|FMODE_WRITE:
1254 res = NFS4_SHARE_ACCESS_BOTH;
1255 }
1256 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1257 goto out;
1258 /* Want no delegation if we're using O_DIRECT */
1259 if (openflags & O_DIRECT)
1260 res |= NFS4_SHARE_WANT_NO_DELEG;
1261out:
1262 return res;
1263}
1264
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001265static enum open_claim_type4
1266nfs4_map_atomic_open_claim(struct nfs_server *server,
1267 enum open_claim_type4 claim)
1268{
1269 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1270 return claim;
1271 switch (claim) {
1272 default:
1273 return claim;
1274 case NFS4_OPEN_CLAIM_FH:
1275 return NFS4_OPEN_CLAIM_NULL;
1276 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1277 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1278 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1279 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1280 }
1281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001284{
1285 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001286 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001287 p->o_res.seqid = p->o_arg.seqid;
1288 p->c_res.seqid = p->c_arg.seqid;
1289 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001290 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001291 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001292 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001293}
1294
Al Viro82a2c1b2011-06-22 18:30:55 -04001295static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001296 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001297 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001298 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001299 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001300{
Al Viro82a2c1b2011-06-22 18:30:55 -04001301 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001302 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001303 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001304 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001305 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001306 struct nfs4_opendata *p;
1307
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001308 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001309 if (p == NULL)
1310 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001311
1312 p->f_label = nfs4_label_alloc(server, gfp_mask);
1313 if (IS_ERR(p->f_label))
1314 goto err_free_p;
1315
Kinglong Meea49c2692015-07-27 15:31:38 +08001316 p->a_label = nfs4_label_alloc(server, gfp_mask);
1317 if (IS_ERR(p->a_label))
1318 goto err_free_f;
1319
Trond Myklebust63f5f792015-01-23 19:19:25 -05001320 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1321 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001322 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001323 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001324 nfs_sb_active(dentry->d_sb);
1325 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001326 p->dir = parent;
1327 p->owner = sp;
1328 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001329 p->o_arg.open_flags = flags;
1330 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001331 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001332 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1333 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001334 if (flags & O_CREAT) {
1335 p->o_arg.umask = current_umask();
1336 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1337 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1338 p->o_arg.u.attrs = &p->attrs;
1339 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1340
1341 memcpy(p->o_arg.u.verifier.data, c->verf,
1342 sizeof(p->o_arg.u.verifier.data));
1343 }
1344 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001345 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1346 * will return permission denied for all bits until close */
1347 if (!(flags & O_EXCL)) {
1348 /* ask server to check for all possible rights as results
1349 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001350 switch (p->o_arg.claim) {
1351 default:
1352 break;
1353 case NFS4_OPEN_CLAIM_NULL:
1354 case NFS4_OPEN_CLAIM_FH:
1355 p->o_arg.access = NFS4_ACCESS_READ |
1356 NFS4_ACCESS_MODIFY |
1357 NFS4_ACCESS_EXTEND |
1358 NFS4_ACCESS_EXECUTE;
1359 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001360 }
David Howells7539bba2006-08-22 20:06:09 -04001361 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001362 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1363 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001364 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001365 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001366 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001367 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001368 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001369 case NFS4_OPEN_CLAIM_NULL:
1370 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1371 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1372 p->o_arg.fh = NFS_FH(dir);
1373 break;
1374 case NFS4_OPEN_CLAIM_PREVIOUS:
1375 case NFS4_OPEN_CLAIM_FH:
1376 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1377 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001378 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001379 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001380 p->c_arg.fh = &p->o_res.fh;
1381 p->c_arg.stateid = &p->o_res.stateid;
1382 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001383 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001384 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001385 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001386
1387err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001388 nfs4_label_free(p->a_label);
1389err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001390 nfs4_label_free(p->f_label);
1391err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001392 kfree(p);
1393err:
1394 dput(parent);
1395 return NULL;
1396}
1397
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001398static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001399{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001400 struct nfs4_opendata *p = container_of(kref,
1401 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001402 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001403
Fred Isaman30ae2412016-10-18 13:39:51 -04001404 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001405 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001406 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001407 if (p->state != NULL)
1408 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001409 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001410
Kinglong Meea49c2692015-07-27 15:31:38 +08001411 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001412 nfs4_label_free(p->f_label);
1413
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001414 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001415 dput(p->dentry);
1416 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001417 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001418 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001419 kfree(p);
1420}
1421
1422static void nfs4_opendata_put(struct nfs4_opendata *p)
1423{
1424 if (p != NULL)
1425 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001426}
1427
Trond Myklebust24311f82015-09-20 10:50:17 -04001428static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1429 fmode_t fmode)
1430{
1431 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1432 case FMODE_READ|FMODE_WRITE:
1433 return state->n_rdwr != 0;
1434 case FMODE_WRITE:
1435 return state->n_wronly != 0;
1436 case FMODE_READ:
1437 return state->n_rdonly != 0;
1438 }
1439 WARN_ON_ONCE(1);
1440 return false;
1441}
1442
Trond Myklebustbe189f72018-09-27 17:12:33 -04001443static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1444 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001445{
1446 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001447
Trond Myklebust536e43d2012-01-17 22:04:26 -05001448 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001449 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001450 switch (claim) {
1451 case NFS4_OPEN_CLAIM_NULL:
1452 case NFS4_OPEN_CLAIM_FH:
1453 goto out;
1454 default:
1455 break;
1456 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001457 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001458 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001459 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1460 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001461 break;
1462 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001463 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1464 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001465 break;
1466 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001467 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1468 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001469 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001470out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001471 return ret;
1472}
1473
Trond Myklebust2a606182015-08-19 22:30:00 -05001474static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1475 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001476{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001477 if (delegation == NULL)
1478 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001479 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001480 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001481 switch (claim) {
1482 case NFS4_OPEN_CLAIM_NULL:
1483 case NFS4_OPEN_CLAIM_FH:
1484 break;
1485 case NFS4_OPEN_CLAIM_PREVIOUS:
1486 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1487 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001488 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001489 default:
1490 return 0;
1491 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001492 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001493 return 1;
1494}
1495
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001496static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001497{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001498 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001499 case FMODE_WRITE:
1500 state->n_wronly++;
1501 break;
1502 case FMODE_READ:
1503 state->n_rdonly++;
1504 break;
1505 case FMODE_READ|FMODE_WRITE:
1506 state->n_rdwr++;
1507 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001508 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001509}
1510
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001511#ifdef CONFIG_NFS_V4_1
1512static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1513{
1514 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1515 return true;
1516 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1517 return true;
1518 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1519 return true;
1520 return false;
1521}
1522#endif /* CONFIG_NFS_V4_1 */
1523
Trond Myklebustc9399f22017-11-06 15:28:01 -05001524static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1525{
1526 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1527 wake_up_all(&state->waitq);
1528}
1529
1530static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1531 const nfs4_stateid *stateid)
1532{
1533 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1534 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1535
1536 if (stateid_seqid == state_seqid + 1U ||
1537 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1538 nfs_state_log_update_open_stateid(state);
1539 else
1540 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1541}
1542
Trond Myklebust4f14c192014-02-12 19:15:06 -05001543static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001544{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001545 struct nfs_client *clp = state->owner->so_server->nfs_client;
1546 bool need_recover = false;
1547
1548 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1549 need_recover = true;
1550 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1551 need_recover = true;
1552 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1553 need_recover = true;
1554 if (need_recover)
1555 nfs4_state_mark_reclaim_nograce(clp, state);
1556}
1557
Trond Myklebustc9399f22017-11-06 15:28:01 -05001558/*
1559 * Check for whether or not the caller may update the open stateid
1560 * to the value passed in by stateid.
1561 *
1562 * Note: This function relies heavily on the server implementing
1563 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1564 * correctly.
1565 * i.e. The stateid seqids have to be initialised to 1, and
1566 * are then incremented on every state transition.
1567 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001568static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001569 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001570{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001571 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1572 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1573 if (stateid->seqid == cpu_to_be32(1))
1574 nfs_state_log_update_open_stateid(state);
1575 else
1576 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001577 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001578 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001579
1580 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1581 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001582 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001583 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001584 return false;
1585}
1586
Trond Myklebustf95549c2015-01-23 18:06:09 -05001587static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1588{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001589 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1590 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001591 if (state->n_wronly)
1592 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1593 if (state->n_rdonly)
1594 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1595 if (state->n_rdwr)
1596 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001597 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001598}
1599
Trond Myklebust226056c52014-02-11 10:41:07 -05001600static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1601 nfs4_stateid *stateid, fmode_t fmode)
1602{
1603 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1604 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1605 case FMODE_WRITE:
1606 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1607 break;
1608 case FMODE_READ:
1609 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1610 break;
1611 case 0:
1612 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1613 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1614 clear_bit(NFS_OPEN_STATE, &state->flags);
1615 }
1616 if (stateid == NULL)
1617 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001618 /* Handle OPEN+OPEN_DOWNGRADE races */
1619 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1620 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001621 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001622 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001623 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001624 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001625 nfs4_stateid_copy(&state->stateid, stateid);
1626 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001627 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001628out:
1629 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c52014-02-11 10:41:07 -05001630}
1631
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001632static void nfs_clear_open_stateid(struct nfs4_state *state,
1633 nfs4_stateid *arg_stateid,
1634 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c52014-02-11 10:41:07 -05001635{
1636 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001637 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1638 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1639 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c52014-02-11 10:41:07 -05001640 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001641 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1642 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c52014-02-11 10:41:07 -05001643}
1644
Trond Myklebust1393d962016-09-22 13:39:13 -04001645static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001646 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001647 __must_hold(&state->owner->so_lock)
1648 __must_hold(&state->seqlock)
1649 __must_hold(RCU)
1650
Trond Myklebust003707c2007-07-05 18:07:55 -04001651{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001652 DEFINE_WAIT(wait);
1653 int status = 0;
1654 for (;;) {
1655
1656 if (!nfs_need_update_open_stateid(state, stateid))
1657 return;
1658 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001659 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001660 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001661 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001662 /* Rely on seqids for serialisation with NFSv4.0 */
1663 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1664 break;
1665
1666 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1667 /*
1668 * Ensure we process the state changes in the same order
1669 * in which the server processed them by delaying the
1670 * update of the stateid until we are in sequence.
1671 */
1672 write_sequnlock(&state->seqlock);
1673 spin_unlock(&state->owner->so_lock);
1674 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001675 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001676 if (!signal_pending(current)) {
1677 if (schedule_timeout(5*HZ) == 0)
1678 status = -EAGAIN;
1679 else
1680 status = 0;
1681 } else
1682 status = -EINTR;
1683 finish_wait(&state->waitq, &wait);
1684 rcu_read_lock();
1685 spin_lock(&state->owner->so_lock);
1686 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001687 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001688
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001689 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1690 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001691 nfs4_stateid_copy(freeme, &state->open_stateid);
1692 nfs_test_and_clear_all_open_stateid(state);
1693 }
1694
Trond Myklebuste999e802014-02-10 18:20:47 -05001695 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1696 nfs4_stateid_copy(&state->stateid, stateid);
1697 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001698 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001699 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001700}
1701
Trond Myklebustc9399f22017-11-06 15:28:01 -05001702static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001703 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001704 fmode_t fmode,
1705 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001707 /*
1708 * Protect the call to nfs4_state_set_mode_locked and
1709 * serialise the stateid update
1710 */
1711 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001712 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1713 switch (fmode) {
1714 case FMODE_READ:
1715 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1716 break;
1717 case FMODE_WRITE:
1718 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1719 break;
1720 case FMODE_READ|FMODE_WRITE:
1721 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001722 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001723 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001724 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001725}
1726
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001727static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1728{
1729 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1730 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1731 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1732 clear_bit(NFS_OPEN_STATE, &state->flags);
1733}
1734
Trond Myklebustc9399f22017-11-06 15:28:01 -05001735static void nfs_state_set_delegation(struct nfs4_state *state,
1736 const nfs4_stateid *deleg_stateid,
1737 fmode_t fmode)
1738{
1739 /*
1740 * Protect the call to nfs4_state_set_mode_locked and
1741 * serialise the stateid update
1742 */
1743 write_seqlock(&state->seqlock);
1744 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1745 set_bit(NFS_DELEGATED_STATE, &state->flags);
1746 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001749static void nfs_state_clear_delegation(struct nfs4_state *state)
1750{
1751 write_seqlock(&state->seqlock);
1752 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1753 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1754 write_sequnlock(&state->seqlock);
1755}
1756
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001757int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001758 const nfs4_stateid *open_stateid,
1759 const nfs4_stateid *delegation,
1760 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001761{
Trond Myklebust1393d962016-09-22 13:39:13 -04001762 struct nfs_server *server = NFS_SERVER(state->inode);
1763 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001764 struct nfs_inode *nfsi = NFS_I(state->inode);
1765 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001766 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001767 int ret = 0;
1768
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001769 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001770
1771 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001772 spin_lock(&state->owner->so_lock);
1773 if (open_stateid != NULL) {
1774 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1775 ret = 1;
1776 }
1777
Trond Myklebust333ac782019-10-22 12:12:17 -04001778 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001779 if (deleg_cur == NULL)
1780 goto no_delegation;
1781
1782 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001783 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001784 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001785 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001786 goto no_delegation_unlock;
1787
1788 if (delegation == NULL)
1789 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001790 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001791 goto no_delegation_unlock;
1792
Trond Myklebustb7391f42008-12-23 15:21:52 -05001793 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001794 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001795 ret = 1;
1796no_delegation_unlock:
1797 spin_unlock(&deleg_cur->lock);
1798no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001799 if (ret)
1800 update_open_stateflags(state, fmode);
1801 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001802 rcu_read_unlock();
1803
Trond Myklebust4f14c192014-02-12 19:15:06 -05001804 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001805 nfs4_schedule_state_manager(clp);
1806 if (freeme.type != 0)
1807 nfs4_test_and_free_stateid(server, &freeme,
1808 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001809
1810 return ret;
1811}
1812
Trond Myklebust39071e62015-01-24 15:07:56 -05001813static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1814 const nfs4_stateid *stateid)
1815{
1816 struct nfs4_state *state = lsp->ls_state;
1817 bool ret = false;
1818
1819 spin_lock(&state->state_lock);
1820 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1821 goto out_noupdate;
1822 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1823 goto out_noupdate;
1824 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1825 ret = true;
1826out_noupdate:
1827 spin_unlock(&state->state_lock);
1828 return ret;
1829}
Trond Myklebust34310432008-12-23 15:21:38 -05001830
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001831static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001832{
1833 struct nfs_delegation *delegation;
1834
Trond Myklebustf5086242018-03-20 16:43:13 -04001835 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001836 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001837 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001838 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001839 rcu_read_unlock();
1840 return;
1841 }
1842 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001843 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001844}
1845
Trond Myklebust6ee41262007-07-08 14:11:36 -04001846static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001847{
1848 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001849 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001850 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001851 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001852 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001853 nfs4_stateid stateid;
1854 int ret = -EAGAIN;
1855
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001857 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001858 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001859 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001860 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001861 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001862 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001863 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001864 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001865 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001866 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001867 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001868 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001869 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001870 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001871 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001872 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001873 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001874 if (!opendata->is_recover) {
1875 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1876 if (ret != 0)
1877 goto out;
1878 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001879 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001880
1881 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001882 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001883 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001884 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001885out:
1886 return ERR_PTR(ret);
1887out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001888 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001889 return state;
1890}
1891
Andy Adamsone23008e2012-10-02 21:07:32 -04001892static void
1893nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1894{
1895 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1896 struct nfs_delegation *delegation;
1897 int delegation_flags = 0;
1898
1899 rcu_read_lock();
1900 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1901 if (delegation)
1902 delegation_flags = delegation->flags;
1903 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001904 switch (data->o_arg.claim) {
1905 default:
1906 break;
1907 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1908 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001909 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1910 "returning a delegation for "
1911 "OPEN(CLAIM_DELEGATE_CUR)\n",
1912 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001913 return;
1914 }
1915 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001916 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001917 data->owner->so_cred,
1918 data->o_res.delegation_type,
1919 &data->o_res.delegation,
1920 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001921 else
1922 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001923 data->owner->so_cred,
1924 data->o_res.delegation_type,
1925 &data->o_res.delegation,
1926 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001927
1928 if (data->o_res.do_recall)
1929 nfs_async_inode_return_delegation(state->inode,
1930 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001931}
1932
1933/*
1934 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1935 * and update the nfs4_state.
1936 */
1937static struct nfs4_state *
1938_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1939{
1940 struct inode *inode = data->state->inode;
1941 struct nfs4_state *state = data->state;
1942 int ret;
1943
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001944 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001945 if (data->rpc_status)
1946 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001947 /* cached opens have already been processed */
1948 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001949 }
1950
Andy Adamsone23008e2012-10-02 21:07:32 -04001951 ret = nfs_refresh_inode(inode, &data->f_attr);
1952 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001953 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001954
1955 if (data->o_res.delegation_type != 0)
1956 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001957update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001958 if (!update_open_stateid(state, &data->o_res.stateid,
1959 NULL, data->o_arg.fmode))
1960 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001961 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001962
1963 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001964}
1965
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001966static struct inode *
1967nfs4_opendata_get_inode(struct nfs4_opendata *data)
1968{
1969 struct inode *inode;
1970
1971 switch (data->o_arg.claim) {
1972 case NFS4_OPEN_CLAIM_NULL:
1973 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1974 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1975 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1976 return ERR_PTR(-EAGAIN);
1977 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1978 &data->f_attr, data->f_label);
1979 break;
1980 default:
1981 inode = d_inode(data->dentry);
1982 ihold(inode);
1983 nfs_refresh_inode(inode, &data->f_attr);
1984 }
1985 return inode;
1986}
1987
Andy Adamsone23008e2012-10-02 21:07:32 -04001988static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001989nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1990{
1991 struct nfs4_state *state;
1992 struct inode *inode;
1993
1994 inode = nfs4_opendata_get_inode(data);
1995 if (IS_ERR(inode))
1996 return ERR_CAST(inode);
1997 if (data->state != NULL && data->state->inode == inode) {
1998 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001999 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002000 } else
2001 state = nfs4_get_open_state(inode, data->owner);
2002 iput(inode);
2003 if (state == NULL)
2004 state = ERR_PTR(-ENOMEM);
2005 return state;
2006}
2007
Andy Adamsone23008e2012-10-02 21:07:32 -04002008static struct nfs4_state *
2009_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002010{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002011 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002012
Trond Myklebustaac00a82007-07-05 19:02:21 -04002013 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002014 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002015 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002016 goto out;
2017 }
2018
Trond Myklebust75e8c482017-08-08 10:38:07 -04002019 state = nfs4_opendata_find_nfs4_state(data);
2020 if (IS_ERR(state))
2021 goto out;
2022
Andy Adamsone23008e2012-10-02 21:07:32 -04002023 if (data->o_res.delegation_type != 0)
2024 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002025 if (!update_open_stateid(state, &data->o_res.stateid,
2026 NULL, data->o_arg.fmode)) {
2027 nfs4_put_open_state(state);
2028 state = ERR_PTR(-EAGAIN);
2029 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002030out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002031 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002032 return state;
2033}
2034
Andy Adamsone23008e2012-10-02 21:07:32 -04002035static struct nfs4_state *
2036nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2037{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002038 struct nfs4_state *ret;
2039
Andy Adamsone23008e2012-10-02 21:07:32 -04002040 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002041 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2042 else
2043 ret = _nfs4_opendata_to_nfs4_state(data);
2044 nfs4_sequence_free_slot(&data->o_res.seq_res);
2045 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002046}
2047
Trond Myklebust0de43972018-09-02 15:57:01 -04002048static struct nfs_open_context *
2049nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002050{
2051 struct nfs_inode *nfsi = NFS_I(state->inode);
2052 struct nfs_open_context *ctx;
2053
Trond Myklebust0de43972018-09-02 15:57:01 -04002054 rcu_read_lock();
2055 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002056 if (ctx->state != state)
2057 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002058 if ((ctx->mode & mode) != mode)
2059 continue;
2060 if (!get_nfs_open_context(ctx))
2061 continue;
2062 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002063 return ctx;
2064 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002065 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002066 return ERR_PTR(-ENOENT);
2067}
2068
Trond Myklebust0de43972018-09-02 15:57:01 -04002069static struct nfs_open_context *
2070nfs4_state_find_open_context(struct nfs4_state *state)
2071{
2072 struct nfs_open_context *ctx;
2073
2074 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2075 if (!IS_ERR(ctx))
2076 return ctx;
2077 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2078 if (!IS_ERR(ctx))
2079 return ctx;
2080 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2081}
2082
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002083static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2084 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002085{
2086 struct nfs4_opendata *opendata;
2087
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002088 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002089 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002090 if (opendata == NULL)
2091 return ERR_PTR(-ENOMEM);
2092 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002093 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002094 return opendata;
2095}
2096
Trond Myklebust24311f82015-09-20 10:50:17 -04002097static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2098 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002099{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002100 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002101 int ret;
2102
Trond Myklebust24311f82015-09-20 10:50:17 -04002103 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002104 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002105 opendata->o_arg.open_flags = 0;
2106 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002107 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2108 NFS_SB(opendata->dentry->d_sb),
2109 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002110 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2111 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2112 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002113 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002114 if (ret != 0)
2115 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002116 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002117 if (IS_ERR(newstate))
2118 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002119 if (newstate != opendata->state)
2120 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002121 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002122 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002123}
2124
2125static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2126{
Trond Myklebust864472e2006-01-03 09:55:15 +01002127 int ret;
2128
2129 /* memory barrier prior to reading state->n_* */
2130 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002131 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2132 if (ret != 0)
2133 return ret;
2134 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2135 if (ret != 0)
2136 return ret;
2137 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2138 if (ret != 0)
2139 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002140 /*
2141 * We may have performed cached opens for all three recoveries.
2142 * Check if we need to update the current stateid.
2143 */
2144 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002145 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002146 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002147 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002148 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002149 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002150 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002151 return 0;
2152}
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154/*
2155 * OPEN_RECLAIM:
2156 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002158static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002160 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002161 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002162 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 int status;
2164
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002165 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2166 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002167 if (IS_ERR(opendata))
2168 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002169 rcu_read_lock();
2170 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002171 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002172 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002173 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002174 opendata->o_arg.u.delegation_type = delegation_type;
2175 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002176 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return status;
2178}
2179
Trond Myklebust539cd032007-06-05 11:46:42 -04002180static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 struct nfs_server *server = NFS_SERVER(state->inode);
2183 struct nfs4_exception exception = { };
2184 int err;
2185 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002186 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002187 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002188 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2189 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002190 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002191 break;
2192 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 } while (exception.retry);
2194 return err;
2195}
2196
Trond Myklebust864472e2006-01-03 09:55:15 +01002197static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2198{
2199 struct nfs_open_context *ctx;
2200 int ret;
2201
2202 ctx = nfs4_state_find_open_context(state);
2203 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002204 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002205 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2206 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002207 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002208 put_nfs_open_context(ctx);
2209 return ret;
2210}
2211
NeilBrowndce26302017-12-13 09:57:09 +11002212static 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 -07002213{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002214 switch (err) {
2215 default:
2216 printk(KERN_ERR "NFS: %s: unhandled error "
2217 "%d.\n", __func__, err);
2218 case 0:
2219 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002220 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002221 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002222 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002223 break;
2224 case -NFS4ERR_BADSESSION:
2225 case -NFS4ERR_BADSLOT:
2226 case -NFS4ERR_BAD_HIGH_SLOT:
2227 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2228 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002229 return -EAGAIN;
2230 case -NFS4ERR_STALE_CLIENTID:
2231 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002232 /* Don't recall a delegation if it was lost */
2233 nfs4_schedule_lease_recovery(server->nfs_client);
2234 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002235 case -NFS4ERR_MOVED:
2236 nfs4_schedule_migration_recovery(server);
2237 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002238 case -NFS4ERR_LEASE_MOVED:
2239 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2240 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002241 case -NFS4ERR_DELEG_REVOKED:
2242 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002243 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002244 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002245 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002246 nfs_inode_find_state_and_recover(state->inode,
2247 stateid);
2248 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002249 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002250 case -NFS4ERR_DELAY:
2251 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002252 ssleep(1);
2253 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002254 case -ENOMEM:
2255 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002256 if (fl) {
2257 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2258 if (lsp)
2259 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2260 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002261 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return err;
2264}
2265
Trond Myklebust24311f82015-09-20 10:50:17 -04002266int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002267 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002268{
2269 struct nfs_server *server = NFS_SERVER(state->inode);
2270 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002271 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002272
2273 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2274 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2275 if (IS_ERR(opendata))
2276 return PTR_ERR(opendata);
2277 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002278 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002279 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2280 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002281 goto out;
2282 }
2283 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002284 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2285 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002286 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002287 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002288 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2289 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2290 if (err)
2291 goto out;
2292 }
2293 nfs_state_clear_delegation(state);
2294out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002295 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002296 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002297}
2298
Chuck Leverbe05c862013-08-09 12:49:47 -04002299static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2300{
2301 struct nfs4_opendata *data = calldata;
2302
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002303 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2304 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002305}
2306
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002307static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2308{
2309 struct nfs4_opendata *data = calldata;
2310
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002311 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002312
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002313 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002314 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002315 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002316 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002317 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002318 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002319 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002320}
2321
2322static void nfs4_open_confirm_release(void *calldata)
2323{
2324 struct nfs4_opendata *data = calldata;
2325 struct nfs4_state *state = NULL;
2326
2327 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002328 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002329 goto out_free;
2330 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002331 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002332 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002333 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002334 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002335 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002336out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002337 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002338}
2339
2340static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002341 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002342 .rpc_call_done = nfs4_open_confirm_done,
2343 .rpc_release = nfs4_open_confirm_release,
2344};
2345
2346/*
2347 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2348 */
2349static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2350{
David Howells2b0143b2015-03-17 22:25:59 +00002351 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002352 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002353 struct rpc_message msg = {
2354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2355 .rpc_argp = &data->c_arg,
2356 .rpc_resp = &data->c_res,
2357 .rpc_cred = data->owner->so_cred,
2358 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002359 struct rpc_task_setup task_setup_data = {
2360 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002361 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002362 .callback_ops = &nfs4_open_confirm_ops,
2363 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002364 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002365 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002366 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 int status;
2368
Anna Schumakerfba83f32018-05-04 16:22:50 -04002369 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2370 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002371 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002372 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002373 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002374 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002375 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002376 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002377 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002378 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002379 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002380 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002381 smp_wmb();
2382 } else
2383 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002384 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return status;
2386}
2387
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002388static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002390 struct nfs4_opendata *data = calldata;
2391 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002392 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002393 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002394
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002395 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002396 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002397 /*
2398 * Check if we still need to send an OPEN call, or if we can use
2399 * a delegation instead.
2400 */
2401 if (data->state != NULL) {
2402 struct nfs_delegation *delegation;
2403
Trond Myklebustbe189f72018-09-27 17:12:33 -04002404 if (can_open_cached(data->state, data->o_arg.fmode,
2405 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002406 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002407 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002408 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002409 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002410 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002411 rcu_read_unlock();
2412 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002413 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002414 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002415 switch (claim) {
2416 default:
2417 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002418 case NFS4_OPEN_CLAIM_PREVIOUS:
2419 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2420 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002421 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002422 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002423 case NFS4_OPEN_CLAIM_FH:
2424 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002425 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002426 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002427 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002428 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002429 &data->o_res.seq_res,
2430 task) != 0)
2431 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002432
2433 /* Set the create mode (note dependency on the session type) */
2434 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2435 if (data->o_arg.open_flags & O_EXCL) {
2436 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2437 if (nfs4_has_persistent_session(clp))
2438 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2439 else if (clp->cl_mvops->minor_version > 0)
2440 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2441 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002442 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002443unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002444 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002445 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002446out_no_action:
2447 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002448out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002449 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002450}
2451
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002452static void nfs4_open_done(struct rpc_task *task, void *calldata)
2453{
2454 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002456 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002457
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002458 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002459 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002460
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002461 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002462 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2463 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002464 case S_IFREG:
2465 break;
2466 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002467 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002468 break;
2469 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002470 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002471 break;
2472 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002473 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002474 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002475 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002476 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002477 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2478 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002479 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002480 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002481}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002482
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002483static void nfs4_open_release(void *calldata)
2484{
2485 struct nfs4_opendata *data = calldata;
2486 struct nfs4_state *state = NULL;
2487
2488 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002489 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002490 goto out_free;
2491 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002492 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002493 goto out_free;
2494 /* In case we need an open_confirm, no cleanup! */
2495 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2496 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002497 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002498 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002499 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002500out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002501 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002502}
2503
2504static const struct rpc_call_ops nfs4_open_ops = {
2505 .rpc_call_prepare = nfs4_open_prepare,
2506 .rpc_call_done = nfs4_open_done,
2507 .rpc_release = nfs4_open_release,
2508};
2509
Fred Isaman3b65a302016-09-19 10:06:49 -04002510static int nfs4_run_open_task(struct nfs4_opendata *data,
2511 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002512{
David Howells2b0143b2015-03-17 22:25:59 +00002513 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002514 struct nfs_server *server = NFS_SERVER(dir);
2515 struct nfs_openargs *o_arg = &data->o_arg;
2516 struct nfs_openres *o_res = &data->o_res;
2517 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002518 struct rpc_message msg = {
2519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2520 .rpc_argp = o_arg,
2521 .rpc_resp = o_res,
2522 .rpc_cred = data->owner->so_cred,
2523 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002524 struct rpc_task_setup task_setup_data = {
2525 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002526 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002527 .callback_ops = &nfs4_open_ops,
2528 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002529 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002530 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002531 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002532 int status;
2533
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002534 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002535 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002536 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002537 data->cancelled = false;
2538 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002539 if (!ctx) {
2540 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002541 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002542 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002543 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002544 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002545 pnfs_lgopen_prepare(data, ctx);
2546 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002547 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002548 if (IS_ERR(task))
2549 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002550 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002551 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002552 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002553 smp_wmb();
2554 } else
2555 status = data->rpc_status;
2556 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002557
2558 return status;
2559}
2560
2561static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2562{
David Howells2b0143b2015-03-17 22:25:59 +00002563 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002564 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002565 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002566
Fred Isaman3b65a302016-09-19 10:06:49 -04002567 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002568 if (status != 0 || !data->rpc_done)
2569 return status;
2570
Trond Myklebust6926afd12012-01-07 13:22:46 -05002571 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2572
Anna Schumakerd7e98252017-01-11 16:13:29 -05002573 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002574 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002575
2576 return status;
2577}
2578
Trond Myklebustf3792d62014-07-10 08:54:32 -04002579/*
2580 * Additional permission checks in order to distinguish between an
2581 * open for read, and an open for execute. This works around the
2582 * fact that NFSv4 OPEN treats read and execute permissions as being
2583 * the same.
2584 * Note that in the non-execute case, we want to turn off permission
2585 * checking if we just created a new file (POSIX open() semantics).
2586 */
NeilBrowna52458b2018-12-03 11:30:31 +11002587static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002588 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002589 struct nfs4_state *state, fmode_t fmode,
2590 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002591{
2592 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002593 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002594
2595 /* access call failed or for some reason the server doesn't
2596 * support any access modes -- defer access call until later */
2597 if (opendata->o_res.access_supported == 0)
2598 return 0;
2599
2600 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002601 /*
2602 * Use openflags to check for exec, because fmode won't
2603 * always have FMODE_EXEC set when file open for exec.
2604 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002605 if (openflags & __FMODE_EXEC) {
2606 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002607 if (S_ISDIR(state->inode->i_mode))
2608 mask = NFS4_ACCESS_LOOKUP;
2609 else
2610 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002611 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002612 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002613
2614 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002615 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2616 nfs_access_add_cache(state->inode, &cache);
2617
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002618 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2619 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002620 return 0;
2621
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002622 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002623}
2624
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002625/*
2626 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2627 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002628static int _nfs4_proc_open(struct nfs4_opendata *data,
2629 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002630{
David Howells2b0143b2015-03-17 22:25:59 +00002631 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002632 struct nfs_server *server = NFS_SERVER(dir);
2633 struct nfs_openargs *o_arg = &data->o_arg;
2634 struct nfs_openres *o_res = &data->o_res;
2635 int status;
2636
Fred Isaman3b65a302016-09-19 10:06:49 -04002637 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002638 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002639 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002640 if (status != 0) {
2641 if (status == -NFS4ERR_BADNAME &&
2642 !(o_arg->open_flags & O_CREAT))
2643 return -ENOENT;
2644 return status;
2645 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002646
Trond Myklebust6926afd12012-01-07 13:22:46 -05002647 nfs_fattr_map_and_free_names(server, &data->f_attr);
2648
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002649 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002650 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002651 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002652 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002653 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002654 if (data->file_created ||
2655 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002656 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002657 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002658 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002659 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2660 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002662 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002664 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002666 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2667 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002668 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2669 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002670 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674/*
2675 * OPEN_EXPIRED:
2676 * reclaim state on the server after a network partition.
2677 * Assumes caller holds the appropriate lock
2678 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002679static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002681 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002682 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002684 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002685 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002686 if (IS_ERR(opendata))
2687 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002688 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002689 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002690 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002691 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002692 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002695static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002696{
Trond Myklebust539cd032007-06-05 11:46:42 -04002697 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002698 struct nfs4_exception exception = { };
2699 int err;
2700
2701 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002702 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002703 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002704 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2705 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002706 switch (err) {
2707 default:
2708 goto out;
2709 case -NFS4ERR_GRACE:
2710 case -NFS4ERR_DELAY:
2711 nfs4_handle_exception(server, err, &exception);
2712 err = 0;
2713 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002714 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002715out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002716 return err;
2717}
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002722 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Trond Myklebust864472e2006-01-03 09:55:15 +01002724 ctx = nfs4_state_find_open_context(state);
2725 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002726 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002727 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002728 put_nfs_open_context(ctx);
2729 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Trond Myklebust41020b62016-09-22 13:38:58 -04002732static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2733 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002734{
Trond Myklebust41020b62016-09-22 13:38:58 -04002735 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002736 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002737}
2738
2739static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2740{
2741 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002742 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002743}
2744
2745static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2746{
2747 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2748 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002749 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002750 return nfs4_open_expired(sp, state);
2751}
2752
Trond Myklebust45870d62016-09-22 13:38:59 -04002753static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2754 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002755 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002756{
2757 return -NFS4ERR_BAD_STATEID;
2758}
2759
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002760#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002761static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2762 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002763 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002764{
2765 int status;
2766
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002767 switch (stateid->type) {
2768 default:
2769 break;
2770 case NFS4_INVALID_STATEID_TYPE:
2771 case NFS4_SPECIAL_STATEID_TYPE:
2772 return -NFS4ERR_BAD_STATEID;
2773 case NFS4_REVOKED_STATEID_TYPE:
2774 goto out_free;
2775 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002776
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002777 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002778 switch (status) {
2779 case -NFS4ERR_EXPIRED:
2780 case -NFS4ERR_ADMIN_REVOKED:
2781 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002782 break;
2783 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002784 return status;
2785 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002786out_free:
2787 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002788 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002789 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002790}
2791
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002792static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002793{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002794 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002795 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002796 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002797 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002798 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002799
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002800 /* Get the delegation credential for use by test/free_stateid */
2801 rcu_read_lock();
2802 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002803 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002804 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002805 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002806 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002807 }
2808
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002809 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002810 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002811
Trond Myklebust994b15b2018-09-05 14:07:14 -04002812 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2813 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002814 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002815 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002816 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002817 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002818
NeilBrowna52458b2018-12-03 11:30:31 +11002819 if (delegation->cred)
2820 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002821 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002822 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002823 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002824 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002825 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002826 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002827 else
2828 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002829
Trond Myklebust8c39a392019-07-19 13:48:44 -04002830 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002831 return ret;
2832}
2833
2834static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2835{
2836 nfs4_stateid tmp;
2837
2838 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2839 nfs4_copy_delegation_stateid(state->inode, state->state,
2840 &tmp, NULL) &&
2841 nfs4_stateid_match_other(&state->stateid, &tmp))
2842 nfs_state_set_delegation(state, &tmp, state->state);
2843 else
2844 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002845}
2846
2847/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002848 * nfs41_check_expired_locks - possibly free a lock stateid
2849 *
2850 * @state: NFSv4 state for an inode
2851 *
2852 * Returns NFS_OK if recovery for this stateid is now finished.
2853 * Otherwise a negative NFS4ERR value is returned.
2854 */
2855static int nfs41_check_expired_locks(struct nfs4_state *state)
2856{
2857 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002858 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002859 struct nfs_server *server = NFS_SERVER(state->inode);
2860
2861 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2862 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002863
2864 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002865 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2866 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002867 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002868
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002869 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002870 spin_unlock(&state->state_lock);
2871
2872 nfs4_put_lock_state(prev);
2873 prev = lsp;
2874
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002875 status = nfs41_test_and_free_expired_stateid(server,
2876 &lsp->ls_stateid,
2877 cred);
2878 trace_nfs4_test_lock_stateid(state, lsp, status);
2879 if (status == -NFS4ERR_EXPIRED ||
2880 status == -NFS4ERR_BAD_STATEID) {
2881 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002882 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002883 if (!recover_lost_locks)
2884 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2885 } else if (status != NFS_OK) {
2886 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002887 nfs4_put_lock_state(prev);
2888 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002889 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002890 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002891 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002892 }
2893 spin_unlock(&state->state_lock);
2894 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002895out:
2896 return ret;
2897}
2898
2899/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002900 * nfs41_check_open_stateid - possibly free an open stateid
2901 *
2902 * @state: NFSv4 state for an inode
2903 *
2904 * Returns NFS_OK if recovery for this stateid is now finished.
2905 * Otherwise a negative NFS4ERR value is returned.
2906 */
2907static int nfs41_check_open_stateid(struct nfs4_state *state)
2908{
2909 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002910 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002911 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002912 int status;
2913
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002914 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002915 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002916 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002917 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002918 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002919 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002920 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002921 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002922 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002923 if (nfs_open_stateid_recover_openmode(state))
2924 return -NFS4ERR_OPENMODE;
2925 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002926}
2927
2928static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2929{
Chuck Levereb64cf92012-07-11 16:30:05 -04002930 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002931
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002932 status = nfs41_check_delegation_stateid(state);
2933 if (status != NFS_OK)
2934 return status;
2935 nfs41_delegation_recover_stateid(state);
2936
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002937 status = nfs41_check_expired_locks(state);
2938 if (status != NFS_OK)
2939 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002940 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002941 if (status != NFS_OK)
2942 status = nfs4_open_expired(sp, state);
2943 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002944}
2945#endif
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002948 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2949 * fields corresponding to attributes that were used to store the verifier.
2950 * Make sure we clobber those fields in the later setattr call
2951 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002952static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002953 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002954{
Trond Myklebust609339c2018-03-28 16:18:17 -04002955 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2956 __u32 attrset[3];
2957 unsigned ret;
2958 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002959
Trond Myklebust609339c2018-03-28 16:18:17 -04002960 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2961 attrset[i] = opendata->o_res.attrset[i];
2962 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2963 attrset[i] &= ~bitmask[i];
2964 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002965
Trond Myklebust609339c2018-03-28 16:18:17 -04002966 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2967 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002968
Trond Myklebust609339c2018-03-28 16:18:17 -04002969 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2970 if (sattr->ia_valid & ATTR_ATIME_SET)
2971 ret |= ATTR_ATIME_SET;
2972 else
2973 ret |= ATTR_ATIME;
2974 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002975
Trond Myklebust609339c2018-03-28 16:18:17 -04002976 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2977 if (sattr->ia_valid & ATTR_MTIME_SET)
2978 ret |= ATTR_MTIME_SET;
2979 else
2980 ret |= ATTR_MTIME;
2981 }
2982
2983 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002984 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002985 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002986}
2987
Trond Myklebustc21443c2013-02-07 14:26:21 -05002988static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002989 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002990{
2991 struct nfs4_state_owner *sp = opendata->owner;
2992 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002993 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002994 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002995 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05002996 struct inode *dir = d_inode(opendata->dir);
2997 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002998 unsigned int seq;
2999 int ret;
3000
3001 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003002 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003003
Fred Isaman3b65a302016-09-19 10:06:49 -04003004 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003005 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003006 goto out;
3007
Trond Myklebustae55e592018-05-22 11:17:16 -04003008 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003009 ret = PTR_ERR(state);
3010 if (IS_ERR(state))
3011 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003012 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003013 if (server->caps & NFS_CAP_POSIX_LOCK)
3014 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003015 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3016 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003017
Trond Myklebust275bb302013-05-29 13:11:28 -04003018 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003019 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003020 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003021 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003022 alias = d_exact_alias(dentry, state->inode);
3023 if (!alias)
3024 alias = d_splice_alias(igrab(state->inode), dentry);
3025 /* d_splice_alias() can't fail here - it's a non-directory */
3026 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003027 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003028 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003029 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003030 }
3031
3032 switch(opendata->o_arg.claim) {
3033 default:
3034 break;
3035 case NFS4_OPEN_CLAIM_NULL:
3036 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3037 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3038 if (!opendata->rpc_done)
3039 break;
3040 if (opendata->o_res.delegation_type != 0)
3041 dir_verifier = nfs_save_change_attribute(dir);
3042 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003043 }
3044
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003045 /* Parse layoutget results before we check for access */
3046 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3047
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003048 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3049 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003050 if (ret != 0)
3051 goto out;
3052
David Howells2b0143b2015-03-17 22:25:59 +00003053 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003054 nfs_inode_attach_open_context(ctx);
3055 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3056 nfs4_schedule_stateid_recovery(server, state);
3057 }
Fred Isaman2409a972016-10-06 12:11:21 -04003058
Trond Myklebustc21443c2013-02-07 14:26:21 -05003059out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003060 if (!opendata->cancelled)
3061 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003062 return ret;
3063}
3064
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003065/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003066 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 */
Andy Adamson82be4172012-05-23 05:02:35 -04003068static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003069 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003070 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003071 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003072 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
3074 struct nfs4_state_owner *sp;
3075 struct nfs4_state *state = NULL;
3076 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003077 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003078 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003079 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003080 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003081 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003082 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003083 struct iattr *sattr = c->sattr;
3084 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003085 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003086 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003090 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3091 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3093 goto out_err;
3094 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003095 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003096 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003097 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003098 if (d_really_is_positive(dentry))
3099 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003100 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003101 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003102 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003103 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3104 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003105 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003106 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
David Quigley14c43f72013-05-22 12:50:43 -04003108 if (label) {
3109 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3110 if (IS_ERR(olabel)) {
3111 status = PTR_ERR(olabel);
3112 goto err_opendata_put;
3113 }
3114 }
3115
Trond Myklebuste911b812014-03-26 13:24:37 -07003116 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3117 if (!opendata->f_attr.mdsthreshold) {
3118 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3119 if (!opendata->f_attr.mdsthreshold)
3120 goto err_free_label;
3121 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003122 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003123 }
David Howells2b0143b2015-03-17 22:25:59 +00003124 if (d_really_is_positive(dentry))
3125 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003126
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003127 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003128 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003129 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003130 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003131
NeilBrownefcbc042015-07-30 13:00:56 +10003132 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003133 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003134 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003135 /*
3136 * send create attributes which was not set by open
3137 * with an extra setattr.
3138 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003139 if (attrs || label) {
3140 unsigned ia_old = sattr->ia_valid;
3141
3142 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003143 nfs_fattr_init(opendata->o_res.f_attr);
3144 status = nfs4_do_setattr(state->inode, cred,
3145 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003146 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003147 if (status == 0) {
3148 nfs_setattr_update_inode(state->inode, sattr,
3149 opendata->o_res.f_attr);
3150 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3151 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003152 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003153 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003154 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003155 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003156 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003157
Trond Myklebuste911b812014-03-26 13:24:37 -07003158 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003159 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003160 opendata->f_attr.mdsthreshold = NULL;
3161 }
Andy Adamson82be4172012-05-23 05:02:35 -04003162
David Quigley14c43f72013-05-22 12:50:43 -04003163 nfs4_label_free(olabel);
3164
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003165 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003168err_free_label:
3169 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003170err_opendata_put:
3171 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003172err_put_state_owner:
3173 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return status;
3176}
3177
3178
Andy Adamson82be4172012-05-23 05:02:35 -04003179static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003180 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003181 int flags,
3182 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003183 struct nfs4_label *label,
3184 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003186 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003187 struct nfs4_exception exception = {
3188 .interruptible = true,
3189 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003191 struct nfs4_open_createattrs c = {
3192 .label = label,
3193 .sattr = sattr,
3194 .verf = {
3195 [0] = (__u32)jiffies,
3196 [1] = (__u32)current->pid,
3197 },
3198 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 int status;
3200
3201 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003202 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003203 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003204 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (status == 0)
3206 break;
3207 /* NOTE: BAD_SEQID means the server and client disagree about the
3208 * book-keeping w.r.t. state-changing operations
3209 * (OPEN/CLOSE/LOCK/LOCKU...)
3210 * It is actually a sign of a bug on the client or on the server.
3211 *
3212 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003213 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 * have unhashed the old state_owner for us, and that we can
3215 * therefore safely retry using a new one. We should still warn
3216 * the user though...
3217 */
3218 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003219 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003220 " returned a bad sequence-id error!\n",
3221 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 exception.retry = 1;
3223 continue;
3224 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003225 /*
3226 * BAD_STATEID on OPEN means that the server cancelled our
3227 * state before it received the OPEN_CONFIRM.
3228 * Recover by retrying the request as per the discussion
3229 * on Page 181 of RFC3530.
3230 */
3231 if (status == -NFS4ERR_BAD_STATEID) {
3232 exception.retry = 1;
3233 continue;
3234 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003235 if (status == -NFS4ERR_EXPIRED) {
3236 nfs4_schedule_lease_recovery(server->nfs_client);
3237 exception.retry = 1;
3238 continue;
3239 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003240 if (status == -EAGAIN) {
3241 /* We must have found a delegation */
3242 exception.retry = 1;
3243 continue;
3244 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003245 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3246 continue;
3247 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 status, &exception));
3249 } while (exception.retry);
3250 return res;
3251}
3252
Trond Myklebust8487c472016-06-26 08:44:35 -04003253static int _nfs4_do_setattr(struct inode *inode,
3254 struct nfs_setattrargs *arg,
3255 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003256 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003257 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003258{
3259 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003260 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3262 .rpc_argp = arg,
3263 .rpc_resp = res,
3264 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003265 };
NeilBrowna52458b2018-12-03 11:30:31 +11003266 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003267 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003268 bool truncate;
3269 int status;
3270
3271 nfs_fattr_init(res->fattr);
3272
3273 /* Servers should only apply open mode checks for file size changes */
3274 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003275 if (!truncate)
3276 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003277
Trond Myklebust991eedb2018-04-09 11:15:30 -04003278 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003279 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003280 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003281 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003282 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003283 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003284 l_ctx = nfs_get_lock_context(ctx);
3285 if (IS_ERR(l_ctx))
3286 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003287 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3288 &arg->stateid, &delegation_cred);
3289 nfs_put_lock_context(l_ctx);
3290 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003291 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003292 else if (status == -EAGAIN)
3293 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003294 } else {
3295zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003296 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003297 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003298 if (delegation_cred)
3299 msg.rpc_cred = delegation_cred;
3300
3301 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3302
NeilBrowna52458b2018-12-03 11:30:31 +11003303 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003304 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003305 renew_lease(server, timestamp);
3306 trace_nfs4_setattr(inode, &arg->stateid, status);
3307 return status;
3308}
3309
NeilBrowna52458b2018-12-03 11:30:31 +11003310static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003311 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003312 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003313 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003315 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003316 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003317 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003318 struct nfs_setattrargs arg = {
3319 .fh = NFS_FH(inode),
3320 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003322 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003323 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003324 };
3325 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003327 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003329 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003330 struct nfs4_exception exception = {
3331 .state = state,
3332 .inode = inode,
3333 .stateid = &arg.stateid,
3334 };
3335 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003338 nfs4_bitmap_copy_adjust_setattr(bitmask,
3339 nfs4_bitmask(server, olabel),
3340 inode);
3341
NeilBrown29b59f92016-10-13 15:26:47 +11003342 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003343 switch (err) {
3344 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003345 if (!(sattr->ia_valid & ATTR_SIZE)) {
3346 pr_warn_once("NFSv4: server %s is incorrectly "
3347 "applying open mode checks to "
3348 "a SETATTR that is not "
3349 "changing file size.\n",
3350 server->nfs_client->cl_hostname);
3351 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003352 if (state && !(state->state & FMODE_WRITE)) {
3353 err = -EBADF;
3354 if (sattr->ia_valid & ATTR_OPEN)
3355 err = -EACCES;
3356 goto out;
3357 }
3358 }
3359 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003361out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 return err;
3363}
3364
Peng Tao500d7012015-09-22 11:35:22 +08003365static bool
3366nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3367{
3368 if (inode == NULL || !nfs_have_layout(inode))
3369 return false;
3370
3371 return pnfs_wait_on_layoutreturn(inode, task);
3372}
3373
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003374/*
3375 * Update the seqid of an open stateid
3376 */
3377static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3378 struct nfs4_state *state)
3379{
3380 __be32 seqid_open;
3381 u32 dst_seqid;
3382 int seq;
3383
3384 for (;;) {
3385 if (!nfs4_valid_open_stateid(state))
3386 break;
3387 seq = read_seqbegin(&state->seqlock);
3388 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3389 nfs4_stateid_copy(dst, &state->open_stateid);
3390 if (read_seqretry(&state->seqlock, seq))
3391 continue;
3392 break;
3393 }
3394 seqid_open = state->open_stateid.seqid;
3395 if (read_seqretry(&state->seqlock, seq))
3396 continue;
3397
3398 dst_seqid = be32_to_cpu(dst->seqid);
3399 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3400 dst->seqid = seqid_open;
3401 break;
3402 }
3403}
3404
3405/*
3406 * Update the seqid of an open stateid after receiving
3407 * NFS4ERR_OLD_STATEID
3408 */
3409static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3410 struct nfs4_state *state)
3411{
3412 __be32 seqid_open;
3413 u32 dst_seqid;
3414 bool ret;
3415 int seq;
3416
3417 for (;;) {
3418 ret = false;
3419 if (!nfs4_valid_open_stateid(state))
3420 break;
3421 seq = read_seqbegin(&state->seqlock);
3422 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3423 if (read_seqretry(&state->seqlock, seq))
3424 continue;
3425 break;
3426 }
3427 seqid_open = state->open_stateid.seqid;
3428 if (read_seqretry(&state->seqlock, seq))
3429 continue;
3430
3431 dst_seqid = be32_to_cpu(dst->seqid);
3432 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3433 dst->seqid = cpu_to_be32(dst_seqid + 1);
3434 else
3435 dst->seqid = seqid_open;
3436 ret = true;
3437 break;
3438 }
3439
3440 return ret;
3441}
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443struct nfs4_closedata {
3444 struct inode *inode;
3445 struct nfs4_state *state;
3446 struct nfs_closeargs arg;
3447 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003448 struct {
3449 struct nfs4_layoutreturn_args arg;
3450 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003451 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003452 u32 roc_barrier;
3453 bool roc;
3454 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003455 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003456 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457};
3458
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003459static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003460{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003461 struct nfs4_closedata *calldata = data;
3462 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003463 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003464
Trond Myklebustcf805162016-11-15 14:56:07 -05003465 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003466 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3467 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003468 nfs4_put_open_state(calldata->state);
3469 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003470 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003471 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003472 kfree(calldata);
3473}
3474
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003475static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003477 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003480 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003481 struct nfs4_exception exception = {
3482 .state = state,
3483 .inode = calldata->inode,
3484 .stateid = &calldata->arg.stateid,
3485 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Chuck Levera3ca5652012-03-01 17:00:40 -05003487 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003488 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3489 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003490 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003491
3492 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04003493 if (pnfs_roc_done(task, calldata->inode,
3494 &calldata->arg.lr_args,
3495 &calldata->res.lr_res,
3496 &calldata->res.lr_ret) == -EAGAIN)
3497 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003498
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003499 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 * the state_owner. we keep this around to process errors
3501 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 switch (task->tk_status) {
3503 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003504 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003505 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003506 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003507 case -NFS4ERR_ACCESS:
3508 if (calldata->arg.bitmask != NULL) {
3509 calldata->arg.bitmask = NULL;
3510 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003511 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003512
3513 }
3514 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003515 case -NFS4ERR_OLD_STATEID:
3516 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003517 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003518 state))
3519 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003520 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003521 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003523 case -NFS4ERR_EXPIRED:
3524 nfs4_free_revoked_stateid(server,
3525 &calldata->arg.stateid,
3526 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003527 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003528 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003529 if (calldata->arg.fmode == 0)
3530 break;
3531 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003533 task->tk_status = nfs4_async_handle_exception(task,
3534 server, task->tk_status, &exception);
3535 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003536 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003538 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3539 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003540out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003541 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003542 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003543 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003544 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003545 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003546out_restart:
3547 task->tk_status = 0;
3548 rpc_restart_call_prepare(task);
3549 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550}
3551
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003552static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003554 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003555 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003556 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003557 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003558 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003559 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003560
Chuck Levera3ca5652012-03-01 17:00:40 -05003561 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003562 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003563 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003564
Trond Myklebust88069f72009-12-08 08:33:16 -05003565 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003566 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003567 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3568 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3569 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003570 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003571 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003572 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003573 if (state->n_rdonly == 0)
3574 call_close |= is_rdonly;
3575 else if (is_rdonly)
3576 calldata->arg.fmode |= FMODE_READ;
3577 if (state->n_wronly == 0)
3578 call_close |= is_wronly;
3579 else if (is_wronly)
3580 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003581 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3582 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003583 } else if (is_rdwr)
3584 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3585
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003586 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3587 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003588 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003589 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003590
3591 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003592 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003593 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003594 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003595
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003596 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003597 nfs_release_seqid(calldata->arg.seqid);
3598 goto out_wait;
3599 }
3600
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003601 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3602 if (lo && !pnfs_layout_is_valid(lo)) {
3603 calldata->arg.lr_args = NULL;
3604 calldata->res.lr_res = NULL;
3605 }
3606
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003607 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003608 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003609
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003610 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003611 /* Close-to-open cache consistency revalidation */
3612 if (!nfs4_have_delegation(inode, FMODE_READ))
3613 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3614 else
3615 calldata->arg.bitmask = NULL;
3616 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003617
Trond Myklebust6ae37332015-01-30 14:21:14 -05003618 calldata->arg.share_access =
3619 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3620 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003621
Trond Myklebustd8d84982016-12-19 12:14:44 -05003622 if (calldata->res.fattr == NULL)
3623 calldata->arg.bitmask = NULL;
3624 else if (calldata->arg.bitmask == NULL)
3625 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003626 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003627 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003628 &calldata->arg.seq_args,
3629 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003630 task) != 0)
3631 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003632 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003633 return;
3634out_no_action:
3635 task->tk_action = NULL;
3636out_wait:
3637 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638}
3639
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003640static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003641 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003642 .rpc_call_done = nfs4_close_done,
3643 .rpc_release = nfs4_free_closedata,
3644};
3645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646/*
3647 * It is possible for data to be read/written from a mem-mapped file
3648 * after the sys_close call (which hits the vfs layer as a flush).
3649 * This means that we can't safely call nfsv4 close on a file until
3650 * the inode is cleared. This in turn means that we are not good
3651 * NFSv4 citizens - we do not indicate to the server to update the file's
3652 * share state even when we are done with one of the three share
3653 * stateid's in the inode.
3654 *
3655 * NOTE: Caller must be holding the sp->so_owner semaphore!
3656 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003657int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003659 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003660 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003662 struct nfs4_state_owner *sp = state->owner;
3663 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003664 struct rpc_message msg = {
3665 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3666 .rpc_cred = state->owner->so_cred,
3667 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003668 struct rpc_task_setup task_setup_data = {
3669 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003670 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003671 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003672 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003673 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003674 };
Trond Myklebust95121352005-10-18 14:20:12 -07003675 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003677 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3678 &task_setup_data.rpc_client, &msg);
3679
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003680 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003682 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003683 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003684 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003686 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003687 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3688 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003690 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3691 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003692 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003693 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003694 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003695 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003696 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003697 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003698 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003699 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003700 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003701 calldata->lr.roc = pnfs_roc(state->inode,
3702 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3703 if (calldata->lr.roc) {
3704 calldata->arg.lr_args = &calldata->lr.arg;
3705 calldata->res.lr_res = &calldata->lr.res;
3706 }
Al Viro643168c2011-06-22 18:20:23 -04003707 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003708
Trond Myklebust1174dd12010-12-21 10:52:24 -05003709 msg.rpc_argp = &calldata->arg;
3710 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003711 task_setup_data.callback_data = calldata;
3712 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003713 if (IS_ERR(task))
3714 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003715 status = 0;
3716 if (wait)
3717 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003718 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003719 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003720out_free_calldata:
3721 kfree(calldata);
3722out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003723 nfs4_put_open_state(state);
3724 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003725 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
Trond Myklebust2b484292010-09-17 10:56:51 -04003728static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003729nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3730 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003733 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3734
3735 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Trond Myklebust565277f2007-10-15 18:17:53 -04003737 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003738 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003739
3740 nfs4_label_release_security(label);
3741
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003742 if (IS_ERR(state))
3743 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003744 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
Trond Myklebust1185a552009-12-03 15:54:02 -05003747static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003748{
3749 if (ctx->state == NULL)
3750 return;
3751 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003752 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003753 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003754 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003755}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Trond Myklebustb944dba2013-11-04 15:20:20 -05003757#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3758#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003759#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3762{
Kinglong Mee8c612822015-08-26 21:12:58 +08003763 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003764 struct nfs4_server_caps_arg args = {
3765 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003766 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003767 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 struct nfs4_server_caps_res res = {};
3769 struct rpc_message msg = {
3770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003771 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 .rpc_resp = &res,
3773 };
3774 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003775 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Kinglong Mee8c612822015-08-26 21:12:58 +08003777 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3778 FATTR4_WORD0_FH_EXPIRE_TYPE |
3779 FATTR4_WORD0_LINK_SUPPORT |
3780 FATTR4_WORD0_SYMLINK_SUPPORT |
3781 FATTR4_WORD0_ACLSUPPORT;
3782 if (minorversion)
3783 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3784
Bryan Schumaker7c513052011-03-24 17:12:24 +00003785 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003787 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003788 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003789 case 0:
3790 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3791 res.attr_bitmask[2] = 0;
3792 break;
3793 case 1:
3794 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3795 break;
3796 case 2:
3797 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003800 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3801 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3802 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3803 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003804 NFS_CAP_CTIME|NFS_CAP_MTIME|
3805 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003806 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3807 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 server->caps |= NFS_CAP_ACLS;
3809 if (res.has_links != 0)
3810 server->caps |= NFS_CAP_HARDLINKS;
3811 if (res.has_symlinks != 0)
3812 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003813 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3814 server->caps |= NFS_CAP_FILEID;
3815 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3816 server->caps |= NFS_CAP_MODE;
3817 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3818 server->caps |= NFS_CAP_NLINK;
3819 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3820 server->caps |= NFS_CAP_OWNER;
3821 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3822 server->caps |= NFS_CAP_OWNER_GROUP;
3823 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3824 server->caps |= NFS_CAP_ATIME;
3825 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3826 server->caps |= NFS_CAP_CTIME;
3827 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3828 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003829#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3830 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3831 server->caps |= NFS_CAP_SECURITY_LABEL;
3832#endif
3833 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3834 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003835 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003836
Trond Myklebusta65318b2009-03-11 14:10:28 -04003837 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3838 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3839 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003840 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003841
3842 /* Avoid a regression due to buggy server */
3843 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3844 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003845 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3846 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003849 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003851
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 return status;
3853}
3854
Trond Myklebust55a97592006-06-09 09:34:19 -04003855int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856{
Trond Myklebust0688e642019-04-07 13:59:09 -04003857 struct nfs4_exception exception = {
3858 .interruptible = true,
3859 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 int err;
3861 do {
3862 err = nfs4_handle_exception(server,
3863 _nfs4_server_capabilities(server, fhandle),
3864 &exception);
3865 } while (exception.retry);
3866 return err;
3867}
3868
3869static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3870 struct nfs_fsinfo *info)
3871{
David Quigleyaa9c2662013-05-22 12:50:44 -04003872 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003874 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 };
3876 struct nfs4_lookup_res res = {
3877 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003878 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 .fh = fhandle,
3880 };
3881 struct rpc_message msg = {
3882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3883 .rpc_argp = &args,
3884 .rpc_resp = &res,
3885 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003886
David Quigleyaa9c2662013-05-22 12:50:44 -04003887 bitmask[0] = nfs4_fattr_bitmap[0];
3888 bitmask[1] = nfs4_fattr_bitmap[1];
3889 /*
3890 * Process the label in the upcoming getfattr
3891 */
3892 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3893
Trond Myklebust0e574af2005-10-27 22:12:38 -04003894 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003895 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896}
3897
3898static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3899 struct nfs_fsinfo *info)
3900{
Trond Myklebust0688e642019-04-07 13:59:09 -04003901 struct nfs4_exception exception = {
3902 .interruptible = true,
3903 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 int err;
3905 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003906 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003907 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003908 switch (err) {
3909 case 0:
3910 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003911 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003912 default:
3913 err = nfs4_handle_exception(server, err, &exception);
3914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003916out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 return err;
3918}
3919
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003920static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3921 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3922{
Trond Myklebustc2190662013-08-26 19:23:04 -04003923 struct rpc_auth_create_args auth_args = {
3924 .pseudoflavor = flavor,
3925 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003926 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003927
Trond Myklebustc2190662013-08-26 19:23:04 -04003928 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003929 if (IS_ERR(auth))
3930 return -EACCES;
3931 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003932}
3933
Chuck Lever9a744ba2013-03-16 15:56:02 -04003934/*
3935 * Retry pseudoroot lookup with various security flavors. We do this when:
3936 *
3937 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3938 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3939 *
3940 * Returns zero on success, or a negative NFS4ERR value, or a
3941 * negative errno value.
3942 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003943static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003944 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003946 /* Per 3530bis 15.33.5 */
3947 static const rpc_authflavor_t flav_array[] = {
3948 RPC_AUTH_GSS_KRB5P,
3949 RPC_AUTH_GSS_KRB5I,
3950 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003951 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003952 RPC_AUTH_NULL,
3953 };
3954 int status = -EPERM;
3955 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003957 if (server->auth_info.flavor_len > 0) {
3958 /* try each flavor specified by user */
3959 for (i = 0; i < server->auth_info.flavor_len; i++) {
3960 status = nfs4_lookup_root_sec(server, fhandle, info,
3961 server->auth_info.flavors[i]);
3962 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3963 continue;
3964 break;
3965 }
3966 } else {
3967 /* no flavors specified by user, try default list */
3968 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3969 status = nfs4_lookup_root_sec(server, fhandle, info,
3970 flav_array[i]);
3971 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3972 continue;
3973 break;
3974 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003975 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003976
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003977 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003978 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003979 * to access the mount. It could also mean that we tried to mount
3980 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3981 * existing mount programs don't handle -EACCES very well so it should
3982 * be mapped to -EPERM instead.
3983 */
3984 if (status == -EACCES)
3985 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003986 return status;
3987}
3988
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003989/**
3990 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3991 * @server: initialized nfs_server handle
3992 * @fhandle: we fill in the pseudo-fs root file handle
3993 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003994 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003995 *
3996 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003997 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003998int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003999 struct nfs_fsinfo *info,
4000 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004001{
Andre Przywarac7757072015-04-23 17:17:40 +01004002 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004003
Andre Przywarac7757072015-04-23 17:17:40 +01004004 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004005 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004006
4007 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c937b2016-07-25 13:31:14 -04004008 status = server->nfs_client->cl_mvops->find_root_sec(server,
4009 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004010
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 if (status == 0)
4012 status = nfs4_server_capabilities(server, fhandle);
4013 if (status == 0)
4014 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004015
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004016 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
Bryan Schumakerbae36242012-05-10 15:07:31 -04004019static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4020 struct nfs_fsinfo *info)
4021{
4022 int error;
4023 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004024 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004025
4026 error = nfs4_server_capabilities(server, mntfh);
4027 if (error < 0) {
4028 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4029 return error;
4030 }
4031
Trond Myklebusta841b542018-04-07 13:50:59 -04004032 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004033 if (error < 0) {
4034 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004035 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004036 }
4037
4038 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4039 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4040 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4041
Scott Mayhew779df6a2020-03-03 17:58:37 -05004042out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004043 return error;
4044}
4045
Manoj Naik6b97fd32006-06-09 09:34:29 -04004046/*
4047 * Get locations and (maybe) other attributes of a referral.
4048 * Note that we'll actually follow the referral later when
4049 * we detect fsid mismatch in inode revalidation
4050 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004051static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4052 const struct qstr *name, struct nfs_fattr *fattr,
4053 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004054{
4055 int status = -ENOMEM;
4056 struct page *page = NULL;
4057 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004058
4059 page = alloc_page(GFP_KERNEL);
4060 if (page == NULL)
4061 goto out;
4062 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4063 if (locations == NULL)
4064 goto out;
4065
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004066 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004067 if (status != 0)
4068 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004069
4070 /*
4071 * If the fsid didn't change, this is a migration event, not a
4072 * referral. Cause us to drop into the exception handler, which
4073 * will kick off migration recovery.
4074 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004075 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004076 dprintk("%s: server did not return a different fsid for"
4077 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004078 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004079 goto out;
4080 }
Andy Adamson533eb462011-06-13 18:25:56 -04004081 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4082 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004083
Andy Adamson533eb462011-06-13 18:25:56 -04004084 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004085 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004086 memset(fhandle, 0, sizeof(struct nfs_fh));
4087out:
4088 if (page)
4089 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004090 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004091 return status;
4092}
4093
David Quigley1775fd32013-05-22 12:50:42 -04004094static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004095 struct nfs_fattr *fattr, struct nfs4_label *label,
4096 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
Trond Myklebust771734f2018-04-07 13:54:23 -04004098 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 struct nfs4_getattr_arg args = {
4100 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004101 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 };
4103 struct nfs4_getattr_res res = {
4104 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004105 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 .server = server,
4107 };
4108 struct rpc_message msg = {
4109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4110 .rpc_argp = &args,
4111 .rpc_resp = &res,
4112 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004113 unsigned short task_flags = 0;
4114
4115 /* Is this is an attribute revalidation, subject to softreval? */
4116 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4117 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004118
Trond Myklebust771734f2018-04-07 13:54:23 -04004119 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004120
Trond Myklebust0e574af2005-10-27 22:12:38 -04004121 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004122 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4123 return nfs4_do_call_sync(server->client, server, &msg,
4124 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125}
4126
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004127int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004128 struct nfs_fattr *fattr, struct nfs4_label *label,
4129 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
Trond Myklebust0688e642019-04-07 13:59:09 -04004131 struct nfs4_exception exception = {
4132 .interruptible = true,
4133 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 int err;
4135 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004136 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004137 trace_nfs4_getattr(server, fhandle, fattr, err);
4138 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 &exception);
4140 } while (exception.retry);
4141 return err;
4142}
4143
4144/*
4145 * The file is not closed if it is opened due to the a request to change
4146 * the size of the file. The open call will not be needed once the
4147 * VFS layer lookup-intents are implemented.
4148 *
4149 * Close is called when the inode is destroyed.
4150 * If we haven't opened the file for O_WRONLY, we
4151 * need to in the size_change case to obtain a stateid.
4152 *
4153 * Got race?
4154 * Because OPEN is always done by name in nfsv4, it is
4155 * possible that we opened a different file by the same
4156 * name. We can recognize this race condition, but we
4157 * can't do anything about it besides returning an error.
4158 *
4159 * This will be fixed with VFS changes (lookup-intent).
4160 */
4161static int
4162nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4163 struct iattr *sattr)
4164{
David Howells2b0143b2015-03-17 22:25:59 +00004165 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004166 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004167 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004168 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 int status;
4170
Peng Tao88ac8152014-09-12 11:04:10 +08004171 if (pnfs_ld_layoutret_on_setattr(inode) &&
4172 sattr->ia_valid & ATTR_SIZE &&
4173 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004174 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004175
Trond Myklebust0e574af2005-10-27 22:12:38 -04004176 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Andy Adamson26699402012-05-30 16:12:24 -04004178 /* Deal with open(O_TRUNC) */
4179 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004180 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004181
4182 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004183 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004184 return 0;
4185
Trond Myklebustd5308382005-11-04 15:33:38 -05004186 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004187 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004188
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004189 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004190 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004191 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004192 }
4193
David Quigley14c43f72013-05-22 12:50:43 -04004194 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4195 if (IS_ERR(label))
4196 return PTR_ERR(label);
4197
Trond Myklebust199366f2018-03-20 16:43:18 -04004198 /* Return any delegations if we're going to change ACLs */
4199 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004200 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004201
NeilBrown29b59f92016-10-13 15:26:47 +11004202 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004203 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004204 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004205 nfs_setsecurity(inode, fattr, label);
4206 }
David Quigley14c43f72013-05-22 12:50:43 -04004207 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 return status;
4209}
4210
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004211static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004212 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004213 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004214{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004215 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004216 int status;
4217 struct nfs4_lookup_arg args = {
4218 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004219 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004220 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004221 };
4222 struct nfs4_lookup_res res = {
4223 .server = server,
4224 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004225 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004226 .fh = fhandle,
4227 };
4228 struct rpc_message msg = {
4229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4230 .rpc_argp = &args,
4231 .rpc_resp = &res,
4232 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004233 unsigned short task_flags = 0;
4234
4235 /* Is this is an attribute revalidation, subject to softreval? */
4236 if (nfs_lookup_is_soft_revalidate(dentry))
4237 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004238
David Quigleyaa9c2662013-05-22 12:50:44 -04004239 args.bitmask = nfs4_bitmask(server, label);
4240
David Howells2b3de442006-08-22 20:06:09 -04004241 nfs_fattr_init(fattr);
4242
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004243 dprintk("NFS call lookup %pd2\n", dentry);
4244 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4245 status = nfs4_do_call_sync(clnt, server, &msg,
4246 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 dprintk("NFS reply lookup: %d\n", status);
4248 return status;
4249}
4250
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004251static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004252{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004253 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004254 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004255 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4256 fattr->nlink = 2;
4257}
4258
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004259static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004260 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004261 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004262{
Trond Myklebust0688e642019-04-07 13:59:09 -04004263 struct nfs4_exception exception = {
4264 .interruptible = true,
4265 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004266 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004267 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004268 int err;
4269 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004270 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004271 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004272 switch (err) {
4273 case -NFS4ERR_BADNAME:
4274 err = -ENOENT;
4275 goto out;
4276 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004277 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004278 if (err == -NFS4ERR_MOVED)
4279 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004280 goto out;
4281 case -NFS4ERR_WRONGSEC:
4282 err = -EPERM;
4283 if (client != *clnt)
4284 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004285 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004286 if (IS_ERR(client))
4287 return PTR_ERR(client);
4288
4289 exception.retry = 1;
4290 break;
4291 default:
4292 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4293 }
4294 } while (exception.retry);
4295
4296out:
4297 if (err == 0)
4298 *clnt = client;
4299 else if (client != *clnt)
4300 rpc_shutdown_client(client);
4301
4302 return err;
4303}
4304
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004305static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004306 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4307 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004309 int status;
4310 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004311
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004312 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004313 if (client != NFS_CLIENT(dir)) {
4314 rpc_shutdown_client(client);
4315 nfs_fixup_secinfo_attributes(fattr);
4316 }
4317 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318}
4319
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004320struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004321nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004322 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4323{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004324 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004325 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004326
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004327 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004328 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004329 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004330 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004331}
4332
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004333static int _nfs4_proc_lookupp(struct inode *inode,
4334 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4335 struct nfs4_label *label)
4336{
4337 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4338 struct nfs_server *server = NFS_SERVER(inode);
4339 int status;
4340 struct nfs4_lookupp_arg args = {
4341 .bitmask = server->attr_bitmask,
4342 .fh = NFS_FH(inode),
4343 };
4344 struct nfs4_lookupp_res res = {
4345 .server = server,
4346 .fattr = fattr,
4347 .label = label,
4348 .fh = fhandle,
4349 };
4350 struct rpc_message msg = {
4351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4352 .rpc_argp = &args,
4353 .rpc_resp = &res,
4354 };
4355
4356 args.bitmask = nfs4_bitmask(server, label);
4357
4358 nfs_fattr_init(fattr);
4359
4360 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4361 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4362 &res.seq_res, 0);
4363 dprintk("NFS reply lookupp: %d\n", status);
4364 return status;
4365}
4366
4367static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4368 struct nfs_fattr *fattr, struct nfs4_label *label)
4369{
Trond Myklebust0688e642019-04-07 13:59:09 -04004370 struct nfs4_exception exception = {
4371 .interruptible = true,
4372 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004373 int err;
4374 do {
4375 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4376 trace_nfs4_lookupp(inode, err);
4377 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4378 &exception);
4379 } while (exception.retry);
4380 return err;
4381}
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4384{
Trond Myklebust76b32992007-08-10 17:45:11 -04004385 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 struct nfs4_accessargs args = {
4387 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004388 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004390 struct nfs4_accessres res = {
4391 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004392 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 struct rpc_message msg = {
4394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4395 .rpc_argp = &args,
4396 .rpc_resp = &res,
4397 .rpc_cred = entry->cred,
4398 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004399 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Trond Myklebust7c672652018-06-04 15:00:53 -04004401 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004402 res.fattr = nfs_alloc_fattr();
4403 if (res.fattr == NULL)
4404 return -ENOMEM;
4405 args.bitmask = server->cache_consistency_bitmask;
4406 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004407 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004409 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004410 if (res.fattr)
4411 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004413 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 return status;
4415}
4416
4417static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4418{
Trond Myklebust0688e642019-04-07 13:59:09 -04004419 struct nfs4_exception exception = {
4420 .interruptible = true,
4421 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 int err;
4423 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004424 err = _nfs4_proc_access(inode, entry);
4425 trace_nfs4_access(inode, err);
4426 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 &exception);
4428 } while (exception.retry);
4429 return err;
4430}
4431
4432/*
4433 * TODO: For the time being, we don't try to get any attributes
4434 * along with any of the zero-copy operations READ, READDIR,
4435 * READLINK, WRITE.
4436 *
4437 * In the case of the first three, we want to put the GETATTR
4438 * after the read-type operation -- this is because it is hard
4439 * to predict the length of a GETATTR response in v4, and thus
4440 * align the READ data correctly. This means that the GETATTR
4441 * may end up partially falling into the page cache, and we should
4442 * shift it into the 'tail' of the xdr_buf before processing.
4443 * To do this efficiently, we need to know the total length
4444 * of data received, which doesn't seem to be available outside
4445 * of the RPC layer.
4446 *
4447 * In the case of WRITE, we also want to put the GETATTR after
4448 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004449 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 *
4451 * Both of these changes to the XDR layer would in fact be quite
4452 * minor, but I decided to leave them for a subsequent patch.
4453 */
4454static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4455 unsigned int pgbase, unsigned int pglen)
4456{
4457 struct nfs4_readlink args = {
4458 .fh = NFS_FH(inode),
4459 .pgbase = pgbase,
4460 .pglen = pglen,
4461 .pages = &page,
4462 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004463 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 struct rpc_message msg = {
4465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4466 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004467 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 };
4469
Bryan Schumaker7c513052011-03-24 17:12:24 +00004470 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 -07004471}
4472
4473static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4474 unsigned int pgbase, unsigned int pglen)
4475{
Trond Myklebust0688e642019-04-07 13:59:09 -04004476 struct nfs4_exception exception = {
4477 .interruptible = true,
4478 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 int err;
4480 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004481 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4482 trace_nfs4_readlink(inode, err);
4483 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 &exception);
4485 } while (exception.retry);
4486 return err;
4487}
4488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004490 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492static int
4493nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004494 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004496 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004497 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004498 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 int status = 0;
4501
NeilBrown532d4de2016-10-13 15:26:47 +11004502 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004503 if (IS_ERR(ctx))
4504 return PTR_ERR(ctx);
4505
David Quigleyaa9c2662013-05-22 12:50:44 -04004506 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4507
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004508 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4509 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004510 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 if (IS_ERR(state)) {
4512 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004516 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004517 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 return status;
4519}
4520
Trond Myklebust3c591172018-07-31 15:54:10 -04004521static int
4522_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523{
Trond Myklebust16e42952005-10-27 22:12:44 -04004524 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004525 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004527 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004529 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004530 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004531 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4534 .rpc_argp = &args,
4535 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004537 unsigned long timestamp = jiffies;
Trond Myklebust778d28172012-04-27 13:48:19 -04004538 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004539
Bryan Schumaker7c513052011-03-24 17:12:24 +00004540 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004541 if (status == 0) {
4542 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004543 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004544 /* Removing a directory decrements nlink in the parent */
4545 if (ftype == NF4DIR && dir->i_nlink > 2)
4546 nfs4_dec_nlink_locked(dir);
4547 spin_unlock(&dir->i_lock);
4548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 return status;
4550}
4551
Trond Myklebust912678d2018-03-20 16:43:15 -04004552static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4553{
Trond Myklebust0688e642019-04-07 13:59:09 -04004554 struct nfs4_exception exception = {
4555 .interruptible = true,
4556 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004557 struct inode *inode = d_inode(dentry);
4558 int err;
4559
Trond Myklebustc01d3642018-03-20 16:43:20 -04004560 if (inode) {
4561 if (inode->i_nlink == 1)
4562 nfs4_inode_return_delegation(inode);
4563 else
4564 nfs4_inode_make_writeable(inode);
4565 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004566 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004567 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004568 trace_nfs4_remove(dir, &dentry->d_name, err);
4569 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4570 &exception);
4571 } while (exception.retry);
4572 return err;
4573}
4574
4575static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576{
Trond Myklebust0688e642019-04-07 13:59:09 -04004577 struct nfs4_exception exception = {
4578 .interruptible = true,
4579 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004583 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004584 trace_nfs4_remove(dir, name, err);
4585 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 &exception);
4587 } while (exception.retry);
4588 return err;
4589}
4590
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004591static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4592 struct dentry *dentry,
4593 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004595 struct nfs_removeargs *args = msg->rpc_argp;
4596 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Trond Myklebust977fcc22018-03-20 16:43:17 -04004598 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004600 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004601
4602 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004603
4604 if (inode)
4605 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606}
4607
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004608static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4609{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004610 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004611 &data->args.seq_args,
4612 &data->res.seq_res,
4613 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614}
4615
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004616static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004618 struct nfs_unlinkdata *data = task->tk_calldata;
4619 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004620
Trond Myklebust14516c32010-07-31 14:29:06 -04004621 if (!nfs4_sequence_done(task, &res->seq_res))
4622 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004623 if (nfs4_async_handle_error(task, res->server, NULL,
4624 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004625 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004626 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004627 update_changeattr(dir, &res->cinfo,
4628 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004629 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630}
4631
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004632static void nfs4_proc_rename_setup(struct rpc_message *msg,
4633 struct dentry *old_dentry,
4634 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004635{
Jeff Laytond3d41522010-09-17 17:31:57 -04004636 struct nfs_renameargs *arg = msg->rpc_argp;
4637 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004638 struct inode *old_inode = d_inode(old_dentry);
4639 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004640
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004641 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004642 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004643 if (new_inode)
4644 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004645 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004646 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004647 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004648}
4649
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004650static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4651{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004652 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004653 &data->args.seq_args,
4654 &data->res.seq_res,
4655 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004656}
4657
4658static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4659 struct inode *new_dir)
4660{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004661 struct nfs_renamedata *data = task->tk_calldata;
4662 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004663
4664 if (!nfs4_sequence_done(task, &res->seq_res))
4665 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004666 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004667 return 0;
4668
Trond Myklebustc733c492017-01-11 12:32:26 -05004669 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004670 if (new_dir != old_dir) {
4671 /* Note: If we moved a directory, nlink will change */
4672 update_changeattr(old_dir, &res->old_cinfo,
4673 res->old_fattr->time_start,
4674 NFS_INO_INVALID_OTHER);
4675 update_changeattr(new_dir, &res->new_cinfo,
4676 res->new_fattr->time_start,
4677 NFS_INO_INVALID_OTHER);
4678 } else
4679 update_changeattr(old_dir, &res->old_cinfo,
4680 res->old_fattr->time_start,
4681 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004682 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004683 return 1;
4684}
4685
Al Virobeffb8f2016-07-20 16:34:42 -04004686static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004688 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004689 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 struct nfs4_link_arg arg = {
4691 .fh = NFS_FH(inode),
4692 .dir_fh = NFS_FH(dir),
4693 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004694 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004696 struct nfs4_link_res res = {
4697 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004698 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004699 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 struct rpc_message msg = {
4701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4702 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004703 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004705 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
Trond Myklebust136f2622010-04-16 16:22:49 -04004707 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d28172012-04-27 13:48:19 -04004708 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004709 goto out;
4710
David Quigley14c43f72013-05-22 12:50:43 -04004711 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4712 if (IS_ERR(res.label)) {
4713 status = PTR_ERR(res.label);
4714 goto out;
4715 }
4716
Trond Myklebustc01d3642018-03-20 16:43:20 -04004717 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004718 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004719
Bryan Schumaker7c513052011-03-24 17:12:24 +00004720 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004721 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004722 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004723 status = nfs_post_op_update_inode(inode, res.fattr);
4724 if (!status)
4725 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004726 }
David Quigley14c43f72013-05-22 12:50:43 -04004727
4728
4729 nfs4_label_free(res.label);
4730
Trond Myklebust136f2622010-04-16 16:22:49 -04004731out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004732 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 return status;
4734}
4735
Al Virobeffb8f2016-07-20 16:34:42 -04004736static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
Trond Myklebust0688e642019-04-07 13:59:09 -04004738 struct nfs4_exception exception = {
4739 .interruptible = true,
4740 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 int err;
4742 do {
4743 err = nfs4_handle_exception(NFS_SERVER(inode),
4744 _nfs4_proc_link(inode, dir, name),
4745 &exception);
4746 } while (exception.retry);
4747 return err;
4748}
4749
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004750struct nfs4_createdata {
4751 struct rpc_message msg;
4752 struct nfs4_create_arg arg;
4753 struct nfs4_create_res res;
4754 struct nfs_fh fh;
4755 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004756 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004757};
4758
4759static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004760 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004761{
4762 struct nfs4_createdata *data;
4763
4764 data = kzalloc(sizeof(*data), GFP_KERNEL);
4765 if (data != NULL) {
4766 struct nfs_server *server = NFS_SERVER(dir);
4767
David Quigley14c43f72013-05-22 12:50:43 -04004768 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4769 if (IS_ERR(data->label))
4770 goto out_free;
4771
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004772 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4773 data->msg.rpc_argp = &data->arg;
4774 data->msg.rpc_resp = &data->res;
4775 data->arg.dir_fh = NFS_FH(dir);
4776 data->arg.server = server;
4777 data->arg.name = name;
4778 data->arg.attrs = sattr;
4779 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004780 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004781 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004782 data->res.server = server;
4783 data->res.fh = &data->fh;
4784 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004785 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004786 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004787 }
4788 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004789out_free:
4790 kfree(data);
4791 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004792}
4793
4794static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4795{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004796 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004797 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004798 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004799 spin_lock(&dir->i_lock);
4800 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004801 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004802 /* Creating a directory bumps nlink in the parent */
4803 if (data->arg.ftype == NF4DIR)
4804 nfs4_inc_nlink_locked(dir);
4805 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004806 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004807 }
4808 return status;
4809}
4810
4811static void nfs4_free_createdata(struct nfs4_createdata *data)
4812{
David Quigley14c43f72013-05-22 12:50:43 -04004813 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004814 kfree(data);
4815}
4816
Chuck Lever4f390c12006-08-22 20:06:22 -04004817static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004818 struct page *page, unsigned int len, struct iattr *sattr,
4819 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004821 struct nfs4_createdata *data;
4822 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
Chuck Lever94a6d752006-08-22 20:06:23 -04004824 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004825 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004826
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004827 status = -ENOMEM;
4828 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4829 if (data == NULL)
4830 goto out;
4831
4832 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4833 data->arg.u.symlink.pages = &page;
4834 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004835 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004837 status = nfs4_do_create(dir, dentry, data);
4838
4839 nfs4_free_createdata(data);
4840out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 return status;
4842}
4843
Chuck Lever4f390c12006-08-22 20:06:22 -04004844static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004845 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846{
Trond Myklebust0688e642019-04-07 13:59:09 -04004847 struct nfs4_exception exception = {
4848 .interruptible = true,
4849 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004850 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004852
4853 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004856 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4857 trace_nfs4_symlink(dir, &dentry->d_name, err);
4858 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 &exception);
4860 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004861
4862 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 return err;
4864}
4865
4866static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004867 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004869 struct nfs4_createdata *data;
4870 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004872 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4873 if (data == NULL)
4874 goto out;
4875
David Quigley1775fd32013-05-22 12:50:42 -04004876 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004877 status = nfs4_do_create(dir, dentry, data);
4878
4879 nfs4_free_createdata(data);
4880out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 return status;
4882}
4883
4884static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4885 struct iattr *sattr)
4886{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004887 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004888 struct nfs4_exception exception = {
4889 .interruptible = true,
4890 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004891 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004893
David Quigleyaa9c2662013-05-22 12:50:44 -04004894 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4895
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004896 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4897 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004899 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4900 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4901 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 &exception);
4903 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004904 nfs4_label_release_security(label);
4905
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 return err;
4907}
4908
NeilBrown684f39b2018-12-03 11:30:30 +11004909static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004910 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911{
David Howells2b0143b2015-03-17 22:25:59 +00004912 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 struct nfs4_readdir_arg args = {
4914 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004915 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 .pgbase = 0,
4917 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004918 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004919 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 };
4921 struct nfs4_readdir_res res;
4922 struct rpc_message msg = {
4923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4924 .rpc_argp = &args,
4925 .rpc_resp = &res,
4926 .rpc_cred = cred,
4927 };
4928 int status;
4929
Al Viro6de14722013-09-16 10:53:17 -04004930 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4931 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004932 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004933 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004935 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 -05004936 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004937 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004938 status += args.pgbase;
4939 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004940
4941 nfs_invalidate_atime(dir);
4942
Harvey Harrison3110ff82008-05-02 13:42:44 -07004943 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 return status;
4945}
4946
NeilBrown684f39b2018-12-03 11:30:30 +11004947static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004948 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949{
Trond Myklebust0688e642019-04-07 13:59:09 -04004950 struct nfs4_exception exception = {
4951 .interruptible = true,
4952 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 int err;
4954 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004955 err = _nfs4_proc_readdir(dentry, cred, cookie,
4956 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004957 trace_nfs4_readdir(d_inode(dentry), err);
4958 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 &exception);
4960 } while (exception.retry);
4961 return err;
4962}
4963
4964static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004965 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004967 struct nfs4_createdata *data;
4968 int mode = sattr->ia_mode;
4969 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004971 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4972 if (data == NULL)
4973 goto out;
4974
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004976 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004978 data->arg.ftype = NF4BLK;
4979 data->arg.u.device.specdata1 = MAJOR(rdev);
4980 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 }
4982 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004983 data->arg.ftype = NF4CHR;
4984 data->arg.u.device.specdata1 = MAJOR(rdev);
4985 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004986 } else if (!S_ISSOCK(mode)) {
4987 status = -EINVAL;
4988 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 }
David Quigley1775fd32013-05-22 12:50:42 -04004990
David Quigleyaa9c2662013-05-22 12:50:44 -04004991 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004992 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004993out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004994 nfs4_free_createdata(data);
4995out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 return status;
4997}
4998
4999static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5000 struct iattr *sattr, dev_t rdev)
5001{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005002 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005003 struct nfs4_exception exception = {
5004 .interruptible = true,
5005 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005006 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005008
David Quigleyaa9c2662013-05-22 12:50:44 -04005009 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5010
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005011 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5012 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005014 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5015 trace_nfs4_mknod(dir, &dentry->d_name, err);
5016 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 &exception);
5018 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005019
5020 nfs4_label_release_security(label);
5021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 return err;
5023}
5024
5025static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5026 struct nfs_fsstat *fsstat)
5027{
5028 struct nfs4_statfs_arg args = {
5029 .fh = fhandle,
5030 .bitmask = server->attr_bitmask,
5031 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005032 struct nfs4_statfs_res res = {
5033 .fsstat = fsstat,
5034 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 struct rpc_message msg = {
5036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5037 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005038 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 };
5040
Trond Myklebust0e574af2005-10-27 22:12:38 -04005041 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005042 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043}
5044
5045static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5046{
Trond Myklebust0688e642019-04-07 13:59:09 -04005047 struct nfs4_exception exception = {
5048 .interruptible = true,
5049 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 int err;
5051 do {
5052 err = nfs4_handle_exception(server,
5053 _nfs4_proc_statfs(server, fhandle, fsstat),
5054 &exception);
5055 } while (exception.retry);
5056 return err;
5057}
5058
5059static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5060 struct nfs_fsinfo *fsinfo)
5061{
5062 struct nfs4_fsinfo_arg args = {
5063 .fh = fhandle,
5064 .bitmask = server->attr_bitmask,
5065 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005066 struct nfs4_fsinfo_res res = {
5067 .fsinfo = fsinfo,
5068 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 struct rpc_message msg = {
5070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5071 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005072 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 };
5074
Bryan Schumaker7c513052011-03-24 17:12:24 +00005075 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076}
5077
5078static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5079{
Trond Myklebust0688e642019-04-07 13:59:09 -04005080 struct nfs4_exception exception = {
5081 .interruptible = true,
5082 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 int err;
5084
5085 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005086 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005087 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005088 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005089 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005090 break;
5091 }
5092 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 } while (exception.retry);
5094 return err;
5095}
5096
5097static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5098{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005099 int error;
5100
Trond Myklebust0e574af2005-10-27 22:12:38 -04005101 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005102 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005103 if (error == 0) {
5104 /* block layout checks this! */
5105 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005106 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005107 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005108
5109 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110}
5111
5112static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5113 struct nfs_pathconf *pathconf)
5114{
5115 struct nfs4_pathconf_arg args = {
5116 .fh = fhandle,
5117 .bitmask = server->attr_bitmask,
5118 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005119 struct nfs4_pathconf_res res = {
5120 .pathconf = pathconf,
5121 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 struct rpc_message msg = {
5123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5124 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005125 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 };
5127
5128 /* None of the pathconf attributes are mandatory to implement */
5129 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5130 memset(pathconf, 0, sizeof(*pathconf));
5131 return 0;
5132 }
5133
Trond Myklebust0e574af2005-10-27 22:12:38 -04005134 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005135 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136}
5137
5138static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5139 struct nfs_pathconf *pathconf)
5140{
Trond Myklebust0688e642019-04-07 13:59:09 -04005141 struct nfs4_exception exception = {
5142 .interruptible = true,
5143 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 int err;
5145
5146 do {
5147 err = nfs4_handle_exception(server,
5148 _nfs4_proc_pathconf(server, fhandle, pathconf),
5149 &exception);
5150 } while (exception.retry);
5151 return err;
5152}
5153
Trond Myklebust5521abf2013-03-16 20:54:34 -04005154int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005155 const struct nfs_open_context *ctx,
5156 const struct nfs_lock_context *l_ctx,
5157 fmode_t fmode)
5158{
NeilBrown17393472016-10-13 15:26:47 +11005159 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005160}
5161EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5162
Trond Myklebust5521abf2013-03-16 20:54:34 -04005163static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5164 const struct nfs_open_context *ctx,
5165 const struct nfs_lock_context *l_ctx,
5166 fmode_t fmode)
5167{
Ben Dooksd49dd112019-10-16 17:28:21 +01005168 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005169
Trond Myklebuste1253be2014-03-05 08:44:23 -05005170 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005171 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005172 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005173 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005174}
5175
5176static bool nfs4_error_stateid_expired(int err)
5177{
5178 switch (err) {
5179 case -NFS4ERR_DELEG_REVOKED:
5180 case -NFS4ERR_ADMIN_REVOKED:
5181 case -NFS4ERR_BAD_STATEID:
5182 case -NFS4ERR_STALE_STATEID:
5183 case -NFS4ERR_OLD_STATEID:
5184 case -NFS4ERR_OPENMODE:
5185 case -NFS4ERR_EXPIRED:
5186 return true;
5187 }
5188 return false;
5189}
5190
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005191static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005193 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005195 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005196 if (task->tk_status < 0) {
5197 struct nfs4_exception exception = {
5198 .inode = hdr->inode,
5199 .state = hdr->args.context->state,
5200 .stateid = &hdr->args.stateid,
5201 };
5202 task->tk_status = nfs4_async_handle_exception(task,
5203 server, task->tk_status, &exception);
5204 if (exception.retry) {
5205 rpc_restart_call_prepare(task);
5206 return -EAGAIN;
5207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005211 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213}
5214
Trond Myklebust5521abf2013-03-16 20:54:34 -04005215static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005216 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005217{
5218
5219 if (!nfs4_error_stateid_expired(task->tk_status) ||
5220 nfs4_stateid_is_current(&args->stateid,
5221 args->context,
5222 args->lock_context,
5223 FMODE_READ))
5224 return false;
5225 rpc_restart_call_prepare(task);
5226 return true;
5227}
5228
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005229static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005230{
5231
5232 dprintk("--> %s\n", __func__);
5233
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005234 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005235 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005236 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005237 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005238 if (task->tk_status > 0)
5239 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005240 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5241 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005242}
5243
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005244static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5245 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005247 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005248 if (!hdr->pgio_done_cb)
5249 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005250 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005251 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252}
5253
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005254static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5255 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005256{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005257 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005258 &hdr->args.seq_args,
5259 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005260 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005261 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005262 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5263 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005264 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005265 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005266 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005267 return -EIO;
5268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269}
5270
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005271static int nfs4_write_done_cb(struct rpc_task *task,
5272 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005274 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005275
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005276 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005277 if (task->tk_status < 0) {
5278 struct nfs4_exception exception = {
5279 .inode = hdr->inode,
5280 .state = hdr->args.context->state,
5281 .stateid = &hdr->args.stateid,
5282 };
5283 task->tk_status = nfs4_async_handle_exception(task,
5284 NFS_SERVER(inode), task->tk_status,
5285 &exception);
5286 if (exception.retry) {
5287 rpc_restart_call_prepare(task);
5288 return -EAGAIN;
5289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 }
Trond Myklebust4f9838c72005-10-27 22:12:44 -04005291 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005292 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005293 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c72005-10-27 22:12:44 -04005294 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296}
5297
Trond Myklebust5521abf2013-03-16 20:54:34 -04005298static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005299 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005300{
5301
5302 if (!nfs4_error_stateid_expired(task->tk_status) ||
5303 nfs4_stateid_is_current(&args->stateid,
5304 args->context,
5305 args->lock_context,
5306 FMODE_WRITE))
5307 return false;
5308 rpc_restart_call_prepare(task);
5309 return true;
5310}
5311
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005312static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005313{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005314 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005315 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005316 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005317 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005318 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5319 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005320}
5321
Trond Myklebust5a37f852012-04-28 14:55:16 -04005322static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005323bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005324{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005325 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005326 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005327 return false;
5328 /* Otherwise, request attributes if and only if we don't hold
5329 * a delegation
5330 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005331 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005332}
Fred Isamana69aef12011-03-03 15:13:47 +00005333
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005334static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005335 struct rpc_message *msg,
5336 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005338 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005339
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005340 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5341 hdr->args.bitmask = NULL;
5342 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005343 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005344 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005345
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005346 if (!hdr->pgio_done_cb)
5347 hdr->pgio_done_cb = nfs4_write_done_cb;
5348 hdr->res.server = server;
5349 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005351 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005352 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005353 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354}
5355
Fred Isaman0b7c0152012-04-20 14:47:39 -04005356static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5357{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005358 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005359 &data->args.seq_args,
5360 &data->res.seq_res,
5361 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362}
5363
Fred Isaman0b7c0152012-04-20 14:47:39 -04005364static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005367
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005368 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005369 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5370 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005371 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005372 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375}
5376
Fred Isaman0b7c0152012-04-20 14:47:39 -04005377static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005378{
5379 if (!nfs4_sequence_done(task, &data->res.seq_res))
5380 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005381 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005382}
5383
Anna Schumakere9ae1ee22018-05-04 16:22:49 -04005384static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5385 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005387 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005388
Fred Isaman0b7c0152012-04-20 14:47:39 -04005389 if (data->commit_done_cb == NULL)
5390 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c72005-10-27 22:12:44 -04005391 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005392 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005393 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee22018-05-04 16:22:49 -04005394 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395}
5396
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005397static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5398 struct nfs_commitres *res)
5399{
5400 struct inode *dst_inode = file_inode(dst);
5401 struct nfs_server *server = NFS_SERVER(dst_inode);
5402 struct rpc_message msg = {
5403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5404 .rpc_argp = args,
5405 .rpc_resp = res,
5406 };
5407
5408 args->fh = NFS_FH(dst_inode);
5409 return nfs4_call_sync(server->client, server, &msg,
5410 &args->seq_args, &res->seq_res, 1);
5411}
5412
5413int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5414{
5415 struct nfs_commitargs args = {
5416 .offset = offset,
5417 .count = count,
5418 };
5419 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5420 struct nfs4_exception exception = { };
5421 int status;
5422
5423 do {
5424 status = _nfs4_proc_commit(dst, &args, res);
5425 status = nfs4_handle_exception(dst_server, status, &exception);
5426 } while (exception.retry);
5427
5428 return status;
5429}
5430
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005431struct nfs4_renewdata {
5432 struct nfs_client *client;
5433 unsigned long timestamp;
5434};
5435
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436/*
5437 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5438 * standalone procedure for queueing an asynchronous RENEW.
5439 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005440static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005441{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005442 struct nfs4_renewdata *data = calldata;
5443 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005444
Elena Reshetova212bf412017-10-20 12:53:38 +03005445 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005446 nfs4_schedule_state_renewal(clp);
5447 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005448 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005449}
5450
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005451static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005453 struct nfs4_renewdata *data = calldata;
5454 struct nfs_client *clp = data->client;
5455 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005457 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005458 switch (task->tk_status) {
5459 case 0:
5460 break;
5461 case -NFS4ERR_LEASE_MOVED:
5462 nfs4_schedule_lease_moved_recovery(clp);
5463 break;
5464 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005465 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005466 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5467 return;
5468 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005469 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005470 return;
5471 }
5472 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005474 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475}
5476
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005477static const struct rpc_call_ops nfs4_renew_ops = {
5478 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005479 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005480};
5481
NeilBrowna52458b2018-12-03 11:30:31 +11005482static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
5484 struct rpc_message msg = {
5485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5486 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005487 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005489 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005491 if (renew_flags == 0)
5492 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005493 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005494 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005495 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005496 if (data == NULL) {
5497 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005498 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005499 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005500 data->client = clp;
5501 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005502 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005503 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504}
5505
NeilBrowna52458b2018-12-03 11:30:31 +11005506static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507{
5508 struct rpc_message msg = {
5509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5510 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005511 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 };
5513 unsigned long now = jiffies;
5514 int status;
5515
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005516 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 if (status < 0)
5518 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005519 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 return 0;
5521}
5522
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005523static inline int nfs4_server_supports_acls(struct nfs_server *server)
5524{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005525 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005526}
5527
Trond Myklebust21f498c2012-08-24 10:59:25 -04005528/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5529 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005530 * the stack.
5531 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005532#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005533
Neil Hormane9e3d722011-03-04 19:26:03 -05005534static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005535 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005536{
5537 struct page *newpage, **spages;
5538 int rc = 0;
5539 size_t len;
5540 spages = pages;
5541
5542 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005543 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005544 newpage = alloc_page(GFP_KERNEL);
5545
5546 if (newpage == NULL)
5547 goto unwind;
5548 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005549 buf += len;
5550 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005551 *pages++ = newpage;
5552 rc++;
5553 } while (buflen != 0);
5554
5555 return rc;
5556
5557unwind:
5558 for(; rc > 0; rc--)
5559 __free_page(spages[rc-1]);
5560 return -ENOMEM;
5561}
5562
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005563struct nfs4_cached_acl {
5564 int cached;
5565 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005566 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005567};
5568
5569static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005570{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005571 struct nfs_inode *nfsi = NFS_I(inode);
5572
5573 spin_lock(&inode->i_lock);
5574 kfree(nfsi->nfs4_acl);
5575 nfsi->nfs4_acl = acl;
5576 spin_unlock(&inode->i_lock);
5577}
5578
5579static void nfs4_zap_acl_attr(struct inode *inode)
5580{
5581 nfs4_set_cached_acl(inode, NULL);
5582}
5583
5584static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5585{
5586 struct nfs_inode *nfsi = NFS_I(inode);
5587 struct nfs4_cached_acl *acl;
5588 int ret = -ENOENT;
5589
5590 spin_lock(&inode->i_lock);
5591 acl = nfsi->nfs4_acl;
5592 if (acl == NULL)
5593 goto out;
5594 if (buf == NULL) /* user is just asking for length */
5595 goto out_len;
5596 if (acl->cached == 0)
5597 goto out;
5598 ret = -ERANGE; /* see getxattr(2) man page */
5599 if (acl->len > buflen)
5600 goto out;
5601 memcpy(buf, acl->data, acl->len);
5602out_len:
5603 ret = acl->len;
5604out:
5605 spin_unlock(&inode->i_lock);
5606 return ret;
5607}
5608
Sachin Prabhu5794d212012-04-17 14:36:40 +01005609static 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 +00005610{
5611 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005612 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005613
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005614 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005615 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005616 if (acl == NULL)
5617 goto out;
5618 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005619 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005620 } else {
5621 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5622 if (acl == NULL)
5623 goto out;
5624 acl->cached = 0;
5625 }
5626 acl->len = acl_len;
5627out:
5628 nfs4_set_cached_acl(inode, acl);
5629}
5630
Andy Adamsonbf118a32011-12-07 11:55:27 -05005631/*
5632 * The getxattr API returns the required buffer length when called with a
5633 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5634 * the required buf. On a NULL buf, we send a page of data to the server
5635 * guessing that the ACL request can be serviced by a page. If so, we cache
5636 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5637 * the cache. If not so, we throw away the page, and cache the required
5638 * length. The next getxattr call will then produce another round trip to
5639 * the server, this time with the input buf of the required size.
5640 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005641static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005642{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005643 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005644 struct nfs_getaclargs args = {
5645 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005646 .acl_len = buflen,
5647 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005648 struct nfs_getaclres res = {
5649 .acl_len = buflen,
5650 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005651 struct rpc_message msg = {
5652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5653 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005654 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005655 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005656 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005657 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005658 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005659
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005660 if (buflen == 0)
5661 buflen = server->rsize;
5662
5663 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5664 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5665 if (!pages)
5666 return -ENOMEM;
5667
5668 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005669
Andy Adamsonbf118a32011-12-07 11:55:27 -05005670 for (i = 0; i < npages; i++) {
5671 pages[i] = alloc_page(GFP_KERNEL);
5672 if (!pages[i])
5673 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005674 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005675
5676 /* for decoding across pages */
5677 res.acl_scratch = alloc_page(GFP_KERNEL);
5678 if (!res.acl_scratch)
5679 goto out_free;
5680
Andy Adamsonbf118a32011-12-07 11:55:27 -05005681 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005682
Peng Taode040be2012-01-10 22:42:47 +08005683 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005684 __func__, buf, buflen, npages, args.acl_len);
5685 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5686 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005687 if (ret)
5688 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005689
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005690 /* Handle the case where the passed-in buffer is too short */
5691 if (res.acl_flags & NFS4_ACL_TRUNC) {
5692 /* Did the user only issue a request for the acl length? */
5693 if (buf == NULL)
5694 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005695 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005696 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005697 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005698 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005699 if (buf) {
5700 if (res.acl_len > buflen) {
5701 ret = -ERANGE;
5702 goto out_free;
5703 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005704 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005705 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005706out_ok:
5707 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005708out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005709 for (i = 0; i < npages; i++)
5710 if (pages[i])
5711 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005712 if (res.acl_scratch)
5713 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005714 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005715 return ret;
5716}
5717
Trond Myklebust16b42892006-08-24 12:27:15 -04005718static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5719{
Trond Myklebust0688e642019-04-07 13:59:09 -04005720 struct nfs4_exception exception = {
5721 .interruptible = true,
5722 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005723 ssize_t ret;
5724 do {
5725 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005726 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005727 if (ret >= 0)
5728 break;
5729 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5730 } while (exception.retry);
5731 return ret;
5732}
5733
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005734static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5735{
5736 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005737 int ret;
5738
5739 if (!nfs4_server_supports_acls(server))
5740 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005741 ret = nfs_revalidate_inode(server, inode);
5742 if (ret < 0)
5743 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005744 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5745 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005746 ret = nfs4_read_cached_acl(inode, buf, buflen);
5747 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005748 /* -ENOENT is returned if there is no ACL or if there is an ACL
5749 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005750 return ret;
5751 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005752}
5753
Trond Myklebust16b42892006-08-24 12:27:15 -04005754static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005755{
5756 struct nfs_server *server = NFS_SERVER(inode);
5757 struct page *pages[NFS4ACL_MAXPAGES];
5758 struct nfs_setaclargs arg = {
5759 .fh = NFS_FH(inode),
5760 .acl_pages = pages,
5761 .acl_len = buflen,
5762 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005763 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005764 struct rpc_message msg = {
5765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5766 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005767 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005768 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005769 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005770 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005771
5772 if (!nfs4_server_supports_acls(server))
5773 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005774 if (npages > ARRAY_SIZE(pages))
5775 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005776 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005777 if (i < 0)
5778 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005779 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005780 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005781
5782 /*
5783 * Free each page after tx, so the only ref left is
5784 * held by the network stack
5785 */
5786 for (; i > 0; i--)
5787 put_page(pages[i-1]);
5788
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005789 /*
5790 * Acl update can result in inode attribute update.
5791 * so mark the attribute cache invalid.
5792 */
5793 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005794 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005795 | NFS_INO_INVALID_CTIME
5796 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005797 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005798 nfs_access_zap_cache(inode);
5799 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005800 return ret;
5801}
5802
Trond Myklebust16b42892006-08-24 12:27:15 -04005803static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5804{
5805 struct nfs4_exception exception = { };
5806 int err;
5807 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005808 err = __nfs4_proc_set_acl(inode, buf, buflen);
5809 trace_nfs4_set_acl(inode, err);
5810 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005811 &exception);
5812 } while (exception.retry);
5813 return err;
5814}
5815
David Quigleyaa9c2662013-05-22 12:50:44 -04005816#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5817static int _nfs4_get_security_label(struct inode *inode, void *buf,
5818 size_t buflen)
5819{
5820 struct nfs_server *server = NFS_SERVER(inode);
5821 struct nfs_fattr fattr;
5822 struct nfs4_label label = {0, 0, buflen, buf};
5823
5824 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005825 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005826 .fh = NFS_FH(inode),
5827 .bitmask = bitmask,
5828 };
5829 struct nfs4_getattr_res res = {
5830 .fattr = &fattr,
5831 .label = &label,
5832 .server = server,
5833 };
5834 struct rpc_message msg = {
5835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005836 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005837 .rpc_resp = &res,
5838 };
5839 int ret;
5840
5841 nfs_fattr_init(&fattr);
5842
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005843 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005844 if (ret)
5845 return ret;
5846 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5847 return -ENOENT;
5848 if (buflen < label.len)
5849 return -ERANGE;
5850 return 0;
5851}
5852
5853static int nfs4_get_security_label(struct inode *inode, void *buf,
5854 size_t buflen)
5855{
Trond Myklebust0688e642019-04-07 13:59:09 -04005856 struct nfs4_exception exception = {
5857 .interruptible = true,
5858 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005859 int err;
5860
5861 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5862 return -EOPNOTSUPP;
5863
5864 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005865 err = _nfs4_get_security_label(inode, buf, buflen);
5866 trace_nfs4_get_security_label(inode, err);
5867 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005868 &exception);
5869 } while (exception.retry);
5870 return err;
5871}
5872
5873static int _nfs4_do_set_security_label(struct inode *inode,
5874 struct nfs4_label *ilabel,
5875 struct nfs_fattr *fattr,
5876 struct nfs4_label *olabel)
5877{
5878
5879 struct iattr sattr = {0};
5880 struct nfs_server *server = NFS_SERVER(inode);
5881 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005882 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005883 .fh = NFS_FH(inode),
5884 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005885 .server = server,
5886 .bitmask = bitmask,
5887 .label = ilabel,
5888 };
5889 struct nfs_setattrres res = {
5890 .fattr = fattr,
5891 .label = olabel,
5892 .server = server,
5893 };
5894 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5896 .rpc_argp = &arg,
5897 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005898 };
5899 int status;
5900
Jeff Layton12207f62013-11-01 10:49:32 -04005901 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005902
Jeff Layton12207f62013-11-01 10:49:32 -04005903 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005904 if (status)
5905 dprintk("%s failed: %d\n", __func__, status);
5906
5907 return status;
5908}
5909
5910static int nfs4_do_set_security_label(struct inode *inode,
5911 struct nfs4_label *ilabel,
5912 struct nfs_fattr *fattr,
5913 struct nfs4_label *olabel)
5914{
5915 struct nfs4_exception exception = { };
5916 int err;
5917
5918 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005919 err = _nfs4_do_set_security_label(inode, ilabel,
5920 fattr, olabel);
5921 trace_nfs4_set_security_label(inode, err);
5922 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005923 &exception);
5924 } while (exception.retry);
5925 return err;
5926}
5927
5928static int
Al Viro59301222016-05-27 10:19:30 -04005929nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005930{
5931 struct nfs4_label ilabel, *olabel = NULL;
5932 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005933 int status;
5934
5935 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5936 return -EOPNOTSUPP;
5937
5938 nfs_fattr_init(&fattr);
5939
5940 ilabel.pi = 0;
5941 ilabel.lfs = 0;
5942 ilabel.label = (char *)buf;
5943 ilabel.len = buflen;
5944
David Quigleyaa9c2662013-05-22 12:50:44 -04005945 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5946 if (IS_ERR(olabel)) {
5947 status = -PTR_ERR(olabel);
5948 goto out;
5949 }
5950
5951 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5952 if (status == 0)
5953 nfs_setsecurity(inode, &fattr, olabel);
5954
5955 nfs4_label_free(olabel);
5956out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005957 return status;
5958}
5959#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5960
5961
Chuck Leverf0920752012-05-21 22:45:41 -04005962static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5963 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005964{
5965 __be32 verf[2];
5966
Chuck Lever2c820d92012-05-21 22:45:33 -04005967 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5968 /* An impossible timestamp guarantees this value
5969 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005970 verf[0] = cpu_to_be32(U32_MAX);
5971 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005972 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005973 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005974 u64 ns = ktime_to_ns(nn->boot_time);
5975
5976 verf[0] = cpu_to_be32(ns >> 32);
5977 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005978 }
Chuck Levercd937102012-03-02 17:14:31 -05005979 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5980}
5981
Jeff Laytona3192682015-06-09 19:43:59 -04005982static int
5983nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005984{
Jeff Laytona3192682015-06-09 19:43:59 -04005985 size_t len;
5986 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005987
Trond Myklebustceb3a162015-01-03 15:16:04 -05005988 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005989 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005990
Jeff Laytona3192682015-06-09 19:43:59 -04005991 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005992 len = 14 +
5993 strlen(clp->cl_rpcclient->cl_nodename) +
5994 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005995 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005996 1;
5997 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005998 if (nfs4_client_id_uniquifier[0] != '\0')
5999 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04006000 if (len > NFS4_OPAQUE_LIMIT + 1)
6001 return -EINVAL;
6002
6003 /*
6004 * Since this string is allocated at mount time, and held until the
6005 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6006 * about a memory-reclaim deadlock.
6007 */
6008 str = kmalloc(len, GFP_KERNEL);
6009 if (!str)
6010 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006011
Chuck Levere984a552012-09-14 17:24:21 -04006012 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006013 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04006014 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006015 clp->cl_rpcclient->cl_nodename,
6016 nfs4_client_id_uniquifier,
6017 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006018 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006019 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006020 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006021 clp->cl_rpcclient->cl_nodename,
6022 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006023 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006024 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006025
Jeff Laytona3192682015-06-09 19:43:59 -04006026 clp->cl_owner_id = str;
6027 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006028}
6029
Jeff Layton873e3852015-06-09 19:44:00 -04006030static int
6031nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006032{
Jeff Layton873e3852015-06-09 19:44:00 -04006033 size_t len;
6034 char *str;
6035
6036 len = 10 + 10 + 1 + 10 + 1 +
6037 strlen(nfs4_client_id_uniquifier) + 1 +
6038 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6039
6040 if (len > NFS4_OPAQUE_LIMIT + 1)
6041 return -EINVAL;
6042
6043 /*
6044 * Since this string is allocated at mount time, and held until the
6045 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6046 * about a memory-reclaim deadlock.
6047 */
6048 str = kmalloc(len, GFP_KERNEL);
6049 if (!str)
6050 return -ENOMEM;
6051
Trond Myklebustf2dd436e2015-10-08 11:33:17 -04006052 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04006053 clp->rpc_ops->version, clp->cl_minorversion,
6054 nfs4_client_id_uniquifier,
6055 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006056 clp->cl_owner_id = str;
6057 return 0;
6058}
6059
6060static int
6061nfs4_init_uniform_client_string(struct nfs_client *clp)
6062{
Jeff Layton873e3852015-06-09 19:44:00 -04006063 size_t len;
6064 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006065
6066 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006067 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006068
6069 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04006070 return nfs4_init_uniquifier_client_string(clp);
6071
6072 len = 10 + 10 + 1 + 10 + 1 +
6073 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6074
6075 if (len > NFS4_OPAQUE_LIMIT + 1)
6076 return -EINVAL;
6077
6078 /*
6079 * Since this string is allocated at mount time, and held until the
6080 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6081 * about a memory-reclaim deadlock.
6082 */
6083 str = kmalloc(len, GFP_KERNEL);
6084 if (!str)
6085 return -ENOMEM;
6086
Trond Myklebustf2dd436e2015-10-08 11:33:17 -04006087 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04006088 clp->rpc_ops->version, clp->cl_minorversion,
6089 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006090 clp->cl_owner_id = str;
6091 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006092}
6093
Chuck Lever706cb8d2014-03-12 12:51:47 -04006094/*
6095 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6096 * services. Advertise one based on the address family of the
6097 * clientaddr.
6098 */
6099static unsigned int
6100nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6101{
6102 if (strchr(clp->cl_ipaddr, ':') != NULL)
6103 return scnprintf(buf, len, "tcp6");
6104 else
6105 return scnprintf(buf, len, "tcp");
6106}
6107
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006108static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6109{
6110 struct nfs4_setclientid *sc = calldata;
6111
6112 if (task->tk_status == 0)
6113 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6114}
6115
6116static const struct rpc_call_ops nfs4_setclientid_ops = {
6117 .rpc_call_done = nfs4_setclientid_done,
6118};
6119
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04006120/**
6121 * nfs4_proc_setclientid - Negotiate client ID
6122 * @clp: state data structure
6123 * @program: RPC program for NFSv4 callback service
6124 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006125 * @cred: credential to use for this call
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04006126 * @res: where to place the result
6127 *
6128 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6129 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006130int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006131 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006132 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133{
6134 nfs4_verifier sc_verifier;
6135 struct nfs4_setclientid setclientid = {
6136 .sc_verifier = &sc_verifier,
6137 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006138 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 };
6140 struct rpc_message msg = {
6141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6142 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006143 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006144 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006146 struct rpc_task_setup task_setup_data = {
6147 .rpc_client = clp->cl_rpcclient,
6148 .rpc_message = &msg,
6149 .callback_ops = &nfs4_setclientid_ops,
6150 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006151 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006152 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006153 unsigned long now = jiffies;
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04006154 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155
Chuck Leverde734832012-07-11 16:30:50 -04006156 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006157 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006158
6159 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6160 status = nfs4_init_uniform_client_string(clp);
6161 else
Jeff Laytona3192682015-06-09 19:43:59 -04006162 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006163
6164 if (status)
6165 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006166
Chuck Leverde734832012-07-11 16:30:50 -04006167 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006168 setclientid.sc_netid_len =
6169 nfs4_init_callback_netid(clp,
6170 setclientid.sc_netid,
6171 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006172 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006173 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 clp->cl_ipaddr, port >> 8, port & 255);
6175
Jeff Layton3a6bb732015-06-09 19:43:57 -04006176 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04006177 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006178 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006179
6180 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006181 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006182 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006183 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6184 put_rpccred(setclientid.sc_cred);
6185 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006186
6187 if (status == 0)
6188 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006189out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006190 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04006191 dprintk("NFS reply setclientid: %d\n", status);
6192 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193}
6194
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04006195/**
6196 * nfs4_proc_setclientid_confirm - Confirm client ID
6197 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006198 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006199 * @cred: credential to use for this call
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04006200 *
6201 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6202 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006203int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006204 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006205 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 struct rpc_message msg = {
6208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006209 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006210 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 int status;
6213
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04006214 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6215 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6216 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006217 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6218 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006219 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae82012-07-11 16:30:59 -04006220 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 return status;
6222}
6223
Trond Myklebustfe650402006-01-03 09:55:18 +01006224struct nfs4_delegreturndata {
6225 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006226 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006227 struct nfs_fh fh;
6228 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006229 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006230 struct {
6231 struct nfs4_layoutreturn_args arg;
6232 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006233 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006234 u32 roc_barrier;
6235 bool roc;
6236 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006237 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006238 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006239 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006240};
6241
Trond Myklebustfe650402006-01-03 09:55:18 +01006242static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6243{
6244 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006245 struct nfs4_exception exception = {
6246 .inode = data->inode,
6247 .stateid = &data->stateid,
6248 };
Andy Adamson938e1012009-04-01 09:22:28 -04006249
Trond Myklebust14516c32010-07-31 14:29:06 -04006250 if (!nfs4_sequence_done(task, &data->res.seq_res))
6251 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006252
Trond Myklebustca8acf82013-08-13 10:36:56 -04006253 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006254
6255 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04006256 if (pnfs_roc_done(task, data->inode,
6257 &data->args.lr_args,
6258 &data->res.lr_res,
6259 &data->res.lr_ret) == -EAGAIN)
6260 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006261
Ricardo Labiaga79708862009-12-07 09:23:21 -05006262 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006263 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006264 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006265 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006266 case -NFS4ERR_ADMIN_REVOKED:
6267 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006268 case -NFS4ERR_EXPIRED:
6269 nfs4_free_revoked_stateid(data->res.server,
6270 data->args.stateid,
6271 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006272 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006273 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006274 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006275 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006276 task->tk_status = 0;
6277 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006278 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006279 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6280 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006281 if (data->args.bitmask) {
6282 data->args.bitmask = NULL;
6283 data->res.fattr = NULL;
6284 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006285 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006286 case -NFS4ERR_ACCESS:
6287 if (data->args.bitmask) {
6288 data->args.bitmask = NULL;
6289 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006290 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006291 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006292 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006293 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006294 task->tk_status = nfs4_async_handle_exception(task,
6295 data->res.server, task->tk_status,
6296 &exception);
6297 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006298 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006299 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006300 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006301 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006302 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006303out_restart:
6304 task->tk_status = 0;
6305 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006306}
6307
6308static void nfs4_delegreturn_release(void *calldata)
6309{
Peng Tao039b7562014-07-03 13:05:02 +08006310 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006311 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006312
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006313 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006314 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006315 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6316 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006317 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006318 nfs_iput_and_deactive(inode);
6319 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006320 kfree(calldata);
6321}
6322
Andy Adamson938e1012009-04-01 09:22:28 -04006323static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6324{
6325 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006326 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006327
6328 d_data = (struct nfs4_delegreturndata *)data;
6329
Trond Myklebust5326de92019-11-13 09:39:36 +01006330 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6331 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006332 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006333 }
Peng Tao500d7012015-09-22 11:35:22 +08006334
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006335 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6336 if (lo && !pnfs_layout_is_valid(lo)) {
6337 d_data->args.lr_args = NULL;
6338 d_data->res.lr_res = NULL;
6339 }
6340
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006341 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006342 &d_data->args.seq_args,
6343 &d_data->res.seq_res,
6344 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006345}
Andy Adamson938e1012009-04-01 09:22:28 -04006346
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006347static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006348 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006349 .rpc_call_done = nfs4_delegreturn_done,
6350 .rpc_release = nfs4_delegreturn_release,
6351};
6352
NeilBrowna52458b2018-12-03 11:30:31 +11006353static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006354{
6355 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006356 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006357 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006358 struct rpc_message msg = {
6359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6360 .rpc_cred = cred,
6361 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006362 struct rpc_task_setup task_setup_data = {
6363 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006364 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006365 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebustf304a802020-05-11 10:42:04 -04006366 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006367 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006368 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006369
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006370 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006371 if (data == NULL)
6372 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006373 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006374
6375 nfs4_state_protect(server->nfs_client,
6376 NFS_SP4_MACH_CRED_CLEANUP,
6377 &task_setup_data.rpc_client, &msg);
6378
Trond Myklebustfe650402006-01-03 09:55:18 +01006379 data->args.fhandle = &data->fh;
6380 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006381 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006382 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006383 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006384 data->res.fattr = &data->fattr;
6385 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006386 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006387 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006388 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006389 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006390 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006391 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006392 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006393 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006394 if (data->lr.roc) {
6395 data->args.lr_args = &data->lr.arg;
6396 data->res.lr_res = &data->lr.res;
6397 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006398 } else if (data->lr.roc) {
6399 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6400 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006401 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006402
Trond Myklebustc970aa82007-07-14 15:39:59 -04006403 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006404 msg.rpc_argp = &data->args;
6405 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006406 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006407 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006408 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006409 if (!issync)
6410 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006411 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006412 if (status != 0)
6413 goto out;
6414 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006415out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006416 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006417 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418}
6419
NeilBrowna52458b2018-12-03 11:30:31 +11006420int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421{
6422 struct nfs_server *server = NFS_SERVER(inode);
6423 struct nfs4_exception exception = { };
6424 int err;
6425 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006426 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006427 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 switch (err) {
6429 case -NFS4ERR_STALE_STATEID:
6430 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 case 0:
6432 return 0;
6433 }
6434 err = nfs4_handle_exception(server, err, &exception);
6435 } while (exception.retry);
6436 return err;
6437}
6438
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6440{
6441 struct inode *inode = state->inode;
6442 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006443 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006444 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006446 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006448 struct nfs_lockt_res res = {
6449 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 };
6451 struct rpc_message msg = {
6452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006453 .rpc_argp = &arg,
6454 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 .rpc_cred = state->owner->so_cred,
6456 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 struct nfs4_lock_state *lsp;
6458 int status;
6459
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006460 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006461 status = nfs4_set_lock_state(state, request);
6462 if (status != 0)
6463 goto out;
6464 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006465 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006466 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006467 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006468 switch (status) {
6469 case 0:
6470 request->fl_type = F_UNLCK;
6471 break;
6472 case -NFS4ERR_DENIED:
6473 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006475 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006476 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006477out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 return status;
6479}
6480
6481static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6482{
Trond Myklebust0688e642019-04-07 13:59:09 -04006483 struct nfs4_exception exception = {
6484 .interruptible = true,
6485 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 int err;
6487
6488 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006489 err = _nfs4_proc_getlk(state, cmd, request);
6490 trace_nfs4_get_lock(request, state, cmd, err);
6491 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 &exception);
6493 } while (exception.retry);
6494 return err;
6495}
6496
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006497/*
6498 * Update the seqid of a lock stateid after receiving
6499 * NFS4ERR_OLD_STATEID
6500 */
6501static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6502 struct nfs4_lock_state *lsp)
6503{
6504 struct nfs4_state *state = lsp->ls_state;
6505 bool ret = false;
6506
6507 spin_lock(&state->state_lock);
6508 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6509 goto out;
6510 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6511 nfs4_stateid_seqid_inc(dst);
6512 else
6513 dst->seqid = lsp->ls_stateid.seqid;
6514 ret = true;
6515out:
6516 spin_unlock(&state->state_lock);
6517 return ret;
6518}
6519
6520static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6521 struct nfs4_lock_state *lsp)
6522{
6523 struct nfs4_state *state = lsp->ls_state;
6524 bool ret;
6525
6526 spin_lock(&state->state_lock);
6527 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6528 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6529 spin_unlock(&state->state_lock);
6530 return ret;
6531}
6532
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006533struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006534 struct nfs_locku_args arg;
6535 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006536 struct nfs4_lock_state *lsp;
6537 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006538 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006539 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006540 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006541 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006542};
6543
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006544static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6545 struct nfs_open_context *ctx,
6546 struct nfs4_lock_state *lsp,
6547 struct nfs_seqid *seqid)
6548{
6549 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006550 struct nfs4_state *state = lsp->ls_state;
6551 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006552
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006553 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006554 if (p == NULL)
6555 return NULL;
6556 p->arg.fh = NFS_FH(inode);
6557 p->arg.fl = &p->fl;
6558 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006559 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006560 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006561 /* Ensure we don't close file until we're done freeing locks! */
6562 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006563 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006564 locks_init_lock(&p->fl);
6565 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006566 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006567 spin_lock(&state->state_lock);
6568 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6569 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006570 return p;
6571}
6572
Trond Myklebust06f814a2006-01-03 09:55:07 +01006573static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006574{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006575 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006576 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006577 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006578 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006579 put_nfs_open_context(calldata->ctx);
6580 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006581}
6582
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006583static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006584{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006585 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006586 struct nfs4_exception exception = {
6587 .inode = calldata->lsp->ls_state->inode,
6588 .stateid = &calldata->arg.stateid,
6589 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006590
Trond Myklebust14516c32010-07-31 14:29:06 -04006591 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6592 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006593 switch (task->tk_status) {
6594 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006595 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006596 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006597 if (nfs4_update_lock_stateid(calldata->lsp,
6598 &calldata->res.stateid))
6599 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006600 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006601 case -NFS4ERR_ADMIN_REVOKED:
6602 case -NFS4ERR_EXPIRED:
6603 nfs4_free_revoked_stateid(calldata->server,
6604 &calldata->arg.stateid,
6605 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006606 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006607 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006608 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006609 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6610 calldata->lsp))
6611 rpc_restart_call_prepare(task);
6612 break;
6613 case -NFS4ERR_OLD_STATEID:
6614 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6615 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006616 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006617 break;
6618 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006619 task->tk_status = nfs4_async_handle_exception(task,
6620 calldata->server, task->tk_status,
6621 &exception);
6622 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006623 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006624 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006625 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006626}
6627
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006628static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006629{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006630 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006632 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6633 nfs_async_iocounter_wait(task, calldata->l_ctx))
6634 return;
6635
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006636 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006637 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006638 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006639 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006640 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006641 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006642 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006643 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006644 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006645 &calldata->res.seq_res,
6646 task) != 0)
6647 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006648 return;
6649out_no_action:
6650 task->tk_action = NULL;
6651out_wait:
6652 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653}
6654
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006655static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006656 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006657 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006658 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006659};
6660
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006661static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6662 struct nfs_open_context *ctx,
6663 struct nfs4_lock_state *lsp,
6664 struct nfs_seqid *seqid)
6665{
6666 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006667 struct rpc_message msg = {
6668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6669 .rpc_cred = ctx->cred,
6670 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006671 struct rpc_task_setup task_setup_data = {
6672 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006673 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006674 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006675 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006676 .flags = RPC_TASK_ASYNC,
6677 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006678
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006679 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6680 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6681
Frank Filz137d6ac2007-07-09 15:32:29 -07006682 /* Ensure this is an unlock - when canceling a lock, the
6683 * canceled lock is passed in, and it won't be an unlock.
6684 */
6685 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006686 if (fl->fl_flags & FL_CLOSE)
6687 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006688
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006689 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6690 if (data == NULL) {
6691 nfs_free_seqid(seqid);
6692 return ERR_PTR(-ENOMEM);
6693 }
6694
Anna Schumakerfba83f32018-05-04 16:22:50 -04006695 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006696 msg.rpc_argp = &data->arg;
6697 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006698 task_setup_data.callback_data = data;
6699 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006700}
6701
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6703{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006704 struct inode *inode = state->inode;
6705 struct nfs4_state_owner *sp = state->owner;
6706 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006707 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006708 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006709 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006710 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006711 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006712 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
Trond Myklebust9b073572006-06-29 16:38:34 -04006714 status = nfs4_set_lock_state(state, request);
6715 /* Unlock _before_ we do the RPC call */
6716 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006717 /* Exclude nfs_delegation_claim_locks() */
6718 mutex_lock(&sp->so_delegreturn_mutex);
6719 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006720 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006721 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006722 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006723 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006724 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006725 }
6726 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006727 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006728 if (status != 0)
6729 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006730 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006731 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006732 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6733 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006734 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6735 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006736 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006737 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006738 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006739 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006740 status = PTR_ERR(task);
6741 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006742 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006743 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006744 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006745out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006746 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006747 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006748 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749}
6750
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006751struct nfs4_lockdata {
6752 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006753 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006754 struct nfs4_lock_state *lsp;
6755 struct nfs_open_context *ctx;
6756 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006757 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006758 int rpc_status;
6759 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006760 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006761};
6762
6763static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006764 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6765 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006766{
6767 struct nfs4_lockdata *p;
6768 struct inode *inode = lsp->ls_state->inode;
6769 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006770 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006771
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006772 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006773 if (p == NULL)
6774 return NULL;
6775
6776 p->arg.fh = NFS_FH(inode);
6777 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006778 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006779 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006780 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006781 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6782 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006783 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006784 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006785 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006786 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006787 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006788 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006789 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006790 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006791 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006792 locks_init_lock(&p->fl);
6793 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006794 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006795out_free_seqid:
6796 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006797out_free:
6798 kfree(p);
6799 return NULL;
6800}
6801
6802static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6803{
6804 struct nfs4_lockdata *data = calldata;
6805 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
Harvey Harrison3110ff82008-05-02 13:42:44 -07006807 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006808 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006809 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006810 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006811 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006812 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006813 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006814 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006815 nfs4_stateid_copy(&data->arg.open_stateid,
6816 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006817 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006818 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006819 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006820 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006821 nfs4_stateid_copy(&data->arg.lock_stateid,
6822 &data->lsp->ls_stateid);
6823 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006824 if (!nfs4_valid_open_stateid(state)) {
6825 data->rpc_status = -EBADF;
6826 task->tk_action = NULL;
6827 goto out_release_open_seqid;
6828 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006829 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006830 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006831 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006832 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006833 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006834 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006835out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006836 nfs_release_seqid(data->arg.open_seqid);
6837out_release_lock_seqid:
6838 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006839out_wait:
6840 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006841 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006842}
6843
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006844static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6845{
6846 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006847 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006848
Harvey Harrison3110ff82008-05-02 13:42:44 -07006849 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006850
Trond Myklebust14516c32010-07-31 14:29:06 -04006851 if (!nfs4_sequence_done(task, &data->res.seq_res))
6852 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006853
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006854 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006855 switch (task->tk_status) {
6856 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006857 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006858 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006859 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006860 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006861 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006862 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006863 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006864 if (data->arg.new_lock_owner != 0) {
6865 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6866 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6867 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006868 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6869 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006870 break;
6871 case -NFS4ERR_BAD_STATEID:
6872 case -NFS4ERR_OLD_STATEID:
6873 case -NFS4ERR_STALE_STATEID:
6874 case -NFS4ERR_EXPIRED:
6875 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006876 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006877 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006878 goto out_restart;
6879 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006880 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006881 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006882 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006883out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006884 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006885 return;
6886out_restart:
6887 if (!data->cancelled)
6888 rpc_restart_call_prepare(task);
6889 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006890}
6891
6892static void nfs4_lock_release(void *calldata)
6893{
6894 struct nfs4_lockdata *data = calldata;
6895
Harvey Harrison3110ff82008-05-02 13:42:44 -07006896 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006897 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006898 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006899 struct rpc_task *task;
6900 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6901 data->arg.lock_seqid);
6902 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006903 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006904 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006905 } else
6906 nfs_free_seqid(data->arg.lock_seqid);
6907 nfs4_put_lock_state(data->lsp);
6908 put_nfs_open_context(data->ctx);
6909 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006910 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006911}
6912
6913static const struct rpc_call_ops nfs4_lock_ops = {
6914 .rpc_call_prepare = nfs4_lock_prepare,
6915 .rpc_call_done = nfs4_lock_done,
6916 .rpc_release = nfs4_lock_release,
6917};
6918
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006919static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6920{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006921 switch (error) {
6922 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006923 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006924 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006925 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006926 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006927 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006928 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006929 break;
6930 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006931 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006932 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08006933 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006934}
6935
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006936static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006937{
6938 struct nfs4_lockdata *data;
6939 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006940 struct rpc_message msg = {
6941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6942 .rpc_cred = state->owner->so_cred,
6943 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006944 struct rpc_task_setup task_setup_data = {
6945 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006946 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006947 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006948 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05006949 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006950 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006951 int ret;
6952
Harvey Harrison3110ff82008-05-02 13:42:44 -07006953 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006954 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006955 fl->fl_u.nfs4_fl.owner,
6956 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006957 if (data == NULL)
6958 return -ENOMEM;
6959 if (IS_SETLKW(cmd))
6960 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006961 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6962 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006963 msg.rpc_argp = &data->arg;
6964 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006965 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006966 if (recovery_type > NFS_LOCK_NEW) {
6967 if (recovery_type == NFS_LOCK_RECLAIM)
6968 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006969 } else
6970 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006971 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006972 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006973 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006974 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006975 if (ret == 0) {
6976 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006977 if (ret)
6978 nfs4_handle_setlk_error(data->server, data->lsp,
6979 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006980 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006981 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006982 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006983 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006984 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006985 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986}
6987
6988static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6989{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006990 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006991 struct nfs4_exception exception = {
6992 .inode = state->inode,
6993 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006994 int err;
6995
6996 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006997 /* Cache the lock if possible... */
6998 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6999 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007000 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007001 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007002 break;
7003 nfs4_handle_exception(server, err, &exception);
7004 } while (exception.retry);
7005 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006}
7007
7008static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7009{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007010 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007011 struct nfs4_exception exception = {
7012 .inode = state->inode,
7013 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007014 int err;
7015
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007016 err = nfs4_set_lock_state(state, request);
7017 if (err != 0)
7018 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007019 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007020 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7021 return 0;
7022 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007023 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007024 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7025 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007026 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007027 switch (err) {
7028 default:
7029 goto out;
7030 case -NFS4ERR_GRACE:
7031 case -NFS4ERR_DELAY:
7032 nfs4_handle_exception(server, err, &exception);
7033 err = 0;
7034 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007035 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007036out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007037 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038}
7039
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007040#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007041static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7042{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007043 struct nfs4_lock_state *lsp;
7044 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007045
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007046 status = nfs4_set_lock_state(state, request);
7047 if (status != 0)
7048 return status;
7049 lsp = request->fl_u.nfs4_fl.owner;
7050 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7051 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7052 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007053 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007054}
7055#endif
7056
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7058{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007059 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007060 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007061 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007062 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
Trond Myklebust01c3b862006-06-29 16:38:39 -04007064 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007065 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007066 if (status < 0)
7067 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007068 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007069 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007070 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007071 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007072 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007073 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007074 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007075 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007076 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007077 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007078 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007079 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007080 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007081 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007082out:
7083 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084 return status;
7085}
7086
7087static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7088{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007089 struct nfs4_exception exception = {
7090 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007091 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007092 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007093 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094 int err;
7095
7096 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007097 err = _nfs4_proc_setlk(state, cmd, request);
7098 if (err == -NFS4ERR_DENIED)
7099 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007101 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 } while (exception.retry);
7103 return err;
7104}
7105
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007106#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7107#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7108
7109static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007110nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7111 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007112{
7113 int status = -ERESTARTSYS;
7114 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7115
7116 while(!signalled()) {
7117 status = nfs4_proc_setlk(state, cmd, request);
7118 if ((status != -EAGAIN) || IS_SETLK(cmd))
7119 break;
7120 freezable_schedule_timeout_interruptible(timeout);
7121 timeout *= 2;
7122 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7123 status = -ERESTARTSYS;
7124 }
7125 return status;
7126}
7127
Jeff Laytona1d617d82016-09-17 18:17:39 -04007128#ifdef CONFIG_NFS_V4_1
7129struct nfs4_lock_waiter {
7130 struct task_struct *task;
7131 struct inode *inode;
7132 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007133};
7134
7135static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007136nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007137{
7138 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007139 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007140
Jeff Layton57174592018-03-18 08:37:03 -04007141 /* NULL key means to wake up everyone */
7142 if (key) {
7143 struct cb_notify_lock_args *cbnl = key;
7144 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7145 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007146
Jeff Layton57174592018-03-18 08:37:03 -04007147 /* Only wake if the callback was for the same owner. */
7148 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7149 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007150
Jeff Layton57174592018-03-18 08:37:03 -04007151 /* Make sure it's for the right inode */
7152 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7153 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007154 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007155
7156 /* override "private" so we can use default_wake_function */
7157 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007158 ret = woken_wake_function(wait, mode, flags, key);
7159 if (ret)
7160 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007161 wait->private = waiter;
7162 return ret;
7163}
7164
7165static int
7166nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7167{
7168 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007169 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7170 struct nfs_server *server = NFS_SERVER(state->inode);
7171 struct nfs_client *clp = server->nfs_client;
7172 wait_queue_head_t *q = &clp->cl_lock_waitq;
7173 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7174 .id = lsp->ls_seqid.owner_id,
7175 .s_dev = server->s_dev };
7176 struct nfs4_lock_waiter waiter = { .task = current,
7177 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007178 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007179 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007180
7181 /* Don't bother with waitqueue if we don't expect a callback */
7182 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7183 return nfs4_retry_setlk_simple(state, cmd, request);
7184
7185 init_wait(&wait);
7186 wait.private = &waiter;
7187 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007188
7189 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007190 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007191 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007192 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7193 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007194 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007195 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007196
7197 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007198 freezer_do_not_count();
7199 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7200 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007201 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007202 }
7203
Jeff Laytona1d617d82016-09-17 18:17:39 -04007204 return status;
7205}
7206#else /* !CONFIG_NFS_V4_1 */
7207static inline int
7208nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7209{
7210 return nfs4_retry_setlk_simple(state, cmd, request);
7211}
7212#endif
7213
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214static int
7215nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7216{
7217 struct nfs_open_context *ctx;
7218 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 int status;
7220
7221 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007222 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 state = ctx->state;
7224
Trond Myklebustd9531262009-07-21 19:22:38 -04007225 if (IS_GETLK(cmd)) {
7226 if (state != NULL)
7227 return nfs4_proc_getlk(state, F_GETLK, request);
7228 return 0;
7229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
7231 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7232 return -EINVAL;
7233
Trond Myklebustd9531262009-07-21 19:22:38 -04007234 if (request->fl_type == F_UNLCK) {
7235 if (state != NULL)
7236 return nfs4_proc_unlck(state, cmd, request);
7237 return 0;
7238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
Trond Myklebustd9531262009-07-21 19:22:38 -04007240 if (state == NULL)
7241 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007242
7243 if ((request->fl_flags & FL_POSIX) &&
7244 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7245 return -ENOLCK;
7246
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007247 /*
7248 * Don't rely on the VFS having checked the file open mode,
7249 * since it won't do this for flock() locks.
7250 */
7251 switch (request->fl_type) {
7252 case F_RDLCK:
7253 if (!(filp->f_mode & FMODE_READ))
7254 return -EBADF;
7255 break;
7256 case F_WRLCK:
7257 if (!(filp->f_mode & FMODE_WRITE))
7258 return -EBADF;
7259 }
7260
Jeff Layton1ea67db2016-09-17 18:17:37 -04007261 status = nfs4_set_lock_state(state, request);
7262 if (status != 0)
7263 return status;
7264
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007265 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266}
7267
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007268int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007269{
7270 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007271 int err;
7272
7273 err = nfs4_set_lock_state(state, fl);
7274 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007275 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007276 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007277 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007278}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007279
Trond Myklebustcf470c32012-03-07 13:49:12 -05007280struct nfs_release_lockowner_data {
7281 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007282 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007283 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007284 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007285 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007286};
7287
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007288static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7289{
7290 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007291 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007292 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7293 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007294 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007295 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007296}
7297
7298static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7299{
7300 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007301 struct nfs_server *server = data->server;
7302
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007303 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007304
7305 switch (task->tk_status) {
7306 case 0:
7307 renew_lease(server, data->timestamp);
7308 break;
7309 case -NFS4ERR_STALE_CLIENTID:
7310 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007311 nfs4_schedule_lease_recovery(server->nfs_client);
7312 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007313 case -NFS4ERR_LEASE_MOVED:
7314 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007315 if (nfs4_async_handle_error(task, server,
7316 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007317 rpc_restart_call_prepare(task);
7318 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007319}
7320
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007321static void nfs4_release_lockowner_release(void *calldata)
7322{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007323 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007324 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007325 kfree(calldata);
7326}
7327
Trond Myklebust17280172012-03-11 13:11:00 -04007328static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007329 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7330 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007331 .rpc_release = nfs4_release_lockowner_release,
7332};
7333
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007334static void
7335nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007336{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007337 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007338 struct rpc_message msg = {
7339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7340 };
7341
7342 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007343 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007344
Trond Myklebustcf470c32012-03-07 13:49:12 -05007345 data = kmalloc(sizeof(*data), GFP_NOFS);
7346 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007347 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007348 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007349 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007350 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7351 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7352 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007353
Trond Myklebustcf470c32012-03-07 13:49:12 -05007354 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007355 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007356 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007357 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007358}
7359
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007360#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7361
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007362static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007363 struct dentry *unused, struct inode *inode,
7364 const char *key, const void *buf,
7365 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007366{
Al Viro59301222016-05-27 10:19:30 -04007367 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007368}
7369
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007370static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007371 struct dentry *unused, struct inode *inode,
7372 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007373{
Al Virob2968212016-04-10 20:48:24 -04007374 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007375}
7376
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007377static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007378{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007379 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007380}
7381
David Quigleyc9bccef2013-05-22 12:50:45 -04007382#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007383
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007384static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007385 struct dentry *unused, struct inode *inode,
7386 const char *key, const void *buf,
7387 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007388{
7389 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007390 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007391
7392 return -EOPNOTSUPP;
7393}
7394
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007395static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007396 struct dentry *unused, struct inode *inode,
7397 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007398{
7399 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007400 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007401 return -EOPNOTSUPP;
7402}
7403
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007404static ssize_t
7405nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007406{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007407 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007408
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007409 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7410 len = security_inode_listsecurity(inode, list, list_len);
7411 if (list_len && len > list_len)
7412 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007413 }
7414 return len;
7415}
7416
7417static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7418 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007419 .get = nfs4_xattr_get_nfs4_label,
7420 .set = nfs4_xattr_set_nfs4_label,
7421};
David Quigleyc9bccef2013-05-22 12:50:45 -04007422
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007423#else
7424
7425static ssize_t
7426nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7427{
7428 return 0;
7429}
7430
7431#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007432
Andy Adamson533eb462011-06-13 18:25:56 -04007433/*
7434 * nfs_fhget will use either the mounted_on_fileid or the fileid
7435 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007436static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7437{
Andy Adamson533eb462011-06-13 18:25:56 -04007438 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7439 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7440 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007441 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007442 return;
7443
7444 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007445 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007446 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7447 fattr->nlink = 2;
7448}
7449
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007450static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7451 const struct qstr *name,
7452 struct nfs4_fs_locations *fs_locations,
7453 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007454{
7455 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007456 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007457 struct nfs4_fs_locations_arg args = {
7458 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007459 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007460 .page = page,
7461 .bitmask = bitmask,
7462 };
Benny Halevy22958462009-04-01 09:22:02 -04007463 struct nfs4_fs_locations_res res = {
7464 .fs_locations = fs_locations,
7465 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007466 struct rpc_message msg = {
7467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7468 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007469 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007470 };
7471 int status;
7472
Harvey Harrison3110ff82008-05-02 13:42:44 -07007473 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007474
Chuck Leverc05cefc2017-11-05 15:45:22 -05007475 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7476 bitmask[1] = nfs4_fattr_bitmap[1];
7477
Andy Adamson533eb462011-06-13 18:25:56 -04007478 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7479 * is not supported */
7480 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007481 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007482 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007483 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007484
Trond Myklebustc228fd32007-01-13 02:28:11 -05007485 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007486 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007487 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007488 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007489 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007490 return status;
7491}
7492
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007493int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7494 const struct qstr *name,
7495 struct nfs4_fs_locations *fs_locations,
7496 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007497{
Trond Myklebust0688e642019-04-07 13:59:09 -04007498 struct nfs4_exception exception = {
7499 .interruptible = true,
7500 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007501 int err;
7502 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007503 err = _nfs4_proc_fs_locations(client, dir, name,
7504 fs_locations, page);
7505 trace_nfs4_get_fs_locations(dir, name, err);
7506 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007507 &exception);
7508 } while (exception.retry);
7509 return err;
7510}
7511
Chuck Leverb03d7352013-10-17 14:12:50 -04007512/*
7513 * This operation also signals the server that this client is
7514 * performing migration recovery. The server can stop returning
7515 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7516 * appended to this compound to identify the client ID which is
7517 * performing recovery.
7518 */
7519static int _nfs40_proc_get_locations(struct inode *inode,
7520 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007521 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007522{
7523 struct nfs_server *server = NFS_SERVER(inode);
7524 struct rpc_clnt *clnt = server->client;
7525 u32 bitmask[2] = {
7526 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7527 };
7528 struct nfs4_fs_locations_arg args = {
7529 .clientid = server->nfs_client->cl_clientid,
7530 .fh = NFS_FH(inode),
7531 .page = page,
7532 .bitmask = bitmask,
7533 .migration = 1, /* skip LOOKUP */
7534 .renew = 1, /* append RENEW */
7535 };
7536 struct nfs4_fs_locations_res res = {
7537 .fs_locations = locations,
7538 .migration = 1,
7539 .renew = 1,
7540 };
7541 struct rpc_message msg = {
7542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7543 .rpc_argp = &args,
7544 .rpc_resp = &res,
7545 .rpc_cred = cred,
7546 };
7547 unsigned long now = jiffies;
7548 int status;
7549
7550 nfs_fattr_init(&locations->fattr);
7551 locations->server = server;
7552 locations->nlocations = 0;
7553
Anna Schumakerfba83f32018-05-04 16:22:50 -04007554 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007555 status = nfs4_call_sync_sequence(clnt, server, &msg,
7556 &args.seq_args, &res.seq_res);
7557 if (status)
7558 return status;
7559
7560 renew_lease(server, now);
7561 return 0;
7562}
7563
7564#ifdef CONFIG_NFS_V4_1
7565
7566/*
7567 * This operation also signals the server that this client is
7568 * performing migration recovery. The server can stop asserting
7569 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7570 * performing this operation is identified in the SEQUENCE
7571 * operation in this compound.
7572 *
7573 * When the client supports GETATTR(fs_locations_info), it can
7574 * be plumbed in here.
7575 */
7576static int _nfs41_proc_get_locations(struct inode *inode,
7577 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007578 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007579{
7580 struct nfs_server *server = NFS_SERVER(inode);
7581 struct rpc_clnt *clnt = server->client;
7582 u32 bitmask[2] = {
7583 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7584 };
7585 struct nfs4_fs_locations_arg args = {
7586 .fh = NFS_FH(inode),
7587 .page = page,
7588 .bitmask = bitmask,
7589 .migration = 1, /* skip LOOKUP */
7590 };
7591 struct nfs4_fs_locations_res res = {
7592 .fs_locations = locations,
7593 .migration = 1,
7594 };
7595 struct rpc_message msg = {
7596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7597 .rpc_argp = &args,
7598 .rpc_resp = &res,
7599 .rpc_cred = cred,
7600 };
7601 int status;
7602
7603 nfs_fattr_init(&locations->fattr);
7604 locations->server = server;
7605 locations->nlocations = 0;
7606
Anna Schumakerfba83f32018-05-04 16:22:50 -04007607 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007608 status = nfs4_call_sync_sequence(clnt, server, &msg,
7609 &args.seq_args, &res.seq_res);
7610 if (status == NFS4_OK &&
7611 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7612 status = -NFS4ERR_LEASE_MOVED;
7613 return status;
7614}
7615
7616#endif /* CONFIG_NFS_V4_1 */
7617
7618/**
7619 * nfs4_proc_get_locations - discover locations for a migrated FSID
7620 * @inode: inode on FSID that is migrating
7621 * @locations: result of query
7622 * @page: buffer
7623 * @cred: credential to use for this operation
7624 *
7625 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7626 * operation failed, or a negative errno if a local error occurred.
7627 *
7628 * On success, "locations" is filled in, but if the server has
7629 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7630 * asserted.
7631 *
7632 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7633 * from this client that require migration recovery.
7634 */
7635int nfs4_proc_get_locations(struct inode *inode,
7636 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007637 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007638{
7639 struct nfs_server *server = NFS_SERVER(inode);
7640 struct nfs_client *clp = server->nfs_client;
7641 const struct nfs4_mig_recovery_ops *ops =
7642 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007643 struct nfs4_exception exception = {
7644 .interruptible = true,
7645 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007646 int status;
7647
7648 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7649 (unsigned long long)server->fsid.major,
7650 (unsigned long long)server->fsid.minor,
7651 clp->cl_hostname);
7652 nfs_display_fhandle(NFS_FH(inode), __func__);
7653
7654 do {
7655 status = ops->get_locations(inode, locations, page, cred);
7656 if (status != -NFS4ERR_DELAY)
7657 break;
7658 nfs4_handle_exception(server, status, &exception);
7659 } while (exception.retry);
7660 return status;
7661}
7662
Chuck Lever44c99932013-10-17 14:13:30 -04007663/*
7664 * This operation also signals the server that this client is
7665 * performing "lease moved" recovery. The server can stop
7666 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7667 * is appended to this compound to identify the client ID which is
7668 * performing recovery.
7669 */
NeilBrowna52458b2018-12-03 11:30:31 +11007670static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007671{
7672 struct nfs_server *server = NFS_SERVER(inode);
7673 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7674 struct rpc_clnt *clnt = server->client;
7675 struct nfs4_fsid_present_arg args = {
7676 .fh = NFS_FH(inode),
7677 .clientid = clp->cl_clientid,
7678 .renew = 1, /* append RENEW */
7679 };
7680 struct nfs4_fsid_present_res res = {
7681 .renew = 1,
7682 };
7683 struct rpc_message msg = {
7684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7685 .rpc_argp = &args,
7686 .rpc_resp = &res,
7687 .rpc_cred = cred,
7688 };
7689 unsigned long now = jiffies;
7690 int status;
7691
7692 res.fh = nfs_alloc_fhandle();
7693 if (res.fh == NULL)
7694 return -ENOMEM;
7695
Anna Schumakerfba83f32018-05-04 16:22:50 -04007696 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007697 status = nfs4_call_sync_sequence(clnt, server, &msg,
7698 &args.seq_args, &res.seq_res);
7699 nfs_free_fhandle(res.fh);
7700 if (status)
7701 return status;
7702
7703 do_renew_lease(clp, now);
7704 return 0;
7705}
7706
7707#ifdef CONFIG_NFS_V4_1
7708
7709/*
7710 * This operation also signals the server that this client is
7711 * performing "lease moved" recovery. The server can stop asserting
7712 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7713 * this operation is identified in the SEQUENCE operation in this
7714 * compound.
7715 */
NeilBrowna52458b2018-12-03 11:30:31 +11007716static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007717{
7718 struct nfs_server *server = NFS_SERVER(inode);
7719 struct rpc_clnt *clnt = server->client;
7720 struct nfs4_fsid_present_arg args = {
7721 .fh = NFS_FH(inode),
7722 };
7723 struct nfs4_fsid_present_res res = {
7724 };
7725 struct rpc_message msg = {
7726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7727 .rpc_argp = &args,
7728 .rpc_resp = &res,
7729 .rpc_cred = cred,
7730 };
7731 int status;
7732
7733 res.fh = nfs_alloc_fhandle();
7734 if (res.fh == NULL)
7735 return -ENOMEM;
7736
Anna Schumakerfba83f32018-05-04 16:22:50 -04007737 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007738 status = nfs4_call_sync_sequence(clnt, server, &msg,
7739 &args.seq_args, &res.seq_res);
7740 nfs_free_fhandle(res.fh);
7741 if (status == NFS4_OK &&
7742 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7743 status = -NFS4ERR_LEASE_MOVED;
7744 return status;
7745}
7746
7747#endif /* CONFIG_NFS_V4_1 */
7748
7749/**
7750 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7751 * @inode: inode on FSID to check
7752 * @cred: credential to use for this operation
7753 *
7754 * Server indicates whether the FSID is present, moved, or not
7755 * recognized. This operation is necessary to clear a LEASE_MOVED
7756 * condition for this client ID.
7757 *
7758 * Returns NFS4_OK if the FSID is present on this server,
7759 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7760 * NFS4ERR code if some error occurred on the server, or a
7761 * negative errno if a local failure occurred.
7762 */
NeilBrowna52458b2018-12-03 11:30:31 +11007763int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007764{
7765 struct nfs_server *server = NFS_SERVER(inode);
7766 struct nfs_client *clp = server->nfs_client;
7767 const struct nfs4_mig_recovery_ops *ops =
7768 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007769 struct nfs4_exception exception = {
7770 .interruptible = true,
7771 };
Chuck Lever44c99932013-10-17 14:13:30 -04007772 int status;
7773
7774 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7775 (unsigned long long)server->fsid.major,
7776 (unsigned long long)server->fsid.minor,
7777 clp->cl_hostname);
7778 nfs_display_fhandle(NFS_FH(inode), __func__);
7779
7780 do {
7781 status = ops->fsid_present(inode, cred);
7782 if (status != -NFS4ERR_DELAY)
7783 break;
7784 nfs4_handle_exception(server, status, &exception);
7785 } while (exception.retry);
7786 return status;
7787}
7788
Trond Myklebust302fad72019-02-18 13:32:38 -05007789/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007790 * If 'use_integrity' is true and the state managment nfs_client
7791 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7792 * and the machine credential as per RFC3530bis and RFC5661 Security
7793 * Considerations sections. Otherwise, just use the user cred with the
7794 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007795 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007796static 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 +00007797{
7798 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04007799 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7800 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007801 struct nfs4_secinfo_arg args = {
7802 .dir_fh = NFS_FH(dir),
7803 .name = name,
7804 };
7805 struct nfs4_secinfo_res res = {
7806 .flavors = flavors,
7807 };
7808 struct rpc_message msg = {
7809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7810 .rpc_argp = &args,
7811 .rpc_resp = &res,
7812 };
Anna Schumaker50493362019-08-14 15:30:16 -04007813 struct nfs4_call_sync_data data = {
7814 .seq_server = NFS_SERVER(dir),
7815 .seq_args = &args.seq_args,
7816 .seq_res = &res.seq_res,
7817 };
7818 struct rpc_task_setup task_setup = {
7819 .rpc_client = clnt,
7820 .rpc_message = &msg,
7821 .callback_ops = clp->cl_mvops->call_sync_ops,
7822 .callback_data = &data,
7823 .flags = RPC_TASK_NO_ROUND_ROBIN,
7824 };
NeilBrowna52458b2018-12-03 11:30:31 +11007825 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007826
7827 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04007828 clnt = clp->cl_rpcclient;
7829 task_setup.rpc_client = clnt;
7830
7831 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007832 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007833 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007834
7835 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007836
Anna Schumaker50493362019-08-14 15:30:16 -04007837 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7838 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7839 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007840
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007841 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007842
NeilBrowna52458b2018-12-03 11:30:31 +11007843 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007844 return status;
7845}
7846
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007847int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7848 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007849{
Trond Myklebust0688e642019-04-07 13:59:09 -04007850 struct nfs4_exception exception = {
7851 .interruptible = true,
7852 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007853 int err;
7854 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007855 err = -NFS4ERR_WRONGSEC;
7856
7857 /* try to use integrity protection with machine cred */
7858 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7859 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7860
7861 /*
7862 * if unable to use integrity protection, or SECINFO with
7863 * integrity protection returns NFS4ERR_WRONGSEC (which is
7864 * disallowed by spec, but exists in deployed servers) use
7865 * the current filesystem's rpc_client and the user cred.
7866 */
7867 if (err == -NFS4ERR_WRONGSEC)
7868 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7869
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007870 trace_nfs4_secinfo(dir, name, err);
7871 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007872 &exception);
7873 } while (exception.retry);
7874 return err;
7875}
7876
Andy Adamson557134a2009-04-01 09:21:53 -04007877#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007878/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007879 * Check the exchange flags returned by the server for invalid flags, having
7880 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7881 * DS flags set.
7882 */
7883static int nfs4_check_cl_exchange_flags(u32 flags)
7884{
7885 if (flags & ~EXCHGID4_FLAG_MASK_R)
7886 goto out_inval;
7887 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7888 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7889 goto out_inval;
7890 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7891 goto out_inval;
7892 return NFS_OK;
7893out_inval:
7894 return -NFS4ERR_INVAL;
7895}
7896
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007897static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007898nfs41_same_server_scope(struct nfs41_server_scope *a,
7899 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007900{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007901 if (a->server_scope_sz != b->server_scope_sz)
7902 return false;
7903 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007904}
7905
Andy Adamson02a95de2016-02-05 16:08:37 -05007906static void
7907nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7908{
Trond Myklebust5c441542019-11-13 08:34:00 +01007909 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04007910 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01007911 struct nfs_client *clp = args->client;
7912
7913 switch (task->tk_status) {
7914 case -NFS4ERR_BADSESSION:
7915 case -NFS4ERR_DEADSESSION:
7916 nfs4_schedule_session_recovery(clp->cl_session,
7917 task->tk_status);
7918 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04007919 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
7920 res->dir != NFS4_CDFS4_BOTH) {
7921 rpc_task_close_connection(task);
7922 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
7923 rpc_restart_call(task);
7924 }
Andy Adamson02a95de2016-02-05 16:08:37 -05007925}
7926
7927static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04007928 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05007929};
7930
Andy Adamson357f54d2010-12-14 10:11:57 -05007931/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007932 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007933 *
7934 * The 4.1 client currently uses the same TCP connection for the
7935 * fore and backchannel.
7936 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007937static
7938int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7939 struct rpc_xprt *xprt,
7940 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007941 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007942{
7943 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007944 struct nfs41_bind_conn_to_session_args args = {
7945 .client = clp,
7946 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04007947 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08007948 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007949 struct nfs41_bind_conn_to_session_res res;
7950 struct rpc_message msg = {
7951 .rpc_proc =
7952 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007953 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007954 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007955 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007956 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007957 struct rpc_task_setup task_setup_data = {
7958 .rpc_client = clnt,
7959 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007960 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007961 .rpc_message = &msg,
7962 .flags = RPC_TASK_TIMEOUT,
7963 };
7964 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007965
Trond Myklebust71a097c2015-02-18 09:27:18 -08007966 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7967 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7968 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007969
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007970 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7971 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7972 args.dir = NFS4_CDFC4_FORE;
7973
7974 task = rpc_run_task(&task_setup_data);
7975 if (!IS_ERR(task)) {
7976 status = task->tk_status;
7977 rpc_put_task(task);
7978 } else
7979 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007980 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007981 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007982 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007983 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7984 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007985 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007986 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007987 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007988 dprintk("NFS: %s: Unexpected direction from server\n",
7989 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007990 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007991 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007992 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007993 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7994 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007995 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007996 }
7997 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007998
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007999 return status;
8000}
8001
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008002struct rpc_bind_conn_calldata {
8003 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008004 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008005};
8006
8007static int
8008nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8009 struct rpc_xprt *xprt,
8010 void *calldata)
8011{
8012 struct rpc_bind_conn_calldata *p = calldata;
8013
8014 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8015}
8016
NeilBrowna52458b2018-12-03 11:30:31 +11008017int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008018{
8019 struct rpc_bind_conn_calldata data = {
8020 .clp = clp,
8021 .cred = cred,
8022 };
8023 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8024 nfs4_proc_bind_conn_to_session_callback, &data);
8025}
8026
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008027/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008028 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8029 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008030 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008031static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8032 .how = SP4_MACH_CRED,
8033 .enforce.u.words = {
8034 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8035 1 << (OP_EXCHANGE_ID - 32) |
8036 1 << (OP_CREATE_SESSION - 32) |
8037 1 << (OP_DESTROY_SESSION - 32) |
8038 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008039 },
8040 .allow.u.words = {
8041 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008042 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008043 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008044 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008045 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008046 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008047 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008048 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008049 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008050 1 << (OP_FREE_STATEID - 32) |
8051 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008052 }
8053};
8054
8055/*
8056 * Select the state protection mode for client `clp' given the server results
8057 * from exchange_id in `sp'.
8058 *
8059 * Returns 0 on success, negative errno otherwise.
8060 */
8061static int nfs4_sp4_select_mode(struct nfs_client *clp,
8062 struct nfs41_state_protection *sp)
8063{
8064 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8065 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8066 1 << (OP_EXCHANGE_ID - 32) |
8067 1 << (OP_CREATE_SESSION - 32) |
8068 1 << (OP_DESTROY_SESSION - 32) |
8069 1 << (OP_DESTROY_CLIENTID - 32)
8070 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008071 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008072 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008073 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008074
8075 if (sp->how == SP4_MACH_CRED) {
8076 /* Print state protect result */
8077 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8078 for (i = 0; i <= LAST_NFS4_OP; i++) {
8079 if (test_bit(i, sp->enforce.u.longs))
8080 dfprintk(MOUNT, " enforce op %d\n", i);
8081 if (test_bit(i, sp->allow.u.longs))
8082 dfprintk(MOUNT, " allow op %d\n", i);
8083 }
8084
8085 /* make sure nothing is on enforce list that isn't supported */
8086 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8087 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8088 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008089 ret = -EINVAL;
8090 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008091 }
8092 }
8093
8094 /*
8095 * Minimal mode - state operations are allowed to use machine
8096 * credential. Note this already happens by default, so the
8097 * client doesn't have to do anything more than the negotiation.
8098 *
8099 * NOTE: we don't care if EXCHANGE_ID is in the list -
8100 * we're already using the machine cred for exchange_id
8101 * and will never use a different cred.
8102 */
8103 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8104 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8105 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8106 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8107 dfprintk(MOUNT, "sp4_mach_cred:\n");
8108 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008109 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008110 } else {
8111 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008112 ret = -EINVAL;
8113 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008114 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008115
8116 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008117 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8118 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008119 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8120 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008121 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008122 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008123
Andrew Elble99ade3c2015-12-02 09:39:51 -05008124 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8125 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008126 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008127 }
8128
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008129 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8130 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8131 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008132 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008133 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008134
8135 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8136 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8137 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008138 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008139 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008140
8141 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8142 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008143 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008144 }
8145
8146 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8147 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008148 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008149 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008150 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008151out:
8152 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008153 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008154}
8155
Andy Adamson8d89bd72016-09-09 09:22:18 -04008156struct nfs41_exchange_id_data {
8157 struct nfs41_exchange_id_res res;
8158 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008159};
8160
Andy Adamson8d89bd72016-09-09 09:22:18 -04008161static void nfs4_exchange_id_release(void *data)
8162{
8163 struct nfs41_exchange_id_data *cdata =
8164 (struct nfs41_exchange_id_data *)data;
8165
Olga Kornievskaia63513232017-03-13 10:36:19 -04008166 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008167 kfree(cdata->res.impl_id);
8168 kfree(cdata->res.server_scope);
8169 kfree(cdata->res.server_owner);
8170 kfree(cdata);
8171}
8172
8173static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008174 .rpc_release = nfs4_exchange_id_release,
8175};
8176
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008177/*
8178 * _nfs4_proc_exchange_id()
8179 *
8180 * Wrapper for EXCHANGE_ID operation.
8181 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008182static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008183nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a72016-09-09 09:22:28 -04008184 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008185{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008186 struct rpc_message msg = {
8187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008188 .rpc_cred = cred,
8189 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008190 struct rpc_task_setup task_setup_data = {
8191 .rpc_client = clp->cl_rpcclient,
8192 .callback_ops = &nfs4_exchange_id_call_ops,
8193 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008194 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008195 };
8196 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008197 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008198
Elena Reshetova212bf412017-10-20 12:53:38 +03008199 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008200 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008201
Trond Myklebust9c760d12017-07-31 18:38:50 -04008202 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008203 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008204 if (!calldata)
8205 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008206
Trond Myklebustfd405592017-08-01 16:02:47 -04008207 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008208
8209 status = nfs4_init_uniform_client_string(clp);
8210 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008211 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008212
Andy Adamson8d89bd72016-09-09 09:22:18 -04008213 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8214 GFP_NOFS);
8215 status = -ENOMEM;
8216 if (unlikely(calldata->res.server_owner == NULL))
8217 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008218
Andy Adamson8d89bd72016-09-09 09:22:18 -04008219 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008220 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008221 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008222 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008223
Andy Adamson8d89bd72016-09-09 09:22:18 -04008224 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8225 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008226 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008227
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008228 switch (sp4_how) {
8229 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008230 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008231 break;
8232
8233 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008234 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008235 break;
8236
8237 default:
8238 /* unsupported! */
8239 WARN_ON_ONCE(1);
8240 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008241 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008242 }
Andy Adamsonad0849a72016-09-09 09:22:28 -04008243 if (xprt) {
Andy Adamsonad0849a72016-09-09 09:22:28 -04008244 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008245 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008246 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8247 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a72016-09-09 09:22:28 -04008248 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008249 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008250 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8251 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008252#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008253 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008254#endif
8255 msg.rpc_argp = &calldata->args;
8256 msg.rpc_resp = &calldata->res;
8257 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008258
Trond Myklebust9c760d12017-07-31 18:38:50 -04008259 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008260
8261out_impl_id:
8262 kfree(calldata->res.impl_id);
8263out_server_scope:
8264 kfree(calldata->res.server_scope);
8265out_server_owner:
8266 kfree(calldata->res.server_owner);
8267out_calldata:
8268 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008269out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008270 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008271 return ERR_PTR(status);
8272}
8273
8274/*
8275 * _nfs4_proc_exchange_id()
8276 *
8277 * Wrapper for EXCHANGE_ID operation.
8278 */
NeilBrowna52458b2018-12-03 11:30:31 +11008279static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008280 u32 sp4_how)
8281{
8282 struct rpc_task *task;
8283 struct nfs41_exchange_id_args *argp;
8284 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008285 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008286 int status;
8287
8288 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8289 if (IS_ERR(task))
8290 return PTR_ERR(task);
8291
8292 argp = task->tk_msg.rpc_argp;
8293 resp = task->tk_msg.rpc_resp;
8294 status = task->tk_status;
8295 if (status != 0)
8296 goto out;
8297
8298 status = nfs4_check_cl_exchange_flags(resp->flags);
8299 if (status != 0)
8300 goto out;
8301
8302 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8303 if (status != 0)
8304 goto out;
8305
Robert Milkowski7dc29932020-01-30 09:43:25 +00008306 do_renew_lease(clp, now);
8307
Trond Myklebust9c760d12017-07-31 18:38:50 -04008308 clp->cl_clientid = resp->clientid;
8309 clp->cl_exchange_flags = resp->flags;
8310 clp->cl_seqid = resp->seqid;
8311 /* Client ID is not confirmed */
8312 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8313 clear_bit(NFS4_SESSION_ESTABLISHED,
8314 &clp->cl_session->session_state);
8315
8316 if (clp->cl_serverscope != NULL &&
8317 !nfs41_same_server_scope(clp->cl_serverscope,
8318 resp->server_scope)) {
8319 dprintk("%s: server_scope mismatch detected\n",
8320 __func__);
8321 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8322 }
8323
8324 swap(clp->cl_serverowner, resp->server_owner);
8325 swap(clp->cl_serverscope, resp->server_scope);
8326 swap(clp->cl_implid, resp->impl_id);
8327
8328 /* Save the EXCHANGE_ID verifier session trunk tests */
8329 memcpy(clp->cl_confirm.data, argp->verifier.data,
8330 sizeof(clp->cl_confirm.data));
8331out:
8332 trace_nfs4_exchange_id(clp, status);
8333 rpc_put_task(task);
8334 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008335}
8336
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008337/*
8338 * nfs4_proc_exchange_id()
8339 *
8340 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8341 *
8342 * Since the clientid has expired, all compounds using sessions
8343 * associated with the stale clientid will be returning
8344 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8345 * be in some phase of session reset.
8346 *
8347 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8348 */
NeilBrowna52458b2018-12-03 11:30:31 +11008349int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008350{
8351 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8352 int status;
8353
8354 /* try SP4_MACH_CRED if krb5i/p */
8355 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8356 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008357 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008358 if (!status)
8359 return 0;
8360 }
8361
8362 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008363 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008364}
8365
Andy Adamson04fa2c62016-09-09 09:22:29 -04008366/**
8367 * nfs4_test_session_trunk
8368 *
8369 * This is an add_xprt_test() test function called from
8370 * rpc_clnt_setup_test_and_add_xprt.
8371 *
8372 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8373 * and is dereferrenced in nfs4_exchange_id_release
8374 *
8375 * Upon success, add the new transport to the rpc_clnt
8376 *
8377 * @clnt: struct rpc_clnt to get new transport
8378 * @xprt: the rpc_xprt to test
8379 * @data: call data for _nfs4_proc_exchange_id.
8380 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308381void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008382 void *data)
8383{
8384 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008385 struct rpc_task *task;
8386 int status;
8387
Andy Adamson04fa2c62016-09-09 09:22:29 -04008388 u32 sp4_how;
8389
8390 dprintk("--> %s try %s\n", __func__,
8391 xprt->address_strings[RPC_DISPLAY_ADDR]);
8392
8393 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8394
8395 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008396 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8397 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308398 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008399
8400 status = task->tk_status;
8401 if (status == 0)
8402 status = nfs4_detect_session_trunking(adata->clp,
8403 task->tk_msg.rpc_resp, xprt);
8404
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308405 if (status == 0)
8406 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8407
Trond Myklebust9c760d12017-07-31 18:38:50 -04008408 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008409}
8410EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8411
Trond Myklebust66245532012-05-25 17:18:09 -04008412static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008413 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008414{
8415 struct rpc_message msg = {
8416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8417 .rpc_argp = clp,
8418 .rpc_cred = cred,
8419 };
8420 int status;
8421
NeilBrown5a0c2572019-05-30 10:41:28 +10008422 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8423 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008424 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008425 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008426 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008427 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8428 return status;
8429}
8430
8431static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008432 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008433{
8434 unsigned int loop;
8435 int ret;
8436
8437 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8438 ret = _nfs4_proc_destroy_clientid(clp, cred);
8439 switch (ret) {
8440 case -NFS4ERR_DELAY:
8441 case -NFS4ERR_CLIENTID_BUSY:
8442 ssleep(1);
8443 break;
8444 default:
8445 return ret;
8446 }
8447 }
8448 return 0;
8449}
8450
8451int nfs4_destroy_clientid(struct nfs_client *clp)
8452{
NeilBrowna52458b2018-12-03 11:30:31 +11008453 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008454 int ret = 0;
8455
8456 if (clp->cl_mvops->minor_version < 1)
8457 goto out;
8458 if (clp->cl_exchange_flags == 0)
8459 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008460 if (clp->cl_preserve_clid)
8461 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008462 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008463 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008464 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008465 switch (ret) {
8466 case 0:
8467 case -NFS4ERR_STALE_CLIENTID:
8468 clp->cl_exchange_flags = 0;
8469 }
8470out:
8471 return ret;
8472}
8473
Donald Buczek0efb01b2019-07-07 21:26:08 +02008474#endif /* CONFIG_NFS_V4_1 */
8475
Andy Adamson2050f0c2009-04-01 09:22:30 -04008476struct nfs4_get_lease_time_data {
8477 struct nfs4_get_lease_time_args *args;
8478 struct nfs4_get_lease_time_res *res;
8479 struct nfs_client *clp;
8480};
8481
8482static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8483 void *calldata)
8484{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008485 struct nfs4_get_lease_time_data *data =
8486 (struct nfs4_get_lease_time_data *)calldata;
8487
8488 dprintk("--> %s\n", __func__);
8489 /* just setup sequence, do not trigger session recovery
8490 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008491 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008492 &data->args->la_seq_args,
8493 &data->res->lr_seq_res,
8494 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008495 dprintk("<-- %s\n", __func__);
8496}
8497
8498/*
8499 * Called from nfs4_state_manager thread for session setup, so don't recover
8500 * from sequence operation or clientid errors.
8501 */
8502static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8503{
8504 struct nfs4_get_lease_time_data *data =
8505 (struct nfs4_get_lease_time_data *)calldata;
8506
8507 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008508 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008509 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008510 switch (task->tk_status) {
8511 case -NFS4ERR_DELAY:
8512 case -NFS4ERR_GRACE:
8513 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8514 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8515 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008516 /* fall through */
8517 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008518 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008519 return;
8520 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008521 dprintk("<-- %s\n", __func__);
8522}
8523
Trond Myklebust17280172012-03-11 13:11:00 -04008524static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008525 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8526 .rpc_call_done = nfs4_get_lease_time_done,
8527};
8528
8529int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8530{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008531 struct nfs4_get_lease_time_args args;
8532 struct nfs4_get_lease_time_res res = {
8533 .lr_fsinfo = fsinfo,
8534 };
8535 struct nfs4_get_lease_time_data data = {
8536 .args = &args,
8537 .res = &res,
8538 .clp = clp,
8539 };
8540 struct rpc_message msg = {
8541 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8542 .rpc_argp = &args,
8543 .rpc_resp = &res,
8544 };
8545 struct rpc_task_setup task_setup = {
8546 .rpc_client = clp->cl_rpcclient,
8547 .rpc_message = &msg,
8548 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008549 .callback_data = &data,
8550 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008551 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008552
Anna Schumakerfba83f32018-05-04 16:22:50 -04008553 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008554 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008555}
8556
Donald Buczek0efb01b2019-07-07 21:26:08 +02008557#ifdef CONFIG_NFS_V4_1
8558
Andy Adamsonfc931582009-04-01 09:22:31 -04008559/*
8560 * Initialize the values to be used by the client in CREATE_SESSION
8561 * If nfs4_init_session set the fore channel request and response sizes,
8562 * use them.
8563 *
8564 * Set the back channel max_resp_sz_cached to zero to force the client to
8565 * always set csa_cachethis to FALSE because the current implementation
8566 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8567 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008568static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8569 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008570{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008571 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008572 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008573 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008574
Andy Adamson18aad3d2013-06-26 12:21:49 -04008575 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8576 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8577
Andy Adamsonfc931582009-04-01 09:22:31 -04008578 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008579 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8580 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008581 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008582 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008583
8584 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008585 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008586 __func__,
8587 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008588 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008589
8590 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008591 args->bc_attrs.max_rqst_sz = max_bc_payload;
8592 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008593 args->bc_attrs.max_resp_sz_cached = 0;
8594 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008595 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008596 if (args->bc_attrs.max_reqs > max_bc_slots)
8597 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008598
8599 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8600 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8601 __func__,
8602 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8603 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8604 args->bc_attrs.max_reqs);
8605}
8606
Trond Myklebust79969dd12015-02-18 11:30:18 -08008607static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8608 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008609{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008610 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008611 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008612
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008613 if (rcvd->max_resp_sz > sent->max_resp_sz)
8614 return -EINVAL;
8615 /*
8616 * Our requested max_ops is the minimum we need; we're not
8617 * prepared to break up compounds into smaller pieces than that.
8618 * So, no point even trying to continue if the server won't
8619 * cooperate:
8620 */
8621 if (rcvd->max_ops < sent->max_ops)
8622 return -EINVAL;
8623 if (rcvd->max_reqs == 0)
8624 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008625 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8626 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008627 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008628}
8629
Trond Myklebust79969dd12015-02-18 11:30:18 -08008630static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8631 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008632{
8633 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008634 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008635
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008636 if (!(res->flags & SESSION4_BACK_CHAN))
8637 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008638 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8639 return -EINVAL;
8640 if (rcvd->max_resp_sz < sent->max_resp_sz)
8641 return -EINVAL;
8642 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8643 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008644 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008645 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008646 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008647 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008648out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008649 return 0;
8650}
Andy Adamson8d353012009-04-01 09:22:32 -04008651
Andy Adamson8d353012009-04-01 09:22:32 -04008652static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008653 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008654{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008655 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008656
Trond Myklebust79969dd12015-02-18 11:30:18 -08008657 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008658 if (ret)
8659 return ret;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008660 return nfs4_verify_back_channel_attrs(args, res);
8661}
8662
8663static void nfs4_update_session(struct nfs4_session *session,
8664 struct nfs41_create_session_res *res)
8665{
8666 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008667 /* Mark client id and session as being confirmed */
8668 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8669 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd12015-02-18 11:30:18 -08008670 session->flags = res->flags;
8671 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008672 if (res->flags & SESSION4_BACK_CHAN)
8673 memcpy(&session->bc_attrs, &res->bc_attrs,
8674 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008675}
8676
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008677static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008678 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008679{
8680 struct nfs4_session *session = clp->cl_session;
8681 struct nfs41_create_session_args args = {
8682 .client = clp,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008683 .clientid = clp->cl_clientid,
8684 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008685 .cb_program = NFS4_CALLBACK,
8686 };
Trond Myklebust79969dd12015-02-18 11:30:18 -08008687 struct nfs41_create_session_res res;
8688
Andy Adamsonfc931582009-04-01 09:22:31 -04008689 struct rpc_message msg = {
8690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8691 .rpc_argp = &args,
8692 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008693 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008694 };
8695 int status;
8696
Chuck Lever6b26cc82016-05-02 14:40:40 -04008697 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008698 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008699
NeilBrown5a0c2572019-05-30 10:41:28 +10008700 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8701 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008702 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008703
Trond Myklebustb519d402016-09-11 14:50:01 -04008704 switch (status) {
8705 case -NFS4ERR_STALE_CLIENTID:
8706 case -NFS4ERR_DELAY:
8707 case -ETIMEDOUT:
8708 case -EACCES:
8709 case -EAGAIN:
8710 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08008711 }
Trond Myklebustb519d402016-09-11 14:50:01 -04008712
8713 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008714 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008715 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008716 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008717 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008718 if (status)
8719 goto out;
8720 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008721 }
Trond Myklebust79969dd12015-02-18 11:30:18 -08008722out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008723 return status;
8724}
8725
8726/*
8727 * Issues a CREATE_SESSION operation to the server.
8728 * It is the responsibility of the caller to verify the session is
8729 * expired before calling this routine.
8730 */
NeilBrowna52458b2018-12-03 11:30:31 +11008731int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008732{
8733 int status;
8734 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008735 struct nfs4_session *session = clp->cl_session;
8736
8737 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8738
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008739 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008740 if (status)
8741 goto out;
8742
Andy Adamsonaacd5532011-11-09 13:58:21 -05008743 /* Init or reset the session slot tables */
8744 status = nfs4_setup_session_slot_tables(session);
8745 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008746 if (status)
8747 goto out;
8748
8749 ptr = (unsigned *)&session->sess_id.data[0];
8750 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8751 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008752out:
8753 dprintk("<-- %s\n", __func__);
8754 return status;
8755}
8756
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008757/*
8758 * Issue the over-the-wire RPC DESTROY_SESSION.
8759 * The caller must serialize access to this routine.
8760 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008761int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008762 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008763{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008764 struct rpc_message msg = {
8765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8766 .rpc_argp = session,
8767 .rpc_cred = cred,
8768 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008769 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008770
8771 dprintk("--> nfs4_proc_destroy_session\n");
8772
8773 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008774 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8775 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008776
NeilBrown5a0c2572019-05-30 10:41:28 +10008777 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8778 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008779 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008780
8781 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008782 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008783 "Session has been destroyed regardless...\n", status);
8784
8785 dprintk("<-- nfs4_proc_destroy_session\n");
8786 return status;
8787}
8788
Trond Myklebust7b38c362012-05-23 13:23:31 -04008789/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008790 * Renew the cl_session lease.
8791 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008792struct nfs4_sequence_data {
8793 struct nfs_client *clp;
8794 struct nfs4_sequence_args args;
8795 struct nfs4_sequence_res res;
8796};
8797
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008798static void nfs41_sequence_release(void *data)
8799{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008800 struct nfs4_sequence_data *calldata = data;
8801 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008802
Elena Reshetova212bf412017-10-20 12:53:38 +03008803 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008804 nfs4_schedule_state_renewal(clp);
8805 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008806 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008807}
8808
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008809static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8810{
8811 switch(task->tk_status) {
8812 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008813 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8814 return -EAGAIN;
8815 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008816 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008817 }
8818 return 0;
8819}
8820
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008821static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008822{
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
Trond Myklebust14516c32010-07-31 14:29:06 -04008826 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8827 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008828
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008829 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008830 if (task->tk_status < 0) {
8831 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008832 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008833 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008834
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008835 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8836 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008837 return;
8838 }
8839 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008840 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008841out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008842 dprintk("<-- %s\n", __func__);
8843}
8844
8845static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8846{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008847 struct nfs4_sequence_data *calldata = data;
8848 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008849 struct nfs4_sequence_args *args;
8850 struct nfs4_sequence_res *res;
8851
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008852 args = task->tk_msg.rpc_argp;
8853 res = task->tk_msg.rpc_resp;
8854
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008855 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008856}
8857
8858static const struct rpc_call_ops nfs41_sequence_ops = {
8859 .rpc_call_done = nfs41_sequence_call_done,
8860 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008861 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008862};
8863
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008864static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008865 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008866 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008867 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008868{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008869 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008870 struct rpc_message msg = {
8871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8872 .rpc_cred = cred,
8873 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008874 struct rpc_task_setup task_setup_data = {
8875 .rpc_client = clp->cl_rpcclient,
8876 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008877 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008878 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008879 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008880 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008881
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008882 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008883 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008884 goto out_err;
8885
8886 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008887 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008888 if (calldata == NULL)
8889 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008890 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008891 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008892 msg.rpc_argp = &calldata->args;
8893 msg.rpc_resp = &calldata->res;
8894 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008895 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008896
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008897 ret = rpc_run_task(&task_setup_data);
8898 if (IS_ERR(ret))
8899 goto out_err;
8900 return ret;
8901out_put_clp:
8902 nfs_put_client(clp);
8903out_err:
8904 nfs41_release_slot(slot);
8905 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008906}
8907
NeilBrowna52458b2018-12-03 11:30:31 +11008908static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008909{
8910 struct rpc_task *task;
8911 int ret = 0;
8912
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008913 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008914 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008915 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008916 if (IS_ERR(task))
8917 ret = PTR_ERR(task);
8918 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008919 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008920 dprintk("<-- %s status=%d\n", __func__, ret);
8921 return ret;
8922}
8923
NeilBrowna52458b2018-12-03 11:30:31 +11008924static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008925{
8926 struct rpc_task *task;
8927 int ret;
8928
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008929 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008930 if (IS_ERR(task)) {
8931 ret = PTR_ERR(task);
8932 goto out;
8933 }
8934 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008935 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008936 ret = task->tk_status;
8937 rpc_put_task(task);
8938out:
8939 dprintk("<-- %s status=%d\n", __func__, ret);
8940 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008941}
8942
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008943struct nfs4_reclaim_complete_data {
8944 struct nfs_client *clp;
8945 struct nfs41_reclaim_complete_args arg;
8946 struct nfs41_reclaim_complete_res res;
8947};
8948
8949static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8950{
8951 struct nfs4_reclaim_complete_data *calldata = data;
8952
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008953 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008954 &calldata->arg.seq_args,
8955 &calldata->res.seq_res,
8956 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008957}
8958
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008959static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8960{
8961 switch(task->tk_status) {
8962 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008963 wake_up_all(&clp->cl_lock_waitq);
8964 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008965 case -NFS4ERR_COMPLETE_ALREADY:
8966 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8967 break;
8968 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008969 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008970 /* fall through */
8971 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008972 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008973 case -NFS4ERR_BADSESSION:
8974 case -NFS4ERR_DEADSESSION:
8975 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008976 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008977 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008978 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008979 }
8980 return 0;
8981}
8982
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008983static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8984{
8985 struct nfs4_reclaim_complete_data *calldata = data;
8986 struct nfs_client *clp = calldata->clp;
8987 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8988
8989 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008990 if (!nfs41_sequence_done(task, res))
8991 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008992
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008993 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008994 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8995 rpc_restart_call_prepare(task);
8996 return;
8997 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008998 dprintk("<-- %s\n", __func__);
8999}
9000
9001static void nfs4_free_reclaim_complete_data(void *data)
9002{
9003 struct nfs4_reclaim_complete_data *calldata = data;
9004
9005 kfree(calldata);
9006}
9007
9008static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9009 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9010 .rpc_call_done = nfs4_reclaim_complete_done,
9011 .rpc_release = nfs4_free_reclaim_complete_data,
9012};
9013
9014/*
9015 * Issue a global reclaim complete.
9016 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009017static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009018 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009019{
9020 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009021 struct rpc_message msg = {
9022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009023 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009024 };
9025 struct rpc_task_setup task_setup_data = {
9026 .rpc_client = clp->cl_rpcclient,
9027 .rpc_message = &msg,
9028 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009029 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009030 };
9031 int status = -ENOMEM;
9032
9033 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009034 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009035 if (calldata == NULL)
9036 goto out;
9037 calldata->clp = clp;
9038 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009039
Anna Schumakerfba83f32018-05-04 16:22:50 -04009040 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009041 msg.rpc_argp = &calldata->arg;
9042 msg.rpc_resp = &calldata->res;
9043 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009044 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009045out:
9046 dprintk("<-- %s status=%d\n", __func__, status);
9047 return status;
9048}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009049
9050static void
9051nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9052{
9053 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009054 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009055
9056 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009057 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009058 &lgp->res.seq_res, task);
9059 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009060}
9061
9062static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9063{
9064 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009065
9066 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009067 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009068 dprintk("<-- %s\n", __func__);
9069}
9070
9071static int
9072nfs4_layoutget_handle_exception(struct rpc_task *task,
9073 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9074{
Trond Myklebustee314c22012-10-01 17:25:48 -07009075 struct inode *inode = lgp->args.inode;
9076 struct nfs_server *server = NFS_SERVER(inode);
9077 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009078 int nfs4err = task->tk_status;
9079 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009080 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009081
Boaz Harroshed7e5422014-01-22 20:34:54 +02009082 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009083
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009084 nfs4_sequence_free_slot(&lgp->res.seq_res);
9085
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009086 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009087 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009088 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009089
9090 /*
9091 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9092 * on the file. set tk_status to -ENODATA to tell upper layer to
9093 * retry go inband.
9094 */
9095 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009096 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009097 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009098 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009099 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9100 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9101 */
9102 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009103 status = -EOVERFLOW;
9104 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009105 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009106 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009107 * (or clients) writing to the same RAID stripe except when
9108 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009109 *
9110 * Treat it like we would RECALLCONFLICT -- we retry for a little
9111 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009112 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009113 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009114 if (lgp->args.minlength == 0) {
9115 status = -EOVERFLOW;
9116 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009117 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009118 status = -EBUSY;
9119 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009120 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009121 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009122 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009123 case -NFS4ERR_DELEG_REVOKED:
9124 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009125 case -NFS4ERR_EXPIRED:
9126 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009127 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009128 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009129 lo = NFS_I(inode)->layout;
9130 /* If the open stateid was bad, then recover it. */
9131 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009132 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009133 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009134 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009135 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009136 break;
9137 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009138
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009139 /*
9140 * Mark the bad layout state as invalid, then retry
9141 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009142 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009143 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009144 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009145 pnfs_free_lseg_list(&head);
9146 status = -EAGAIN;
9147 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009148 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009149
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009150 err = nfs4_handle_exception(server, nfs4err, exception);
9151 if (!status) {
9152 if (exception->retry)
9153 status = -EAGAIN;
9154 else
9155 status = err;
9156 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009157out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009158 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009159 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009160}
9161
Fred Isamandacb4522016-09-19 17:47:09 -04009162size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009163{
9164 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9165 return nfs_page_array_len(0, max_resp_sz);
9166}
9167
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009168static void nfs4_layoutget_release(void *calldata)
9169{
9170 struct nfs4_layoutget *lgp = calldata;
9171
9172 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009173 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009174 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009175 dprintk("<-- %s\n", __func__);
9176}
9177
9178static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9179 .rpc_call_prepare = nfs4_layoutget_prepare,
9180 .rpc_call_done = nfs4_layoutget_done,
9181 .rpc_release = nfs4_layoutget_release,
9182};
9183
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009184struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009185nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009186{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009187 struct inode *inode = lgp->args.inode;
9188 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009189 struct rpc_task *task;
9190 struct rpc_message msg = {
9191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9192 .rpc_argp = &lgp->args,
9193 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009194 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009195 };
9196 struct rpc_task_setup task_setup_data = {
9197 .rpc_client = server->client,
9198 .rpc_message = &msg,
9199 .callback_ops = &nfs4_layoutget_call_ops,
9200 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009201 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009202 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009203 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009204 struct nfs4_exception exception = {
9205 .inode = inode,
9206 .timeout = *timeout,
9207 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009208 int status = 0;
9209
9210 dprintk("--> %s\n", __func__);
9211
Peng Tao4bd5a982014-11-17 11:05:17 +08009212 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9213 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9214
Anna Schumakerfba83f32018-05-04 16:22:50 -04009215 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009216
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009217 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009218
Anna Schumaker820bf852017-01-11 15:01:43 -05009219 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009220 if (status != 0)
9221 goto out;
9222
Trond Myklebust18c07782019-02-13 07:55:31 -05009223 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009224 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9225 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009226 } else if (lgp->res.layoutp->len == 0) {
9227 status = -EAGAIN;
9228 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009229 } else
9230 lseg = pnfs_layout_process(lgp);
9231out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009232 trace_nfs4_layoutget(lgp->args.ctx,
9233 &lgp->args.range,
9234 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009235 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009236 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009237
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009238 rpc_put_task(task);
9239 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009240 if (status)
9241 return ERR_PTR(status);
9242 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009243}
9244
Benny Halevycbe82602011-05-22 19:52:37 +03009245static void
9246nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9247{
9248 struct nfs4_layoutreturn *lrp = calldata;
9249
9250 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009251 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009252 &lrp->args.seq_args,
9253 &lrp->res.seq_res,
9254 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009255 if (!pnfs_layout_is_valid(lrp->args.layout))
9256 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009257}
9258
9259static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9260{
9261 struct nfs4_layoutreturn *lrp = calldata;
9262 struct nfs_server *server;
9263
9264 dprintk("--> %s\n", __func__);
9265
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009266 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009267 return;
9268
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009269 /*
9270 * Was there an RPC level error? Assume the call succeeded,
9271 * and that we need to release the layout
9272 */
9273 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9274 lrp->res.lrs_present = 0;
9275 return;
9276 }
9277
Benny Halevycbe82602011-05-22 19:52:37 +03009278 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009279 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009280 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009281 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009282 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009283 lrp->args.inode))
9284 goto out_restart;
9285 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009286 default:
9287 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05009288 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009289 case 0:
9290 break;
9291 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009292 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009293 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009294 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009295 }
Benny Halevycbe82602011-05-22 19:52:37 +03009296 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009297 return;
9298out_restart:
9299 task->tk_status = 0;
9300 nfs4_sequence_free_slot(&lrp->res.seq_res);
9301 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009302}
9303
9304static void nfs4_layoutreturn_release(void *calldata)
9305{
9306 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009307 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009308
9309 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009310 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009311 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009312 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009313 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9314 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009315 pnfs_put_layout_hdr(lrp->args.layout);
9316 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009317 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009318 kfree(calldata);
9319 dprintk("<-- %s\n", __func__);
9320}
9321
9322static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9323 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9324 .rpc_call_done = nfs4_layoutreturn_done,
9325 .rpc_release = nfs4_layoutreturn_release,
9326};
9327
Peng Tao6c166052014-11-17 09:30:40 +08009328int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009329{
9330 struct rpc_task *task;
9331 struct rpc_message msg = {
9332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9333 .rpc_argp = &lrp->args,
9334 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009335 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009336 };
9337 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c5772013-07-22 12:42:05 -04009338 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009339 .rpc_message = &msg,
9340 .callback_ops = &nfs4_layoutreturn_call_ops,
9341 .callback_data = lrp,
9342 };
Peng Tao6c166052014-11-17 09:30:40 +08009343 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009344
Andrew Elble99ade3c2015-12-02 09:39:51 -05009345 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9346 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9347 &task_setup_data.rpc_client, &msg);
9348
Benny Halevycbe82602011-05-22 19:52:37 +03009349 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009350 if (!sync) {
9351 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9352 if (!lrp->inode) {
9353 nfs4_layoutreturn_release(lrp);
9354 return -EAGAIN;
9355 }
9356 task_setup_data.flags |= RPC_TASK_ASYNC;
9357 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009358 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009359 task = rpc_run_task(&task_setup_data);
9360 if (IS_ERR(task))
9361 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009362 if (sync)
9363 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009364 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009365 dprintk("<-- %s status=%d\n", __func__, status);
9366 rpc_put_task(task);
9367 return status;
9368}
9369
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009370static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009371_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9372 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009373 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009374{
9375 struct nfs4_getdeviceinfo_args args = {
9376 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009377 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9378 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009379 };
9380 struct nfs4_getdeviceinfo_res res = {
9381 .pdev = pdev,
9382 };
9383 struct rpc_message msg = {
9384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9385 .rpc_argp = &args,
9386 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009387 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009388 };
9389 int status;
9390
9391 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009392 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009393 if (res.notification & ~args.notify_types)
9394 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009395 if (res.notification != args.notify_types)
9396 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009397
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009398 dprintk("<-- %s status=%d\n", __func__, status);
9399
9400 return status;
9401}
9402
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009403int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9404 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009405 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009406{
9407 struct nfs4_exception exception = { };
9408 int err;
9409
9410 do {
9411 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009412 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009413 &exception);
9414 } while (exception.retry);
9415 return err;
9416}
9417EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9418
Andy Adamson863a3c62011-03-23 13:27:54 +00009419static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9420{
9421 struct nfs4_layoutcommit_data *data = calldata;
9422 struct nfs_server *server = NFS_SERVER(data->args.inode);
9423
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009424 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009425 &data->args.seq_args,
9426 &data->res.seq_res,
9427 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009428}
9429
9430static void
9431nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9432{
9433 struct nfs4_layoutcommit_data *data = calldata;
9434 struct nfs_server *server = NFS_SERVER(data->args.inode);
9435
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009436 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009437 return;
9438
9439 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009440 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9441 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9442 case -NFS4ERR_BADLAYOUT: /* no layout */
9443 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009444 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009445 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009446 break;
9447 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009448 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009449 rpc_restart_call_prepare(task);
9450 return;
9451 }
9452 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009453}
9454
9455static void nfs4_layoutcommit_release(void *calldata)
9456{
9457 struct nfs4_layoutcommit_data *data = calldata;
9458
Andy Adamsondb29c082011-07-30 20:52:38 -04009459 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009460 nfs_post_op_update_inode_force_wcc(data->args.inode,
9461 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009462 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009463 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009464 kfree(data);
9465}
9466
9467static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9468 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9469 .rpc_call_done = nfs4_layoutcommit_done,
9470 .rpc_release = nfs4_layoutcommit_release,
9471};
9472
9473int
Andy Adamsonef311532011-03-12 02:58:10 -05009474nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009475{
9476 struct rpc_message msg = {
9477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9478 .rpc_argp = &data->args,
9479 .rpc_resp = &data->res,
9480 .rpc_cred = data->cred,
9481 };
9482 struct rpc_task_setup task_setup_data = {
9483 .task = &data->task,
9484 .rpc_client = NFS_CLIENT(data->args.inode),
9485 .rpc_message = &msg,
9486 .callback_ops = &nfs4_layoutcommit_ops,
9487 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009488 };
9489 struct rpc_task *task;
9490 int status = 0;
9491
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009492 dprintk("NFS: initiating layoutcommit call. sync %d "
9493 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009494 data->args.lastbytewritten,
9495 data->args.inode->i_ino);
9496
Trond Myklebust472e2592015-02-05 16:50:30 -05009497 if (!sync) {
9498 data->inode = nfs_igrab_and_active(data->args.inode);
9499 if (data->inode == NULL) {
9500 nfs4_layoutcommit_release(data);
9501 return -EAGAIN;
9502 }
9503 task_setup_data.flags = RPC_TASK_ASYNC;
9504 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009505 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009506 task = rpc_run_task(&task_setup_data);
9507 if (IS_ERR(task))
9508 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009509 if (sync)
9510 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009511 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009512 dprintk("%s: status %d\n", __func__, status);
9513 rpc_put_task(task);
9514 return status;
9515}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009516
Trond Myklebust302fad72019-02-18 13:32:38 -05009517/*
Andy Adamson97431202013-08-08 10:57:56 -04009518 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9519 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9520 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009521static int
9522_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009523 struct nfs_fsinfo *info,
9524 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009525{
9526 struct nfs41_secinfo_no_name_args args = {
9527 .style = SECINFO_STYLE_CURRENT_FH,
9528 };
9529 struct nfs4_secinfo_res res = {
9530 .flavors = flavors,
9531 };
9532 struct rpc_message msg = {
9533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9534 .rpc_argp = &args,
9535 .rpc_resp = &res,
9536 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009537 struct rpc_clnt *clnt = server->client;
Anna Schumakercc15e242019-08-14 16:22:31 -04009538 struct nfs4_call_sync_data data = {
9539 .seq_server = server,
9540 .seq_args = &args.seq_args,
9541 .seq_res = &res.seq_res,
9542 };
9543 struct rpc_task_setup task_setup = {
9544 .rpc_client = server->client,
9545 .rpc_message = &msg,
9546 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9547 .callback_data = &data,
9548 .flags = RPC_TASK_NO_ROUND_ROBIN,
9549 };
NeilBrowna52458b2018-12-03 11:30:31 +11009550 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009551 int status;
9552
9553 if (use_integrity) {
9554 clnt = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009555 task_setup.rpc_client = clnt;
9556
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009557 cred = nfs4_get_clid_cred(server->nfs_client);
9558 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009559 }
9560
9561 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009562 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9563 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009564 dprintk("<-- %s status=%d\n", __func__, status);
9565
NeilBrowna52458b2018-12-03 11:30:31 +11009566 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009567
9568 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009569}
9570
9571static int
9572nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9573 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9574{
Trond Myklebust0688e642019-04-07 13:59:09 -04009575 struct nfs4_exception exception = {
9576 .interruptible = true,
9577 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009578 int err;
9579 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009580 /* first try using integrity protection */
9581 err = -NFS4ERR_WRONGSEC;
9582
9583 /* try to use integrity protection with machine cred */
9584 if (_nfs4_is_integrity_protected(server->nfs_client))
9585 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9586 flavors, true);
9587
9588 /*
9589 * if unable to use integrity protection, or SECINFO with
9590 * integrity protection returns NFS4ERR_WRONGSEC (which is
9591 * disallowed by spec, but exists in deployed servers) use
9592 * the current filesystem's rpc_client and the user cred.
9593 */
9594 if (err == -NFS4ERR_WRONGSEC)
9595 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9596 flavors, false);
9597
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009598 switch (err) {
9599 case 0:
9600 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009601 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009602 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009603 default:
9604 err = nfs4_handle_exception(server, err, &exception);
9605 }
9606 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009607out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009608 return err;
9609}
9610
9611static int
9612nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9613 struct nfs_fsinfo *info)
9614{
9615 int err;
9616 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009617 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009618 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009619 struct nfs4_secinfo4 *secinfo;
9620 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009621
9622 page = alloc_page(GFP_KERNEL);
9623 if (!page) {
9624 err = -ENOMEM;
9625 goto out;
9626 }
9627
9628 flavors = page_address(page);
9629 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9630
9631 /*
9632 * Fall back on "guess and check" method if
9633 * the server doesn't support SECINFO_NO_NAME
9634 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009635 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009636 err = nfs4_find_root_sec(server, fhandle, info);
9637 goto out_freepage;
9638 }
9639 if (err)
9640 goto out_freepage;
9641
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009642 for (i = 0; i < flavors->num_flavors; i++) {
9643 secinfo = &flavors->flavors[i];
9644
9645 switch (secinfo->flavor) {
9646 case RPC_AUTH_NULL:
9647 case RPC_AUTH_UNIX:
9648 case RPC_AUTH_GSS:
9649 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9650 &secinfo->flavor_info);
9651 break;
9652 default:
9653 flavor = RPC_AUTH_MAXFLAVOR;
9654 break;
9655 }
9656
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009657 if (!nfs_auth_info_match(&server->auth_info, flavor))
9658 flavor = RPC_AUTH_MAXFLAVOR;
9659
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009660 if (flavor != RPC_AUTH_MAXFLAVOR) {
9661 err = nfs4_lookup_root_sec(server, fhandle,
9662 info, flavor);
9663 if (!err)
9664 break;
9665 }
9666 }
9667
9668 if (flavor == RPC_AUTH_MAXFLAVOR)
9669 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009670
9671out_freepage:
9672 put_page(page);
9673 if (err == -EACCES)
9674 return -EPERM;
9675out:
9676 return err;
9677}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009678
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009679static int _nfs41_test_stateid(struct nfs_server *server,
9680 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009681 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009682{
9683 int status;
9684 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009685 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009686 };
9687 struct nfs41_test_stateid_res res;
9688 struct rpc_message msg = {
9689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9690 .rpc_argp = &args,
9691 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009692 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009693 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009694 struct rpc_clnt *rpc_client = server->client;
9695
9696 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9697 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009698
Chuck Lever38527b12012-07-11 16:30:23 -04009699 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009700 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009701 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009702 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009703 if (status != NFS_OK) {
9704 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009705 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009706 }
9707 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009708 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009709}
9710
Trond Myklebust43912bb2016-09-22 13:38:56 -04009711static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9712 int err, struct nfs4_exception *exception)
9713{
9714 exception->retry = 0;
9715 switch(err) {
9716 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009717 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009718 nfs4_handle_exception(server, err, exception);
9719 break;
9720 case -NFS4ERR_BADSESSION:
9721 case -NFS4ERR_BADSLOT:
9722 case -NFS4ERR_BAD_HIGH_SLOT:
9723 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9724 case -NFS4ERR_DEADSESSION:
9725 nfs4_do_handle_exception(server, err, exception);
9726 }
9727}
9728
Chuck Lever38527b12012-07-11 16:30:23 -04009729/**
9730 * nfs41_test_stateid - perform a TEST_STATEID operation
9731 *
9732 * @server: server / transport on which to perform the operation
9733 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009734 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009735 *
9736 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9737 * Otherwise a negative NFS4ERR value is returned if the operation
9738 * failed or the state ID is not currently valid.
9739 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009740static int nfs41_test_stateid(struct nfs_server *server,
9741 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009742 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009743{
Trond Myklebust0688e642019-04-07 13:59:09 -04009744 struct nfs4_exception exception = {
9745 .interruptible = true,
9746 };
Bryan Schumaker7d974792011-06-02 14:59:08 -04009747 int err;
9748 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009749 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009750 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009751 } while (exception.retry);
9752 return err;
9753}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009754
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009755struct nfs_free_stateid_data {
9756 struct nfs_server *server;
9757 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009758 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009759};
9760
9761static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9762{
9763 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009764 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009765 &data->args.seq_args,
9766 &data->res.seq_res,
9767 task);
9768}
9769
9770static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9771{
9772 struct nfs_free_stateid_data *data = calldata;
9773
9774 nfs41_sequence_done(task, &data->res.seq_res);
9775
9776 switch (task->tk_status) {
9777 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009778 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009779 rpc_restart_call_prepare(task);
9780 }
9781}
9782
9783static void nfs41_free_stateid_release(void *calldata)
9784{
9785 kfree(calldata);
9786}
9787
Trond Myklebust17f26b12013-08-21 15:48:42 -04009788static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009789 .rpc_call_prepare = nfs41_free_stateid_prepare,
9790 .rpc_call_done = nfs41_free_stateid_done,
9791 .rpc_release = nfs41_free_stateid_release,
9792};
9793
Anna Schumaker2f261022018-05-15 13:03:39 -04009794/**
9795 * nfs41_free_stateid - perform a FREE_STATEID operation
9796 *
9797 * @server: server / transport on which to perform the operation
9798 * @stateid: state ID to release
9799 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009800 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009801 *
9802 * Note: this function is always asynchronous.
9803 */
9804static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009805 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009806 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009807 bool privileged)
9808{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009809 struct rpc_message msg = {
9810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009811 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009812 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009813 struct rpc_task_setup task_setup = {
9814 .rpc_client = server->client,
9815 .rpc_message = &msg,
9816 .callback_ops = &nfs41_free_stateid_ops,
9817 .flags = RPC_TASK_ASYNC,
9818 };
9819 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009820 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009821
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009822 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9823 &task_setup.rpc_client, &msg);
9824
Chuck Lever38527b12012-07-11 16:30:23 -04009825 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009826 data = kmalloc(sizeof(*data), GFP_NOFS);
9827 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009828 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009829 data->server = server;
9830 nfs4_stateid_copy(&data->args.stateid, stateid);
9831
9832 task_setup.callback_data = data;
9833
9834 msg.rpc_argp = &data->args;
9835 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009836 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009837 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009838 if (IS_ERR(task))
9839 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009840 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009841 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009842}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009843
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009844static void
9845nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009846{
NeilBrowna52458b2018-12-03 11:30:31 +11009847 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009848
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009849 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009850 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009851}
9852
Trond Myklebust36281ca2012-03-04 18:13:56 -05009853static bool nfs41_match_stateid(const nfs4_stateid *s1,
9854 const nfs4_stateid *s2)
9855{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009856 if (s1->type != s2->type)
9857 return false;
9858
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009859 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009860 return false;
9861
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009862 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009863 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009864
Anna Schumaker045c5512017-01-11 16:59:48 -05009865 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009866}
9867
Andy Adamson557134a2009-04-01 09:21:53 -04009868#endif /* CONFIG_NFS_V4_1 */
9869
Trond Myklebust36281ca2012-03-04 18:13:56 -05009870static bool nfs4_match_stateid(const nfs4_stateid *s1,
9871 const nfs4_stateid *s2)
9872{
Trond Myklebustf597c532012-03-04 18:13:56 -05009873 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009874}
9875
9876
Trond Myklebust17280172012-03-11 13:11:00 -04009877static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009878 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009879 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009880 .recover_open = nfs4_open_reclaim,
9881 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009882 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009883 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009884};
9885
Andy Adamson591d71c2009-04-01 09:22:47 -04009886#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009887static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009888 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9889 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9890 .recover_open = nfs4_open_reclaim,
9891 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009892 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009893 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009894 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009895};
9896#endif /* CONFIG_NFS_V4_1 */
9897
Trond Myklebust17280172012-03-11 13:11:00 -04009898static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009899 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009900 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009901 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009902 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009903 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009904};
9905
Andy Adamson591d71c2009-04-01 09:22:47 -04009906#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009907static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009908 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9909 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009910 .recover_open = nfs41_open_expired,
9911 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009912 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009913};
9914#endif /* CONFIG_NFS_V4_1 */
9915
Trond Myklebust17280172012-03-11 13:11:00 -04009916static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009917 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009918 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009919 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009920};
9921
9922#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009923static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009924 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009925 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009926 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009927};
9928#endif
9929
Chuck Leverec011fe2013-10-17 14:12:39 -04009930static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009931 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009932 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009933};
9934
9935#if defined(CONFIG_NFS_V4_1)
9936static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009937 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009938 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009939};
9940#endif /* CONFIG_NFS_V4_1 */
9941
Trond Myklebust97dc1352010-06-16 09:52:26 -04009942static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9943 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009944 .init_caps = NFS_CAP_READDIRPLUS
9945 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009946 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009947 .init_client = nfs40_init_client,
9948 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009949 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009950 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009951 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009952 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009953 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009954 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009955 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9956 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9957 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009958 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009959};
9960
9961#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009962static struct nfs_seqid *
9963nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9964{
9965 return NULL;
9966}
9967
Trond Myklebust97dc1352010-06-16 09:52:26 -04009968static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9969 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009970 .init_caps = NFS_CAP_READDIRPLUS
9971 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009972 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009973 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009974 | NFS_CAP_ATOMIC_OPEN_V1
9975 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009976 .init_client = nfs41_init_client,
9977 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009978 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009979 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009980 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009981 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009982 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009983 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009984 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009985 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9986 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9987 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009988 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009989};
9990#endif
9991
Steve Dickson42c2c422013-05-22 12:50:38 -04009992#if defined(CONFIG_NFS_V4_2)
9993static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9994 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009995 .init_caps = NFS_CAP_READDIRPLUS
9996 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009997 | NFS_CAP_POSIX_LOCK
9998 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009999 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010000 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010001 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010002 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010003 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010004 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010005 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010006 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010007 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010008 | NFS_CAP_CLONE
10009 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010010 .init_client = nfs41_init_client,
10011 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010012 .match_stateid = nfs41_match_stateid,
10013 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010014 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010015 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010016 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010017 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010018 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010019 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10020 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10021 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010022 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010023};
10024#endif
10025
Trond Myklebust97dc1352010-06-16 09:52:26 -040010026const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10027 [0] = &nfs_v4_0_minor_ops,
10028#if defined(CONFIG_NFS_V4_1)
10029 [1] = &nfs_v4_1_minor_ops,
10030#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010031#if defined(CONFIG_NFS_V4_2)
10032 [2] = &nfs_v4_2_minor_ops,
10033#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010034};
10035
Trond Myklebust13997822016-07-24 17:10:52 -040010036static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010037{
10038 ssize_t error, error2;
10039
10040 error = generic_listxattr(dentry, list, size);
10041 if (error < 0)
10042 return error;
10043 if (list) {
10044 list += error;
10045 size -= error;
10046 }
10047
10048 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10049 if (error2 < 0)
10050 return error2;
10051 return error + error2;
10052}
10053
Trond Myklebust17f26b12013-08-21 15:48:42 -040010054static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010055 .create = nfs_create,
10056 .lookup = nfs_lookup,
10057 .atomic_open = nfs_atomic_open,
10058 .link = nfs_link,
10059 .unlink = nfs_unlink,
10060 .symlink = nfs_symlink,
10061 .mkdir = nfs_mkdir,
10062 .rmdir = nfs_rmdir,
10063 .mknod = nfs_mknod,
10064 .rename = nfs_rename,
10065 .permission = nfs_permission,
10066 .getattr = nfs_getattr,
10067 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010068 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010069};
10070
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010071static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010072 .permission = nfs_permission,
10073 .getattr = nfs_getattr,
10074 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010075 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010076};
10077
David Howells509de812006-08-22 20:06:11 -040010078const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010079 .version = 4, /* protocol version */
10080 .dentry_ops = &nfs4_dentry_operations,
10081 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010082 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010083 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010085 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010086 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010087 .getattr = nfs4_proc_getattr,
10088 .setattr = nfs4_proc_setattr,
10089 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010090 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010091 .access = nfs4_proc_access,
10092 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010093 .create = nfs4_proc_create,
10094 .remove = nfs4_proc_remove,
10095 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010096 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010098 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010099 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010100 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010101 .link = nfs4_proc_link,
10102 .symlink = nfs4_proc_symlink,
10103 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010104 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105 .readdir = nfs4_proc_readdir,
10106 .mknod = nfs4_proc_mknod,
10107 .statfs = nfs4_proc_statfs,
10108 .fsinfo = nfs4_proc_fsinfo,
10109 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010110 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010112 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010114 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010115 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010116 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010118 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010119 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010121 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010122 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010123 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010124 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010125 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010126 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -040010127 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010128 .create_server = nfs4_create_server,
10129 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130};
10131
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010132static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010133 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010134 .list = nfs4_xattr_list_nfs4_acl,
10135 .get = nfs4_xattr_get_nfs4_acl,
10136 .set = nfs4_xattr_set_nfs4_acl,
10137};
10138
10139const struct xattr_handler *nfs4_xattr_handlers[] = {
10140 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010141#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10142 &nfs4_xattr_nfs4_label_handler,
10143#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010144 NULL
10145};
10146
Linus Torvalds1da177e2005-04-16 15:20:36 -070010147/*
10148 * Local variables:
10149 * c-basic-offset: 8
10150 * End:
10151 */