blob: a4ace14756ac36c519ae14d0cc595f128a63436e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust30846df2018-04-07 13:44:28 -040074#define NFS4_BITMASK_SZ 3
75
Trond Myklebust2066fe82006-09-15 08:30:46 -040076#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define NFS4_POLL_RETRY_MAX (15*HZ)
78
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020079/* file attributes which can be mapped to nfs attributes */
80#define NFS4_VALID_ATTRS (ATTR_MODE \
81 | ATTR_UID \
82 | ATTR_GID \
83 | ATTR_SIZE \
84 | ATTR_ATIME \
85 | ATTR_MTIME \
86 | ATTR_CTIME \
87 | ATTR_ATIME_SET \
88 | ATTR_MTIME_SET)
89
Trond Myklebustcdd4e682006-01-03 09:55:12 +010090struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080091static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050093static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040094static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110095static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040096 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110097 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040098 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400100static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100101 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400102 struct nfs4_slot *slot,
103 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400104static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100105 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400106static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100107 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400108#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400109
110#ifdef CONFIG_NFS_V4_SECURITY_LABEL
111static inline struct nfs4_label *
112nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
113 struct iattr *sattr, struct nfs4_label *label)
114{
115 int err;
116
117 if (label == NULL)
118 return NULL;
119
120 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
121 return NULL;
122
David Quigleyaa9c2662013-05-22 12:50:44 -0400123 err = security_dentry_init_security(dentry, sattr->ia_mode,
124 &dentry->d_name, (void **)&label->label, &label->len);
125 if (err == 0)
126 return label;
127
128 return NULL;
129}
130static inline void
131nfs4_label_release_security(struct nfs4_label *label)
132{
133 if (label)
134 security_release_secctx(label->label, label->len);
135}
136static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
137{
138 if (label)
139 return server->attr_bitmask;
140
141 return server->attr_bitmask_nl;
142}
143#else
144static inline struct nfs4_label *
145nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
146 struct iattr *sattr, struct nfs4_label *l)
147{ return NULL; }
148static inline void
149nfs4_label_release_security(struct nfs4_label *label)
150{ return; }
151static inline u32 *
152nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
153{ return server->attr_bitmask; }
154#endif
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500157static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Trond Myklebust52567b02009-10-23 14:46:42 -0400159 if (err >= -1000)
160 return err;
161 switch (err) {
162 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500163 case -NFS4ERR_LAYOUTTRYLATER:
164 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000166 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400167 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000168 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800169 case -NFS4ERR_BADOWNER:
170 case -NFS4ERR_BADNAME:
171 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400172 case -NFS4ERR_SHARE_DENIED:
173 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400174 case -NFS4ERR_MINOR_VERS_MISMATCH:
175 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400176 case -NFS4ERR_FILE_OPEN:
177 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700180 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400183 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/*
187 * This is our standard bitmap for GETATTR requests.
188 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400189const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 FATTR4_WORD0_TYPE
191 | FATTR4_WORD0_CHANGE
192 | FATTR4_WORD0_SIZE
193 | FATTR4_WORD0_FSID
194 | FATTR4_WORD0_FILEID,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400205#ifdef CONFIG_NFS_V4_SECURITY_LABEL
206 FATTR4_WORD2_SECURITY_LABEL
207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Trond Myklebust1549210f2012-06-05 09:16:47 -0400210static const u32 nfs4_pnfs_open_bitmap[3] = {
211 FATTR4_WORD0_TYPE
212 | FATTR4_WORD0_CHANGE
213 | FATTR4_WORD0_SIZE
214 | FATTR4_WORD0_FSID
215 | FATTR4_WORD0_FILEID,
216 FATTR4_WORD1_MODE
217 | FATTR4_WORD1_NUMLINKS
218 | FATTR4_WORD1_OWNER
219 | FATTR4_WORD1_OWNER_GROUP
220 | FATTR4_WORD1_RAWDEV
221 | FATTR4_WORD1_SPACE_USED
222 | FATTR4_WORD1_TIME_ACCESS
223 | FATTR4_WORD1_TIME_METADATA
224 | FATTR4_WORD1_TIME_MODIFY,
225 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500226#ifdef CONFIG_NFS_V4_SECURITY_LABEL
227 | FATTR4_WORD2_SECURITY_LABEL
228#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400229};
230
Andy Adamsone23008e2012-10-02 21:07:32 -0400231static const u32 nfs4_open_noattr_bitmap[3] = {
232 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400233 | FATTR4_WORD0_FILEID,
234};
235
David Quigleya09df2c2013-05-22 12:50:41 -0400236const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 FATTR4_WORD0_FILES_AVAIL
238 | FATTR4_WORD0_FILES_FREE
239 | FATTR4_WORD0_FILES_TOTAL,
240 FATTR4_WORD1_SPACE_AVAIL
241 | FATTR4_WORD1_SPACE_FREE
242 | FATTR4_WORD1_SPACE_TOTAL
243};
244
David Quigleya09df2c2013-05-22 12:50:41 -0400245const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 FATTR4_WORD0_MAXLINK
247 | FATTR4_WORD0_MAXNAME,
248 0
249};
250
Fred Isamandae100c2011-07-30 20:52:37 -0400251const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 | FATTR4_WORD0_MAXREAD
253 | FATTR4_WORD0_MAXWRITE
254 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700255 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400256 | FATTR4_WORD1_FS_LAYOUT_TYPES,
257 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800258 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
David Quigleya09df2c2013-05-22 12:50:41 -0400261const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500262 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400263 | FATTR4_WORD0_SIZE
264 | FATTR4_WORD0_FSID
265 | FATTR4_WORD0_FILEID
266 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500267 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Trond Myklebust30846df2018-04-07 13:44:28 -0400277static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
278 struct inode *inode)
279{
280 unsigned long cache_validity;
281
282 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
283 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
284 return;
285
286 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
287 if (!(cache_validity & NFS_INO_REVAL_FORCED))
288 cache_validity &= ~(NFS_INO_INVALID_CHANGE
289 | NFS_INO_INVALID_SIZE);
290
291 if (!(cache_validity & NFS_INO_INVALID_SIZE))
292 dst[0] &= ~FATTR4_WORD0_SIZE;
293
294 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
295 dst[0] &= ~FATTR4_WORD0_CHANGE;
296}
297
298static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
299 const __u32 *src, struct inode *inode)
300{
301 nfs4_bitmap_copy_adjust(dst, src, inode);
302}
303
Al Virobc4785c2006-10-19 23:28:51 -0700304static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct nfs4_readdir_arg *readdir)
306{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400307 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700308 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000311 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
313 return;
314 }
315
316 readdir->cookie = 0;
317 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
318 if (cookie == 2)
319 return;
320
321 /*
322 * NFSv4 servers do not return entries for '.' and '..'
323 * Therefore, we fake these entries here. We let '.'
324 * have cookie 0 and '..' have cookie 1. Note that
325 * when talking to the server, we always send cookie 0
326 * instead of 1 or 2.
327 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (cookie == 0) {
331 *p++ = xdr_one; /* next */
332 *p++ = xdr_zero; /* cookie, first word */
333 *p++ = xdr_one; /* cookie, second word */
334 *p++ = xdr_one; /* entry len */
335 memcpy(p, ".\0\0\0", 4); /* entry */
336 p++;
337 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400338 *p++ = htonl(attrs); /* bitmap */
339 *p++ = htonl(12); /* attribute buffer length */
340 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000341 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
344 *p++ = xdr_one; /* next */
345 *p++ = xdr_zero; /* cookie, first word */
346 *p++ = xdr_two; /* cookie, second word */
347 *p++ = xdr_two; /* entry len */
348 memcpy(p, "..\0\0", 4); /* entry */
349 p++;
350 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400351 *p++ = htonl(attrs); /* bitmap */
352 *p++ = htonl(12); /* attribute buffer length */
353 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000354 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 readdir->pgbase = (char *)p - (char *)start;
357 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800358 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Trond Myklebust26d36302016-09-22 13:39:05 -0400361static void nfs4_test_and_free_stateid(struct nfs_server *server,
362 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100363 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400364{
365 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
366
367 ops->test_and_free_expired(server, stateid, cred);
368}
369
370static void __nfs4_free_revoked_stateid(struct nfs_server *server,
371 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100372 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400373{
374 stateid->type = NFS4_REVOKED_STATEID_TYPE;
375 nfs4_test_and_free_stateid(server, stateid, cred);
376}
377
378static void nfs4_free_revoked_stateid(struct nfs_server *server,
379 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100380 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400381{
382 nfs4_stateid tmp;
383
384 nfs4_stateid_copy(&tmp, stateid);
385 __nfs4_free_revoked_stateid(server, &tmp, cred);
386}
387
NeilBrown8478eaa2014-09-18 16:09:27 +1000388static long nfs4_update_delay(long *timeout)
389{
390 long ret;
391 if (!timeout)
392 return NFS4_POLL_RETRY_MAX;
393 if (*timeout <= 0)
394 *timeout = NFS4_POLL_RETRY_MIN;
395 if (*timeout > NFS4_POLL_RETRY_MAX)
396 *timeout = NFS4_POLL_RETRY_MAX;
397 ret = *timeout;
398 *timeout <<= 1;
399 return ret;
400}
401
Trond Myklebust0688e642019-04-07 13:59:09 -0400402static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500403{
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 might_sleep();
405
NeilBrown8478eaa2014-09-18 16:09:27 +1000406 freezable_schedule_timeout_killable_unsafe(
407 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400408 if (!__fatal_signal_pending(current))
409 return 0;
410 return -EINTR;
411}
412
413static int nfs4_delay_interruptible(long *timeout)
414{
415 might_sleep();
416
417 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
418 if (!signal_pending(current))
419 return 0;
420 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
421}
422
423static int nfs4_delay(long *timeout, bool interruptible)
424{
425 if (interruptible)
426 return nfs4_delay_interruptible(timeout);
427 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500428}
429
Trond Myklebust50c80002019-07-11 19:02:18 -0400430static const nfs4_stateid *
431nfs4_recoverable_stateid(const nfs4_stateid *stateid)
432{
433 if (!stateid)
434 return NULL;
435 switch (stateid->type) {
436 case NFS4_OPEN_STATEID_TYPE:
437 case NFS4_LOCK_STATEID_TYPE:
438 case NFS4_DELEGATION_STATEID_TYPE:
439 return stateid;
440 default:
441 break;
442 }
443 return NULL;
444}
445
Trond Myklebust65de8722008-12-23 15:21:44 -0500446/* This is the error handling routine for processes that are allowed
447 * to sleep.
448 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400449static int nfs4_do_handle_exception(struct nfs_server *server,
450 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500451{
452 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500453 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400454 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500455 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500456 int ret = errorcode;
457
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400458 exception->delay = 0;
459 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500460 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400461
Trond Myklebust50c80002019-07-11 19:02:18 -0400462 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400463 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400464 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400465
Trond Myklebust65de8722008-12-23 15:21:44 -0500466 switch(errorcode) {
467 case 0:
468 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400469 case -NFS4ERR_BADHANDLE:
470 case -ESTALE:
471 if (inode != NULL && S_ISREG(inode->i_mode))
472 pnfs_destroy_layout(NFS_I(inode));
473 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400474 case -NFS4ERR_DELEG_REVOKED:
475 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400476 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400477 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400478 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400479 if (inode != NULL && stateid != NULL) {
480 nfs_inode_find_state_and_recover(inode,
481 stateid);
482 goto wait_on_recovery;
483 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500484 /* Fall through */
Trond Myklebust272289a2016-09-22 13:39:15 -0400485 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400486 if (inode) {
487 int err;
488
489 err = nfs_async_inode_return_delegation(inode,
490 stateid);
491 if (err == 0)
492 goto wait_on_recovery;
493 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
494 exception->retry = 1;
495 break;
496 }
497 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500498 if (state == NULL)
499 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400500 ret = nfs4_schedule_stateid_recovery(server, state);
501 if (ret < 0)
502 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500503 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500504 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500505 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500506 nfs4_schedule_lease_recovery(clp);
507 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400508 case -NFS4ERR_MOVED:
509 ret = nfs4_schedule_migration_recovery(server);
510 if (ret < 0)
511 break;
512 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400513 case -NFS4ERR_LEASE_MOVED:
514 nfs4_schedule_lease_moved_recovery(clp);
515 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500516#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400517 case -NFS4ERR_BADSESSION:
518 case -NFS4ERR_BADSLOT:
519 case -NFS4ERR_BAD_HIGH_SLOT:
520 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
521 case -NFS4ERR_DEADSESSION:
522 case -NFS4ERR_SEQ_FALSE_RETRY:
523 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100524 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400525 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500526#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500527 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500528 if (exception->timeout > HZ) {
529 /* We have retried a decent amount, time to
530 * fail
531 */
532 ret = -EBUSY;
533 break;
534 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500535 /* Fall through */
Trond Myklebust65de8722008-12-23 15:21:44 -0500536 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400537 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500538 /* Fall through */
Trond Myklebust2598ed32015-09-20 16:10:18 -0400539 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400540 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400541 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400542 exception->delay = 1;
543 return 0;
544
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400545 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500546 case -NFS4ERR_OLD_STATEID:
547 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800548 break;
549 case -NFS4ERR_BADOWNER:
550 /* The following works around a Linux server bug! */
551 case -NFS4ERR_BADNAME:
552 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
553 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
554 exception->retry = 1;
555 printk(KERN_WARNING "NFS: v4 server %s "
556 "does not accept raw "
557 "uid/gids. "
558 "Reenabling the idmapper.\n",
559 server->nfs_client->cl_hostname);
560 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500561 }
562 /* We failed to handle the error */
563 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500564wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400565 exception->recovering = 1;
566 return 0;
567}
568
569/* This is the error handling routine for processes that are allowed
570 * to sleep.
571 */
572int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
573{
574 struct nfs_client *clp = server->nfs_client;
575 int ret;
576
577 ret = nfs4_do_handle_exception(server, errorcode, exception);
578 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400579 ret = nfs4_delay(&exception->timeout,
580 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400581 goto out_retry;
582 }
583 if (exception->recovering) {
584 ret = nfs4_wait_clnt_recover(clp);
585 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
586 return -EIO;
587 goto out_retry;
588 }
589 return ret;
590out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500591 if (ret == 0)
592 exception->retry = 1;
593 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500594}
595
Trond Myklebust037fc982015-09-20 15:51:00 -0400596static int
597nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
598 int errorcode, struct nfs4_exception *exception)
599{
600 struct nfs_client *clp = server->nfs_client;
601 int ret;
602
603 ret = nfs4_do_handle_exception(server, errorcode, exception);
604 if (exception->delay) {
605 rpc_delay(task, nfs4_update_delay(&exception->timeout));
606 goto out_retry;
607 }
608 if (exception->recovering) {
609 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
610 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
611 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
612 goto out_retry;
613 }
614 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
615 ret = -EIO;
616 return ret;
617out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500618 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400619 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500620 /*
621 * For NFS4ERR_MOVED, the client transport will need to
622 * be recomputed after migration recovery has completed.
623 */
624 if (errorcode == -NFS4ERR_MOVED)
625 rpc_task_release_transport(task);
626 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400627 return ret;
628}
629
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400630int
Trond Myklebust037fc982015-09-20 15:51:00 -0400631nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
632 struct nfs4_state *state, long *timeout)
633{
634 struct nfs4_exception exception = {
635 .state = state,
636 };
637
638 if (task->tk_status >= 0)
639 return 0;
640 if (timeout)
641 exception.timeout = *timeout;
642 task->tk_status = nfs4_async_handle_exception(task, server,
643 task->tk_status,
644 &exception);
645 if (exception.delay && timeout)
646 *timeout = exception.timeout;
647 if (exception.retry)
648 return -EAGAIN;
649 return 0;
650}
651
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400652/*
653 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
654 * or 'false' otherwise.
655 */
656static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
657{
658 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500659 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400660}
Trond Myklebust65de8722008-12-23 15:21:44 -0500661
Trond Myklebust452e9352010-07-31 14:29:06 -0400662static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 spin_lock(&clp->cl_lock);
665 if (time_before(clp->cl_last_renewal,timestamp))
666 clp->cl_last_renewal = timestamp;
667 spin_unlock(&clp->cl_lock);
668}
669
Trond Myklebust452e9352010-07-31 14:29:06 -0400670static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
671{
Trond Myklebustbe824162015-07-05 14:50:46 -0400672 struct nfs_client *clp = server->nfs_client;
673
674 if (!nfs4_has_session(clp))
675 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400676}
677
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400678struct nfs4_call_sync_data {
679 const struct nfs_server *seq_server;
680 struct nfs4_sequence_args *seq_args;
681 struct nfs4_sequence_res *seq_res;
682};
683
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800684void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400685 struct nfs4_sequence_res *res, int cache_reply,
686 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400687{
688 args->sa_slot = NULL;
689 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400690 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400691
692 res->sr_slot = NULL;
693}
694
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400695static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400696{
697 struct nfs4_slot *slot = res->sr_slot;
698 struct nfs4_slot_table *tbl;
699
Chuck Lever3bd23842013-08-09 12:49:19 -0400700 tbl = slot->table;
701 spin_lock(&tbl->slot_tbl_lock);
702 if (!nfs41_wake_and_assign_slot(tbl, slot))
703 nfs4_free_slot(tbl, slot);
704 spin_unlock(&tbl->slot_tbl_lock);
705
706 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400707}
708
709static int nfs40_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
711{
712 if (res->sr_slot != NULL)
713 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400714 return 1;
715}
716
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400717#if defined(CONFIG_NFS_V4_1)
718
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400719static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400720{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500721 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400722 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500723 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400724
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400725 if (!slot)
726 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500727 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500728 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500729
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400730 /* Bump the slot sequence number */
731 if (slot->seq_done)
732 slot->seq_nr++;
733 slot->seq_done = 0;
734
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500735 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500736 /* Be nice to the server: try to ensure that the last transmitted
737 * value for highest_user_slotid <= target_highest_slotid
738 */
739 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
740 send_new_highest_used_slotid = true;
741
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500742 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500743 send_new_highest_used_slotid = false;
744 goto out_unlock;
745 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500746 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500747
748 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
749 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500750out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500751 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500752 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400753 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400754 if (waitqueue_active(&tbl->slot_waitq))
755 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400756}
757
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400758static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
759{
760 nfs41_release_slot(res->sr_slot);
761 res->sr_slot = NULL;
762}
763
Trond Myklebust3453d572018-06-20 17:53:34 -0400764static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
765 u32 seqnr)
766{
767 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
768 slot->seq_nr_highest_sent = seqnr;
769}
770static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
771 u32 seqnr)
772{
773 slot->seq_nr_highest_sent = seqnr;
774 slot->seq_nr_last_acked = seqnr;
775}
776
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400777static int nfs41_sequence_process(struct rpc_task *task,
778 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400779{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500780 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500781 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400782 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100783 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500784 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400785
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500786 if (slot == NULL)
787 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400788 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500789 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400790 goto out;
791
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500792 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500793
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400794 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100795
796 status = res->sr_status;
797 if (task->tk_status == -NFS4ERR_DEADSESSION)
798 status = -NFS4ERR_DEADSESSION;
799
Andy Adamson691daf32009-12-04 15:55:39 -0500800 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100801 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400802 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400803 /* Mark this sequence number as having been acked */
804 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400805 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400806 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500807 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500808 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500809 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400810 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
811 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500812 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400813 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500814 case 1:
815 /*
816 * sr_status remains 1 if an RPC level error occurred.
817 * The server may or may not have processed the sequence
818 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500819 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400820 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
821 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500822 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 case -NFS4ERR_DELAY:
824 /* The server detected a resend of the RPC call and
825 * returned NFS4ERR_DELAY as per Section 2.10.6.2
826 * of RFC5661.
827 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500828 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400829 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500830 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500831 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400832 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400833 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400834 case -NFS4ERR_RETRY_UNCACHED_REP:
835 case -NFS4ERR_SEQ_FALSE_RETRY:
836 /*
837 * The server thinks we tried to replay a request.
838 * Retry the call after bumping the sequence ID.
839 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400840 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400841 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500842 case -NFS4ERR_BADSLOT:
843 /*
844 * The slot id we used was probably retired. Try again
845 * using a different slot id.
846 */
Trond Myklebust99589102018-06-09 12:50:50 -0400847 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400848 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500849 goto retry_nowait;
850 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400851 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500852 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400853 * Were one or more calls using this slot interrupted?
854 * If the server never received the request, then our
855 * transmitted slot sequence number may be too high.
Trond Myklebustac20d162012-12-15 15:36:07 -0500856 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400857 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
858 slot->seq_nr--;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500859 goto retry_nowait;
860 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400861 /*
862 * RFC5661:
863 * A retry might be sent while the original request is
864 * still in progress on the replier. The replier SHOULD
865 * deal with the issue by returning NFS4ERR_DELAY as the
866 * reply to SEQUENCE or CB_SEQUENCE operation, but
867 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
868 *
869 * Restart the search after a delay.
870 */
871 slot->seq_nr = slot->seq_nr_highest_sent;
872 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100873 case -NFS4ERR_BADSESSION:
874 case -NFS4ERR_DEADSESSION:
875 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
876 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400877 default:
878 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400879 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400880 }
881out:
882 /* The session may be reset by one of the error handlers. */
883 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500884out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500885 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400886session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100887 nfs4_schedule_session_recovery(session, status);
888 dprintk("%s ERROR: %d Reset session\n", __func__, status);
889 nfs41_sequence_free_slot(res);
890 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400891retry_new_seq:
892 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500893retry_nowait:
894 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400895 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500896 task->tk_status = 0;
897 ret = 0;
898 }
899 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400900out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400901 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400902 goto out;
903 rpc_delay(task, NFS4_POLL_RETRY_MAX);
904 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400905}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400906
907int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
908{
909 if (!nfs41_sequence_process(task, res))
910 return 0;
911 if (res->sr_slot != NULL)
912 nfs41_sequence_free_slot(res);
913 return 1;
914
915}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500916EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400917
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400918static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
919{
920 if (res->sr_slot == NULL)
921 return 1;
922 if (res->sr_slot->table->session != NULL)
923 return nfs41_sequence_process(task, res);
924 return nfs40_sequence_done(task, res);
925}
926
927static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
928{
929 if (res->sr_slot != NULL) {
930 if (res->sr_slot->table->session != NULL)
931 nfs41_sequence_free_slot(res);
932 else
933 nfs40_sequence_free_slot(res);
934 }
935}
936
Peng Tao2c4b1312014-06-11 05:24:16 +0800937int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400938{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500939 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400940 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400941 if (!res->sr_slot->table->session)
942 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400943 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400944}
Peng Tao2c4b1312014-06-11 05:24:16 +0800945EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400946
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
948{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400949 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950
Trond Myklebust035168ab2010-06-16 09:52:26 -0400951 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
952
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500953 nfs4_setup_sequence(data->seq_server->nfs_client,
954 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955}
956
Andy Adamson69ab40c2009-04-01 09:22:19 -0400957static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
958{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400959 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400960
Trond Myklebust14516c32010-07-31 14:29:06 -0400961 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400962}
963
Trond Myklebust17280172012-03-11 13:11:00 -0400964static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400965 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400966 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400967};
968
Chuck Lever3bd23842013-08-09 12:49:19 -0400969#else /* !CONFIG_NFS_V4_1 */
970
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400971static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
972{
973 return nfs40_sequence_done(task, res);
974}
975
976static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
977{
978 if (res->sr_slot != NULL)
979 nfs40_sequence_free_slot(res);
980}
981
Peng Tao2c4b1312014-06-11 05:24:16 +0800982int nfs4_sequence_done(struct rpc_task *task,
983 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400984{
Chuck Lever3bd23842013-08-09 12:49:19 -0400985 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400986}
Peng Tao2c4b1312014-06-11 05:24:16 +0800987EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400988
989#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400990
Trond Myklebustc1dffe02019-03-01 12:13:34 -0500991static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
992{
993 res->sr_timestamp = jiffies;
994 res->sr_status_flags = 0;
995 res->sr_status = 1;
996}
997
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400998static
999void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1000 struct nfs4_sequence_res *res,
1001 struct nfs4_slot *slot)
1002{
1003 if (!slot)
1004 return;
1005 slot->privileged = args->sa_privileged ? 1 : 0;
1006 args->sa_slot = slot;
1007
1008 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001009}
1010
1011int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001012 struct nfs4_sequence_args *args,
1013 struct nfs4_sequence_res *res,
1014 struct rpc_task *task)
1015{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001016 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001017 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001018 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001019
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001020 /* slot already allocated? */
1021 if (res->sr_slot != NULL)
1022 goto out_start;
1023
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001024 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001025 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001026
Trond Myklebust3453d572018-06-20 17:53:34 -04001027 spin_lock(&tbl->slot_tbl_lock);
1028 /* The state manager will wait until the slot table is empty */
1029 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1030 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001031
Trond Myklebust3453d572018-06-20 17:53:34 -04001032 slot = nfs4_alloc_slot(tbl);
1033 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001034 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001035 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001036 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001037 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001038 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001039
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001040 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001041
Anna Schumakerad05cc02017-01-11 13:37:06 -05001042 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001043out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001044 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001045 rpc_call_start(task);
1046 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001047out_sleep_timeout:
1048 /* Try again in 1/4 second */
1049 if (args->sa_privileged)
1050 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1051 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1052 else
1053 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1054 NULL, jiffies + (HZ >> 2));
1055 spin_unlock(&tbl->slot_tbl_lock);
1056 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001057out_sleep:
1058 if (args->sa_privileged)
1059 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001060 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001061 else
1062 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1063 spin_unlock(&tbl->slot_tbl_lock);
1064 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001065}
1066EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1067
Chuck Lever9915ea72013-08-09 12:48:27 -04001068static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1069{
1070 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001071 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001072 data->seq_args, data->seq_res, task);
1073}
1074
1075static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1076{
1077 struct nfs4_call_sync_data *data = calldata;
1078 nfs4_sequence_done(task, data->seq_res);
1079}
1080
1081static const struct rpc_call_ops nfs40_call_sync_ops = {
1082 .rpc_call_prepare = nfs40_call_sync_prepare,
1083 .rpc_call_done = nfs40_call_sync_done,
1084};
1085
Anna Schumaker48c05852019-08-14 15:27:00 -04001086static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1087{
1088 int ret;
1089 struct rpc_task *task;
1090
1091 task = rpc_run_task(task_setup);
1092 if (IS_ERR(task))
1093 return PTR_ERR(task);
1094
1095 ret = task->tk_status;
1096 rpc_put_task(task);
1097 return ret;
1098}
1099
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001100static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1101 struct nfs_server *server,
1102 struct rpc_message *msg,
1103 struct nfs4_sequence_args *args,
1104 struct nfs4_sequence_res *res,
1105 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001106{
Chuck Lever9915ea72013-08-09 12:48:27 -04001107 struct nfs_client *clp = server->nfs_client;
1108 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001109 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001110 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001111 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112 };
1113 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001114 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001115 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001116 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001117 .callback_data = &data,
1118 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001119 };
1120
Anna Schumaker48c05852019-08-14 15:27:00 -04001121 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001122}
1123
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001124static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1125 struct nfs_server *server,
1126 struct rpc_message *msg,
1127 struct nfs4_sequence_args *args,
1128 struct nfs4_sequence_res *res)
1129{
1130 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1131}
1132
1133
Bryan Schumaker7c513052011-03-24 17:12:24 +00001134int nfs4_call_sync(struct rpc_clnt *clnt,
1135 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001136 struct rpc_message *msg,
1137 struct nfs4_sequence_args *args,
1138 struct nfs4_sequence_res *res,
1139 int cache_reply)
1140{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001141 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001142 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Trond Myklebust3c591172018-07-31 15:54:10 -04001145static void
1146nfs4_inc_nlink_locked(struct inode *inode)
1147{
1148 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1149 inc_nlink(inode);
1150}
1151
1152static void
1153nfs4_dec_nlink_locked(struct inode *inode)
1154{
1155 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1156 drop_nlink(inode);
1157}
1158
1159static void
1160update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001161 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 struct nfs_inode *nfsi = NFS_I(dir);
1164
Trond Myklebust16e14372018-03-20 16:53:31 -04001165 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1166 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001167 | NFS_INO_INVALID_DATA
1168 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001169 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001170 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1171 nfsi->attrtimeo_timestamp = jiffies;
1172 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001174 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001175 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1176 NFS_INO_INVALID_ACL;
1177 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001178 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001179 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001180 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001181 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001182 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001183}
1184
1185static void
1186update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001187 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001188{
1189 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001190 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 spin_unlock(&dir->i_lock);
1192}
1193
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001194struct nfs4_open_createattrs {
1195 struct nfs4_label *label;
1196 struct iattr *sattr;
1197 const __u32 verf[2];
1198};
1199
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001200static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1201 int err, struct nfs4_exception *exception)
1202{
1203 if (err != -EINVAL)
1204 return false;
1205 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1206 return false;
1207 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1208 exception->retry = 1;
1209 return true;
1210}
1211
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001212static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1213{
1214 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1215}
1216
1217static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1218{
1219 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1220
1221 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1222}
1223
Trond Myklebust6ae37332015-01-30 14:21:14 -05001224static u32
1225nfs4_map_atomic_open_share(struct nfs_server *server,
1226 fmode_t fmode, int openflags)
1227{
1228 u32 res = 0;
1229
1230 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1231 case FMODE_READ:
1232 res = NFS4_SHARE_ACCESS_READ;
1233 break;
1234 case FMODE_WRITE:
1235 res = NFS4_SHARE_ACCESS_WRITE;
1236 break;
1237 case FMODE_READ|FMODE_WRITE:
1238 res = NFS4_SHARE_ACCESS_BOTH;
1239 }
1240 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1241 goto out;
1242 /* Want no delegation if we're using O_DIRECT */
1243 if (openflags & O_DIRECT)
1244 res |= NFS4_SHARE_WANT_NO_DELEG;
1245out:
1246 return res;
1247}
1248
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001249static enum open_claim_type4
1250nfs4_map_atomic_open_claim(struct nfs_server *server,
1251 enum open_claim_type4 claim)
1252{
1253 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1254 return claim;
1255 switch (claim) {
1256 default:
1257 return claim;
1258 case NFS4_OPEN_CLAIM_FH:
1259 return NFS4_OPEN_CLAIM_NULL;
1260 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1261 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1262 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1263 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1264 }
1265}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268{
1269 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001270 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 p->o_res.seqid = p->o_arg.seqid;
1272 p->c_res.seqid = p->c_arg.seqid;
1273 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001274 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001275 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001276 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001277}
1278
Al Viro82a2c1b2011-06-22 18:30:55 -04001279static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001280 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001281 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001282 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001283 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001284{
Al Viro82a2c1b2011-06-22 18:30:55 -04001285 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001286 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001287 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001288 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001289 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290 struct nfs4_opendata *p;
1291
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001292 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001293 if (p == NULL)
1294 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001295
1296 p->f_label = nfs4_label_alloc(server, gfp_mask);
1297 if (IS_ERR(p->f_label))
1298 goto err_free_p;
1299
Kinglong Meea49c2692015-07-27 15:31:38 +08001300 p->a_label = nfs4_label_alloc(server, gfp_mask);
1301 if (IS_ERR(p->a_label))
1302 goto err_free_f;
1303
Trond Myklebust63f5f792015-01-23 19:19:25 -05001304 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1305 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001306 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001307 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001308 nfs_sb_active(dentry->d_sb);
1309 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001310 p->dir = parent;
1311 p->owner = sp;
1312 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001313 p->o_arg.open_flags = flags;
1314 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001315 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001316 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1317 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001318 if (flags & O_CREAT) {
1319 p->o_arg.umask = current_umask();
1320 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1321 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1322 p->o_arg.u.attrs = &p->attrs;
1323 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1324
1325 memcpy(p->o_arg.u.verifier.data, c->verf,
1326 sizeof(p->o_arg.u.verifier.data));
1327 }
1328 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001329 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1330 * will return permission denied for all bits until close */
1331 if (!(flags & O_EXCL)) {
1332 /* ask server to check for all possible rights as results
1333 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001334 switch (p->o_arg.claim) {
1335 default:
1336 break;
1337 case NFS4_OPEN_CLAIM_NULL:
1338 case NFS4_OPEN_CLAIM_FH:
1339 p->o_arg.access = NFS4_ACCESS_READ |
1340 NFS4_ACCESS_MODIFY |
1341 NFS4_ACCESS_EXTEND |
1342 NFS4_ACCESS_EXECUTE;
1343 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001344 }
David Howells7539bba2006-08-22 20:06:09 -04001345 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001346 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1347 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001348 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001349 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001350 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001351 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001352 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001353 case NFS4_OPEN_CLAIM_NULL:
1354 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1355 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1356 p->o_arg.fh = NFS_FH(dir);
1357 break;
1358 case NFS4_OPEN_CLAIM_PREVIOUS:
1359 case NFS4_OPEN_CLAIM_FH:
1360 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1361 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001362 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001363 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001364 p->c_arg.fh = &p->o_res.fh;
1365 p->c_arg.stateid = &p->o_res.stateid;
1366 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001367 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001368 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001369 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001370
1371err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001372 nfs4_label_free(p->a_label);
1373err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001374 nfs4_label_free(p->f_label);
1375err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001376 kfree(p);
1377err:
1378 dput(parent);
1379 return NULL;
1380}
1381
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001382static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001383{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001384 struct nfs4_opendata *p = container_of(kref,
1385 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001386 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001387
Fred Isaman30ae2412016-10-18 13:39:51 -04001388 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001389 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001390 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001391 if (p->state != NULL)
1392 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001393 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001394
Kinglong Meea49c2692015-07-27 15:31:38 +08001395 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001396 nfs4_label_free(p->f_label);
1397
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001398 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001399 dput(p->dentry);
1400 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001401 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001402 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001403 kfree(p);
1404}
1405
1406static void nfs4_opendata_put(struct nfs4_opendata *p)
1407{
1408 if (p != NULL)
1409 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001410}
1411
Trond Myklebust24311f82015-09-20 10:50:17 -04001412static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1413 fmode_t fmode)
1414{
1415 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1416 case FMODE_READ|FMODE_WRITE:
1417 return state->n_rdwr != 0;
1418 case FMODE_WRITE:
1419 return state->n_wronly != 0;
1420 case FMODE_READ:
1421 return state->n_rdonly != 0;
1422 }
1423 WARN_ON_ONCE(1);
1424 return false;
1425}
1426
Trond Myklebustbe189f72018-09-27 17:12:33 -04001427static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1428 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001429{
1430 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001431
Trond Myklebust536e43d2012-01-17 22:04:26 -05001432 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001433 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001434 switch (claim) {
1435 case NFS4_OPEN_CLAIM_NULL:
1436 case NFS4_OPEN_CLAIM_FH:
1437 goto out;
1438 default:
1439 break;
1440 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001442 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001443 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1444 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001445 break;
1446 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001447 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1448 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001449 break;
1450 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001451 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1452 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001453 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001454out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001455 return ret;
1456}
1457
Trond Myklebust2a606182015-08-19 22:30:00 -05001458static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1459 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001460{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001461 if (delegation == NULL)
1462 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001463 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001464 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001465 switch (claim) {
1466 case NFS4_OPEN_CLAIM_NULL:
1467 case NFS4_OPEN_CLAIM_FH:
1468 break;
1469 case NFS4_OPEN_CLAIM_PREVIOUS:
1470 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1471 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001472 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001473 default:
1474 return 0;
1475 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001476 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001477 return 1;
1478}
1479
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001480static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001481{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001482 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001483 case FMODE_WRITE:
1484 state->n_wronly++;
1485 break;
1486 case FMODE_READ:
1487 state->n_rdonly++;
1488 break;
1489 case FMODE_READ|FMODE_WRITE:
1490 state->n_rdwr++;
1491 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001492 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001493}
1494
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001495#ifdef CONFIG_NFS_V4_1
1496static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1497{
1498 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1499 return true;
1500 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1501 return true;
1502 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1503 return true;
1504 return false;
1505}
1506#endif /* CONFIG_NFS_V4_1 */
1507
Trond Myklebustc9399f22017-11-06 15:28:01 -05001508static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1509{
1510 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1511 wake_up_all(&state->waitq);
1512}
1513
1514static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1515 const nfs4_stateid *stateid)
1516{
1517 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1518 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1519
1520 if (stateid_seqid == state_seqid + 1U ||
1521 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1522 nfs_state_log_update_open_stateid(state);
1523 else
1524 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1525}
1526
Trond Myklebust4f14c192014-02-12 19:15:06 -05001527static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001528{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001529 struct nfs_client *clp = state->owner->so_server->nfs_client;
1530 bool need_recover = false;
1531
1532 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1533 need_recover = true;
1534 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1535 need_recover = true;
1536 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1537 need_recover = true;
1538 if (need_recover)
1539 nfs4_state_mark_reclaim_nograce(clp, state);
1540}
1541
Trond Myklebustc9399f22017-11-06 15:28:01 -05001542/*
1543 * Check for whether or not the caller may update the open stateid
1544 * to the value passed in by stateid.
1545 *
1546 * Note: This function relies heavily on the server implementing
1547 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1548 * correctly.
1549 * i.e. The stateid seqids have to be initialised to 1, and
1550 * are then incremented on every state transition.
1551 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001552static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001553 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001554{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001555 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1556 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1557 if (stateid->seqid == cpu_to_be32(1))
1558 nfs_state_log_update_open_stateid(state);
1559 else
1560 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001561 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001562 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001563
1564 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1565 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001566 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001567 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001568 return false;
1569}
1570
Trond Myklebustf95549c2015-01-23 18:06:09 -05001571static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1572{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001573 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1574 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001575 if (state->n_wronly)
1576 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1577 if (state->n_rdonly)
1578 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1579 if (state->n_rdwr)
1580 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001581 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001582}
1583
Trond Myklebust226056c2014-02-11 10:41:07 -05001584static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1585 nfs4_stateid *stateid, fmode_t fmode)
1586{
1587 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1588 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1589 case FMODE_WRITE:
1590 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1591 break;
1592 case FMODE_READ:
1593 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1594 break;
1595 case 0:
1596 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1597 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1598 clear_bit(NFS_OPEN_STATE, &state->flags);
1599 }
1600 if (stateid == NULL)
1601 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001602 /* Handle OPEN+OPEN_DOWNGRADE races */
1603 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1604 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001605 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001606 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001607 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001608 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001609 nfs4_stateid_copy(&state->stateid, stateid);
1610 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001611 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001612out:
1613 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001614}
1615
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001616static void nfs_clear_open_stateid(struct nfs4_state *state,
1617 nfs4_stateid *arg_stateid,
1618 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001619{
1620 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001621 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1622 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1623 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001624 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001625 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1626 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001627}
1628
Trond Myklebust1393d962016-09-22 13:39:13 -04001629static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001630 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001631 __must_hold(&state->owner->so_lock)
1632 __must_hold(&state->seqlock)
1633 __must_hold(RCU)
1634
Trond Myklebust003707c2007-07-05 18:07:55 -04001635{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001636 DEFINE_WAIT(wait);
1637 int status = 0;
1638 for (;;) {
1639
1640 if (!nfs_need_update_open_stateid(state, stateid))
1641 return;
1642 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001643 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001644 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001645 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001646 /* Rely on seqids for serialisation with NFSv4.0 */
1647 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1648 break;
1649
1650 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1651 /*
1652 * Ensure we process the state changes in the same order
1653 * in which the server processed them by delaying the
1654 * update of the stateid until we are in sequence.
1655 */
1656 write_sequnlock(&state->seqlock);
1657 spin_unlock(&state->owner->so_lock);
1658 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001659 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001660 if (!signal_pending(current)) {
1661 if (schedule_timeout(5*HZ) == 0)
1662 status = -EAGAIN;
1663 else
1664 status = 0;
1665 } else
1666 status = -EINTR;
1667 finish_wait(&state->waitq, &wait);
1668 rcu_read_lock();
1669 spin_lock(&state->owner->so_lock);
1670 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001671 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001672
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001673 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1674 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001675 nfs4_stateid_copy(freeme, &state->open_stateid);
1676 nfs_test_and_clear_all_open_stateid(state);
1677 }
1678
Trond Myklebuste999e802014-02-10 18:20:47 -05001679 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1680 nfs4_stateid_copy(&state->stateid, stateid);
1681 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001682 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001683 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001684}
1685
Trond Myklebustc9399f22017-11-06 15:28:01 -05001686static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001687 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001688 fmode_t fmode,
1689 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001691 /*
1692 * Protect the call to nfs4_state_set_mode_locked and
1693 * serialise the stateid update
1694 */
1695 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001696 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1697 switch (fmode) {
1698 case FMODE_READ:
1699 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1700 break;
1701 case FMODE_WRITE:
1702 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1703 break;
1704 case FMODE_READ|FMODE_WRITE:
1705 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001706 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001707 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001708 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001709}
1710
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001711static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1712{
1713 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1714 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1715 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1716 clear_bit(NFS_OPEN_STATE, &state->flags);
1717}
1718
Trond Myklebustc9399f22017-11-06 15:28:01 -05001719static void nfs_state_set_delegation(struct nfs4_state *state,
1720 const nfs4_stateid *deleg_stateid,
1721 fmode_t fmode)
1722{
1723 /*
1724 * Protect the call to nfs4_state_set_mode_locked and
1725 * serialise the stateid update
1726 */
1727 write_seqlock(&state->seqlock);
1728 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1729 set_bit(NFS_DELEGATED_STATE, &state->flags);
1730 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001733static void nfs_state_clear_delegation(struct nfs4_state *state)
1734{
1735 write_seqlock(&state->seqlock);
1736 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1737 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1738 write_sequnlock(&state->seqlock);
1739}
1740
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001741int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001742 const nfs4_stateid *open_stateid,
1743 const nfs4_stateid *delegation,
1744 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001745{
Trond Myklebust1393d962016-09-22 13:39:13 -04001746 struct nfs_server *server = NFS_SERVER(state->inode);
1747 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001748 struct nfs_inode *nfsi = NFS_I(state->inode);
1749 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001750 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001751 int ret = 0;
1752
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001753 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001754
1755 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001756 spin_lock(&state->owner->so_lock);
1757 if (open_stateid != NULL) {
1758 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1759 ret = 1;
1760 }
1761
Trond Myklebust333ac782019-10-22 12:12:17 -04001762 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001763 if (deleg_cur == NULL)
1764 goto no_delegation;
1765
1766 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001767 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001768 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001769 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001770 goto no_delegation_unlock;
1771
1772 if (delegation == NULL)
1773 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001774 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001775 goto no_delegation_unlock;
1776
Trond Myklebustb7391f42008-12-23 15:21:52 -05001777 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001778 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001779 ret = 1;
1780no_delegation_unlock:
1781 spin_unlock(&deleg_cur->lock);
1782no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001783 if (ret)
1784 update_open_stateflags(state, fmode);
1785 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001786 rcu_read_unlock();
1787
Trond Myklebust4f14c192014-02-12 19:15:06 -05001788 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001789 nfs4_schedule_state_manager(clp);
1790 if (freeme.type != 0)
1791 nfs4_test_and_free_stateid(server, &freeme,
1792 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001793
1794 return ret;
1795}
1796
Trond Myklebust39071e62015-01-24 15:07:56 -05001797static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1798 const nfs4_stateid *stateid)
1799{
1800 struct nfs4_state *state = lsp->ls_state;
1801 bool ret = false;
1802
1803 spin_lock(&state->state_lock);
1804 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1805 goto out_noupdate;
1806 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1807 goto out_noupdate;
1808 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1809 ret = true;
1810out_noupdate:
1811 spin_unlock(&state->state_lock);
1812 return ret;
1813}
Trond Myklebust34310432008-12-23 15:21:38 -05001814
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001815static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001816{
1817 struct nfs_delegation *delegation;
1818
Trond Myklebustf5086242018-03-20 16:43:13 -04001819 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001820 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001821 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001822 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001823 rcu_read_unlock();
1824 return;
1825 }
1826 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001827 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001828}
1829
Trond Myklebust6ee41262007-07-08 14:11:36 -04001830static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001831{
1832 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001833 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001834 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001835 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001836 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001837 nfs4_stateid stateid;
1838 int ret = -EAGAIN;
1839
Trond Myklebustaac00a82007-07-05 19:02:21 -04001840 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001841 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001842 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001843 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001844 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001845 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001846 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001847 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001848 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001849 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001850 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001851 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001852 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001853 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001854 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001855 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001857 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001858 if (!opendata->is_recover) {
1859 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1860 if (ret != 0)
1861 goto out;
1862 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001863 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001864
1865 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001866 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001867 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001868 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001869out:
1870 return ERR_PTR(ret);
1871out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001872 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001873 return state;
1874}
1875
Andy Adamsone23008e2012-10-02 21:07:32 -04001876static void
1877nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1878{
1879 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1880 struct nfs_delegation *delegation;
1881 int delegation_flags = 0;
1882
1883 rcu_read_lock();
1884 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1885 if (delegation)
1886 delegation_flags = delegation->flags;
1887 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001888 switch (data->o_arg.claim) {
1889 default:
1890 break;
1891 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1892 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001893 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1894 "returning a delegation for "
1895 "OPEN(CLAIM_DELEGATE_CUR)\n",
1896 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001897 return;
1898 }
1899 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001900 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001901 data->owner->so_cred,
1902 data->o_res.delegation_type,
1903 &data->o_res.delegation,
1904 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001905 else
1906 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001907 data->owner->so_cred,
1908 data->o_res.delegation_type,
1909 &data->o_res.delegation,
1910 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001911
1912 if (data->o_res.do_recall)
1913 nfs_async_inode_return_delegation(state->inode,
1914 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001915}
1916
1917/*
1918 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1919 * and update the nfs4_state.
1920 */
1921static struct nfs4_state *
1922_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1923{
1924 struct inode *inode = data->state->inode;
1925 struct nfs4_state *state = data->state;
1926 int ret;
1927
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001928 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001929 if (data->rpc_status)
1930 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001931 /* cached opens have already been processed */
1932 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001933 }
1934
Andy Adamsone23008e2012-10-02 21:07:32 -04001935 ret = nfs_refresh_inode(inode, &data->f_attr);
1936 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001937 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001938
1939 if (data->o_res.delegation_type != 0)
1940 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001941update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001942 if (!update_open_stateid(state, &data->o_res.stateid,
1943 NULL, data->o_arg.fmode))
1944 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001945 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001946
1947 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001948}
1949
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001950static struct inode *
1951nfs4_opendata_get_inode(struct nfs4_opendata *data)
1952{
1953 struct inode *inode;
1954
1955 switch (data->o_arg.claim) {
1956 case NFS4_OPEN_CLAIM_NULL:
1957 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1958 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1959 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1960 return ERR_PTR(-EAGAIN);
1961 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1962 &data->f_attr, data->f_label);
1963 break;
1964 default:
1965 inode = d_inode(data->dentry);
1966 ihold(inode);
1967 nfs_refresh_inode(inode, &data->f_attr);
1968 }
1969 return inode;
1970}
1971
Andy Adamsone23008e2012-10-02 21:07:32 -04001972static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001973nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1974{
1975 struct nfs4_state *state;
1976 struct inode *inode;
1977
1978 inode = nfs4_opendata_get_inode(data);
1979 if (IS_ERR(inode))
1980 return ERR_CAST(inode);
1981 if (data->state != NULL && data->state->inode == inode) {
1982 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001983 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04001984 } else
1985 state = nfs4_get_open_state(inode, data->owner);
1986 iput(inode);
1987 if (state == NULL)
1988 state = ERR_PTR(-ENOMEM);
1989 return state;
1990}
1991
Andy Adamsone23008e2012-10-02 21:07:32 -04001992static struct nfs4_state *
1993_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001994{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001995 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001996
Trond Myklebustaac00a82007-07-05 19:02:21 -04001997 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001998 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001999 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002000 goto out;
2001 }
2002
Trond Myklebust75e8c482017-08-08 10:38:07 -04002003 state = nfs4_opendata_find_nfs4_state(data);
2004 if (IS_ERR(state))
2005 goto out;
2006
Andy Adamsone23008e2012-10-02 21:07:32 -04002007 if (data->o_res.delegation_type != 0)
2008 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002009 if (!update_open_stateid(state, &data->o_res.stateid,
2010 NULL, data->o_arg.fmode)) {
2011 nfs4_put_open_state(state);
2012 state = ERR_PTR(-EAGAIN);
2013 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002014out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002015 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002016 return state;
2017}
2018
Andy Adamsone23008e2012-10-02 21:07:32 -04002019static struct nfs4_state *
2020nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2021{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002022 struct nfs4_state *ret;
2023
Andy Adamsone23008e2012-10-02 21:07:32 -04002024 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002025 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2026 else
2027 ret = _nfs4_opendata_to_nfs4_state(data);
2028 nfs4_sequence_free_slot(&data->o_res.seq_res);
2029 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002030}
2031
Trond Myklebust0de43972018-09-02 15:57:01 -04002032static struct nfs_open_context *
2033nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002034{
2035 struct nfs_inode *nfsi = NFS_I(state->inode);
2036 struct nfs_open_context *ctx;
2037
Trond Myklebust0de43972018-09-02 15:57:01 -04002038 rcu_read_lock();
2039 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002040 if (ctx->state != state)
2041 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002042 if ((ctx->mode & mode) != mode)
2043 continue;
2044 if (!get_nfs_open_context(ctx))
2045 continue;
2046 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002047 return ctx;
2048 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002049 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002050 return ERR_PTR(-ENOENT);
2051}
2052
Trond Myklebust0de43972018-09-02 15:57:01 -04002053static struct nfs_open_context *
2054nfs4_state_find_open_context(struct nfs4_state *state)
2055{
2056 struct nfs_open_context *ctx;
2057
2058 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2059 if (!IS_ERR(ctx))
2060 return ctx;
2061 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2062 if (!IS_ERR(ctx))
2063 return ctx;
2064 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2065}
2066
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002067static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2068 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002069{
2070 struct nfs4_opendata *opendata;
2071
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002072 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002073 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002074 if (opendata == NULL)
2075 return ERR_PTR(-ENOMEM);
2076 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002077 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002078 return opendata;
2079}
2080
Trond Myklebust24311f82015-09-20 10:50:17 -04002081static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2082 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002083{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002084 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002085 int ret;
2086
Trond Myklebust24311f82015-09-20 10:50:17 -04002087 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002088 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002089 opendata->o_arg.open_flags = 0;
2090 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002091 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2092 NFS_SB(opendata->dentry->d_sb),
2093 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002094 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2095 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2096 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002097 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002098 if (ret != 0)
2099 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002100 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002101 if (IS_ERR(newstate))
2102 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002103 if (newstate != opendata->state)
2104 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002105 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002106 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002107}
2108
2109static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2110{
Trond Myklebust864472e2006-01-03 09:55:15 +01002111 int ret;
2112
2113 /* memory barrier prior to reading state->n_* */
2114 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002115 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2116 if (ret != 0)
2117 return ret;
2118 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2119 if (ret != 0)
2120 return ret;
2121 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2122 if (ret != 0)
2123 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002124 /*
2125 * We may have performed cached opens for all three recoveries.
2126 * Check if we need to update the current stateid.
2127 */
2128 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002129 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002130 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002131 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002132 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002133 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002134 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002135 return 0;
2136}
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138/*
2139 * OPEN_RECLAIM:
2140 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002142static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002144 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002145 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002146 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int status;
2148
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002149 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2150 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002151 if (IS_ERR(opendata))
2152 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002153 rcu_read_lock();
2154 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002155 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002156 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002157 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002158 opendata->o_arg.u.delegation_type = delegation_type;
2159 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002160 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return status;
2162}
2163
Trond Myklebust539cd032007-06-05 11:46:42 -04002164static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 struct nfs_server *server = NFS_SERVER(state->inode);
2167 struct nfs4_exception exception = { };
2168 int err;
2169 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002170 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002171 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002172 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2173 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002174 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002175 break;
2176 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 } while (exception.retry);
2178 return err;
2179}
2180
Trond Myklebust864472e2006-01-03 09:55:15 +01002181static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2182{
2183 struct nfs_open_context *ctx;
2184 int ret;
2185
2186 ctx = nfs4_state_find_open_context(state);
2187 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002188 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002189 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2190 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002191 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002192 put_nfs_open_context(ctx);
2193 return ret;
2194}
2195
NeilBrowndce26302017-12-13 09:57:09 +11002196static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002198 switch (err) {
2199 default:
2200 printk(KERN_ERR "NFS: %s: unhandled error "
2201 "%d.\n", __func__, err);
2202 case 0:
2203 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002204 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002205 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002206 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002207 break;
2208 case -NFS4ERR_BADSESSION:
2209 case -NFS4ERR_BADSLOT:
2210 case -NFS4ERR_BAD_HIGH_SLOT:
2211 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2212 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002213 return -EAGAIN;
2214 case -NFS4ERR_STALE_CLIENTID:
2215 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002216 /* Don't recall a delegation if it was lost */
2217 nfs4_schedule_lease_recovery(server->nfs_client);
2218 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002219 case -NFS4ERR_MOVED:
2220 nfs4_schedule_migration_recovery(server);
2221 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002222 case -NFS4ERR_LEASE_MOVED:
2223 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2224 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002225 case -NFS4ERR_DELEG_REVOKED:
2226 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002227 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002228 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002229 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002230 nfs_inode_find_state_and_recover(state->inode,
2231 stateid);
2232 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002233 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002234 case -NFS4ERR_DELAY:
2235 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002236 ssleep(1);
2237 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002238 case -ENOMEM:
2239 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002240 if (fl) {
2241 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2242 if (lsp)
2243 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2244 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002245 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return err;
2248}
2249
Trond Myklebust24311f82015-09-20 10:50:17 -04002250int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002251 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002252{
2253 struct nfs_server *server = NFS_SERVER(state->inode);
2254 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002255 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002256
2257 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2258 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2259 if (IS_ERR(opendata))
2260 return PTR_ERR(opendata);
2261 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002262 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002263 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2264 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002265 goto out;
2266 }
2267 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002268 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2269 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002270 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002271 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002272 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2273 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2274 if (err)
2275 goto out;
2276 }
2277 nfs_state_clear_delegation(state);
2278out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002279 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002280 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002281}
2282
Chuck Leverbe05c862013-08-09 12:49:47 -04002283static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2284{
2285 struct nfs4_opendata *data = calldata;
2286
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002287 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2288 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002289}
2290
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002291static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2292{
2293 struct nfs4_opendata *data = calldata;
2294
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002295 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002296
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002297 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002298 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002299 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002300 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002301 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002302 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002303 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002304}
2305
2306static void nfs4_open_confirm_release(void *calldata)
2307{
2308 struct nfs4_opendata *data = calldata;
2309 struct nfs4_state *state = NULL;
2310
2311 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002312 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002313 goto out_free;
2314 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002315 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002316 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002317 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002318 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002319 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002320out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002321 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002322}
2323
2324static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002325 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002326 .rpc_call_done = nfs4_open_confirm_done,
2327 .rpc_release = nfs4_open_confirm_release,
2328};
2329
2330/*
2331 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2332 */
2333static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2334{
David Howells2b0143b2015-03-17 22:25:59 +00002335 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002336 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002337 struct rpc_message msg = {
2338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2339 .rpc_argp = &data->c_arg,
2340 .rpc_resp = &data->c_res,
2341 .rpc_cred = data->owner->so_cred,
2342 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002343 struct rpc_task_setup task_setup_data = {
2344 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002345 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002346 .callback_ops = &nfs4_open_confirm_ops,
2347 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002348 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002349 .flags = RPC_TASK_ASYNC,
2350 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int status;
2352
Anna Schumakerfba83f32018-05-04 16:22:50 -04002353 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2354 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002355 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002356 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002357 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002358 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002359 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002360 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002361 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002362 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002363 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002364 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002365 smp_wmb();
2366 } else
2367 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002368 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return status;
2370}
2371
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002372static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002374 struct nfs4_opendata *data = calldata;
2375 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002376 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002377 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002378
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002379 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002380 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002381 /*
2382 * Check if we still need to send an OPEN call, or if we can use
2383 * a delegation instead.
2384 */
2385 if (data->state != NULL) {
2386 struct nfs_delegation *delegation;
2387
Trond Myklebustbe189f72018-09-27 17:12:33 -04002388 if (can_open_cached(data->state, data->o_arg.fmode,
2389 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002390 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002391 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002392 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002393 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002394 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002395 rcu_read_unlock();
2396 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002397 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002398 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002399 switch (claim) {
2400 default:
2401 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002402 case NFS4_OPEN_CLAIM_PREVIOUS:
2403 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2404 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002405 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002406 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002407 case NFS4_OPEN_CLAIM_FH:
2408 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002409 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002410 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002411 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002412 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002413 &data->o_res.seq_res,
2414 task) != 0)
2415 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002416
2417 /* Set the create mode (note dependency on the session type) */
2418 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2419 if (data->o_arg.open_flags & O_EXCL) {
2420 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2421 if (nfs4_has_persistent_session(clp))
2422 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2423 else if (clp->cl_mvops->minor_version > 0)
2424 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2425 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002426 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002427unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002428 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002429 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002430out_no_action:
2431 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002432out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002433 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002434}
2435
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002436static void nfs4_open_done(struct rpc_task *task, void *calldata)
2437{
2438 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002440 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002441
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002442 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002443 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002444
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002445 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002446 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2447 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002448 case S_IFREG:
2449 break;
2450 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002451 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002452 break;
2453 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002454 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002455 break;
2456 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002457 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002458 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002459 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002460 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002461 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2462 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002463 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002464 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002465}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002466
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002467static void nfs4_open_release(void *calldata)
2468{
2469 struct nfs4_opendata *data = calldata;
2470 struct nfs4_state *state = NULL;
2471
2472 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002473 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002474 goto out_free;
2475 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002476 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002477 goto out_free;
2478 /* In case we need an open_confirm, no cleanup! */
2479 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2480 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002481 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002482 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002483 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002484out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002485 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002486}
2487
2488static const struct rpc_call_ops nfs4_open_ops = {
2489 .rpc_call_prepare = nfs4_open_prepare,
2490 .rpc_call_done = nfs4_open_done,
2491 .rpc_release = nfs4_open_release,
2492};
2493
Fred Isaman3b65a302016-09-19 10:06:49 -04002494static int nfs4_run_open_task(struct nfs4_opendata *data,
2495 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002496{
David Howells2b0143b2015-03-17 22:25:59 +00002497 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002498 struct nfs_server *server = NFS_SERVER(dir);
2499 struct nfs_openargs *o_arg = &data->o_arg;
2500 struct nfs_openres *o_res = &data->o_res;
2501 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002502 struct rpc_message msg = {
2503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2504 .rpc_argp = o_arg,
2505 .rpc_resp = o_res,
2506 .rpc_cred = data->owner->so_cred,
2507 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002508 struct rpc_task_setup task_setup_data = {
2509 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002510 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002511 .callback_ops = &nfs4_open_ops,
2512 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002513 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002514 .flags = RPC_TASK_ASYNC,
2515 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002516 int status;
2517
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002518 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002519 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002520 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002521 data->cancelled = false;
2522 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002523 if (!ctx) {
2524 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002525 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002526 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002527 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002528 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002529 pnfs_lgopen_prepare(data, ctx);
2530 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002531 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002532 if (IS_ERR(task))
2533 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002534 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002535 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002536 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002537 smp_wmb();
2538 } else
2539 status = data->rpc_status;
2540 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002541
2542 return status;
2543}
2544
2545static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2546{
David Howells2b0143b2015-03-17 22:25:59 +00002547 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002548 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002549 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002550
Fred Isaman3b65a302016-09-19 10:06:49 -04002551 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002552 if (status != 0 || !data->rpc_done)
2553 return status;
2554
Trond Myklebust6926afd12012-01-07 13:22:46 -05002555 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2556
Anna Schumakerd7e98252017-01-11 16:13:29 -05002557 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002558 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002559
2560 return status;
2561}
2562
Trond Myklebustf3792d62014-07-10 08:54:32 -04002563/*
2564 * Additional permission checks in order to distinguish between an
2565 * open for read, and an open for execute. This works around the
2566 * fact that NFSv4 OPEN treats read and execute permissions as being
2567 * the same.
2568 * Note that in the non-execute case, we want to turn off permission
2569 * checking if we just created a new file (POSIX open() semantics).
2570 */
NeilBrowna52458b2018-12-03 11:30:31 +11002571static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002572 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002573 struct nfs4_state *state, fmode_t fmode,
2574 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002575{
2576 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002577 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002578
2579 /* access call failed or for some reason the server doesn't
2580 * support any access modes -- defer access call until later */
2581 if (opendata->o_res.access_supported == 0)
2582 return 0;
2583
2584 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002585 /*
2586 * Use openflags to check for exec, because fmode won't
2587 * always have FMODE_EXEC set when file open for exec.
2588 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002589 if (openflags & __FMODE_EXEC) {
2590 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002591 if (S_ISDIR(state->inode->i_mode))
2592 mask = NFS4_ACCESS_LOOKUP;
2593 else
2594 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002595 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002596 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002597
2598 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002599 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2600 nfs_access_add_cache(state->inode, &cache);
2601
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002602 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2603 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002604 return 0;
2605
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002606 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002607}
2608
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002609/*
2610 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2611 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002612static int _nfs4_proc_open(struct nfs4_opendata *data,
2613 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002614{
David Howells2b0143b2015-03-17 22:25:59 +00002615 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002616 struct nfs_server *server = NFS_SERVER(dir);
2617 struct nfs_openargs *o_arg = &data->o_arg;
2618 struct nfs_openres *o_res = &data->o_res;
2619 int status;
2620
Fred Isaman3b65a302016-09-19 10:06:49 -04002621 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002622 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002623 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002624 if (status != 0) {
2625 if (status == -NFS4ERR_BADNAME &&
2626 !(o_arg->open_flags & O_CREAT))
2627 return -ENOENT;
2628 return status;
2629 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002630
Trond Myklebust6926afd12012-01-07 13:22:46 -05002631 nfs_fattr_map_and_free_names(server, &data->f_attr);
2632
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002633 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002634 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002635 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002636 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002637 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002638 if (data->file_created ||
2639 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002640 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002641 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002642 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002643 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2644 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002646 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002648 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002650 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2651 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002652 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2653 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002654 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658/*
2659 * OPEN_EXPIRED:
2660 * reclaim state on the server after a network partition.
2661 * Assumes caller holds the appropriate lock
2662 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002663static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002665 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002666 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002668 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002669 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002670 if (IS_ERR(opendata))
2671 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002672 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002673 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002674 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002675 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002679static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002680{
Trond Myklebust539cd032007-06-05 11:46:42 -04002681 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002682 struct nfs4_exception exception = { };
2683 int err;
2684
2685 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002686 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002687 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002688 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2689 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002690 switch (err) {
2691 default:
2692 goto out;
2693 case -NFS4ERR_GRACE:
2694 case -NFS4ERR_DELAY:
2695 nfs4_handle_exception(server, err, &exception);
2696 err = 0;
2697 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002698 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002699out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002700 return err;
2701}
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002706 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Trond Myklebust864472e2006-01-03 09:55:15 +01002708 ctx = nfs4_state_find_open_context(state);
2709 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002710 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002711 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002712 put_nfs_open_context(ctx);
2713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Trond Myklebust41020b62016-09-22 13:38:58 -04002716static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2717 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002718{
Trond Myklebust41020b62016-09-22 13:38:58 -04002719 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002720 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002721}
2722
2723static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2724{
2725 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002726 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002727}
2728
2729static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2730{
2731 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2732 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002733 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002734 return nfs4_open_expired(sp, state);
2735}
2736
Trond Myklebust45870d62016-09-22 13:38:59 -04002737static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2738 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002739 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002740{
2741 return -NFS4ERR_BAD_STATEID;
2742}
2743
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002744#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002745static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2746 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002747 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002748{
2749 int status;
2750
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002751 switch (stateid->type) {
2752 default:
2753 break;
2754 case NFS4_INVALID_STATEID_TYPE:
2755 case NFS4_SPECIAL_STATEID_TYPE:
2756 return -NFS4ERR_BAD_STATEID;
2757 case NFS4_REVOKED_STATEID_TYPE:
2758 goto out_free;
2759 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002760
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002761 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002762 switch (status) {
2763 case -NFS4ERR_EXPIRED:
2764 case -NFS4ERR_ADMIN_REVOKED:
2765 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002766 break;
2767 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002768 return status;
2769 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002770out_free:
2771 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002772 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002773 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002774}
2775
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002776static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002777{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002778 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002779 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002780 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002781 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002782 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002783
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002784 /* Get the delegation credential for use by test/free_stateid */
2785 rcu_read_lock();
2786 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002787 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002788 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002789 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002790 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002791 }
2792
2793 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002794
Trond Myklebust994b15b2018-09-05 14:07:14 -04002795 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2796 &delegation->flags)) {
2797 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002798 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002799 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002800
NeilBrowna52458b2018-12-03 11:30:31 +11002801 if (delegation->cred)
2802 cred = get_cred(delegation->cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002803 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002804 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002805 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002806 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002807 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002808 else
2809 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002810
Trond Myklebust8c39a392019-07-19 13:48:44 -04002811 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002812 return ret;
2813}
2814
2815static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2816{
2817 nfs4_stateid tmp;
2818
2819 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2820 nfs4_copy_delegation_stateid(state->inode, state->state,
2821 &tmp, NULL) &&
2822 nfs4_stateid_match_other(&state->stateid, &tmp))
2823 nfs_state_set_delegation(state, &tmp, state->state);
2824 else
2825 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002826}
2827
2828/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002829 * nfs41_check_expired_locks - possibly free a lock stateid
2830 *
2831 * @state: NFSv4 state for an inode
2832 *
2833 * Returns NFS_OK if recovery for this stateid is now finished.
2834 * Otherwise a negative NFS4ERR value is returned.
2835 */
2836static int nfs41_check_expired_locks(struct nfs4_state *state)
2837{
2838 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002839 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002840 struct nfs_server *server = NFS_SERVER(state->inode);
2841
2842 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2843 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002844
2845 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002846 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2847 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002848 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002849
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002850 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002851 spin_unlock(&state->state_lock);
2852
2853 nfs4_put_lock_state(prev);
2854 prev = lsp;
2855
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002856 status = nfs41_test_and_free_expired_stateid(server,
2857 &lsp->ls_stateid,
2858 cred);
2859 trace_nfs4_test_lock_stateid(state, lsp, status);
2860 if (status == -NFS4ERR_EXPIRED ||
2861 status == -NFS4ERR_BAD_STATEID) {
2862 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002863 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002864 if (!recover_lost_locks)
2865 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2866 } else if (status != NFS_OK) {
2867 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002868 nfs4_put_lock_state(prev);
2869 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002870 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002871 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002872 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002873 }
2874 spin_unlock(&state->state_lock);
2875 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002876out:
2877 return ret;
2878}
2879
2880/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002881 * nfs41_check_open_stateid - possibly free an open stateid
2882 *
2883 * @state: NFSv4 state for an inode
2884 *
2885 * Returns NFS_OK if recovery for this stateid is now finished.
2886 * Otherwise a negative NFS4ERR value is returned.
2887 */
2888static int nfs41_check_open_stateid(struct nfs4_state *state)
2889{
2890 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002891 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002892 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002893 int status;
2894
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002895 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002896 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002897 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002898 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002899 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002900 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002901 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002902 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002903 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002904 if (nfs_open_stateid_recover_openmode(state))
2905 return -NFS4ERR_OPENMODE;
2906 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002907}
2908
2909static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2910{
Chuck Levereb64cf92012-07-11 16:30:05 -04002911 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002912
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002913 status = nfs41_check_delegation_stateid(state);
2914 if (status != NFS_OK)
2915 return status;
2916 nfs41_delegation_recover_stateid(state);
2917
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002918 status = nfs41_check_expired_locks(state);
2919 if (status != NFS_OK)
2920 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002921 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002922 if (status != NFS_OK)
2923 status = nfs4_open_expired(sp, state);
2924 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002925}
2926#endif
2927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002929 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2930 * fields corresponding to attributes that were used to store the verifier.
2931 * Make sure we clobber those fields in the later setattr call
2932 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002933static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002934 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002935{
Trond Myklebust609339c2018-03-28 16:18:17 -04002936 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2937 __u32 attrset[3];
2938 unsigned ret;
2939 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002940
Trond Myklebust609339c2018-03-28 16:18:17 -04002941 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2942 attrset[i] = opendata->o_res.attrset[i];
2943 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2944 attrset[i] &= ~bitmask[i];
2945 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002946
Trond Myklebust609339c2018-03-28 16:18:17 -04002947 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2948 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002949
Trond Myklebust609339c2018-03-28 16:18:17 -04002950 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2951 if (sattr->ia_valid & ATTR_ATIME_SET)
2952 ret |= ATTR_ATIME_SET;
2953 else
2954 ret |= ATTR_ATIME;
2955 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002956
Trond Myklebust609339c2018-03-28 16:18:17 -04002957 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2958 if (sattr->ia_valid & ATTR_MTIME_SET)
2959 ret |= ATTR_MTIME_SET;
2960 else
2961 ret |= ATTR_MTIME;
2962 }
2963
2964 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002965 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002966 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002967}
2968
Trond Myklebustc21443c2013-02-07 14:26:21 -05002969static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002970 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002971{
2972 struct nfs4_state_owner *sp = opendata->owner;
2973 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002974 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002975 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002976 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002977 unsigned int seq;
2978 int ret;
2979
2980 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2981
Fred Isaman3b65a302016-09-19 10:06:49 -04002982 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002983 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002984 goto out;
2985
Trond Myklebustae55e592018-05-22 11:17:16 -04002986 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002987 ret = PTR_ERR(state);
2988 if (IS_ERR(state))
2989 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002990 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002991 if (server->caps & NFS_CAP_POSIX_LOCK)
2992 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002993 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2994 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002995
Trond Myklebust275bb302013-05-29 13:11:28 -04002996 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002997 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002998 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002999 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003000 alias = d_exact_alias(dentry, state->inode);
3001 if (!alias)
3002 alias = d_splice_alias(igrab(state->inode), dentry);
3003 /* d_splice_alias() can't fail here - it's a non-directory */
3004 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003005 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003006 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003007 }
3008 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00003009 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04003010 }
3011
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003012 /* Parse layoutget results before we check for access */
3013 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3014
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003015 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3016 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003017 if (ret != 0)
3018 goto out;
3019
David Howells2b0143b2015-03-17 22:25:59 +00003020 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003021 nfs_inode_attach_open_context(ctx);
3022 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3023 nfs4_schedule_stateid_recovery(server, state);
3024 }
Fred Isaman2409a972016-10-06 12:11:21 -04003025
Trond Myklebustc21443c2013-02-07 14:26:21 -05003026out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003027 if (!opendata->cancelled)
3028 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003029 return ret;
3030}
3031
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003032/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003033 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 */
Andy Adamson82be4172012-05-23 05:02:35 -04003035static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003036 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003037 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003038 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003039 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
3041 struct nfs4_state_owner *sp;
3042 struct nfs4_state *state = NULL;
3043 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003044 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003045 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003046 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003047 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003048 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003049 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003050 struct iattr *sattr = c->sattr;
3051 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003052 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003053 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
3055 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003057 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3058 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3060 goto out_err;
3061 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003062 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003063 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003064 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003065 if (d_really_is_positive(dentry))
3066 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003067 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003068 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003069 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003070 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3071 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003072 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003073 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
David Quigley14c43f72013-05-22 12:50:43 -04003075 if (label) {
3076 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3077 if (IS_ERR(olabel)) {
3078 status = PTR_ERR(olabel);
3079 goto err_opendata_put;
3080 }
3081 }
3082
Trond Myklebuste911b812014-03-26 13:24:37 -07003083 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3084 if (!opendata->f_attr.mdsthreshold) {
3085 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3086 if (!opendata->f_attr.mdsthreshold)
3087 goto err_free_label;
3088 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003089 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003090 }
David Howells2b0143b2015-03-17 22:25:59 +00003091 if (d_really_is_positive(dentry))
3092 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003093
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003094 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003095 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003096 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003097 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003098
NeilBrownefcbc042015-07-30 13:00:56 +10003099 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003100 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003101 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003102 /*
3103 * send create attributes which was not set by open
3104 * with an extra setattr.
3105 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003106 if (attrs || label) {
3107 unsigned ia_old = sattr->ia_valid;
3108
3109 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003110 nfs_fattr_init(opendata->o_res.f_attr);
3111 status = nfs4_do_setattr(state->inode, cred,
3112 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003113 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003114 if (status == 0) {
3115 nfs_setattr_update_inode(state->inode, sattr,
3116 opendata->o_res.f_attr);
3117 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3118 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003119 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003120 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003121 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003122 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003123 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003124
Trond Myklebuste911b812014-03-26 13:24:37 -07003125 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003126 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003127 opendata->f_attr.mdsthreshold = NULL;
3128 }
Andy Adamson82be4172012-05-23 05:02:35 -04003129
David Quigley14c43f72013-05-22 12:50:43 -04003130 nfs4_label_free(olabel);
3131
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003132 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003135err_free_label:
3136 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003137err_opendata_put:
3138 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003139err_put_state_owner:
3140 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return status;
3143}
3144
3145
Andy Adamson82be4172012-05-23 05:02:35 -04003146static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003147 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003148 int flags,
3149 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003150 struct nfs4_label *label,
3151 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003153 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003154 struct nfs4_exception exception = {
3155 .interruptible = true,
3156 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003158 struct nfs4_open_createattrs c = {
3159 .label = label,
3160 .sattr = sattr,
3161 .verf = {
3162 [0] = (__u32)jiffies,
3163 [1] = (__u32)current->pid,
3164 },
3165 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 int status;
3167
3168 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003169 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003170 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003171 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 if (status == 0)
3173 break;
3174 /* NOTE: BAD_SEQID means the server and client disagree about the
3175 * book-keeping w.r.t. state-changing operations
3176 * (OPEN/CLOSE/LOCK/LOCKU...)
3177 * It is actually a sign of a bug on the client or on the server.
3178 *
3179 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003180 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 * have unhashed the old state_owner for us, and that we can
3182 * therefore safely retry using a new one. We should still warn
3183 * the user though...
3184 */
3185 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003186 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003187 " returned a bad sequence-id error!\n",
3188 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 exception.retry = 1;
3190 continue;
3191 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003192 /*
3193 * BAD_STATEID on OPEN means that the server cancelled our
3194 * state before it received the OPEN_CONFIRM.
3195 * Recover by retrying the request as per the discussion
3196 * on Page 181 of RFC3530.
3197 */
3198 if (status == -NFS4ERR_BAD_STATEID) {
3199 exception.retry = 1;
3200 continue;
3201 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003202 if (status == -NFS4ERR_EXPIRED) {
3203 nfs4_schedule_lease_recovery(server->nfs_client);
3204 exception.retry = 1;
3205 continue;
3206 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003207 if (status == -EAGAIN) {
3208 /* We must have found a delegation */
3209 exception.retry = 1;
3210 continue;
3211 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003212 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3213 continue;
3214 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 status, &exception));
3216 } while (exception.retry);
3217 return res;
3218}
3219
Trond Myklebust8487c472016-06-26 08:44:35 -04003220static int _nfs4_do_setattr(struct inode *inode,
3221 struct nfs_setattrargs *arg,
3222 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003223 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003224 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003225{
3226 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003227 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3229 .rpc_argp = arg,
3230 .rpc_resp = res,
3231 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003232 };
NeilBrowna52458b2018-12-03 11:30:31 +11003233 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003234 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003235 bool truncate;
3236 int status;
3237
3238 nfs_fattr_init(res->fattr);
3239
3240 /* Servers should only apply open mode checks for file size changes */
3241 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003242 if (!truncate)
3243 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003244
Trond Myklebust991eedb2018-04-09 11:15:30 -04003245 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003246 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003247 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003248 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003249 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003250 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003251 l_ctx = nfs_get_lock_context(ctx);
3252 if (IS_ERR(l_ctx))
3253 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003254 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3255 &arg->stateid, &delegation_cred);
3256 nfs_put_lock_context(l_ctx);
3257 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003258 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003259 else if (status == -EAGAIN)
3260 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003261 } else {
3262zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003263 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003264 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003265 if (delegation_cred)
3266 msg.rpc_cred = delegation_cred;
3267
3268 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3269
NeilBrowna52458b2018-12-03 11:30:31 +11003270 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003271 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003272 renew_lease(server, timestamp);
3273 trace_nfs4_setattr(inode, &arg->stateid, status);
3274 return status;
3275}
3276
NeilBrowna52458b2018-12-03 11:30:31 +11003277static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003278 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003279 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003280 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003282 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003283 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003284 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003285 struct nfs_setattrargs arg = {
3286 .fh = NFS_FH(inode),
3287 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003289 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003290 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003291 };
3292 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003294 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003296 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003297 struct nfs4_exception exception = {
3298 .state = state,
3299 .inode = inode,
3300 .stateid = &arg.stateid,
3301 };
3302 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003305 nfs4_bitmap_copy_adjust_setattr(bitmask,
3306 nfs4_bitmask(server, olabel),
3307 inode);
3308
NeilBrown29b59f92016-10-13 15:26:47 +11003309 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003310 switch (err) {
3311 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003312 if (!(sattr->ia_valid & ATTR_SIZE)) {
3313 pr_warn_once("NFSv4: server %s is incorrectly "
3314 "applying open mode checks to "
3315 "a SETATTR that is not "
3316 "changing file size.\n",
3317 server->nfs_client->cl_hostname);
3318 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003319 if (state && !(state->state & FMODE_WRITE)) {
3320 err = -EBADF;
3321 if (sattr->ia_valid & ATTR_OPEN)
3322 err = -EACCES;
3323 goto out;
3324 }
3325 }
3326 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003328out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return err;
3330}
3331
Peng Tao500d7012015-09-22 11:35:22 +08003332static bool
3333nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3334{
3335 if (inode == NULL || !nfs_have_layout(inode))
3336 return false;
3337
3338 return pnfs_wait_on_layoutreturn(inode, task);
3339}
3340
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003341/*
3342 * Update the seqid of an open stateid
3343 */
3344static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3345 struct nfs4_state *state)
3346{
3347 __be32 seqid_open;
3348 u32 dst_seqid;
3349 int seq;
3350
3351 for (;;) {
3352 if (!nfs4_valid_open_stateid(state))
3353 break;
3354 seq = read_seqbegin(&state->seqlock);
3355 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3356 nfs4_stateid_copy(dst, &state->open_stateid);
3357 if (read_seqretry(&state->seqlock, seq))
3358 continue;
3359 break;
3360 }
3361 seqid_open = state->open_stateid.seqid;
3362 if (read_seqretry(&state->seqlock, seq))
3363 continue;
3364
3365 dst_seqid = be32_to_cpu(dst->seqid);
3366 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3367 dst->seqid = seqid_open;
3368 break;
3369 }
3370}
3371
3372/*
3373 * Update the seqid of an open stateid after receiving
3374 * NFS4ERR_OLD_STATEID
3375 */
3376static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3377 struct nfs4_state *state)
3378{
3379 __be32 seqid_open;
3380 u32 dst_seqid;
3381 bool ret;
3382 int seq;
3383
3384 for (;;) {
3385 ret = false;
3386 if (!nfs4_valid_open_stateid(state))
3387 break;
3388 seq = read_seqbegin(&state->seqlock);
3389 if (!nfs4_state_match_open_stateid_other(state, dst)) {
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 = cpu_to_be32(dst_seqid + 1);
3401 else
3402 dst->seqid = seqid_open;
3403 ret = true;
3404 break;
3405 }
3406
3407 return ret;
3408}
3409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410struct nfs4_closedata {
3411 struct inode *inode;
3412 struct nfs4_state *state;
3413 struct nfs_closeargs arg;
3414 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003415 struct {
3416 struct nfs4_layoutreturn_args arg;
3417 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003418 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003419 u32 roc_barrier;
3420 bool roc;
3421 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003422 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003423 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424};
3425
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003426static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003427{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003428 struct nfs4_closedata *calldata = data;
3429 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003430 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003431
Trond Myklebustcf805162016-11-15 14:56:07 -05003432 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003433 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3434 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003435 nfs4_put_open_state(calldata->state);
3436 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003437 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003438 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003439 kfree(calldata);
3440}
3441
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003442static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003444 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003447 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003448 struct nfs4_exception exception = {
3449 .state = state,
3450 .inode = calldata->inode,
3451 .stateid = &calldata->arg.stateid,
3452 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Chuck Levera3ca5652012-03-01 17:00:40 -05003454 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003455 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3456 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003457 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003458
3459 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04003460 if (pnfs_roc_done(task, calldata->inode,
3461 &calldata->arg.lr_args,
3462 &calldata->res.lr_res,
3463 &calldata->res.lr_ret) == -EAGAIN)
3464 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003465
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003466 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 * the state_owner. we keep this around to process errors
3468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 switch (task->tk_status) {
3470 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003471 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003472 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003473 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003474 case -NFS4ERR_ACCESS:
3475 if (calldata->arg.bitmask != NULL) {
3476 calldata->arg.bitmask = NULL;
3477 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003478 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003479
3480 }
3481 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003482 case -NFS4ERR_OLD_STATEID:
3483 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003484 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003485 state))
3486 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003487 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003488 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003490 case -NFS4ERR_EXPIRED:
3491 nfs4_free_revoked_stateid(server,
3492 &calldata->arg.stateid,
3493 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003494 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003495 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003496 if (calldata->arg.fmode == 0)
3497 break;
3498 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003500 task->tk_status = nfs4_async_handle_exception(task,
3501 server, task->tk_status, &exception);
3502 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003503 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003505 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3506 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003507out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003508 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003509 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003510 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003511 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003512 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003513out_restart:
3514 task->tk_status = 0;
3515 rpc_restart_call_prepare(task);
3516 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003519static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003521 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003522 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003523 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003524 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003525 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003526 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003527
Chuck Levera3ca5652012-03-01 17:00:40 -05003528 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003529 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003530 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003531
Trond Myklebust88069f72009-12-08 08:33:16 -05003532 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003533 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003534 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3535 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3536 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003537 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003538 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003539 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003540 if (state->n_rdonly == 0)
3541 call_close |= is_rdonly;
3542 else if (is_rdonly)
3543 calldata->arg.fmode |= FMODE_READ;
3544 if (state->n_wronly == 0)
3545 call_close |= is_wronly;
3546 else if (is_wronly)
3547 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003548 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3549 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003550 } else if (is_rdwr)
3551 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3552
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003553 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3554 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003555 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003556 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003557
3558 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003559 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003560 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003561 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003562
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003563 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003564 nfs_release_seqid(calldata->arg.seqid);
3565 goto out_wait;
3566 }
3567
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003568 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3569 if (lo && !pnfs_layout_is_valid(lo)) {
3570 calldata->arg.lr_args = NULL;
3571 calldata->res.lr_res = NULL;
3572 }
3573
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003574 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003575 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003576
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003577 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003578 /* Close-to-open cache consistency revalidation */
3579 if (!nfs4_have_delegation(inode, FMODE_READ))
3580 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3581 else
3582 calldata->arg.bitmask = NULL;
3583 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003584
Trond Myklebust6ae37332015-01-30 14:21:14 -05003585 calldata->arg.share_access =
3586 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3587 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003588
Trond Myklebustd8d84982016-12-19 12:14:44 -05003589 if (calldata->res.fattr == NULL)
3590 calldata->arg.bitmask = NULL;
3591 else if (calldata->arg.bitmask == NULL)
3592 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003593 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003594 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003595 &calldata->arg.seq_args,
3596 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003597 task) != 0)
3598 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003599 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003600 return;
3601out_no_action:
3602 task->tk_action = NULL;
3603out_wait:
3604 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003607static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003608 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003609 .rpc_call_done = nfs4_close_done,
3610 .rpc_release = nfs4_free_closedata,
3611};
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613/*
3614 * It is possible for data to be read/written from a mem-mapped file
3615 * after the sys_close call (which hits the vfs layer as a flush).
3616 * This means that we can't safely call nfsv4 close on a file until
3617 * the inode is cleared. This in turn means that we are not good
3618 * NFSv4 citizens - we do not indicate to the server to update the file's
3619 * share state even when we are done with one of the three share
3620 * stateid's in the inode.
3621 *
3622 * NOTE: Caller must be holding the sp->so_owner semaphore!
3623 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003624int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003626 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003627 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003629 struct nfs4_state_owner *sp = state->owner;
3630 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003631 struct rpc_message msg = {
3632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3633 .rpc_cred = state->owner->so_cred,
3634 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003635 struct rpc_task_setup task_setup_data = {
3636 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003637 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003638 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003639 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003640 .flags = RPC_TASK_ASYNC,
3641 };
Trond Myklebust95121352005-10-18 14:20:12 -07003642 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003644 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3645 &task_setup_data.rpc_client, &msg);
3646
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003647 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003649 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003650 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003651 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003653 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003654 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3655 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003657 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3658 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003659 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003660 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003661 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003662 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003663 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003664 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003665 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003666 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003667 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003668 calldata->lr.roc = pnfs_roc(state->inode,
3669 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3670 if (calldata->lr.roc) {
3671 calldata->arg.lr_args = &calldata->lr.arg;
3672 calldata->res.lr_res = &calldata->lr.res;
3673 }
Al Viro643168c2011-06-22 18:20:23 -04003674 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003675
Trond Myklebust1174dd12010-12-21 10:52:24 -05003676 msg.rpc_argp = &calldata->arg;
3677 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003678 task_setup_data.callback_data = calldata;
3679 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003680 if (IS_ERR(task))
3681 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003682 status = 0;
3683 if (wait)
3684 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003685 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003686 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003687out_free_calldata:
3688 kfree(calldata);
3689out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003690 nfs4_put_open_state(state);
3691 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003692 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
Trond Myklebust2b484292010-09-17 10:56:51 -04003695static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003696nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3697 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003700 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3701
3702 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
Trond Myklebust565277f2007-10-15 18:17:53 -04003704 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003705 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003706
3707 nfs4_label_release_security(label);
3708
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003709 if (IS_ERR(state))
3710 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003711 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712}
3713
Trond Myklebust1185a552009-12-03 15:54:02 -05003714static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003715{
3716 if (ctx->state == NULL)
3717 return;
3718 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003719 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003720 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003721 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003722}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Trond Myklebustb944dba2013-11-04 15:20:20 -05003724#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3725#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003726#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003727
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3729{
Kinglong Mee8c612822015-08-26 21:12:58 +08003730 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003731 struct nfs4_server_caps_arg args = {
3732 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003733 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003734 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 struct nfs4_server_caps_res res = {};
3736 struct rpc_message msg = {
3737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003738 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 .rpc_resp = &res,
3740 };
3741 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003742 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Kinglong Mee8c612822015-08-26 21:12:58 +08003744 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3745 FATTR4_WORD0_FH_EXPIRE_TYPE |
3746 FATTR4_WORD0_LINK_SUPPORT |
3747 FATTR4_WORD0_SYMLINK_SUPPORT |
3748 FATTR4_WORD0_ACLSUPPORT;
3749 if (minorversion)
3750 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3751
Bryan Schumaker7c513052011-03-24 17:12:24 +00003752 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003754 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003755 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003756 case 0:
3757 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3758 res.attr_bitmask[2] = 0;
3759 break;
3760 case 1:
3761 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3762 break;
3763 case 2:
3764 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003767 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3768 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3769 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3770 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003771 NFS_CAP_CTIME|NFS_CAP_MTIME|
3772 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003773 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3774 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 server->caps |= NFS_CAP_ACLS;
3776 if (res.has_links != 0)
3777 server->caps |= NFS_CAP_HARDLINKS;
3778 if (res.has_symlinks != 0)
3779 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003780 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3781 server->caps |= NFS_CAP_FILEID;
3782 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3783 server->caps |= NFS_CAP_MODE;
3784 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3785 server->caps |= NFS_CAP_NLINK;
3786 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3787 server->caps |= NFS_CAP_OWNER;
3788 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3789 server->caps |= NFS_CAP_OWNER_GROUP;
3790 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3791 server->caps |= NFS_CAP_ATIME;
3792 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3793 server->caps |= NFS_CAP_CTIME;
3794 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3795 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003796#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3797 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3798 server->caps |= NFS_CAP_SECURITY_LABEL;
3799#endif
3800 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3801 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003802 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003803
Trond Myklebusta65318b2009-03-11 14:10:28 -04003804 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3805 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3806 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003807 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003808
3809 /* Avoid a regression due to buggy server */
3810 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3811 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003812 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3813 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003814
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003816 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 return status;
3820}
3821
Trond Myklebust55a97592006-06-09 09:34:19 -04003822int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823{
Trond Myklebust0688e642019-04-07 13:59:09 -04003824 struct nfs4_exception exception = {
3825 .interruptible = true,
3826 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 int err;
3828 do {
3829 err = nfs4_handle_exception(server,
3830 _nfs4_server_capabilities(server, fhandle),
3831 &exception);
3832 } while (exception.retry);
3833 return err;
3834}
3835
3836static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3837 struct nfs_fsinfo *info)
3838{
David Quigleyaa9c2662013-05-22 12:50:44 -04003839 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003841 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 };
3843 struct nfs4_lookup_res res = {
3844 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003845 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 .fh = fhandle,
3847 };
3848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3850 .rpc_argp = &args,
3851 .rpc_resp = &res,
3852 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003853
David Quigleyaa9c2662013-05-22 12:50:44 -04003854 bitmask[0] = nfs4_fattr_bitmap[0];
3855 bitmask[1] = nfs4_fattr_bitmap[1];
3856 /*
3857 * Process the label in the upcoming getfattr
3858 */
3859 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3860
Trond Myklebust0e574af2005-10-27 22:12:38 -04003861 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003862 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863}
3864
3865static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3866 struct nfs_fsinfo *info)
3867{
Trond Myklebust0688e642019-04-07 13:59:09 -04003868 struct nfs4_exception exception = {
3869 .interruptible = true,
3870 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 int err;
3872 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003873 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003874 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003875 switch (err) {
3876 case 0:
3877 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003878 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003879 default:
3880 err = nfs4_handle_exception(server, err, &exception);
3881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003883out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 return err;
3885}
3886
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003887static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3888 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3889{
Trond Myklebustc2190662013-08-26 19:23:04 -04003890 struct rpc_auth_create_args auth_args = {
3891 .pseudoflavor = flavor,
3892 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003893 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003894
Trond Myklebustc2190662013-08-26 19:23:04 -04003895 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003896 if (IS_ERR(auth))
3897 return -EACCES;
3898 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003899}
3900
Chuck Lever9a744ba2013-03-16 15:56:02 -04003901/*
3902 * Retry pseudoroot lookup with various security flavors. We do this when:
3903 *
3904 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3905 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3906 *
3907 * Returns zero on success, or a negative NFS4ERR value, or a
3908 * negative errno value.
3909 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003910static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003911 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003913 /* Per 3530bis 15.33.5 */
3914 static const rpc_authflavor_t flav_array[] = {
3915 RPC_AUTH_GSS_KRB5P,
3916 RPC_AUTH_GSS_KRB5I,
3917 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003918 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003919 RPC_AUTH_NULL,
3920 };
3921 int status = -EPERM;
3922 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003924 if (server->auth_info.flavor_len > 0) {
3925 /* try each flavor specified by user */
3926 for (i = 0; i < server->auth_info.flavor_len; i++) {
3927 status = nfs4_lookup_root_sec(server, fhandle, info,
3928 server->auth_info.flavors[i]);
3929 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3930 continue;
3931 break;
3932 }
3933 } else {
3934 /* no flavors specified by user, try default list */
3935 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3936 status = nfs4_lookup_root_sec(server, fhandle, info,
3937 flav_array[i]);
3938 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3939 continue;
3940 break;
3941 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003942 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003943
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003944 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003945 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003946 * to access the mount. It could also mean that we tried to mount
3947 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3948 * existing mount programs don't handle -EACCES very well so it should
3949 * be mapped to -EPERM instead.
3950 */
3951 if (status == -EACCES)
3952 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003953 return status;
3954}
3955
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003956/**
3957 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3958 * @server: initialized nfs_server handle
3959 * @fhandle: we fill in the pseudo-fs root file handle
3960 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003961 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003962 *
3963 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003964 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003965int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003966 struct nfs_fsinfo *info,
3967 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003968{
Andre Przywarac7757072015-04-23 17:17:40 +01003969 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003970
Andre Przywarac7757072015-04-23 17:17:40 +01003971 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003972 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003973
3974 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003975 status = server->nfs_client->cl_mvops->find_root_sec(server,
3976 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 if (status == 0)
3979 status = nfs4_server_capabilities(server, fhandle);
3980 if (status == 0)
3981 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003982
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003983 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984}
3985
Bryan Schumakerbae36242012-05-10 15:07:31 -04003986static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3987 struct nfs_fsinfo *info)
3988{
3989 int error;
3990 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003991 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003992
3993 error = nfs4_server_capabilities(server, mntfh);
3994 if (error < 0) {
3995 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3996 return error;
3997 }
3998
David Quigley14c43f72013-05-22 12:50:43 -04003999 label = nfs4_label_alloc(server, GFP_KERNEL);
4000 if (IS_ERR(label))
4001 return PTR_ERR(label);
4002
Trond Myklebusta841b542018-04-07 13:50:59 -04004003 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004004 if (error < 0) {
4005 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04004006 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004007 }
4008
4009 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4010 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4011 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4012
David Quigley14c43f72013-05-22 12:50:43 -04004013err_free_label:
4014 nfs4_label_free(label);
4015
Bryan Schumakerbae36242012-05-10 15:07:31 -04004016 return error;
4017}
4018
Manoj Naik6b97fd32006-06-09 09:34:29 -04004019/*
4020 * Get locations and (maybe) other attributes of a referral.
4021 * Note that we'll actually follow the referral later when
4022 * we detect fsid mismatch in inode revalidation
4023 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004024static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4025 const struct qstr *name, struct nfs_fattr *fattr,
4026 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004027{
4028 int status = -ENOMEM;
4029 struct page *page = NULL;
4030 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004031
4032 page = alloc_page(GFP_KERNEL);
4033 if (page == NULL)
4034 goto out;
4035 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4036 if (locations == NULL)
4037 goto out;
4038
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004039 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004040 if (status != 0)
4041 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004042
4043 /*
4044 * If the fsid didn't change, this is a migration event, not a
4045 * referral. Cause us to drop into the exception handler, which
4046 * will kick off migration recovery.
4047 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004048 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004049 dprintk("%s: server did not return a different fsid for"
4050 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004051 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004052 goto out;
4053 }
Andy Adamson533eb462011-06-13 18:25:56 -04004054 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4055 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004056
Andy Adamson533eb462011-06-13 18:25:56 -04004057 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004058 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004059 memset(fhandle, 0, sizeof(struct nfs_fh));
4060out:
4061 if (page)
4062 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004063 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004064 return status;
4065}
4066
David Quigley1775fd32013-05-22 12:50:42 -04004067static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004068 struct nfs_fattr *fattr, struct nfs4_label *label,
4069 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
Trond Myklebust771734f2018-04-07 13:54:23 -04004071 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 struct nfs4_getattr_arg args = {
4073 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004074 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 };
4076 struct nfs4_getattr_res res = {
4077 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004078 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 .server = server,
4080 };
4081 struct rpc_message msg = {
4082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4083 .rpc_argp = &args,
4084 .rpc_resp = &res,
4085 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004086 unsigned short task_flags = 0;
4087
4088 /* Is this is an attribute revalidation, subject to softreval? */
4089 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4090 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004091
Trond Myklebust771734f2018-04-07 13:54:23 -04004092 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004093
Trond Myklebust0e574af2005-10-27 22:12:38 -04004094 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004095 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4096 return nfs4_do_call_sync(server->client, server, &msg,
4097 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098}
4099
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004100int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004101 struct nfs_fattr *fattr, struct nfs4_label *label,
4102 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103{
Trond Myklebust0688e642019-04-07 13:59:09 -04004104 struct nfs4_exception exception = {
4105 .interruptible = true,
4106 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 int err;
4108 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004109 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004110 trace_nfs4_getattr(server, fhandle, fattr, err);
4111 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 &exception);
4113 } while (exception.retry);
4114 return err;
4115}
4116
4117/*
4118 * The file is not closed if it is opened due to the a request to change
4119 * the size of the file. The open call will not be needed once the
4120 * VFS layer lookup-intents are implemented.
4121 *
4122 * Close is called when the inode is destroyed.
4123 * If we haven't opened the file for O_WRONLY, we
4124 * need to in the size_change case to obtain a stateid.
4125 *
4126 * Got race?
4127 * Because OPEN is always done by name in nfsv4, it is
4128 * possible that we opened a different file by the same
4129 * name. We can recognize this race condition, but we
4130 * can't do anything about it besides returning an error.
4131 *
4132 * This will be fixed with VFS changes (lookup-intent).
4133 */
4134static int
4135nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4136 struct iattr *sattr)
4137{
David Howells2b0143b2015-03-17 22:25:59 +00004138 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004139 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004140 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004141 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 int status;
4143
Peng Tao88ac8152014-09-12 11:04:10 +08004144 if (pnfs_ld_layoutret_on_setattr(inode) &&
4145 sattr->ia_valid & ATTR_SIZE &&
4146 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004147 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004148
Trond Myklebust0e574af2005-10-27 22:12:38 -04004149 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Andy Adamson26699402012-05-30 16:12:24 -04004151 /* Deal with open(O_TRUNC) */
4152 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004153 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004154
4155 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004156 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004157 return 0;
4158
Trond Myklebustd5308382005-11-04 15:33:38 -05004159 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004160 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004161
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004162 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004163 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004164 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004165 }
4166
David Quigley14c43f72013-05-22 12:50:43 -04004167 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4168 if (IS_ERR(label))
4169 return PTR_ERR(label);
4170
Trond Myklebust199366f2018-03-20 16:43:18 -04004171 /* Return any delegations if we're going to change ACLs */
4172 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004173 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004174
NeilBrown29b59f92016-10-13 15:26:47 +11004175 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004176 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004177 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004178 nfs_setsecurity(inode, fattr, label);
4179 }
David Quigley14c43f72013-05-22 12:50:43 -04004180 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return status;
4182}
4183
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004184static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004185 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004186 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004187{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004188 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004189 int status;
4190 struct nfs4_lookup_arg args = {
4191 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004192 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004193 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004194 };
4195 struct nfs4_lookup_res res = {
4196 .server = server,
4197 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004198 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004199 .fh = fhandle,
4200 };
4201 struct rpc_message msg = {
4202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4203 .rpc_argp = &args,
4204 .rpc_resp = &res,
4205 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004206 unsigned short task_flags = 0;
4207
4208 /* Is this is an attribute revalidation, subject to softreval? */
4209 if (nfs_lookup_is_soft_revalidate(dentry))
4210 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004211
David Quigleyaa9c2662013-05-22 12:50:44 -04004212 args.bitmask = nfs4_bitmask(server, label);
4213
David Howells2b3de442006-08-22 20:06:09 -04004214 nfs_fattr_init(fattr);
4215
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004216 dprintk("NFS call lookup %pd2\n", dentry);
4217 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4218 status = nfs4_do_call_sync(clnt, server, &msg,
4219 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 dprintk("NFS reply lookup: %d\n", status);
4221 return status;
4222}
4223
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004224static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004225{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004226 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004227 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004228 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4229 fattr->nlink = 2;
4230}
4231
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004232static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004233 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004234 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004235{
Trond Myklebust0688e642019-04-07 13:59:09 -04004236 struct nfs4_exception exception = {
4237 .interruptible = true,
4238 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004239 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004240 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004241 int err;
4242 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004243 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004244 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004245 switch (err) {
4246 case -NFS4ERR_BADNAME:
4247 err = -ENOENT;
4248 goto out;
4249 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004250 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004251 if (err == -NFS4ERR_MOVED)
4252 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004253 goto out;
4254 case -NFS4ERR_WRONGSEC:
4255 err = -EPERM;
4256 if (client != *clnt)
4257 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004258 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004259 if (IS_ERR(client))
4260 return PTR_ERR(client);
4261
4262 exception.retry = 1;
4263 break;
4264 default:
4265 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4266 }
4267 } while (exception.retry);
4268
4269out:
4270 if (err == 0)
4271 *clnt = client;
4272 else if (client != *clnt)
4273 rpc_shutdown_client(client);
4274
4275 return err;
4276}
4277
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004278static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004279 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4280 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004282 int status;
4283 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004284
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004285 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004286 if (client != NFS_CLIENT(dir)) {
4287 rpc_shutdown_client(client);
4288 nfs_fixup_secinfo_attributes(fattr);
4289 }
4290 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291}
4292
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004293struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004294nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004295 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4296{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004297 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004298 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004299
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004300 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004301 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004302 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004303 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004304}
4305
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004306static int _nfs4_proc_lookupp(struct inode *inode,
4307 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4308 struct nfs4_label *label)
4309{
4310 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4311 struct nfs_server *server = NFS_SERVER(inode);
4312 int status;
4313 struct nfs4_lookupp_arg args = {
4314 .bitmask = server->attr_bitmask,
4315 .fh = NFS_FH(inode),
4316 };
4317 struct nfs4_lookupp_res res = {
4318 .server = server,
4319 .fattr = fattr,
4320 .label = label,
4321 .fh = fhandle,
4322 };
4323 struct rpc_message msg = {
4324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4325 .rpc_argp = &args,
4326 .rpc_resp = &res,
4327 };
4328
4329 args.bitmask = nfs4_bitmask(server, label);
4330
4331 nfs_fattr_init(fattr);
4332
4333 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4334 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4335 &res.seq_res, 0);
4336 dprintk("NFS reply lookupp: %d\n", status);
4337 return status;
4338}
4339
4340static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4341 struct nfs_fattr *fattr, struct nfs4_label *label)
4342{
Trond Myklebust0688e642019-04-07 13:59:09 -04004343 struct nfs4_exception exception = {
4344 .interruptible = true,
4345 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004346 int err;
4347 do {
4348 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4349 trace_nfs4_lookupp(inode, err);
4350 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4351 &exception);
4352 } while (exception.retry);
4353 return err;
4354}
4355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4357{
Trond Myklebust76b32992007-08-10 17:45:11 -04004358 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 struct nfs4_accessargs args = {
4360 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004361 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004363 struct nfs4_accessres res = {
4364 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004365 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 struct rpc_message msg = {
4367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4368 .rpc_argp = &args,
4369 .rpc_resp = &res,
4370 .rpc_cred = entry->cred,
4371 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004372 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
Trond Myklebust7c672652018-06-04 15:00:53 -04004374 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004375 res.fattr = nfs_alloc_fattr();
4376 if (res.fattr == NULL)
4377 return -ENOMEM;
4378 args.bitmask = server->cache_consistency_bitmask;
4379 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004380 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004382 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004383 if (res.fattr)
4384 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004386 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 return status;
4388}
4389
4390static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4391{
Trond Myklebust0688e642019-04-07 13:59:09 -04004392 struct nfs4_exception exception = {
4393 .interruptible = true,
4394 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 int err;
4396 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004397 err = _nfs4_proc_access(inode, entry);
4398 trace_nfs4_access(inode, err);
4399 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 &exception);
4401 } while (exception.retry);
4402 return err;
4403}
4404
4405/*
4406 * TODO: For the time being, we don't try to get any attributes
4407 * along with any of the zero-copy operations READ, READDIR,
4408 * READLINK, WRITE.
4409 *
4410 * In the case of the first three, we want to put the GETATTR
4411 * after the read-type operation -- this is because it is hard
4412 * to predict the length of a GETATTR response in v4, and thus
4413 * align the READ data correctly. This means that the GETATTR
4414 * may end up partially falling into the page cache, and we should
4415 * shift it into the 'tail' of the xdr_buf before processing.
4416 * To do this efficiently, we need to know the total length
4417 * of data received, which doesn't seem to be available outside
4418 * of the RPC layer.
4419 *
4420 * In the case of WRITE, we also want to put the GETATTR after
4421 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004422 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 *
4424 * Both of these changes to the XDR layer would in fact be quite
4425 * minor, but I decided to leave them for a subsequent patch.
4426 */
4427static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4428 unsigned int pgbase, unsigned int pglen)
4429{
4430 struct nfs4_readlink args = {
4431 .fh = NFS_FH(inode),
4432 .pgbase = pgbase,
4433 .pglen = pglen,
4434 .pages = &page,
4435 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004436 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 struct rpc_message msg = {
4438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4439 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004440 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 };
4442
Bryan Schumaker7c513052011-03-24 17:12:24 +00004443 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 -07004444}
4445
4446static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4447 unsigned int pgbase, unsigned int pglen)
4448{
Trond Myklebust0688e642019-04-07 13:59:09 -04004449 struct nfs4_exception exception = {
4450 .interruptible = true,
4451 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 int err;
4453 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004454 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4455 trace_nfs4_readlink(inode, err);
4456 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 &exception);
4458 } while (exception.retry);
4459 return err;
4460}
4461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004463 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465static int
4466nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004467 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004469 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004470 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004471 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 int status = 0;
4474
NeilBrown532d4de2016-10-13 15:26:47 +11004475 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004476 if (IS_ERR(ctx))
4477 return PTR_ERR(ctx);
4478
David Quigleyaa9c2662013-05-22 12:50:44 -04004479 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4480
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004481 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4482 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004483 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 if (IS_ERR(state)) {
4485 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004486 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004489 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004490 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 return status;
4492}
4493
Trond Myklebust3c591172018-07-31 15:54:10 -04004494static int
4495_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496{
Trond Myklebust16e42952005-10-27 22:12:44 -04004497 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004498 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004500 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004502 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004503 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004504 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4507 .rpc_argp = &args,
4508 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004510 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004511 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004512
Bryan Schumaker7c513052011-03-24 17:12:24 +00004513 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004514 if (status == 0) {
4515 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004516 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004517 /* Removing a directory decrements nlink in the parent */
4518 if (ftype == NF4DIR && dir->i_nlink > 2)
4519 nfs4_dec_nlink_locked(dir);
4520 spin_unlock(&dir->i_lock);
4521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 return status;
4523}
4524
Trond Myklebust912678d2018-03-20 16:43:15 -04004525static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4526{
Trond Myklebust0688e642019-04-07 13:59:09 -04004527 struct nfs4_exception exception = {
4528 .interruptible = true,
4529 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004530 struct inode *inode = d_inode(dentry);
4531 int err;
4532
Trond Myklebustc01d3642018-03-20 16:43:20 -04004533 if (inode) {
4534 if (inode->i_nlink == 1)
4535 nfs4_inode_return_delegation(inode);
4536 else
4537 nfs4_inode_make_writeable(inode);
4538 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004539 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004540 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004541 trace_nfs4_remove(dir, &dentry->d_name, err);
4542 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4543 &exception);
4544 } while (exception.retry);
4545 return err;
4546}
4547
4548static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Trond Myklebust0688e642019-04-07 13:59:09 -04004550 struct nfs4_exception exception = {
4551 .interruptible = true,
4552 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004556 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004557 trace_nfs4_remove(dir, name, err);
4558 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 &exception);
4560 } while (exception.retry);
4561 return err;
4562}
4563
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004564static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4565 struct dentry *dentry,
4566 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004568 struct nfs_removeargs *args = msg->rpc_argp;
4569 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
Trond Myklebust977fcc22018-03-20 16:43:17 -04004571 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004573 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004574
4575 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004576
4577 if (inode)
4578 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579}
4580
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004581static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4582{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004583 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004584 &data->args.seq_args,
4585 &data->res.seq_res,
4586 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587}
4588
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004589static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004591 struct nfs_unlinkdata *data = task->tk_calldata;
4592 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004593
Trond Myklebust14516c32010-07-31 14:29:06 -04004594 if (!nfs4_sequence_done(task, &res->seq_res))
4595 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004596 if (nfs4_async_handle_error(task, res->server, NULL,
4597 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004598 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004599 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004600 update_changeattr(dir, &res->cinfo,
4601 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004602 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603}
4604
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004605static void nfs4_proc_rename_setup(struct rpc_message *msg,
4606 struct dentry *old_dentry,
4607 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004608{
Jeff Laytond3d41522010-09-17 17:31:57 -04004609 struct nfs_renameargs *arg = msg->rpc_argp;
4610 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004611 struct inode *old_inode = d_inode(old_dentry);
4612 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004613
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004614 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004615 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004616 if (new_inode)
4617 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004618 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004619 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004620 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004621}
4622
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004623static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4624{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004625 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004626 &data->args.seq_args,
4627 &data->res.seq_res,
4628 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004629}
4630
4631static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4632 struct inode *new_dir)
4633{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004634 struct nfs_renamedata *data = task->tk_calldata;
4635 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004636
4637 if (!nfs4_sequence_done(task, &res->seq_res))
4638 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004639 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004640 return 0;
4641
Trond Myklebustc733c492017-01-11 12:32:26 -05004642 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004643 if (new_dir != old_dir) {
4644 /* Note: If we moved a directory, nlink will change */
4645 update_changeattr(old_dir, &res->old_cinfo,
4646 res->old_fattr->time_start,
4647 NFS_INO_INVALID_OTHER);
4648 update_changeattr(new_dir, &res->new_cinfo,
4649 res->new_fattr->time_start,
4650 NFS_INO_INVALID_OTHER);
4651 } else
4652 update_changeattr(old_dir, &res->old_cinfo,
4653 res->old_fattr->time_start,
4654 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004655 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004656 return 1;
4657}
4658
Al Virobeffb8f2016-07-20 16:34:42 -04004659static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004661 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004662 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 struct nfs4_link_arg arg = {
4664 .fh = NFS_FH(inode),
4665 .dir_fh = NFS_FH(dir),
4666 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004667 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004669 struct nfs4_link_res res = {
4670 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004671 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004672 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 struct rpc_message msg = {
4674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4675 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004676 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004678 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
Trond Myklebust136f2622010-04-16 16:22:49 -04004680 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004681 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004682 goto out;
4683
David Quigley14c43f72013-05-22 12:50:43 -04004684 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4685 if (IS_ERR(res.label)) {
4686 status = PTR_ERR(res.label);
4687 goto out;
4688 }
4689
Trond Myklebustc01d3642018-03-20 16:43:20 -04004690 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004691 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004692
Bryan Schumaker7c513052011-03-24 17:12:24 +00004693 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004694 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004695 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004696 status = nfs_post_op_update_inode(inode, res.fattr);
4697 if (!status)
4698 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004699 }
David Quigley14c43f72013-05-22 12:50:43 -04004700
4701
4702 nfs4_label_free(res.label);
4703
Trond Myklebust136f2622010-04-16 16:22:49 -04004704out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004705 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 return status;
4707}
4708
Al Virobeffb8f2016-07-20 16:34:42 -04004709static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710{
Trond Myklebust0688e642019-04-07 13:59:09 -04004711 struct nfs4_exception exception = {
4712 .interruptible = true,
4713 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 int err;
4715 do {
4716 err = nfs4_handle_exception(NFS_SERVER(inode),
4717 _nfs4_proc_link(inode, dir, name),
4718 &exception);
4719 } while (exception.retry);
4720 return err;
4721}
4722
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004723struct nfs4_createdata {
4724 struct rpc_message msg;
4725 struct nfs4_create_arg arg;
4726 struct nfs4_create_res res;
4727 struct nfs_fh fh;
4728 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004729 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004730};
4731
4732static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004733 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004734{
4735 struct nfs4_createdata *data;
4736
4737 data = kzalloc(sizeof(*data), GFP_KERNEL);
4738 if (data != NULL) {
4739 struct nfs_server *server = NFS_SERVER(dir);
4740
David Quigley14c43f72013-05-22 12:50:43 -04004741 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4742 if (IS_ERR(data->label))
4743 goto out_free;
4744
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004745 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4746 data->msg.rpc_argp = &data->arg;
4747 data->msg.rpc_resp = &data->res;
4748 data->arg.dir_fh = NFS_FH(dir);
4749 data->arg.server = server;
4750 data->arg.name = name;
4751 data->arg.attrs = sattr;
4752 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004753 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004754 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004755 data->res.server = server;
4756 data->res.fh = &data->fh;
4757 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004758 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004759 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004760 }
4761 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004762out_free:
4763 kfree(data);
4764 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004765}
4766
4767static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4768{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004769 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004770 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004771 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004772 spin_lock(&dir->i_lock);
4773 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004774 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004775 /* Creating a directory bumps nlink in the parent */
4776 if (data->arg.ftype == NF4DIR)
4777 nfs4_inc_nlink_locked(dir);
4778 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004779 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004780 }
4781 return status;
4782}
4783
4784static void nfs4_free_createdata(struct nfs4_createdata *data)
4785{
David Quigley14c43f72013-05-22 12:50:43 -04004786 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004787 kfree(data);
4788}
4789
Chuck Lever4f390c12006-08-22 20:06:22 -04004790static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004791 struct page *page, unsigned int len, struct iattr *sattr,
4792 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004794 struct nfs4_createdata *data;
4795 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Chuck Lever94a6d752006-08-22 20:06:23 -04004797 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004798 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004799
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004800 status = -ENOMEM;
4801 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4802 if (data == NULL)
4803 goto out;
4804
4805 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4806 data->arg.u.symlink.pages = &page;
4807 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004808 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004810 status = nfs4_do_create(dir, dentry, data);
4811
4812 nfs4_free_createdata(data);
4813out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 return status;
4815}
4816
Chuck Lever4f390c12006-08-22 20:06:22 -04004817static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004818 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819{
Trond Myklebust0688e642019-04-07 13:59:09 -04004820 struct nfs4_exception exception = {
4821 .interruptible = true,
4822 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004823 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004825
4826 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4827
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004829 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4830 trace_nfs4_symlink(dir, &dentry->d_name, err);
4831 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 &exception);
4833 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004834
4835 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 return err;
4837}
4838
4839static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004840 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004842 struct nfs4_createdata *data;
4843 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004845 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4846 if (data == NULL)
4847 goto out;
4848
David Quigley1775fd32013-05-22 12:50:42 -04004849 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004850 status = nfs4_do_create(dir, dentry, data);
4851
4852 nfs4_free_createdata(data);
4853out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 return status;
4855}
4856
4857static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4858 struct iattr *sattr)
4859{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004860 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004861 struct nfs4_exception exception = {
4862 .interruptible = true,
4863 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004864 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004866
David Quigleyaa9c2662013-05-22 12:50:44 -04004867 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4868
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004869 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4870 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004872 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4873 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4874 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 &exception);
4876 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004877 nfs4_label_release_security(label);
4878
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 return err;
4880}
4881
NeilBrown684f39b2018-12-03 11:30:30 +11004882static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004883 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884{
David Howells2b0143b2015-03-17 22:25:59 +00004885 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 struct nfs4_readdir_arg args = {
4887 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004888 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 .pgbase = 0,
4890 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004891 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004892 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 };
4894 struct nfs4_readdir_res res;
4895 struct rpc_message msg = {
4896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4897 .rpc_argp = &args,
4898 .rpc_resp = &res,
4899 .rpc_cred = cred,
4900 };
4901 int status;
4902
Al Viro6de14722013-09-16 10:53:17 -04004903 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4904 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004905 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004906 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004908 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 -05004909 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004910 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004911 status += args.pgbase;
4912 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004913
4914 nfs_invalidate_atime(dir);
4915
Harvey Harrison3110ff82008-05-02 13:42:44 -07004916 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 return status;
4918}
4919
NeilBrown684f39b2018-12-03 11:30:30 +11004920static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004921 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922{
Trond Myklebust0688e642019-04-07 13:59:09 -04004923 struct nfs4_exception exception = {
4924 .interruptible = true,
4925 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 int err;
4927 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004928 err = _nfs4_proc_readdir(dentry, cred, cookie,
4929 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004930 trace_nfs4_readdir(d_inode(dentry), err);
4931 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 &exception);
4933 } while (exception.retry);
4934 return err;
4935}
4936
4937static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004938 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004940 struct nfs4_createdata *data;
4941 int mode = sattr->ia_mode;
4942 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004944 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4945 if (data == NULL)
4946 goto out;
4947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004949 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004951 data->arg.ftype = NF4BLK;
4952 data->arg.u.device.specdata1 = MAJOR(rdev);
4953 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 }
4955 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004956 data->arg.ftype = NF4CHR;
4957 data->arg.u.device.specdata1 = MAJOR(rdev);
4958 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004959 } else if (!S_ISSOCK(mode)) {
4960 status = -EINVAL;
4961 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 }
David Quigley1775fd32013-05-22 12:50:42 -04004963
David Quigleyaa9c2662013-05-22 12:50:44 -04004964 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004965 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004966out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004967 nfs4_free_createdata(data);
4968out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 return status;
4970}
4971
4972static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4973 struct iattr *sattr, dev_t rdev)
4974{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004975 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004976 struct nfs4_exception exception = {
4977 .interruptible = true,
4978 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004979 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004981
David Quigleyaa9c2662013-05-22 12:50:44 -04004982 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4983
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004984 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4985 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004987 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4988 trace_nfs4_mknod(dir, &dentry->d_name, err);
4989 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 &exception);
4991 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004992
4993 nfs4_label_release_security(label);
4994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 return err;
4996}
4997
4998static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4999 struct nfs_fsstat *fsstat)
5000{
5001 struct nfs4_statfs_arg args = {
5002 .fh = fhandle,
5003 .bitmask = server->attr_bitmask,
5004 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005005 struct nfs4_statfs_res res = {
5006 .fsstat = fsstat,
5007 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 struct rpc_message msg = {
5009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5010 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005011 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 };
5013
Trond Myklebust0e574af2005-10-27 22:12:38 -04005014 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005015 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016}
5017
5018static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5019{
Trond Myklebust0688e642019-04-07 13:59:09 -04005020 struct nfs4_exception exception = {
5021 .interruptible = true,
5022 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 int err;
5024 do {
5025 err = nfs4_handle_exception(server,
5026 _nfs4_proc_statfs(server, fhandle, fsstat),
5027 &exception);
5028 } while (exception.retry);
5029 return err;
5030}
5031
5032static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5033 struct nfs_fsinfo *fsinfo)
5034{
5035 struct nfs4_fsinfo_arg args = {
5036 .fh = fhandle,
5037 .bitmask = server->attr_bitmask,
5038 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005039 struct nfs4_fsinfo_res res = {
5040 .fsinfo = fsinfo,
5041 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 struct rpc_message msg = {
5043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5044 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005045 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 };
5047
Bryan Schumaker7c513052011-03-24 17:12:24 +00005048 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
5050
5051static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5052{
Trond Myklebust0688e642019-04-07 13:59:09 -04005053 struct nfs4_exception exception = {
5054 .interruptible = true,
5055 };
Chuck Lever83ca7f52013-03-16 15:55:53 -04005056 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 int err;
5058
5059 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005060 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005061 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005062 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04005063 nfs4_set_lease_period(server->nfs_client,
5064 fsinfo->lease_time * HZ,
5065 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005066 break;
5067 }
5068 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 } while (exception.retry);
5070 return err;
5071}
5072
5073static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5074{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005075 int error;
5076
Trond Myklebust0e574af2005-10-27 22:12:38 -04005077 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005078 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005079 if (error == 0) {
5080 /* block layout checks this! */
5081 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005082 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005083 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005084
5085 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086}
5087
5088static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5089 struct nfs_pathconf *pathconf)
5090{
5091 struct nfs4_pathconf_arg args = {
5092 .fh = fhandle,
5093 .bitmask = server->attr_bitmask,
5094 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005095 struct nfs4_pathconf_res res = {
5096 .pathconf = pathconf,
5097 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 struct rpc_message msg = {
5099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5100 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005101 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 };
5103
5104 /* None of the pathconf attributes are mandatory to implement */
5105 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5106 memset(pathconf, 0, sizeof(*pathconf));
5107 return 0;
5108 }
5109
Trond Myklebust0e574af2005-10-27 22:12:38 -04005110 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005111 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112}
5113
5114static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5115 struct nfs_pathconf *pathconf)
5116{
Trond Myklebust0688e642019-04-07 13:59:09 -04005117 struct nfs4_exception exception = {
5118 .interruptible = true,
5119 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 int err;
5121
5122 do {
5123 err = nfs4_handle_exception(server,
5124 _nfs4_proc_pathconf(server, fhandle, pathconf),
5125 &exception);
5126 } while (exception.retry);
5127 return err;
5128}
5129
Trond Myklebust5521abf2013-03-16 20:54:34 -04005130int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005131 const struct nfs_open_context *ctx,
5132 const struct nfs_lock_context *l_ctx,
5133 fmode_t fmode)
5134{
NeilBrown17393472016-10-13 15:26:47 +11005135 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005136}
5137EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5138
Trond Myklebust5521abf2013-03-16 20:54:34 -04005139static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5140 const struct nfs_open_context *ctx,
5141 const struct nfs_lock_context *l_ctx,
5142 fmode_t fmode)
5143{
Ben Dooksd49dd112019-10-16 17:28:21 +01005144 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005145
Trond Myklebuste1253be2014-03-05 08:44:23 -05005146 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005147 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005148 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005149 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005150}
5151
5152static bool nfs4_error_stateid_expired(int err)
5153{
5154 switch (err) {
5155 case -NFS4ERR_DELEG_REVOKED:
5156 case -NFS4ERR_ADMIN_REVOKED:
5157 case -NFS4ERR_BAD_STATEID:
5158 case -NFS4ERR_STALE_STATEID:
5159 case -NFS4ERR_OLD_STATEID:
5160 case -NFS4ERR_OPENMODE:
5161 case -NFS4ERR_EXPIRED:
5162 return true;
5163 }
5164 return false;
5165}
5166
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005167static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005169 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005171 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005172 if (task->tk_status < 0) {
5173 struct nfs4_exception exception = {
5174 .inode = hdr->inode,
5175 .state = hdr->args.context->state,
5176 .stateid = &hdr->args.stateid,
5177 };
5178 task->tk_status = nfs4_async_handle_exception(task,
5179 server, task->tk_status, &exception);
5180 if (exception.retry) {
5181 rpc_restart_call_prepare(task);
5182 return -EAGAIN;
5183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005185
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005187 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005188 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189}
5190
Trond Myklebust5521abf2013-03-16 20:54:34 -04005191static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005192 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005193{
5194
5195 if (!nfs4_error_stateid_expired(task->tk_status) ||
5196 nfs4_stateid_is_current(&args->stateid,
5197 args->context,
5198 args->lock_context,
5199 FMODE_READ))
5200 return false;
5201 rpc_restart_call_prepare(task);
5202 return true;
5203}
5204
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005205static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005206{
5207
5208 dprintk("--> %s\n", __func__);
5209
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005210 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005211 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005212 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005213 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005214 if (task->tk_status > 0)
5215 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005216 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5217 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005218}
5219
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005220static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5221 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005223 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005224 if (!hdr->pgio_done_cb)
5225 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005226 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005227 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228}
5229
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005230static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5231 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005232{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005233 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005234 &hdr->args.seq_args,
5235 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005236 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005237 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005238 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5239 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005240 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005241 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005242 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005243 return -EIO;
5244 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245}
5246
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005247static int nfs4_write_done_cb(struct rpc_task *task,
5248 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005250 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005251
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005252 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005253 if (task->tk_status < 0) {
5254 struct nfs4_exception exception = {
5255 .inode = hdr->inode,
5256 .state = hdr->args.context->state,
5257 .stateid = &hdr->args.stateid,
5258 };
5259 task->tk_status = nfs4_async_handle_exception(task,
5260 NFS_SERVER(inode), task->tk_status,
5261 &exception);
5262 if (exception.retry) {
5263 rpc_restart_call_prepare(task);
5264 return -EAGAIN;
5265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005267 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005268 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005269 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005270 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272}
5273
Trond Myklebust5521abf2013-03-16 20:54:34 -04005274static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005275 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005276{
5277
5278 if (!nfs4_error_stateid_expired(task->tk_status) ||
5279 nfs4_stateid_is_current(&args->stateid,
5280 args->context,
5281 args->lock_context,
5282 FMODE_WRITE))
5283 return false;
5284 rpc_restart_call_prepare(task);
5285 return true;
5286}
5287
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005288static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005289{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005290 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005291 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005292 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005293 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005294 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5295 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005296}
5297
Trond Myklebust5a37f852012-04-28 14:55:16 -04005298static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005299bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005300{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005301 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005302 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005303 return false;
5304 /* Otherwise, request attributes if and only if we don't hold
5305 * a delegation
5306 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005307 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005308}
Fred Isamana69aef12011-03-03 15:13:47 +00005309
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005310static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005311 struct rpc_message *msg,
5312 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005314 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005315
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005316 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5317 hdr->args.bitmask = NULL;
5318 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005319 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005320 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005321
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005322 if (!hdr->pgio_done_cb)
5323 hdr->pgio_done_cb = nfs4_write_done_cb;
5324 hdr->res.server = server;
5325 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005327 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005328 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005329 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330}
5331
Fred Isaman0b7c0152012-04-20 14:47:39 -04005332static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5333{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005334 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005335 &data->args.seq_args,
5336 &data->res.seq_res,
5337 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338}
5339
Fred Isaman0b7c0152012-04-20 14:47:39 -04005340static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005343
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005344 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005345 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5346 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005347 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005348 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351}
5352
Fred Isaman0b7c0152012-04-20 14:47:39 -04005353static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005354{
5355 if (!nfs4_sequence_done(task, &data->res.seq_res))
5356 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005357 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005358}
5359
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005360static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5361 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005363 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005364
Fred Isaman0b7c0152012-04-20 14:47:39 -04005365 if (data->commit_done_cb == NULL)
5366 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005367 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005368 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005369 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005370 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371}
5372
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005373static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5374 struct nfs_commitres *res)
5375{
5376 struct inode *dst_inode = file_inode(dst);
5377 struct nfs_server *server = NFS_SERVER(dst_inode);
5378 struct rpc_message msg = {
5379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5380 .rpc_argp = args,
5381 .rpc_resp = res,
5382 };
5383
5384 args->fh = NFS_FH(dst_inode);
5385 return nfs4_call_sync(server->client, server, &msg,
5386 &args->seq_args, &res->seq_res, 1);
5387}
5388
5389int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5390{
5391 struct nfs_commitargs args = {
5392 .offset = offset,
5393 .count = count,
5394 };
5395 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5396 struct nfs4_exception exception = { };
5397 int status;
5398
5399 do {
5400 status = _nfs4_proc_commit(dst, &args, res);
5401 status = nfs4_handle_exception(dst_server, status, &exception);
5402 } while (exception.retry);
5403
5404 return status;
5405}
5406
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005407struct nfs4_renewdata {
5408 struct nfs_client *client;
5409 unsigned long timestamp;
5410};
5411
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412/*
5413 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5414 * standalone procedure for queueing an asynchronous RENEW.
5415 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005416static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005417{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005418 struct nfs4_renewdata *data = calldata;
5419 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005420
Elena Reshetova212bf412017-10-20 12:53:38 +03005421 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005422 nfs4_schedule_state_renewal(clp);
5423 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005424 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005425}
5426
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005427static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005429 struct nfs4_renewdata *data = calldata;
5430 struct nfs_client *clp = data->client;
5431 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005433 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005434 switch (task->tk_status) {
5435 case 0:
5436 break;
5437 case -NFS4ERR_LEASE_MOVED:
5438 nfs4_schedule_lease_moved_recovery(clp);
5439 break;
5440 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005441 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005442 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5443 return;
5444 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005445 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005446 return;
5447 }
5448 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005450 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451}
5452
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005453static const struct rpc_call_ops nfs4_renew_ops = {
5454 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005455 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005456};
5457
NeilBrowna52458b2018-12-03 11:30:31 +11005458static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459{
5460 struct rpc_message msg = {
5461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5462 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005463 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005465 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005467 if (renew_flags == 0)
5468 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005469 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005470 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005471 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005472 if (data == NULL) {
5473 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005474 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005475 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005476 data->client = clp;
5477 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005478 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005479 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480}
5481
NeilBrowna52458b2018-12-03 11:30:31 +11005482static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
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 };
5489 unsigned long now = jiffies;
5490 int status;
5491
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005492 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 if (status < 0)
5494 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005495 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 return 0;
5497}
5498
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005499static inline int nfs4_server_supports_acls(struct nfs_server *server)
5500{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005501 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005502}
5503
Trond Myklebust21f498c2012-08-24 10:59:25 -04005504/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5505 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005506 * the stack.
5507 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005508#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005509
Neil Hormane9e3d722011-03-04 19:26:03 -05005510static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005511 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005512{
5513 struct page *newpage, **spages;
5514 int rc = 0;
5515 size_t len;
5516 spages = pages;
5517
5518 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005519 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005520 newpage = alloc_page(GFP_KERNEL);
5521
5522 if (newpage == NULL)
5523 goto unwind;
5524 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005525 buf += len;
5526 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005527 *pages++ = newpage;
5528 rc++;
5529 } while (buflen != 0);
5530
5531 return rc;
5532
5533unwind:
5534 for(; rc > 0; rc--)
5535 __free_page(spages[rc-1]);
5536 return -ENOMEM;
5537}
5538
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005539struct nfs4_cached_acl {
5540 int cached;
5541 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005542 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005543};
5544
5545static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005546{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005547 struct nfs_inode *nfsi = NFS_I(inode);
5548
5549 spin_lock(&inode->i_lock);
5550 kfree(nfsi->nfs4_acl);
5551 nfsi->nfs4_acl = acl;
5552 spin_unlock(&inode->i_lock);
5553}
5554
5555static void nfs4_zap_acl_attr(struct inode *inode)
5556{
5557 nfs4_set_cached_acl(inode, NULL);
5558}
5559
5560static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5561{
5562 struct nfs_inode *nfsi = NFS_I(inode);
5563 struct nfs4_cached_acl *acl;
5564 int ret = -ENOENT;
5565
5566 spin_lock(&inode->i_lock);
5567 acl = nfsi->nfs4_acl;
5568 if (acl == NULL)
5569 goto out;
5570 if (buf == NULL) /* user is just asking for length */
5571 goto out_len;
5572 if (acl->cached == 0)
5573 goto out;
5574 ret = -ERANGE; /* see getxattr(2) man page */
5575 if (acl->len > buflen)
5576 goto out;
5577 memcpy(buf, acl->data, acl->len);
5578out_len:
5579 ret = acl->len;
5580out:
5581 spin_unlock(&inode->i_lock);
5582 return ret;
5583}
5584
Sachin Prabhu5794d212012-04-17 14:36:40 +01005585static 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 +00005586{
5587 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005588 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005589
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005590 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005591 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005592 if (acl == NULL)
5593 goto out;
5594 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005595 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005596 } else {
5597 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5598 if (acl == NULL)
5599 goto out;
5600 acl->cached = 0;
5601 }
5602 acl->len = acl_len;
5603out:
5604 nfs4_set_cached_acl(inode, acl);
5605}
5606
Andy Adamsonbf118a32011-12-07 11:55:27 -05005607/*
5608 * The getxattr API returns the required buffer length when called with a
5609 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5610 * the required buf. On a NULL buf, we send a page of data to the server
5611 * guessing that the ACL request can be serviced by a page. If so, we cache
5612 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5613 * the cache. If not so, we throw away the page, and cache the required
5614 * length. The next getxattr call will then produce another round trip to
5615 * the server, this time with the input buf of the required size.
5616 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005617static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005618{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005619 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005620 struct nfs_getaclargs args = {
5621 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005622 .acl_len = buflen,
5623 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005624 struct nfs_getaclres res = {
5625 .acl_len = buflen,
5626 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005627 struct rpc_message msg = {
5628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5629 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005630 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005631 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005632 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005633 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005634 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005635
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005636 if (buflen == 0)
5637 buflen = server->rsize;
5638
5639 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5640 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5641 if (!pages)
5642 return -ENOMEM;
5643
5644 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005645
Andy Adamsonbf118a32011-12-07 11:55:27 -05005646 for (i = 0; i < npages; i++) {
5647 pages[i] = alloc_page(GFP_KERNEL);
5648 if (!pages[i])
5649 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005650 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005651
5652 /* for decoding across pages */
5653 res.acl_scratch = alloc_page(GFP_KERNEL);
5654 if (!res.acl_scratch)
5655 goto out_free;
5656
Andy Adamsonbf118a32011-12-07 11:55:27 -05005657 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005658
Peng Taode040be2012-01-10 22:42:47 +08005659 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005660 __func__, buf, buflen, npages, args.acl_len);
5661 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5662 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005663 if (ret)
5664 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005665
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005666 /* Handle the case where the passed-in buffer is too short */
5667 if (res.acl_flags & NFS4_ACL_TRUNC) {
5668 /* Did the user only issue a request for the acl length? */
5669 if (buf == NULL)
5670 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005671 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005672 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005673 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005674 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005675 if (buf) {
5676 if (res.acl_len > buflen) {
5677 ret = -ERANGE;
5678 goto out_free;
5679 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005680 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005681 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005682out_ok:
5683 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005684out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005685 for (i = 0; i < npages; i++)
5686 if (pages[i])
5687 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005688 if (res.acl_scratch)
5689 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005690 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005691 return ret;
5692}
5693
Trond Myklebust16b42892006-08-24 12:27:15 -04005694static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5695{
Trond Myklebust0688e642019-04-07 13:59:09 -04005696 struct nfs4_exception exception = {
5697 .interruptible = true,
5698 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005699 ssize_t ret;
5700 do {
5701 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005702 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005703 if (ret >= 0)
5704 break;
5705 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5706 } while (exception.retry);
5707 return ret;
5708}
5709
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005710static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5711{
5712 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005713 int ret;
5714
5715 if (!nfs4_server_supports_acls(server))
5716 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005717 ret = nfs_revalidate_inode(server, inode);
5718 if (ret < 0)
5719 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005720 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5721 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005722 ret = nfs4_read_cached_acl(inode, buf, buflen);
5723 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005724 /* -ENOENT is returned if there is no ACL or if there is an ACL
5725 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005726 return ret;
5727 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005728}
5729
Trond Myklebust16b42892006-08-24 12:27:15 -04005730static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005731{
5732 struct nfs_server *server = NFS_SERVER(inode);
5733 struct page *pages[NFS4ACL_MAXPAGES];
5734 struct nfs_setaclargs arg = {
5735 .fh = NFS_FH(inode),
5736 .acl_pages = pages,
5737 .acl_len = buflen,
5738 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005739 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005740 struct rpc_message msg = {
5741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5742 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005743 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005744 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005745 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005746 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005747
5748 if (!nfs4_server_supports_acls(server))
5749 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005750 if (npages > ARRAY_SIZE(pages))
5751 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005752 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005753 if (i < 0)
5754 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005755 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005756 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005757
5758 /*
5759 * Free each page after tx, so the only ref left is
5760 * held by the network stack
5761 */
5762 for (; i > 0; i--)
5763 put_page(pages[i-1]);
5764
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005765 /*
5766 * Acl update can result in inode attribute update.
5767 * so mark the attribute cache invalid.
5768 */
5769 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005770 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005771 | NFS_INO_INVALID_CTIME
5772 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005773 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005774 nfs_access_zap_cache(inode);
5775 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005776 return ret;
5777}
5778
Trond Myklebust16b42892006-08-24 12:27:15 -04005779static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5780{
5781 struct nfs4_exception exception = { };
5782 int err;
5783 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005784 err = __nfs4_proc_set_acl(inode, buf, buflen);
5785 trace_nfs4_set_acl(inode, err);
5786 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005787 &exception);
5788 } while (exception.retry);
5789 return err;
5790}
5791
David Quigleyaa9c2662013-05-22 12:50:44 -04005792#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5793static int _nfs4_get_security_label(struct inode *inode, void *buf,
5794 size_t buflen)
5795{
5796 struct nfs_server *server = NFS_SERVER(inode);
5797 struct nfs_fattr fattr;
5798 struct nfs4_label label = {0, 0, buflen, buf};
5799
5800 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005801 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005802 .fh = NFS_FH(inode),
5803 .bitmask = bitmask,
5804 };
5805 struct nfs4_getattr_res res = {
5806 .fattr = &fattr,
5807 .label = &label,
5808 .server = server,
5809 };
5810 struct rpc_message msg = {
5811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005812 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005813 .rpc_resp = &res,
5814 };
5815 int ret;
5816
5817 nfs_fattr_init(&fattr);
5818
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005819 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005820 if (ret)
5821 return ret;
5822 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5823 return -ENOENT;
5824 if (buflen < label.len)
5825 return -ERANGE;
5826 return 0;
5827}
5828
5829static int nfs4_get_security_label(struct inode *inode, void *buf,
5830 size_t buflen)
5831{
Trond Myklebust0688e642019-04-07 13:59:09 -04005832 struct nfs4_exception exception = {
5833 .interruptible = true,
5834 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005835 int err;
5836
5837 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5838 return -EOPNOTSUPP;
5839
5840 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005841 err = _nfs4_get_security_label(inode, buf, buflen);
5842 trace_nfs4_get_security_label(inode, err);
5843 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005844 &exception);
5845 } while (exception.retry);
5846 return err;
5847}
5848
5849static int _nfs4_do_set_security_label(struct inode *inode,
5850 struct nfs4_label *ilabel,
5851 struct nfs_fattr *fattr,
5852 struct nfs4_label *olabel)
5853{
5854
5855 struct iattr sattr = {0};
5856 struct nfs_server *server = NFS_SERVER(inode);
5857 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005858 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005859 .fh = NFS_FH(inode),
5860 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005861 .server = server,
5862 .bitmask = bitmask,
5863 .label = ilabel,
5864 };
5865 struct nfs_setattrres res = {
5866 .fattr = fattr,
5867 .label = olabel,
5868 .server = server,
5869 };
5870 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5872 .rpc_argp = &arg,
5873 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005874 };
5875 int status;
5876
Jeff Layton12207f62013-11-01 10:49:32 -04005877 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005878
Jeff Layton12207f62013-11-01 10:49:32 -04005879 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005880 if (status)
5881 dprintk("%s failed: %d\n", __func__, status);
5882
5883 return status;
5884}
5885
5886static int nfs4_do_set_security_label(struct inode *inode,
5887 struct nfs4_label *ilabel,
5888 struct nfs_fattr *fattr,
5889 struct nfs4_label *olabel)
5890{
5891 struct nfs4_exception exception = { };
5892 int err;
5893
5894 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005895 err = _nfs4_do_set_security_label(inode, ilabel,
5896 fattr, olabel);
5897 trace_nfs4_set_security_label(inode, err);
5898 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005899 &exception);
5900 } while (exception.retry);
5901 return err;
5902}
5903
5904static int
Al Viro59301222016-05-27 10:19:30 -04005905nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005906{
5907 struct nfs4_label ilabel, *olabel = NULL;
5908 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005909 int status;
5910
5911 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5912 return -EOPNOTSUPP;
5913
5914 nfs_fattr_init(&fattr);
5915
5916 ilabel.pi = 0;
5917 ilabel.lfs = 0;
5918 ilabel.label = (char *)buf;
5919 ilabel.len = buflen;
5920
David Quigleyaa9c2662013-05-22 12:50:44 -04005921 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5922 if (IS_ERR(olabel)) {
5923 status = -PTR_ERR(olabel);
5924 goto out;
5925 }
5926
5927 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5928 if (status == 0)
5929 nfs_setsecurity(inode, &fattr, olabel);
5930
5931 nfs4_label_free(olabel);
5932out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005933 return status;
5934}
5935#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5936
5937
Chuck Leverf0920752012-05-21 22:45:41 -04005938static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5939 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005940{
5941 __be32 verf[2];
5942
Chuck Lever2c820d92012-05-21 22:45:33 -04005943 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5944 /* An impossible timestamp guarantees this value
5945 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005946 verf[0] = cpu_to_be32(U32_MAX);
5947 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005948 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005949 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005950 u64 ns = ktime_to_ns(nn->boot_time);
5951
5952 verf[0] = cpu_to_be32(ns >> 32);
5953 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005954 }
Chuck Levercd937102012-03-02 17:14:31 -05005955 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5956}
5957
Jeff Laytona3192682015-06-09 19:43:59 -04005958static int
5959nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005960{
Jeff Laytona3192682015-06-09 19:43:59 -04005961 size_t len;
5962 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005963
Trond Myklebustceb3a162015-01-03 15:16:04 -05005964 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005965 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005966
Jeff Laytona3192682015-06-09 19:43:59 -04005967 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005968 len = 14 +
5969 strlen(clp->cl_rpcclient->cl_nodename) +
5970 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005971 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005972 1;
5973 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005974 if (nfs4_client_id_uniquifier[0] != '\0')
5975 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005976 if (len > NFS4_OPAQUE_LIMIT + 1)
5977 return -EINVAL;
5978
5979 /*
5980 * Since this string is allocated at mount time, and held until the
5981 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5982 * about a memory-reclaim deadlock.
5983 */
5984 str = kmalloc(len, GFP_KERNEL);
5985 if (!str)
5986 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005987
Chuck Levere984a552012-09-14 17:24:21 -04005988 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005989 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005990 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005991 clp->cl_rpcclient->cl_nodename,
5992 nfs4_client_id_uniquifier,
5993 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005994 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005995 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005996 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005997 clp->cl_rpcclient->cl_nodename,
5998 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005999 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006000 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006001
Jeff Laytona3192682015-06-09 19:43:59 -04006002 clp->cl_owner_id = str;
6003 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006004}
6005
Jeff Layton873e3852015-06-09 19:44:00 -04006006static int
6007nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006008{
Jeff Layton873e3852015-06-09 19:44:00 -04006009 size_t len;
6010 char *str;
6011
6012 len = 10 + 10 + 1 + 10 + 1 +
6013 strlen(nfs4_client_id_uniquifier) + 1 +
6014 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6015
6016 if (len > NFS4_OPAQUE_LIMIT + 1)
6017 return -EINVAL;
6018
6019 /*
6020 * Since this string is allocated at mount time, and held until the
6021 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6022 * about a memory-reclaim deadlock.
6023 */
6024 str = kmalloc(len, GFP_KERNEL);
6025 if (!str)
6026 return -ENOMEM;
6027
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006028 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04006029 clp->rpc_ops->version, clp->cl_minorversion,
6030 nfs4_client_id_uniquifier,
6031 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006032 clp->cl_owner_id = str;
6033 return 0;
6034}
6035
6036static int
6037nfs4_init_uniform_client_string(struct nfs_client *clp)
6038{
Jeff Layton873e3852015-06-09 19:44:00 -04006039 size_t len;
6040 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006041
6042 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006043 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006044
6045 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04006046 return nfs4_init_uniquifier_client_string(clp);
6047
6048 len = 10 + 10 + 1 + 10 + 1 +
6049 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6050
6051 if (len > NFS4_OPAQUE_LIMIT + 1)
6052 return -EINVAL;
6053
6054 /*
6055 * Since this string is allocated at mount time, and held until the
6056 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6057 * about a memory-reclaim deadlock.
6058 */
6059 str = kmalloc(len, GFP_KERNEL);
6060 if (!str)
6061 return -ENOMEM;
6062
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006063 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04006064 clp->rpc_ops->version, clp->cl_minorversion,
6065 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006066 clp->cl_owner_id = str;
6067 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006068}
6069
Chuck Lever706cb8d2014-03-12 12:51:47 -04006070/*
6071 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6072 * services. Advertise one based on the address family of the
6073 * clientaddr.
6074 */
6075static unsigned int
6076nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6077{
6078 if (strchr(clp->cl_ipaddr, ':') != NULL)
6079 return scnprintf(buf, len, "tcp6");
6080 else
6081 return scnprintf(buf, len, "tcp");
6082}
6083
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006084static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6085{
6086 struct nfs4_setclientid *sc = calldata;
6087
6088 if (task->tk_status == 0)
6089 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6090}
6091
6092static const struct rpc_call_ops nfs4_setclientid_ops = {
6093 .rpc_call_done = nfs4_setclientid_done,
6094};
6095
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006096/**
6097 * nfs4_proc_setclientid - Negotiate client ID
6098 * @clp: state data structure
6099 * @program: RPC program for NFSv4 callback service
6100 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006101 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006102 * @res: where to place the result
6103 *
6104 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6105 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006106int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006107 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006108 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109{
6110 nfs4_verifier sc_verifier;
6111 struct nfs4_setclientid setclientid = {
6112 .sc_verifier = &sc_verifier,
6113 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006114 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 };
6116 struct rpc_message msg = {
6117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6118 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006119 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006120 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006122 struct rpc_task_setup task_setup_data = {
6123 .rpc_client = clp->cl_rpcclient,
6124 .rpc_message = &msg,
6125 .callback_ops = &nfs4_setclientid_ops,
6126 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006127 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006128 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006129 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
Chuck Leverde734832012-07-11 16:30:50 -04006131 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006132 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006133
6134 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6135 status = nfs4_init_uniform_client_string(clp);
6136 else
Jeff Laytona3192682015-06-09 19:43:59 -04006137 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006138
6139 if (status)
6140 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006141
Chuck Leverde734832012-07-11 16:30:50 -04006142 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006143 setclientid.sc_netid_len =
6144 nfs4_init_callback_netid(clp,
6145 setclientid.sc_netid,
6146 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006147 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006148 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 clp->cl_ipaddr, port >> 8, port & 255);
6150
Jeff Layton3a6bb732015-06-09 19:43:57 -04006151 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006152 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006153 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006154
6155 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006156 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006157 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006158 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6159 put_rpccred(setclientid.sc_cred);
6160 }
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006161out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006162 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006163 dprintk("NFS reply setclientid: %d\n", status);
6164 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165}
6166
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006167/**
6168 * nfs4_proc_setclientid_confirm - Confirm client ID
6169 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006170 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006171 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006172 *
6173 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6174 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006175int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006176 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006177 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 struct rpc_message msg = {
6180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006181 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006182 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 int status;
6185
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006186 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6187 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6188 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006189 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6190 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006191 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006192 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 return status;
6194}
6195
Trond Myklebustfe650402006-01-03 09:55:18 +01006196struct nfs4_delegreturndata {
6197 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006198 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006199 struct nfs_fh fh;
6200 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006201 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006202 struct {
6203 struct nfs4_layoutreturn_args arg;
6204 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006205 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006206 u32 roc_barrier;
6207 bool roc;
6208 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006209 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006210 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006211 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006212};
6213
Trond Myklebustfe650402006-01-03 09:55:18 +01006214static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6215{
6216 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006217 struct nfs4_exception exception = {
6218 .inode = data->inode,
6219 .stateid = &data->stateid,
6220 };
Andy Adamson938e1012009-04-01 09:22:28 -04006221
Trond Myklebust14516c32010-07-31 14:29:06 -04006222 if (!nfs4_sequence_done(task, &data->res.seq_res))
6223 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006224
Trond Myklebustca8acf82013-08-13 10:36:56 -04006225 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006226
6227 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04006228 if (pnfs_roc_done(task, data->inode,
6229 &data->args.lr_args,
6230 &data->res.lr_res,
6231 &data->res.lr_ret) == -EAGAIN)
6232 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006233
Ricardo Labiaga79708862009-12-07 09:23:21 -05006234 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006235 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006236 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006237 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006238 case -NFS4ERR_ADMIN_REVOKED:
6239 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006240 case -NFS4ERR_EXPIRED:
6241 nfs4_free_revoked_stateid(data->res.server,
6242 data->args.stateid,
6243 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006244 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006245 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006246 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006247 task->tk_status = 0;
6248 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006249 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006250 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6251 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006252 if (data->args.bitmask) {
6253 data->args.bitmask = NULL;
6254 data->res.fattr = NULL;
6255 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006256 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006257 case -NFS4ERR_ACCESS:
6258 if (data->args.bitmask) {
6259 data->args.bitmask = NULL;
6260 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006261 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006262 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006263 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006264 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006265 task->tk_status = nfs4_async_handle_exception(task,
6266 data->res.server, task->tk_status,
6267 &exception);
6268 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006269 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006270 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006271 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006272 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006273 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006274out_restart:
6275 task->tk_status = 0;
6276 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006277}
6278
6279static void nfs4_delegreturn_release(void *calldata)
6280{
Peng Tao039b7562014-07-03 13:05:02 +08006281 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006282 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006283
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006284 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006285 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006286 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6287 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006288 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006289 nfs_iput_and_deactive(inode);
6290 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006291 kfree(calldata);
6292}
6293
Andy Adamson938e1012009-04-01 09:22:28 -04006294static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6295{
6296 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006297 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006298
6299 d_data = (struct nfs4_delegreturndata *)data;
6300
Trond Myklebust5326de92019-11-13 09:39:36 +01006301 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6302 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006303 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006304 }
Peng Tao500d7012015-09-22 11:35:22 +08006305
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006306 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6307 if (lo && !pnfs_layout_is_valid(lo)) {
6308 d_data->args.lr_args = NULL;
6309 d_data->res.lr_res = NULL;
6310 }
6311
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006312 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006313 &d_data->args.seq_args,
6314 &d_data->res.seq_res,
6315 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006316}
Andy Adamson938e1012009-04-01 09:22:28 -04006317
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006318static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006319 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006320 .rpc_call_done = nfs4_delegreturn_done,
6321 .rpc_release = nfs4_delegreturn_release,
6322};
6323
NeilBrowna52458b2018-12-03 11:30:31 +11006324static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006325{
6326 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006327 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006328 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006329 struct rpc_message msg = {
6330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6331 .rpc_cred = cred,
6332 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006333 struct rpc_task_setup task_setup_data = {
6334 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006335 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006336 .callback_ops = &nfs4_delegreturn_ops,
6337 .flags = RPC_TASK_ASYNC,
6338 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006339 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006340
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006341 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006342 if (data == NULL)
6343 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006344 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006345
6346 nfs4_state_protect(server->nfs_client,
6347 NFS_SP4_MACH_CRED_CLEANUP,
6348 &task_setup_data.rpc_client, &msg);
6349
Trond Myklebustfe650402006-01-03 09:55:18 +01006350 data->args.fhandle = &data->fh;
6351 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006352 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006353 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006354 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006355 data->res.fattr = &data->fattr;
6356 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006357 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006358 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006359 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006360 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006361 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006362 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006363 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006364 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006365 if (data->lr.roc) {
6366 data->args.lr_args = &data->lr.arg;
6367 data->res.lr_res = &data->lr.res;
6368 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006369 } else if (data->lr.roc) {
6370 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6371 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006372 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006373
Trond Myklebustc970aa82007-07-14 15:39:59 -04006374 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006375 msg.rpc_argp = &data->args;
6376 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006377 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006378 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006379 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006380 if (!issync)
6381 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006382 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006383 if (status != 0)
6384 goto out;
6385 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006386out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006387 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006388 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389}
6390
NeilBrowna52458b2018-12-03 11:30:31 +11006391int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392{
6393 struct nfs_server *server = NFS_SERVER(inode);
6394 struct nfs4_exception exception = { };
6395 int err;
6396 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006397 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006398 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 switch (err) {
6400 case -NFS4ERR_STALE_STATEID:
6401 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 case 0:
6403 return 0;
6404 }
6405 err = nfs4_handle_exception(server, err, &exception);
6406 } while (exception.retry);
6407 return err;
6408}
6409
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6411{
6412 struct inode *inode = state->inode;
6413 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006414 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006415 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006417 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006419 struct nfs_lockt_res res = {
6420 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 };
6422 struct rpc_message msg = {
6423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006424 .rpc_argp = &arg,
6425 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 .rpc_cred = state->owner->so_cred,
6427 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 struct nfs4_lock_state *lsp;
6429 int status;
6430
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006431 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006432 status = nfs4_set_lock_state(state, request);
6433 if (status != 0)
6434 goto out;
6435 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006436 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006437 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006438 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006439 switch (status) {
6440 case 0:
6441 request->fl_type = F_UNLCK;
6442 break;
6443 case -NFS4ERR_DENIED:
6444 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006446 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006447 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006448out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 return status;
6450}
6451
6452static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6453{
Trond Myklebust0688e642019-04-07 13:59:09 -04006454 struct nfs4_exception exception = {
6455 .interruptible = true,
6456 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 int err;
6458
6459 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006460 err = _nfs4_proc_getlk(state, cmd, request);
6461 trace_nfs4_get_lock(request, state, cmd, err);
6462 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 &exception);
6464 } while (exception.retry);
6465 return err;
6466}
6467
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006468/*
6469 * Update the seqid of a lock stateid after receiving
6470 * NFS4ERR_OLD_STATEID
6471 */
6472static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6473 struct nfs4_lock_state *lsp)
6474{
6475 struct nfs4_state *state = lsp->ls_state;
6476 bool ret = false;
6477
6478 spin_lock(&state->state_lock);
6479 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6480 goto out;
6481 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6482 nfs4_stateid_seqid_inc(dst);
6483 else
6484 dst->seqid = lsp->ls_stateid.seqid;
6485 ret = true;
6486out:
6487 spin_unlock(&state->state_lock);
6488 return ret;
6489}
6490
6491static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6492 struct nfs4_lock_state *lsp)
6493{
6494 struct nfs4_state *state = lsp->ls_state;
6495 bool ret;
6496
6497 spin_lock(&state->state_lock);
6498 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6499 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6500 spin_unlock(&state->state_lock);
6501 return ret;
6502}
6503
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006504struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006505 struct nfs_locku_args arg;
6506 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006507 struct nfs4_lock_state *lsp;
6508 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006509 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006510 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006511 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006512 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006513};
6514
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006515static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6516 struct nfs_open_context *ctx,
6517 struct nfs4_lock_state *lsp,
6518 struct nfs_seqid *seqid)
6519{
6520 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006521 struct nfs4_state *state = lsp->ls_state;
6522 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006523
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006524 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006525 if (p == NULL)
6526 return NULL;
6527 p->arg.fh = NFS_FH(inode);
6528 p->arg.fl = &p->fl;
6529 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006530 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006531 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006532 /* Ensure we don't close file until we're done freeing locks! */
6533 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006534 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006535 locks_init_lock(&p->fl);
6536 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006537 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006538 spin_lock(&state->state_lock);
6539 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6540 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006541 return p;
6542}
6543
Trond Myklebust06f814a2006-01-03 09:55:07 +01006544static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006545{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006546 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006547 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006548 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006549 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006550 put_nfs_open_context(calldata->ctx);
6551 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006552}
6553
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006554static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006555{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006556 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006557 struct nfs4_exception exception = {
6558 .inode = calldata->lsp->ls_state->inode,
6559 .stateid = &calldata->arg.stateid,
6560 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006561
Trond Myklebust14516c32010-07-31 14:29:06 -04006562 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6563 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006564 switch (task->tk_status) {
6565 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006566 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006567 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006568 if (nfs4_update_lock_stateid(calldata->lsp,
6569 &calldata->res.stateid))
6570 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006571 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006572 case -NFS4ERR_ADMIN_REVOKED:
6573 case -NFS4ERR_EXPIRED:
6574 nfs4_free_revoked_stateid(calldata->server,
6575 &calldata->arg.stateid,
6576 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006577 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006578 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006579 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006580 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6581 calldata->lsp))
6582 rpc_restart_call_prepare(task);
6583 break;
6584 case -NFS4ERR_OLD_STATEID:
6585 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6586 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006587 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006588 break;
6589 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006590 task->tk_status = nfs4_async_handle_exception(task,
6591 calldata->server, task->tk_status,
6592 &exception);
6593 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006594 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006595 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006596 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006597}
6598
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006599static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006600{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006601 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006603 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6604 nfs_async_iocounter_wait(task, calldata->l_ctx))
6605 return;
6606
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006607 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006608 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006609 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006610 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006611 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006612 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006613 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006614 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006615 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006616 &calldata->res.seq_res,
6617 task) != 0)
6618 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006619 return;
6620out_no_action:
6621 task->tk_action = NULL;
6622out_wait:
6623 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624}
6625
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006626static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006627 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006628 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006629 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006630};
6631
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006632static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6633 struct nfs_open_context *ctx,
6634 struct nfs4_lock_state *lsp,
6635 struct nfs_seqid *seqid)
6636{
6637 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006638 struct rpc_message msg = {
6639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6640 .rpc_cred = ctx->cred,
6641 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006642 struct rpc_task_setup task_setup_data = {
6643 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006644 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006645 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006646 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006647 .flags = RPC_TASK_ASYNC,
6648 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006649
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006650 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6651 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6652
Frank Filz137d6ac2007-07-09 15:32:29 -07006653 /* Ensure this is an unlock - when canceling a lock, the
6654 * canceled lock is passed in, and it won't be an unlock.
6655 */
6656 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006657 if (fl->fl_flags & FL_CLOSE)
6658 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006659
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006660 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6661 if (data == NULL) {
6662 nfs_free_seqid(seqid);
6663 return ERR_PTR(-ENOMEM);
6664 }
6665
Anna Schumakerfba83f32018-05-04 16:22:50 -04006666 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006667 msg.rpc_argp = &data->arg;
6668 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006669 task_setup_data.callback_data = data;
6670 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006671}
6672
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6674{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006675 struct inode *inode = state->inode;
6676 struct nfs4_state_owner *sp = state->owner;
6677 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006678 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006679 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006680 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006681 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006682 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006683 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684
Trond Myklebust9b073572006-06-29 16:38:34 -04006685 status = nfs4_set_lock_state(state, request);
6686 /* Unlock _before_ we do the RPC call */
6687 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006688 /* Exclude nfs_delegation_claim_locks() */
6689 mutex_lock(&sp->so_delegreturn_mutex);
6690 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006691 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006692 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006693 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006694 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006695 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006696 }
6697 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006698 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006699 if (status != 0)
6700 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006701 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006702 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006703 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6704 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006705 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6706 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006707 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006708 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006709 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006710 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006711 status = PTR_ERR(task);
6712 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006713 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006714 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006715 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006716out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006717 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006718 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006719 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720}
6721
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006722struct nfs4_lockdata {
6723 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006724 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006725 struct nfs4_lock_state *lsp;
6726 struct nfs_open_context *ctx;
6727 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006728 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006729 int rpc_status;
6730 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006731 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006732};
6733
6734static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006735 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6736 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006737{
6738 struct nfs4_lockdata *p;
6739 struct inode *inode = lsp->ls_state->inode;
6740 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006741 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006742
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006743 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006744 if (p == NULL)
6745 return NULL;
6746
6747 p->arg.fh = NFS_FH(inode);
6748 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006749 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006750 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006751 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006752 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6753 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006754 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006755 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006756 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006757 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006758 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006759 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006760 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006761 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006762 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006763 locks_init_lock(&p->fl);
6764 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006765 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006766out_free_seqid:
6767 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006768out_free:
6769 kfree(p);
6770 return NULL;
6771}
6772
6773static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6774{
6775 struct nfs4_lockdata *data = calldata;
6776 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777
Harvey Harrison3110ff82008-05-02 13:42:44 -07006778 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006779 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006780 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006781 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006782 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006783 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006784 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006785 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006786 nfs4_stateid_copy(&data->arg.open_stateid,
6787 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006788 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006789 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006790 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006791 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006792 nfs4_stateid_copy(&data->arg.lock_stateid,
6793 &data->lsp->ls_stateid);
6794 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006795 if (!nfs4_valid_open_stateid(state)) {
6796 data->rpc_status = -EBADF;
6797 task->tk_action = NULL;
6798 goto out_release_open_seqid;
6799 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006800 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006801 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006802 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006803 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006804 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006805 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006806out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006807 nfs_release_seqid(data->arg.open_seqid);
6808out_release_lock_seqid:
6809 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006810out_wait:
6811 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006812 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006813}
6814
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006815static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6816{
6817 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006818 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006819
Harvey Harrison3110ff82008-05-02 13:42:44 -07006820 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006821
Trond Myklebust14516c32010-07-31 14:29:06 -04006822 if (!nfs4_sequence_done(task, &data->res.seq_res))
6823 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006824
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006825 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006826 switch (task->tk_status) {
6827 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006828 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006829 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006830 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006831 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006832 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006833 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006834 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006835 if (data->arg.new_lock_owner != 0) {
6836 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6837 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6838 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006839 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6840 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006841 break;
6842 case -NFS4ERR_BAD_STATEID:
6843 case -NFS4ERR_OLD_STATEID:
6844 case -NFS4ERR_STALE_STATEID:
6845 case -NFS4ERR_EXPIRED:
6846 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006847 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006848 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006849 goto out_restart;
6850 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006851 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006852 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006853 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006854out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006855 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006856 return;
6857out_restart:
6858 if (!data->cancelled)
6859 rpc_restart_call_prepare(task);
6860 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006861}
6862
6863static void nfs4_lock_release(void *calldata)
6864{
6865 struct nfs4_lockdata *data = calldata;
6866
Harvey Harrison3110ff82008-05-02 13:42:44 -07006867 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006868 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006869 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006870 struct rpc_task *task;
6871 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6872 data->arg.lock_seqid);
6873 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006874 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006875 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006876 } else
6877 nfs_free_seqid(data->arg.lock_seqid);
6878 nfs4_put_lock_state(data->lsp);
6879 put_nfs_open_context(data->ctx);
6880 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006881 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006882}
6883
6884static const struct rpc_call_ops nfs4_lock_ops = {
6885 .rpc_call_prepare = nfs4_lock_prepare,
6886 .rpc_call_done = nfs4_lock_done,
6887 .rpc_release = nfs4_lock_release,
6888};
6889
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006890static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6891{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006892 switch (error) {
6893 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006894 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006895 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006896 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006897 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006898 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006899 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006900 break;
6901 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006902 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006903 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08006904 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006905}
6906
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006907static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006908{
6909 struct nfs4_lockdata *data;
6910 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006911 struct rpc_message msg = {
6912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6913 .rpc_cred = state->owner->so_cred,
6914 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006915 struct rpc_task_setup task_setup_data = {
6916 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006917 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006918 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006919 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006920 .flags = RPC_TASK_ASYNC,
6921 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006922 int ret;
6923
Harvey Harrison3110ff82008-05-02 13:42:44 -07006924 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006925 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006926 fl->fl_u.nfs4_fl.owner,
6927 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006928 if (data == NULL)
6929 return -ENOMEM;
6930 if (IS_SETLKW(cmd))
6931 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006932 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6933 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006934 msg.rpc_argp = &data->arg;
6935 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006936 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006937 if (recovery_type > NFS_LOCK_NEW) {
6938 if (recovery_type == NFS_LOCK_RECLAIM)
6939 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006940 } else
6941 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006942 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006943 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006944 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006945 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006946 if (ret == 0) {
6947 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006948 if (ret)
6949 nfs4_handle_setlk_error(data->server, data->lsp,
6950 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006951 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006952 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006953 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006954 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006955 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006956 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957}
6958
6959static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6960{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006961 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006962 struct nfs4_exception exception = {
6963 .inode = state->inode,
6964 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006965 int err;
6966
6967 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006968 /* Cache the lock if possible... */
6969 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6970 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006971 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006972 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006973 break;
6974 nfs4_handle_exception(server, err, &exception);
6975 } while (exception.retry);
6976 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977}
6978
6979static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6980{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006981 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006982 struct nfs4_exception exception = {
6983 .inode = state->inode,
6984 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006985 int err;
6986
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006987 err = nfs4_set_lock_state(state, request);
6988 if (err != 0)
6989 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006990 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006991 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6992 return 0;
6993 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006994 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006995 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6996 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006997 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006998 switch (err) {
6999 default:
7000 goto out;
7001 case -NFS4ERR_GRACE:
7002 case -NFS4ERR_DELAY:
7003 nfs4_handle_exception(server, err, &exception);
7004 err = 0;
7005 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007006 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007007out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007008 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009}
7010
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007011#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007012static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7013{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007014 struct nfs4_lock_state *lsp;
7015 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007016
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007017 status = nfs4_set_lock_state(state, request);
7018 if (status != 0)
7019 return status;
7020 lsp = request->fl_u.nfs4_fl.owner;
7021 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7022 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7023 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007024 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007025}
7026#endif
7027
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7029{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007030 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007031 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007032 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007033 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034
Trond Myklebust01c3b862006-06-29 16:38:39 -04007035 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007036 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007037 if (status < 0)
7038 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007039 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007040 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007041 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007042 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007043 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007044 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007045 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007046 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007047 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007048 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007049 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007050 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007051 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007052 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007053out:
7054 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 return status;
7056}
7057
7058static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7059{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007060 struct nfs4_exception exception = {
7061 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007062 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007063 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007064 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 int err;
7066
7067 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007068 err = _nfs4_proc_setlk(state, cmd, request);
7069 if (err == -NFS4ERR_DENIED)
7070 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007072 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 } while (exception.retry);
7074 return err;
7075}
7076
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007077#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7078#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7079
7080static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007081nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7082 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007083{
7084 int status = -ERESTARTSYS;
7085 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7086
7087 while(!signalled()) {
7088 status = nfs4_proc_setlk(state, cmd, request);
7089 if ((status != -EAGAIN) || IS_SETLK(cmd))
7090 break;
7091 freezable_schedule_timeout_interruptible(timeout);
7092 timeout *= 2;
7093 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7094 status = -ERESTARTSYS;
7095 }
7096 return status;
7097}
7098
Jeff Laytona1d617d82016-09-17 18:17:39 -04007099#ifdef CONFIG_NFS_V4_1
7100struct nfs4_lock_waiter {
7101 struct task_struct *task;
7102 struct inode *inode;
7103 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007104};
7105
7106static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007107nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007108{
7109 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007110 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007111
Jeff Layton57174592018-03-18 08:37:03 -04007112 /* NULL key means to wake up everyone */
7113 if (key) {
7114 struct cb_notify_lock_args *cbnl = key;
7115 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7116 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007117
Jeff Layton57174592018-03-18 08:37:03 -04007118 /* Only wake if the callback was for the same owner. */
7119 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7120 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007121
Jeff Layton57174592018-03-18 08:37:03 -04007122 /* Make sure it's for the right inode */
7123 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7124 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007125 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007126
7127 /* override "private" so we can use default_wake_function */
7128 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007129 ret = woken_wake_function(wait, mode, flags, key);
7130 if (ret)
7131 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007132 wait->private = waiter;
7133 return ret;
7134}
7135
7136static int
7137nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7138{
7139 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007140 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7141 struct nfs_server *server = NFS_SERVER(state->inode);
7142 struct nfs_client *clp = server->nfs_client;
7143 wait_queue_head_t *q = &clp->cl_lock_waitq;
7144 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7145 .id = lsp->ls_seqid.owner_id,
7146 .s_dev = server->s_dev };
7147 struct nfs4_lock_waiter waiter = { .task = current,
7148 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007149 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007150 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007151
7152 /* Don't bother with waitqueue if we don't expect a callback */
7153 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7154 return nfs4_retry_setlk_simple(state, cmd, request);
7155
7156 init_wait(&wait);
7157 wait.private = &waiter;
7158 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007159
7160 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007161 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007162 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007163 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7164 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007165 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007166 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007167
7168 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007169 freezer_do_not_count();
7170 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7171 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007172 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007173 }
7174
Jeff Laytona1d617d82016-09-17 18:17:39 -04007175 return status;
7176}
7177#else /* !CONFIG_NFS_V4_1 */
7178static inline int
7179nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7180{
7181 return nfs4_retry_setlk_simple(state, cmd, request);
7182}
7183#endif
7184
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185static int
7186nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7187{
7188 struct nfs_open_context *ctx;
7189 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 int status;
7191
7192 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007193 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194 state = ctx->state;
7195
Trond Myklebustd9531262009-07-21 19:22:38 -04007196 if (IS_GETLK(cmd)) {
7197 if (state != NULL)
7198 return nfs4_proc_getlk(state, F_GETLK, request);
7199 return 0;
7200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201
7202 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7203 return -EINVAL;
7204
Trond Myklebustd9531262009-07-21 19:22:38 -04007205 if (request->fl_type == F_UNLCK) {
7206 if (state != NULL)
7207 return nfs4_proc_unlck(state, cmd, request);
7208 return 0;
7209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210
Trond Myklebustd9531262009-07-21 19:22:38 -04007211 if (state == NULL)
7212 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007213
7214 if ((request->fl_flags & FL_POSIX) &&
7215 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7216 return -ENOLCK;
7217
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007218 /*
7219 * Don't rely on the VFS having checked the file open mode,
7220 * since it won't do this for flock() locks.
7221 */
7222 switch (request->fl_type) {
7223 case F_RDLCK:
7224 if (!(filp->f_mode & FMODE_READ))
7225 return -EBADF;
7226 break;
7227 case F_WRLCK:
7228 if (!(filp->f_mode & FMODE_WRITE))
7229 return -EBADF;
7230 }
7231
Jeff Layton1ea67db2016-09-17 18:17:37 -04007232 status = nfs4_set_lock_state(state, request);
7233 if (status != 0)
7234 return status;
7235
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007236 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237}
7238
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007239int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007240{
7241 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007242 int err;
7243
7244 err = nfs4_set_lock_state(state, fl);
7245 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007246 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007247 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007248 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007249}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007250
Trond Myklebustcf470c32012-03-07 13:49:12 -05007251struct nfs_release_lockowner_data {
7252 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007253 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007254 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007255 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007256 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007257};
7258
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007259static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7260{
7261 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007262 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007263 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7264 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007265 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007266 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007267}
7268
7269static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7270{
7271 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007272 struct nfs_server *server = data->server;
7273
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007274 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007275
7276 switch (task->tk_status) {
7277 case 0:
7278 renew_lease(server, data->timestamp);
7279 break;
7280 case -NFS4ERR_STALE_CLIENTID:
7281 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007282 nfs4_schedule_lease_recovery(server->nfs_client);
7283 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007284 case -NFS4ERR_LEASE_MOVED:
7285 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007286 if (nfs4_async_handle_error(task, server,
7287 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007288 rpc_restart_call_prepare(task);
7289 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007290}
7291
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007292static void nfs4_release_lockowner_release(void *calldata)
7293{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007294 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007295 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007296 kfree(calldata);
7297}
7298
Trond Myklebust17280172012-03-11 13:11:00 -04007299static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007300 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7301 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007302 .rpc_release = nfs4_release_lockowner_release,
7303};
7304
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007305static void
7306nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007307{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007308 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007309 struct rpc_message msg = {
7310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7311 };
7312
7313 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007314 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007315
Trond Myklebustcf470c32012-03-07 13:49:12 -05007316 data = kmalloc(sizeof(*data), GFP_NOFS);
7317 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007318 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007319 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007320 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007321 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7322 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7323 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007324
Trond Myklebustcf470c32012-03-07 13:49:12 -05007325 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007326 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007327 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007328 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007329}
7330
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007331#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7332
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007333static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007334 struct dentry *unused, struct inode *inode,
7335 const char *key, const void *buf,
7336 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007337{
Al Viro59301222016-05-27 10:19:30 -04007338 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007339}
7340
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007341static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007342 struct dentry *unused, struct inode *inode,
7343 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007344{
Al Virob2968212016-04-10 20:48:24 -04007345 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007346}
7347
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007348static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007349{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007350 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007351}
7352
David Quigleyc9bccef2013-05-22 12:50:45 -04007353#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007354
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007355static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007356 struct dentry *unused, struct inode *inode,
7357 const char *key, const void *buf,
7358 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007359{
7360 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007361 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007362
7363 return -EOPNOTSUPP;
7364}
7365
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007366static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007367 struct dentry *unused, struct inode *inode,
7368 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007369{
7370 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007371 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007372 return -EOPNOTSUPP;
7373}
7374
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007375static ssize_t
7376nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007377{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007378 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007379
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007380 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7381 len = security_inode_listsecurity(inode, list, list_len);
7382 if (list_len && len > list_len)
7383 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007384 }
7385 return len;
7386}
7387
7388static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7389 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007390 .get = nfs4_xattr_get_nfs4_label,
7391 .set = nfs4_xattr_set_nfs4_label,
7392};
David Quigleyc9bccef2013-05-22 12:50:45 -04007393
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007394#else
7395
7396static ssize_t
7397nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7398{
7399 return 0;
7400}
7401
7402#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007403
Andy Adamson533eb462011-06-13 18:25:56 -04007404/*
7405 * nfs_fhget will use either the mounted_on_fileid or the fileid
7406 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007407static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7408{
Andy Adamson533eb462011-06-13 18:25:56 -04007409 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7410 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7411 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007412 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007413 return;
7414
7415 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007416 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007417 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7418 fattr->nlink = 2;
7419}
7420
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007421static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7422 const struct qstr *name,
7423 struct nfs4_fs_locations *fs_locations,
7424 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007425{
7426 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007427 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007428 struct nfs4_fs_locations_arg args = {
7429 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007430 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007431 .page = page,
7432 .bitmask = bitmask,
7433 };
Benny Halevy22958462009-04-01 09:22:02 -04007434 struct nfs4_fs_locations_res res = {
7435 .fs_locations = fs_locations,
7436 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007437 struct rpc_message msg = {
7438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7439 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007440 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007441 };
7442 int status;
7443
Harvey Harrison3110ff82008-05-02 13:42:44 -07007444 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007445
Chuck Leverc05cefc2017-11-05 15:45:22 -05007446 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7447 bitmask[1] = nfs4_fattr_bitmap[1];
7448
Andy Adamson533eb462011-06-13 18:25:56 -04007449 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7450 * is not supported */
7451 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007452 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007453 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007454 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007455
Trond Myklebustc228fd32007-01-13 02:28:11 -05007456 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007457 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007458 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007459 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007460 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007461 return status;
7462}
7463
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007464int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7465 const struct qstr *name,
7466 struct nfs4_fs_locations *fs_locations,
7467 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007468{
Trond Myklebust0688e642019-04-07 13:59:09 -04007469 struct nfs4_exception exception = {
7470 .interruptible = true,
7471 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007472 int err;
7473 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007474 err = _nfs4_proc_fs_locations(client, dir, name,
7475 fs_locations, page);
7476 trace_nfs4_get_fs_locations(dir, name, err);
7477 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007478 &exception);
7479 } while (exception.retry);
7480 return err;
7481}
7482
Chuck Leverb03d7352013-10-17 14:12:50 -04007483/*
7484 * This operation also signals the server that this client is
7485 * performing migration recovery. The server can stop returning
7486 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7487 * appended to this compound to identify the client ID which is
7488 * performing recovery.
7489 */
7490static int _nfs40_proc_get_locations(struct inode *inode,
7491 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007492 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007493{
7494 struct nfs_server *server = NFS_SERVER(inode);
7495 struct rpc_clnt *clnt = server->client;
7496 u32 bitmask[2] = {
7497 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7498 };
7499 struct nfs4_fs_locations_arg args = {
7500 .clientid = server->nfs_client->cl_clientid,
7501 .fh = NFS_FH(inode),
7502 .page = page,
7503 .bitmask = bitmask,
7504 .migration = 1, /* skip LOOKUP */
7505 .renew = 1, /* append RENEW */
7506 };
7507 struct nfs4_fs_locations_res res = {
7508 .fs_locations = locations,
7509 .migration = 1,
7510 .renew = 1,
7511 };
7512 struct rpc_message msg = {
7513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7514 .rpc_argp = &args,
7515 .rpc_resp = &res,
7516 .rpc_cred = cred,
7517 };
7518 unsigned long now = jiffies;
7519 int status;
7520
7521 nfs_fattr_init(&locations->fattr);
7522 locations->server = server;
7523 locations->nlocations = 0;
7524
Anna Schumakerfba83f32018-05-04 16:22:50 -04007525 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007526 status = nfs4_call_sync_sequence(clnt, server, &msg,
7527 &args.seq_args, &res.seq_res);
7528 if (status)
7529 return status;
7530
7531 renew_lease(server, now);
7532 return 0;
7533}
7534
7535#ifdef CONFIG_NFS_V4_1
7536
7537/*
7538 * This operation also signals the server that this client is
7539 * performing migration recovery. The server can stop asserting
7540 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7541 * performing this operation is identified in the SEQUENCE
7542 * operation in this compound.
7543 *
7544 * When the client supports GETATTR(fs_locations_info), it can
7545 * be plumbed in here.
7546 */
7547static int _nfs41_proc_get_locations(struct inode *inode,
7548 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007549 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007550{
7551 struct nfs_server *server = NFS_SERVER(inode);
7552 struct rpc_clnt *clnt = server->client;
7553 u32 bitmask[2] = {
7554 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7555 };
7556 struct nfs4_fs_locations_arg args = {
7557 .fh = NFS_FH(inode),
7558 .page = page,
7559 .bitmask = bitmask,
7560 .migration = 1, /* skip LOOKUP */
7561 };
7562 struct nfs4_fs_locations_res res = {
7563 .fs_locations = locations,
7564 .migration = 1,
7565 };
7566 struct rpc_message msg = {
7567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7568 .rpc_argp = &args,
7569 .rpc_resp = &res,
7570 .rpc_cred = cred,
7571 };
7572 int status;
7573
7574 nfs_fattr_init(&locations->fattr);
7575 locations->server = server;
7576 locations->nlocations = 0;
7577
Anna Schumakerfba83f32018-05-04 16:22:50 -04007578 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007579 status = nfs4_call_sync_sequence(clnt, server, &msg,
7580 &args.seq_args, &res.seq_res);
7581 if (status == NFS4_OK &&
7582 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7583 status = -NFS4ERR_LEASE_MOVED;
7584 return status;
7585}
7586
7587#endif /* CONFIG_NFS_V4_1 */
7588
7589/**
7590 * nfs4_proc_get_locations - discover locations for a migrated FSID
7591 * @inode: inode on FSID that is migrating
7592 * @locations: result of query
7593 * @page: buffer
7594 * @cred: credential to use for this operation
7595 *
7596 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7597 * operation failed, or a negative errno if a local error occurred.
7598 *
7599 * On success, "locations" is filled in, but if the server has
7600 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7601 * asserted.
7602 *
7603 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7604 * from this client that require migration recovery.
7605 */
7606int nfs4_proc_get_locations(struct inode *inode,
7607 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007608 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007609{
7610 struct nfs_server *server = NFS_SERVER(inode);
7611 struct nfs_client *clp = server->nfs_client;
7612 const struct nfs4_mig_recovery_ops *ops =
7613 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007614 struct nfs4_exception exception = {
7615 .interruptible = true,
7616 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007617 int status;
7618
7619 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7620 (unsigned long long)server->fsid.major,
7621 (unsigned long long)server->fsid.minor,
7622 clp->cl_hostname);
7623 nfs_display_fhandle(NFS_FH(inode), __func__);
7624
7625 do {
7626 status = ops->get_locations(inode, locations, page, cred);
7627 if (status != -NFS4ERR_DELAY)
7628 break;
7629 nfs4_handle_exception(server, status, &exception);
7630 } while (exception.retry);
7631 return status;
7632}
7633
Chuck Lever44c99932013-10-17 14:13:30 -04007634/*
7635 * This operation also signals the server that this client is
7636 * performing "lease moved" recovery. The server can stop
7637 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7638 * is appended to this compound to identify the client ID which is
7639 * performing recovery.
7640 */
NeilBrowna52458b2018-12-03 11:30:31 +11007641static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007642{
7643 struct nfs_server *server = NFS_SERVER(inode);
7644 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7645 struct rpc_clnt *clnt = server->client;
7646 struct nfs4_fsid_present_arg args = {
7647 .fh = NFS_FH(inode),
7648 .clientid = clp->cl_clientid,
7649 .renew = 1, /* append RENEW */
7650 };
7651 struct nfs4_fsid_present_res res = {
7652 .renew = 1,
7653 };
7654 struct rpc_message msg = {
7655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7656 .rpc_argp = &args,
7657 .rpc_resp = &res,
7658 .rpc_cred = cred,
7659 };
7660 unsigned long now = jiffies;
7661 int status;
7662
7663 res.fh = nfs_alloc_fhandle();
7664 if (res.fh == NULL)
7665 return -ENOMEM;
7666
Anna Schumakerfba83f32018-05-04 16:22:50 -04007667 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007668 status = nfs4_call_sync_sequence(clnt, server, &msg,
7669 &args.seq_args, &res.seq_res);
7670 nfs_free_fhandle(res.fh);
7671 if (status)
7672 return status;
7673
7674 do_renew_lease(clp, now);
7675 return 0;
7676}
7677
7678#ifdef CONFIG_NFS_V4_1
7679
7680/*
7681 * This operation also signals the server that this client is
7682 * performing "lease moved" recovery. The server can stop asserting
7683 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7684 * this operation is identified in the SEQUENCE operation in this
7685 * compound.
7686 */
NeilBrowna52458b2018-12-03 11:30:31 +11007687static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007688{
7689 struct nfs_server *server = NFS_SERVER(inode);
7690 struct rpc_clnt *clnt = server->client;
7691 struct nfs4_fsid_present_arg args = {
7692 .fh = NFS_FH(inode),
7693 };
7694 struct nfs4_fsid_present_res res = {
7695 };
7696 struct rpc_message msg = {
7697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7698 .rpc_argp = &args,
7699 .rpc_resp = &res,
7700 .rpc_cred = cred,
7701 };
7702 int status;
7703
7704 res.fh = nfs_alloc_fhandle();
7705 if (res.fh == NULL)
7706 return -ENOMEM;
7707
Anna Schumakerfba83f32018-05-04 16:22:50 -04007708 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007709 status = nfs4_call_sync_sequence(clnt, server, &msg,
7710 &args.seq_args, &res.seq_res);
7711 nfs_free_fhandle(res.fh);
7712 if (status == NFS4_OK &&
7713 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7714 status = -NFS4ERR_LEASE_MOVED;
7715 return status;
7716}
7717
7718#endif /* CONFIG_NFS_V4_1 */
7719
7720/**
7721 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7722 * @inode: inode on FSID to check
7723 * @cred: credential to use for this operation
7724 *
7725 * Server indicates whether the FSID is present, moved, or not
7726 * recognized. This operation is necessary to clear a LEASE_MOVED
7727 * condition for this client ID.
7728 *
7729 * Returns NFS4_OK if the FSID is present on this server,
7730 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7731 * NFS4ERR code if some error occurred on the server, or a
7732 * negative errno if a local failure occurred.
7733 */
NeilBrowna52458b2018-12-03 11:30:31 +11007734int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007735{
7736 struct nfs_server *server = NFS_SERVER(inode);
7737 struct nfs_client *clp = server->nfs_client;
7738 const struct nfs4_mig_recovery_ops *ops =
7739 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007740 struct nfs4_exception exception = {
7741 .interruptible = true,
7742 };
Chuck Lever44c99932013-10-17 14:13:30 -04007743 int status;
7744
7745 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7746 (unsigned long long)server->fsid.major,
7747 (unsigned long long)server->fsid.minor,
7748 clp->cl_hostname);
7749 nfs_display_fhandle(NFS_FH(inode), __func__);
7750
7751 do {
7752 status = ops->fsid_present(inode, cred);
7753 if (status != -NFS4ERR_DELAY)
7754 break;
7755 nfs4_handle_exception(server, status, &exception);
7756 } while (exception.retry);
7757 return status;
7758}
7759
Trond Myklebust302fad72019-02-18 13:32:38 -05007760/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007761 * If 'use_integrity' is true and the state managment nfs_client
7762 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7763 * and the machine credential as per RFC3530bis and RFC5661 Security
7764 * Considerations sections. Otherwise, just use the user cred with the
7765 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007766 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007767static 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 +00007768{
7769 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04007770 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7771 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007772 struct nfs4_secinfo_arg args = {
7773 .dir_fh = NFS_FH(dir),
7774 .name = name,
7775 };
7776 struct nfs4_secinfo_res res = {
7777 .flavors = flavors,
7778 };
7779 struct rpc_message msg = {
7780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7781 .rpc_argp = &args,
7782 .rpc_resp = &res,
7783 };
Anna Schumaker50493362019-08-14 15:30:16 -04007784 struct nfs4_call_sync_data data = {
7785 .seq_server = NFS_SERVER(dir),
7786 .seq_args = &args.seq_args,
7787 .seq_res = &res.seq_res,
7788 };
7789 struct rpc_task_setup task_setup = {
7790 .rpc_client = clnt,
7791 .rpc_message = &msg,
7792 .callback_ops = clp->cl_mvops->call_sync_ops,
7793 .callback_data = &data,
7794 .flags = RPC_TASK_NO_ROUND_ROBIN,
7795 };
NeilBrowna52458b2018-12-03 11:30:31 +11007796 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007797
7798 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04007799 clnt = clp->cl_rpcclient;
7800 task_setup.rpc_client = clnt;
7801
7802 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007803 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007804 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007805
7806 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007807
Anna Schumaker50493362019-08-14 15:30:16 -04007808 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7809 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7810 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007811
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007812 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007813
NeilBrowna52458b2018-12-03 11:30:31 +11007814 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007815 return status;
7816}
7817
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007818int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7819 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007820{
Trond Myklebust0688e642019-04-07 13:59:09 -04007821 struct nfs4_exception exception = {
7822 .interruptible = true,
7823 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007824 int err;
7825 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007826 err = -NFS4ERR_WRONGSEC;
7827
7828 /* try to use integrity protection with machine cred */
7829 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7830 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7831
7832 /*
7833 * if unable to use integrity protection, or SECINFO with
7834 * integrity protection returns NFS4ERR_WRONGSEC (which is
7835 * disallowed by spec, but exists in deployed servers) use
7836 * the current filesystem's rpc_client and the user cred.
7837 */
7838 if (err == -NFS4ERR_WRONGSEC)
7839 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7840
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007841 trace_nfs4_secinfo(dir, name, err);
7842 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007843 &exception);
7844 } while (exception.retry);
7845 return err;
7846}
7847
Andy Adamson557134a2009-04-01 09:21:53 -04007848#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007849/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007850 * Check the exchange flags returned by the server for invalid flags, having
7851 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7852 * DS flags set.
7853 */
7854static int nfs4_check_cl_exchange_flags(u32 flags)
7855{
7856 if (flags & ~EXCHGID4_FLAG_MASK_R)
7857 goto out_inval;
7858 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7859 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7860 goto out_inval;
7861 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7862 goto out_inval;
7863 return NFS_OK;
7864out_inval:
7865 return -NFS4ERR_INVAL;
7866}
7867
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007868static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007869nfs41_same_server_scope(struct nfs41_server_scope *a,
7870 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007871{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007872 if (a->server_scope_sz != b->server_scope_sz)
7873 return false;
7874 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007875}
7876
Andy Adamson02a95de2016-02-05 16:08:37 -05007877static void
7878nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7879{
Trond Myklebust5c441542019-11-13 08:34:00 +01007880 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
7881 struct nfs_client *clp = args->client;
7882
7883 switch (task->tk_status) {
7884 case -NFS4ERR_BADSESSION:
7885 case -NFS4ERR_DEADSESSION:
7886 nfs4_schedule_session_recovery(clp->cl_session,
7887 task->tk_status);
7888 }
Andy Adamson02a95de2016-02-05 16:08:37 -05007889}
7890
7891static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7892 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7893};
7894
Andy Adamson357f54d2010-12-14 10:11:57 -05007895/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007896 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007897 *
7898 * The 4.1 client currently uses the same TCP connection for the
7899 * fore and backchannel.
7900 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007901static
7902int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7903 struct rpc_xprt *xprt,
7904 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007905 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007906{
7907 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007908 struct nfs41_bind_conn_to_session_args args = {
7909 .client = clp,
7910 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7911 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007912 struct nfs41_bind_conn_to_session_res res;
7913 struct rpc_message msg = {
7914 .rpc_proc =
7915 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007916 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007917 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007918 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007919 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007920 struct rpc_task_setup task_setup_data = {
7921 .rpc_client = clnt,
7922 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007923 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007924 .rpc_message = &msg,
7925 .flags = RPC_TASK_TIMEOUT,
7926 };
7927 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007928
Trond Myklebust71a097c2015-02-18 09:27:18 -08007929 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7930 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7931 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007932
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007933 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7934 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7935 args.dir = NFS4_CDFC4_FORE;
7936
7937 task = rpc_run_task(&task_setup_data);
7938 if (!IS_ERR(task)) {
7939 status = task->tk_status;
7940 rpc_put_task(task);
7941 } else
7942 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007943 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007944 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007945 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007946 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7947 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007948 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007949 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007950 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007951 dprintk("NFS: %s: Unexpected direction from server\n",
7952 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007953 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007954 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007955 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007956 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7957 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007958 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007959 }
7960 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007961
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007962 return status;
7963}
7964
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007965struct rpc_bind_conn_calldata {
7966 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007967 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007968};
7969
7970static int
7971nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7972 struct rpc_xprt *xprt,
7973 void *calldata)
7974{
7975 struct rpc_bind_conn_calldata *p = calldata;
7976
7977 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7978}
7979
NeilBrowna52458b2018-12-03 11:30:31 +11007980int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007981{
7982 struct rpc_bind_conn_calldata data = {
7983 .clp = clp,
7984 .cred = cred,
7985 };
7986 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7987 nfs4_proc_bind_conn_to_session_callback, &data);
7988}
7989
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007990/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007991 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7992 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007993 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007994static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7995 .how = SP4_MACH_CRED,
7996 .enforce.u.words = {
7997 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7998 1 << (OP_EXCHANGE_ID - 32) |
7999 1 << (OP_CREATE_SESSION - 32) |
8000 1 << (OP_DESTROY_SESSION - 32) |
8001 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008002 },
8003 .allow.u.words = {
8004 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008005 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008006 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008007 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008008 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008009 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008010 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008011 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008012 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008013 1 << (OP_FREE_STATEID - 32) |
8014 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008015 }
8016};
8017
8018/*
8019 * Select the state protection mode for client `clp' given the server results
8020 * from exchange_id in `sp'.
8021 *
8022 * Returns 0 on success, negative errno otherwise.
8023 */
8024static int nfs4_sp4_select_mode(struct nfs_client *clp,
8025 struct nfs41_state_protection *sp)
8026{
8027 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8028 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8029 1 << (OP_EXCHANGE_ID - 32) |
8030 1 << (OP_CREATE_SESSION - 32) |
8031 1 << (OP_DESTROY_SESSION - 32) |
8032 1 << (OP_DESTROY_CLIENTID - 32)
8033 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008034 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008035 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008036 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008037
8038 if (sp->how == SP4_MACH_CRED) {
8039 /* Print state protect result */
8040 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8041 for (i = 0; i <= LAST_NFS4_OP; i++) {
8042 if (test_bit(i, sp->enforce.u.longs))
8043 dfprintk(MOUNT, " enforce op %d\n", i);
8044 if (test_bit(i, sp->allow.u.longs))
8045 dfprintk(MOUNT, " allow op %d\n", i);
8046 }
8047
8048 /* make sure nothing is on enforce list that isn't supported */
8049 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8050 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8051 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008052 ret = -EINVAL;
8053 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008054 }
8055 }
8056
8057 /*
8058 * Minimal mode - state operations are allowed to use machine
8059 * credential. Note this already happens by default, so the
8060 * client doesn't have to do anything more than the negotiation.
8061 *
8062 * NOTE: we don't care if EXCHANGE_ID is in the list -
8063 * we're already using the machine cred for exchange_id
8064 * and will never use a different cred.
8065 */
8066 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8067 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8068 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8069 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8070 dfprintk(MOUNT, "sp4_mach_cred:\n");
8071 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008072 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008073 } else {
8074 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008075 ret = -EINVAL;
8076 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008077 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008078
8079 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008080 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8081 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008082 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8083 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008084 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008085 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008086
Andrew Elble99ade3c2015-12-02 09:39:51 -05008087 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8088 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008089 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008090 }
8091
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008092 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8093 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8094 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008095 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008096 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008097
8098 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8099 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8100 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008101 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008102 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008103
8104 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8105 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008106 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008107 }
8108
8109 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8110 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008111 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008112 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008113 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008114out:
8115 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008116 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008117}
8118
Andy Adamson8d89bd72016-09-09 09:22:18 -04008119struct nfs41_exchange_id_data {
8120 struct nfs41_exchange_id_res res;
8121 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008122};
8123
Andy Adamson8d89bd72016-09-09 09:22:18 -04008124static void nfs4_exchange_id_release(void *data)
8125{
8126 struct nfs41_exchange_id_data *cdata =
8127 (struct nfs41_exchange_id_data *)data;
8128
Olga Kornievskaia63513232017-03-13 10:36:19 -04008129 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008130 kfree(cdata->res.impl_id);
8131 kfree(cdata->res.server_scope);
8132 kfree(cdata->res.server_owner);
8133 kfree(cdata);
8134}
8135
8136static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008137 .rpc_release = nfs4_exchange_id_release,
8138};
8139
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008140/*
8141 * _nfs4_proc_exchange_id()
8142 *
8143 * Wrapper for EXCHANGE_ID operation.
8144 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008145static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008146nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008147 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008148{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008149 struct rpc_message msg = {
8150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008151 .rpc_cred = cred,
8152 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008153 struct rpc_task_setup task_setup_data = {
8154 .rpc_client = clp->cl_rpcclient,
8155 .callback_ops = &nfs4_exchange_id_call_ops,
8156 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008157 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008158 };
8159 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008160 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008161
Elena Reshetova212bf412017-10-20 12:53:38 +03008162 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008163 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008164
Trond Myklebust9c760d12017-07-31 18:38:50 -04008165 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008166 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008167 if (!calldata)
8168 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008169
Trond Myklebustfd405592017-08-01 16:02:47 -04008170 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008171
8172 status = nfs4_init_uniform_client_string(clp);
8173 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008174 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008175
Andy Adamson8d89bd72016-09-09 09:22:18 -04008176 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8177 GFP_NOFS);
8178 status = -ENOMEM;
8179 if (unlikely(calldata->res.server_owner == NULL))
8180 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008181
Andy Adamson8d89bd72016-09-09 09:22:18 -04008182 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008183 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008184 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008185 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008186
Andy Adamson8d89bd72016-09-09 09:22:18 -04008187 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8188 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008189 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008190
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008191 switch (sp4_how) {
8192 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008193 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008194 break;
8195
8196 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008197 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008198 break;
8199
8200 default:
8201 /* unsupported! */
8202 WARN_ON_ONCE(1);
8203 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008204 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008205 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008206 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008207 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008208 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008209 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8210 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008211 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008212 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008213 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8214 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008215#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008216 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008217#endif
8218 msg.rpc_argp = &calldata->args;
8219 msg.rpc_resp = &calldata->res;
8220 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008221
Trond Myklebust9c760d12017-07-31 18:38:50 -04008222 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008223
8224out_impl_id:
8225 kfree(calldata->res.impl_id);
8226out_server_scope:
8227 kfree(calldata->res.server_scope);
8228out_server_owner:
8229 kfree(calldata->res.server_owner);
8230out_calldata:
8231 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008232out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008233 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008234 return ERR_PTR(status);
8235}
8236
8237/*
8238 * _nfs4_proc_exchange_id()
8239 *
8240 * Wrapper for EXCHANGE_ID operation.
8241 */
NeilBrowna52458b2018-12-03 11:30:31 +11008242static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008243 u32 sp4_how)
8244{
8245 struct rpc_task *task;
8246 struct nfs41_exchange_id_args *argp;
8247 struct nfs41_exchange_id_res *resp;
8248 int status;
8249
8250 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8251 if (IS_ERR(task))
8252 return PTR_ERR(task);
8253
8254 argp = task->tk_msg.rpc_argp;
8255 resp = task->tk_msg.rpc_resp;
8256 status = task->tk_status;
8257 if (status != 0)
8258 goto out;
8259
8260 status = nfs4_check_cl_exchange_flags(resp->flags);
8261 if (status != 0)
8262 goto out;
8263
8264 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8265 if (status != 0)
8266 goto out;
8267
8268 clp->cl_clientid = resp->clientid;
8269 clp->cl_exchange_flags = resp->flags;
8270 clp->cl_seqid = resp->seqid;
8271 /* Client ID is not confirmed */
8272 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8273 clear_bit(NFS4_SESSION_ESTABLISHED,
8274 &clp->cl_session->session_state);
8275
8276 if (clp->cl_serverscope != NULL &&
8277 !nfs41_same_server_scope(clp->cl_serverscope,
8278 resp->server_scope)) {
8279 dprintk("%s: server_scope mismatch detected\n",
8280 __func__);
8281 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8282 }
8283
8284 swap(clp->cl_serverowner, resp->server_owner);
8285 swap(clp->cl_serverscope, resp->server_scope);
8286 swap(clp->cl_implid, resp->impl_id);
8287
8288 /* Save the EXCHANGE_ID verifier session trunk tests */
8289 memcpy(clp->cl_confirm.data, argp->verifier.data,
8290 sizeof(clp->cl_confirm.data));
8291out:
8292 trace_nfs4_exchange_id(clp, status);
8293 rpc_put_task(task);
8294 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008295}
8296
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008297/*
8298 * nfs4_proc_exchange_id()
8299 *
8300 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8301 *
8302 * Since the clientid has expired, all compounds using sessions
8303 * associated with the stale clientid will be returning
8304 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8305 * be in some phase of session reset.
8306 *
8307 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8308 */
NeilBrowna52458b2018-12-03 11:30:31 +11008309int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008310{
8311 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8312 int status;
8313
8314 /* try SP4_MACH_CRED if krb5i/p */
8315 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8316 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008317 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008318 if (!status)
8319 return 0;
8320 }
8321
8322 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008323 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008324}
8325
Andy Adamson04fa2c62016-09-09 09:22:29 -04008326/**
8327 * nfs4_test_session_trunk
8328 *
8329 * This is an add_xprt_test() test function called from
8330 * rpc_clnt_setup_test_and_add_xprt.
8331 *
8332 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8333 * and is dereferrenced in nfs4_exchange_id_release
8334 *
8335 * Upon success, add the new transport to the rpc_clnt
8336 *
8337 * @clnt: struct rpc_clnt to get new transport
8338 * @xprt: the rpc_xprt to test
8339 * @data: call data for _nfs4_proc_exchange_id.
8340 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308341void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008342 void *data)
8343{
8344 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008345 struct rpc_task *task;
8346 int status;
8347
Andy Adamson04fa2c62016-09-09 09:22:29 -04008348 u32 sp4_how;
8349
8350 dprintk("--> %s try %s\n", __func__,
8351 xprt->address_strings[RPC_DISPLAY_ADDR]);
8352
8353 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8354
8355 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008356 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8357 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308358 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008359
8360 status = task->tk_status;
8361 if (status == 0)
8362 status = nfs4_detect_session_trunking(adata->clp,
8363 task->tk_msg.rpc_resp, xprt);
8364
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308365 if (status == 0)
8366 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8367
Trond Myklebust9c760d12017-07-31 18:38:50 -04008368 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008369}
8370EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8371
Trond Myklebust66245532012-05-25 17:18:09 -04008372static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008373 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008374{
8375 struct rpc_message msg = {
8376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8377 .rpc_argp = clp,
8378 .rpc_cred = cred,
8379 };
8380 int status;
8381
NeilBrown5a0c2572019-05-30 10:41:28 +10008382 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8383 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008384 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008385 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008386 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008387 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8388 return status;
8389}
8390
8391static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008392 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008393{
8394 unsigned int loop;
8395 int ret;
8396
8397 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8398 ret = _nfs4_proc_destroy_clientid(clp, cred);
8399 switch (ret) {
8400 case -NFS4ERR_DELAY:
8401 case -NFS4ERR_CLIENTID_BUSY:
8402 ssleep(1);
8403 break;
8404 default:
8405 return ret;
8406 }
8407 }
8408 return 0;
8409}
8410
8411int nfs4_destroy_clientid(struct nfs_client *clp)
8412{
NeilBrowna52458b2018-12-03 11:30:31 +11008413 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008414 int ret = 0;
8415
8416 if (clp->cl_mvops->minor_version < 1)
8417 goto out;
8418 if (clp->cl_exchange_flags == 0)
8419 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008420 if (clp->cl_preserve_clid)
8421 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008422 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008423 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008424 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008425 switch (ret) {
8426 case 0:
8427 case -NFS4ERR_STALE_CLIENTID:
8428 clp->cl_exchange_flags = 0;
8429 }
8430out:
8431 return ret;
8432}
8433
Donald Buczek0efb01b2019-07-07 21:26:08 +02008434#endif /* CONFIG_NFS_V4_1 */
8435
Andy Adamson2050f0c2009-04-01 09:22:30 -04008436struct nfs4_get_lease_time_data {
8437 struct nfs4_get_lease_time_args *args;
8438 struct nfs4_get_lease_time_res *res;
8439 struct nfs_client *clp;
8440};
8441
8442static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8443 void *calldata)
8444{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008445 struct nfs4_get_lease_time_data *data =
8446 (struct nfs4_get_lease_time_data *)calldata;
8447
8448 dprintk("--> %s\n", __func__);
8449 /* just setup sequence, do not trigger session recovery
8450 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008451 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008452 &data->args->la_seq_args,
8453 &data->res->lr_seq_res,
8454 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008455 dprintk("<-- %s\n", __func__);
8456}
8457
8458/*
8459 * Called from nfs4_state_manager thread for session setup, so don't recover
8460 * from sequence operation or clientid errors.
8461 */
8462static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8463{
8464 struct nfs4_get_lease_time_data *data =
8465 (struct nfs4_get_lease_time_data *)calldata;
8466
8467 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008468 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008469 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008470 switch (task->tk_status) {
8471 case -NFS4ERR_DELAY:
8472 case -NFS4ERR_GRACE:
8473 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8474 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8475 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008476 /* fall through */
8477 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008478 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008479 return;
8480 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008481 dprintk("<-- %s\n", __func__);
8482}
8483
Trond Myklebust17280172012-03-11 13:11:00 -04008484static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008485 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8486 .rpc_call_done = nfs4_get_lease_time_done,
8487};
8488
8489int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8490{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008491 struct nfs4_get_lease_time_args args;
8492 struct nfs4_get_lease_time_res res = {
8493 .lr_fsinfo = fsinfo,
8494 };
8495 struct nfs4_get_lease_time_data data = {
8496 .args = &args,
8497 .res = &res,
8498 .clp = clp,
8499 };
8500 struct rpc_message msg = {
8501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8502 .rpc_argp = &args,
8503 .rpc_resp = &res,
8504 };
8505 struct rpc_task_setup task_setup = {
8506 .rpc_client = clp->cl_rpcclient,
8507 .rpc_message = &msg,
8508 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008509 .callback_data = &data,
8510 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008511 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008512
Anna Schumakerfba83f32018-05-04 16:22:50 -04008513 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008514 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008515}
8516
Donald Buczek0efb01b2019-07-07 21:26:08 +02008517#ifdef CONFIG_NFS_V4_1
8518
Andy Adamsonfc931582009-04-01 09:22:31 -04008519/*
8520 * Initialize the values to be used by the client in CREATE_SESSION
8521 * If nfs4_init_session set the fore channel request and response sizes,
8522 * use them.
8523 *
8524 * Set the back channel max_resp_sz_cached to zero to force the client to
8525 * always set csa_cachethis to FALSE because the current implementation
8526 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8527 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008528static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8529 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008530{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008531 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008532 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008533 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008534
Andy Adamson18aad3d2013-06-26 12:21:49 -04008535 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8536 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8537
Andy Adamsonfc931582009-04-01 09:22:31 -04008538 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008539 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8540 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008541 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008542 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008543
8544 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008545 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008546 __func__,
8547 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008548 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008549
8550 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008551 args->bc_attrs.max_rqst_sz = max_bc_payload;
8552 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008553 args->bc_attrs.max_resp_sz_cached = 0;
8554 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008555 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008556 if (args->bc_attrs.max_reqs > max_bc_slots)
8557 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008558
8559 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8560 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8561 __func__,
8562 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8563 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8564 args->bc_attrs.max_reqs);
8565}
8566
Trond Myklebust79969dd2015-02-18 11:30:18 -08008567static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8568 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008569{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008570 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008571 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008572
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008573 if (rcvd->max_resp_sz > sent->max_resp_sz)
8574 return -EINVAL;
8575 /*
8576 * Our requested max_ops is the minimum we need; we're not
8577 * prepared to break up compounds into smaller pieces than that.
8578 * So, no point even trying to continue if the server won't
8579 * cooperate:
8580 */
8581 if (rcvd->max_ops < sent->max_ops)
8582 return -EINVAL;
8583 if (rcvd->max_reqs == 0)
8584 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008585 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8586 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008587 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008588}
8589
Trond Myklebust79969dd2015-02-18 11:30:18 -08008590static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8591 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008592{
8593 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008594 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008595
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008596 if (!(res->flags & SESSION4_BACK_CHAN))
8597 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008598 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8599 return -EINVAL;
8600 if (rcvd->max_resp_sz < sent->max_resp_sz)
8601 return -EINVAL;
8602 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8603 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008604 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008605 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008606 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008607 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008608out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008609 return 0;
8610}
Andy Adamson8d353012009-04-01 09:22:32 -04008611
Andy Adamson8d353012009-04-01 09:22:32 -04008612static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008613 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008614{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008615 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008616
Trond Myklebust79969dd2015-02-18 11:30:18 -08008617 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008618 if (ret)
8619 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008620 return nfs4_verify_back_channel_attrs(args, res);
8621}
8622
8623static void nfs4_update_session(struct nfs4_session *session,
8624 struct nfs41_create_session_res *res)
8625{
8626 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008627 /* Mark client id and session as being confirmed */
8628 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8629 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008630 session->flags = res->flags;
8631 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008632 if (res->flags & SESSION4_BACK_CHAN)
8633 memcpy(&session->bc_attrs, &res->bc_attrs,
8634 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008635}
8636
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008637static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008638 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008639{
8640 struct nfs4_session *session = clp->cl_session;
8641 struct nfs41_create_session_args args = {
8642 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008643 .clientid = clp->cl_clientid,
8644 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008645 .cb_program = NFS4_CALLBACK,
8646 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008647 struct nfs41_create_session_res res;
8648
Andy Adamsonfc931582009-04-01 09:22:31 -04008649 struct rpc_message msg = {
8650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8651 .rpc_argp = &args,
8652 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008653 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008654 };
8655 int status;
8656
Chuck Lever6b26cc82016-05-02 14:40:40 -04008657 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008658 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008659
NeilBrown5a0c2572019-05-30 10:41:28 +10008660 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8661 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008662 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008663
Trond Myklebustb519d402016-09-11 14:50:01 -04008664 switch (status) {
8665 case -NFS4ERR_STALE_CLIENTID:
8666 case -NFS4ERR_DELAY:
8667 case -ETIMEDOUT:
8668 case -EACCES:
8669 case -EAGAIN:
8670 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08008671 }
Trond Myklebustb519d402016-09-11 14:50:01 -04008672
8673 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008674 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008675 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008676 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008677 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008678 if (status)
8679 goto out;
8680 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008681 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008682out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008683 return status;
8684}
8685
8686/*
8687 * Issues a CREATE_SESSION operation to the server.
8688 * It is the responsibility of the caller to verify the session is
8689 * expired before calling this routine.
8690 */
NeilBrowna52458b2018-12-03 11:30:31 +11008691int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008692{
8693 int status;
8694 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008695 struct nfs4_session *session = clp->cl_session;
8696
8697 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8698
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008699 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008700 if (status)
8701 goto out;
8702
Andy Adamsonaacd5532011-11-09 13:58:21 -05008703 /* Init or reset the session slot tables */
8704 status = nfs4_setup_session_slot_tables(session);
8705 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008706 if (status)
8707 goto out;
8708
8709 ptr = (unsigned *)&session->sess_id.data[0];
8710 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8711 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008712out:
8713 dprintk("<-- %s\n", __func__);
8714 return status;
8715}
8716
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008717/*
8718 * Issue the over-the-wire RPC DESTROY_SESSION.
8719 * The caller must serialize access to this routine.
8720 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008721int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008722 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008723{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008724 struct rpc_message msg = {
8725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8726 .rpc_argp = session,
8727 .rpc_cred = cred,
8728 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008729 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008730
8731 dprintk("--> nfs4_proc_destroy_session\n");
8732
8733 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008734 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8735 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008736
NeilBrown5a0c2572019-05-30 10:41:28 +10008737 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8738 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008739 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008740
8741 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008742 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008743 "Session has been destroyed regardless...\n", status);
8744
8745 dprintk("<-- nfs4_proc_destroy_session\n");
8746 return status;
8747}
8748
Trond Myklebust7b38c362012-05-23 13:23:31 -04008749/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008750 * Renew the cl_session lease.
8751 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008752struct nfs4_sequence_data {
8753 struct nfs_client *clp;
8754 struct nfs4_sequence_args args;
8755 struct nfs4_sequence_res res;
8756};
8757
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008758static void nfs41_sequence_release(void *data)
8759{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008760 struct nfs4_sequence_data *calldata = data;
8761 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008762
Elena Reshetova212bf412017-10-20 12:53:38 +03008763 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008764 nfs4_schedule_state_renewal(clp);
8765 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008766 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008767}
8768
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008769static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8770{
8771 switch(task->tk_status) {
8772 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008773 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8774 return -EAGAIN;
8775 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008776 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008777 }
8778 return 0;
8779}
8780
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008781static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008782{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008783 struct nfs4_sequence_data *calldata = data;
8784 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008785
Trond Myklebust14516c32010-07-31 14:29:06 -04008786 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8787 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008788
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008789 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008790 if (task->tk_status < 0) {
8791 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008792 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008793 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008794
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008795 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8796 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008797 return;
8798 }
8799 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008800 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008801out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008802 dprintk("<-- %s\n", __func__);
8803}
8804
8805static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8806{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008807 struct nfs4_sequence_data *calldata = data;
8808 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008809 struct nfs4_sequence_args *args;
8810 struct nfs4_sequence_res *res;
8811
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008812 args = task->tk_msg.rpc_argp;
8813 res = task->tk_msg.rpc_resp;
8814
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008815 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008816}
8817
8818static const struct rpc_call_ops nfs41_sequence_ops = {
8819 .rpc_call_done = nfs41_sequence_call_done,
8820 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008821 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008822};
8823
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008824static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008825 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008826 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008827 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008828{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008829 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008830 struct rpc_message msg = {
8831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8832 .rpc_cred = cred,
8833 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008834 struct rpc_task_setup task_setup_data = {
8835 .rpc_client = clp->cl_rpcclient,
8836 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008837 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008838 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008839 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008840 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008841
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008842 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008843 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008844 goto out_err;
8845
8846 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008847 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008848 if (calldata == NULL)
8849 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008850 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008851 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008852 msg.rpc_argp = &calldata->args;
8853 msg.rpc_resp = &calldata->res;
8854 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008855 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008856
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008857 ret = rpc_run_task(&task_setup_data);
8858 if (IS_ERR(ret))
8859 goto out_err;
8860 return ret;
8861out_put_clp:
8862 nfs_put_client(clp);
8863out_err:
8864 nfs41_release_slot(slot);
8865 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008866}
8867
NeilBrowna52458b2018-12-03 11:30:31 +11008868static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008869{
8870 struct rpc_task *task;
8871 int ret = 0;
8872
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008873 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008874 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008875 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008876 if (IS_ERR(task))
8877 ret = PTR_ERR(task);
8878 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008879 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008880 dprintk("<-- %s status=%d\n", __func__, ret);
8881 return ret;
8882}
8883
NeilBrowna52458b2018-12-03 11:30:31 +11008884static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008885{
8886 struct rpc_task *task;
8887 int ret;
8888
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008889 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008890 if (IS_ERR(task)) {
8891 ret = PTR_ERR(task);
8892 goto out;
8893 }
8894 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008895 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008896 ret = task->tk_status;
8897 rpc_put_task(task);
8898out:
8899 dprintk("<-- %s status=%d\n", __func__, ret);
8900 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008901}
8902
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008903struct nfs4_reclaim_complete_data {
8904 struct nfs_client *clp;
8905 struct nfs41_reclaim_complete_args arg;
8906 struct nfs41_reclaim_complete_res res;
8907};
8908
8909static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8910{
8911 struct nfs4_reclaim_complete_data *calldata = data;
8912
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008913 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008914 &calldata->arg.seq_args,
8915 &calldata->res.seq_res,
8916 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008917}
8918
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008919static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8920{
8921 switch(task->tk_status) {
8922 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008923 wake_up_all(&clp->cl_lock_waitq);
8924 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008925 case -NFS4ERR_COMPLETE_ALREADY:
8926 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8927 break;
8928 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008929 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008930 /* fall through */
8931 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008932 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008933 case -NFS4ERR_BADSESSION:
8934 case -NFS4ERR_DEADSESSION:
8935 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008936 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008937 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008938 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008939 }
8940 return 0;
8941}
8942
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008943static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8944{
8945 struct nfs4_reclaim_complete_data *calldata = data;
8946 struct nfs_client *clp = calldata->clp;
8947 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8948
8949 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008950 if (!nfs41_sequence_done(task, res))
8951 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008952
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008953 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008954 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8955 rpc_restart_call_prepare(task);
8956 return;
8957 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008958 dprintk("<-- %s\n", __func__);
8959}
8960
8961static void nfs4_free_reclaim_complete_data(void *data)
8962{
8963 struct nfs4_reclaim_complete_data *calldata = data;
8964
8965 kfree(calldata);
8966}
8967
8968static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8969 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8970 .rpc_call_done = nfs4_reclaim_complete_done,
8971 .rpc_release = nfs4_free_reclaim_complete_data,
8972};
8973
8974/*
8975 * Issue a global reclaim complete.
8976 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008977static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008978 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008979{
8980 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008981 struct rpc_message msg = {
8982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008983 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008984 };
8985 struct rpc_task_setup task_setup_data = {
8986 .rpc_client = clp->cl_rpcclient,
8987 .rpc_message = &msg,
8988 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04008989 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008990 };
8991 int status = -ENOMEM;
8992
8993 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008994 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008995 if (calldata == NULL)
8996 goto out;
8997 calldata->clp = clp;
8998 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008999
Anna Schumakerfba83f32018-05-04 16:22:50 -04009000 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009001 msg.rpc_argp = &calldata->arg;
9002 msg.rpc_resp = &calldata->res;
9003 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009004 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009005out:
9006 dprintk("<-- %s status=%d\n", __func__, status);
9007 return status;
9008}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009009
9010static void
9011nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9012{
9013 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009014 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009015
9016 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009017 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009018 &lgp->res.seq_res, task);
9019 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009020}
9021
9022static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9023{
9024 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009025
9026 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009027 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009028 dprintk("<-- %s\n", __func__);
9029}
9030
9031static int
9032nfs4_layoutget_handle_exception(struct rpc_task *task,
9033 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9034{
Trond Myklebustee314c22012-10-01 17:25:48 -07009035 struct inode *inode = lgp->args.inode;
9036 struct nfs_server *server = NFS_SERVER(inode);
9037 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009038 int nfs4err = task->tk_status;
9039 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009040 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009041
Boaz Harroshed7e5422014-01-22 20:34:54 +02009042 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009043
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009044 nfs4_sequence_free_slot(&lgp->res.seq_res);
9045
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009046 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009047 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009048 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009049
9050 /*
9051 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9052 * on the file. set tk_status to -ENODATA to tell upper layer to
9053 * retry go inband.
9054 */
9055 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009056 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009057 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009058 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009059 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9060 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9061 */
9062 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009063 status = -EOVERFLOW;
9064 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009065 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009066 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009067 * (or clients) writing to the same RAID stripe except when
9068 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009069 *
9070 * Treat it like we would RECALLCONFLICT -- we retry for a little
9071 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009072 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009073 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009074 if (lgp->args.minlength == 0) {
9075 status = -EOVERFLOW;
9076 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009077 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009078 status = -EBUSY;
9079 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009080 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009081 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009082 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009083 case -NFS4ERR_DELEG_REVOKED:
9084 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009085 case -NFS4ERR_EXPIRED:
9086 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009087 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009088 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009089 lo = NFS_I(inode)->layout;
9090 /* If the open stateid was bad, then recover it. */
9091 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009092 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009093 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009094 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009095 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009096 break;
9097 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009098
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009099 /*
9100 * Mark the bad layout state as invalid, then retry
9101 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009102 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009103 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009104 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009105 pnfs_free_lseg_list(&head);
9106 status = -EAGAIN;
9107 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009108 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009109
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009110 err = nfs4_handle_exception(server, nfs4err, exception);
9111 if (!status) {
9112 if (exception->retry)
9113 status = -EAGAIN;
9114 else
9115 status = err;
9116 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009117out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009118 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009119 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009120}
9121
Fred Isamandacb4522016-09-19 17:47:09 -04009122size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009123{
9124 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9125 return nfs_page_array_len(0, max_resp_sz);
9126}
9127
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009128static void nfs4_layoutget_release(void *calldata)
9129{
9130 struct nfs4_layoutget *lgp = calldata;
9131
9132 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009133 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009134 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009135 dprintk("<-- %s\n", __func__);
9136}
9137
9138static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9139 .rpc_call_prepare = nfs4_layoutget_prepare,
9140 .rpc_call_done = nfs4_layoutget_done,
9141 .rpc_release = nfs4_layoutget_release,
9142};
9143
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009144struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009145nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009146{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009147 struct inode *inode = lgp->args.inode;
9148 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009149 struct rpc_task *task;
9150 struct rpc_message msg = {
9151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9152 .rpc_argp = &lgp->args,
9153 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009154 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009155 };
9156 struct rpc_task_setup task_setup_data = {
9157 .rpc_client = server->client,
9158 .rpc_message = &msg,
9159 .callback_ops = &nfs4_layoutget_call_ops,
9160 .callback_data = lgp,
9161 .flags = RPC_TASK_ASYNC,
9162 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009163 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009164 struct nfs4_exception exception = {
9165 .inode = inode,
9166 .timeout = *timeout,
9167 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009168 int status = 0;
9169
9170 dprintk("--> %s\n", __func__);
9171
Peng Tao4bd5a982014-11-17 11:05:17 +08009172 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9173 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9174
Anna Schumakerfba83f32018-05-04 16:22:50 -04009175 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009176
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009177 task = rpc_run_task(&task_setup_data);
9178 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009179 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05009180 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009181 if (status != 0)
9182 goto out;
9183
Trond Myklebust18c07782019-02-13 07:55:31 -05009184 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009185 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9186 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009187 } else if (lgp->res.layoutp->len == 0) {
9188 status = -EAGAIN;
9189 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009190 } else
9191 lseg = pnfs_layout_process(lgp);
9192out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009193 trace_nfs4_layoutget(lgp->args.ctx,
9194 &lgp->args.range,
9195 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009196 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009197 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009198
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009199 rpc_put_task(task);
9200 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009201 if (status)
9202 return ERR_PTR(status);
9203 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009204}
9205
Benny Halevycbe82602011-05-22 19:52:37 +03009206static void
9207nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9208{
9209 struct nfs4_layoutreturn *lrp = calldata;
9210
9211 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009212 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009213 &lrp->args.seq_args,
9214 &lrp->res.seq_res,
9215 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009216 if (!pnfs_layout_is_valid(lrp->args.layout))
9217 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009218}
9219
9220static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9221{
9222 struct nfs4_layoutreturn *lrp = calldata;
9223 struct nfs_server *server;
9224
9225 dprintk("--> %s\n", __func__);
9226
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009227 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009228 return;
9229
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009230 /*
9231 * Was there an RPC level error? Assume the call succeeded,
9232 * and that we need to release the layout
9233 */
9234 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9235 lrp->res.lrs_present = 0;
9236 return;
9237 }
9238
Benny Halevycbe82602011-05-22 19:52:37 +03009239 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009240 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009241 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009242 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009243 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009244 lrp->args.inode))
9245 goto out_restart;
9246 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009247 default:
9248 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05009249 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009250 case 0:
9251 break;
9252 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009253 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009254 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009255 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009256 }
Benny Halevycbe82602011-05-22 19:52:37 +03009257 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009258 return;
9259out_restart:
9260 task->tk_status = 0;
9261 nfs4_sequence_free_slot(&lrp->res.seq_res);
9262 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009263}
9264
9265static void nfs4_layoutreturn_release(void *calldata)
9266{
9267 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009268 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009269
9270 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009271 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009272 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009273 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009274 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9275 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009276 pnfs_put_layout_hdr(lrp->args.layout);
9277 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009278 kfree(calldata);
9279 dprintk("<-- %s\n", __func__);
9280}
9281
9282static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9283 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9284 .rpc_call_done = nfs4_layoutreturn_done,
9285 .rpc_release = nfs4_layoutreturn_release,
9286};
9287
Peng Tao6c166052014-11-17 09:30:40 +08009288int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009289{
9290 struct rpc_task *task;
9291 struct rpc_message msg = {
9292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9293 .rpc_argp = &lrp->args,
9294 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009295 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009296 };
9297 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009298 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009299 .rpc_message = &msg,
9300 .callback_ops = &nfs4_layoutreturn_call_ops,
9301 .callback_data = lrp,
9302 };
Peng Tao6c166052014-11-17 09:30:40 +08009303 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009304
Andrew Elble99ade3c2015-12-02 09:39:51 -05009305 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9306 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9307 &task_setup_data.rpc_client, &msg);
9308
Benny Halevycbe82602011-05-22 19:52:37 +03009309 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009310 if (!sync) {
9311 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9312 if (!lrp->inode) {
9313 nfs4_layoutreturn_release(lrp);
9314 return -EAGAIN;
9315 }
9316 task_setup_data.flags |= RPC_TASK_ASYNC;
9317 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009318 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009319 task = rpc_run_task(&task_setup_data);
9320 if (IS_ERR(task))
9321 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009322 if (sync)
9323 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009324 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009325 dprintk("<-- %s status=%d\n", __func__, status);
9326 rpc_put_task(task);
9327 return status;
9328}
9329
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009330static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009331_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9332 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009333 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009334{
9335 struct nfs4_getdeviceinfo_args args = {
9336 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009337 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9338 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009339 };
9340 struct nfs4_getdeviceinfo_res res = {
9341 .pdev = pdev,
9342 };
9343 struct rpc_message msg = {
9344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9345 .rpc_argp = &args,
9346 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009347 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009348 };
9349 int status;
9350
9351 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009352 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009353 if (res.notification & ~args.notify_types)
9354 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009355 if (res.notification != args.notify_types)
9356 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009357
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009358 dprintk("<-- %s status=%d\n", __func__, status);
9359
9360 return status;
9361}
9362
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009363int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9364 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009365 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009366{
9367 struct nfs4_exception exception = { };
9368 int err;
9369
9370 do {
9371 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009372 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009373 &exception);
9374 } while (exception.retry);
9375 return err;
9376}
9377EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9378
Andy Adamson863a3c62011-03-23 13:27:54 +00009379static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9380{
9381 struct nfs4_layoutcommit_data *data = calldata;
9382 struct nfs_server *server = NFS_SERVER(data->args.inode);
9383
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009384 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009385 &data->args.seq_args,
9386 &data->res.seq_res,
9387 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009388}
9389
9390static void
9391nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9392{
9393 struct nfs4_layoutcommit_data *data = calldata;
9394 struct nfs_server *server = NFS_SERVER(data->args.inode);
9395
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009396 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009397 return;
9398
9399 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009400 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9401 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9402 case -NFS4ERR_BADLAYOUT: /* no layout */
9403 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009404 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009405 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009406 break;
9407 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009408 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009409 rpc_restart_call_prepare(task);
9410 return;
9411 }
9412 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009413}
9414
9415static void nfs4_layoutcommit_release(void *calldata)
9416{
9417 struct nfs4_layoutcommit_data *data = calldata;
9418
Andy Adamsondb29c082011-07-30 20:52:38 -04009419 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009420 nfs_post_op_update_inode_force_wcc(data->args.inode,
9421 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009422 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009423 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009424 kfree(data);
9425}
9426
9427static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9428 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9429 .rpc_call_done = nfs4_layoutcommit_done,
9430 .rpc_release = nfs4_layoutcommit_release,
9431};
9432
9433int
Andy Adamsonef311532011-03-12 02:58:10 -05009434nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009435{
9436 struct rpc_message msg = {
9437 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9438 .rpc_argp = &data->args,
9439 .rpc_resp = &data->res,
9440 .rpc_cred = data->cred,
9441 };
9442 struct rpc_task_setup task_setup_data = {
9443 .task = &data->task,
9444 .rpc_client = NFS_CLIENT(data->args.inode),
9445 .rpc_message = &msg,
9446 .callback_ops = &nfs4_layoutcommit_ops,
9447 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009448 };
9449 struct rpc_task *task;
9450 int status = 0;
9451
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009452 dprintk("NFS: initiating layoutcommit call. sync %d "
9453 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009454 data->args.lastbytewritten,
9455 data->args.inode->i_ino);
9456
Trond Myklebust472e2592015-02-05 16:50:30 -05009457 if (!sync) {
9458 data->inode = nfs_igrab_and_active(data->args.inode);
9459 if (data->inode == NULL) {
9460 nfs4_layoutcommit_release(data);
9461 return -EAGAIN;
9462 }
9463 task_setup_data.flags = RPC_TASK_ASYNC;
9464 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009465 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009466 task = rpc_run_task(&task_setup_data);
9467 if (IS_ERR(task))
9468 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009469 if (sync)
9470 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009471 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009472 dprintk("%s: status %d\n", __func__, status);
9473 rpc_put_task(task);
9474 return status;
9475}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009476
Trond Myklebust302fad72019-02-18 13:32:38 -05009477/*
Andy Adamson97431202013-08-08 10:57:56 -04009478 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9479 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9480 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009481static int
9482_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009483 struct nfs_fsinfo *info,
9484 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009485{
9486 struct nfs41_secinfo_no_name_args args = {
9487 .style = SECINFO_STYLE_CURRENT_FH,
9488 };
9489 struct nfs4_secinfo_res res = {
9490 .flavors = flavors,
9491 };
9492 struct rpc_message msg = {
9493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9494 .rpc_argp = &args,
9495 .rpc_resp = &res,
9496 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009497 struct rpc_clnt *clnt = server->client;
Anna Schumakercc15e242019-08-14 16:22:31 -04009498 struct nfs4_call_sync_data data = {
9499 .seq_server = server,
9500 .seq_args = &args.seq_args,
9501 .seq_res = &res.seq_res,
9502 };
9503 struct rpc_task_setup task_setup = {
9504 .rpc_client = server->client,
9505 .rpc_message = &msg,
9506 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9507 .callback_data = &data,
9508 .flags = RPC_TASK_NO_ROUND_ROBIN,
9509 };
NeilBrowna52458b2018-12-03 11:30:31 +11009510 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009511 int status;
9512
9513 if (use_integrity) {
9514 clnt = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009515 task_setup.rpc_client = clnt;
9516
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009517 cred = nfs4_get_clid_cred(server->nfs_client);
9518 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009519 }
9520
9521 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009522 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9523 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009524 dprintk("<-- %s status=%d\n", __func__, status);
9525
NeilBrowna52458b2018-12-03 11:30:31 +11009526 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009527
9528 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009529}
9530
9531static int
9532nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9533 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9534{
Trond Myklebust0688e642019-04-07 13:59:09 -04009535 struct nfs4_exception exception = {
9536 .interruptible = true,
9537 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009538 int err;
9539 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009540 /* first try using integrity protection */
9541 err = -NFS4ERR_WRONGSEC;
9542
9543 /* try to use integrity protection with machine cred */
9544 if (_nfs4_is_integrity_protected(server->nfs_client))
9545 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9546 flavors, true);
9547
9548 /*
9549 * if unable to use integrity protection, or SECINFO with
9550 * integrity protection returns NFS4ERR_WRONGSEC (which is
9551 * disallowed by spec, but exists in deployed servers) use
9552 * the current filesystem's rpc_client and the user cred.
9553 */
9554 if (err == -NFS4ERR_WRONGSEC)
9555 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9556 flavors, false);
9557
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009558 switch (err) {
9559 case 0:
9560 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009561 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009562 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009563 default:
9564 err = nfs4_handle_exception(server, err, &exception);
9565 }
9566 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009567out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009568 return err;
9569}
9570
9571static int
9572nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9573 struct nfs_fsinfo *info)
9574{
9575 int err;
9576 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009577 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009578 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009579 struct nfs4_secinfo4 *secinfo;
9580 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009581
9582 page = alloc_page(GFP_KERNEL);
9583 if (!page) {
9584 err = -ENOMEM;
9585 goto out;
9586 }
9587
9588 flavors = page_address(page);
9589 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9590
9591 /*
9592 * Fall back on "guess and check" method if
9593 * the server doesn't support SECINFO_NO_NAME
9594 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009595 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009596 err = nfs4_find_root_sec(server, fhandle, info);
9597 goto out_freepage;
9598 }
9599 if (err)
9600 goto out_freepage;
9601
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009602 for (i = 0; i < flavors->num_flavors; i++) {
9603 secinfo = &flavors->flavors[i];
9604
9605 switch (secinfo->flavor) {
9606 case RPC_AUTH_NULL:
9607 case RPC_AUTH_UNIX:
9608 case RPC_AUTH_GSS:
9609 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9610 &secinfo->flavor_info);
9611 break;
9612 default:
9613 flavor = RPC_AUTH_MAXFLAVOR;
9614 break;
9615 }
9616
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009617 if (!nfs_auth_info_match(&server->auth_info, flavor))
9618 flavor = RPC_AUTH_MAXFLAVOR;
9619
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009620 if (flavor != RPC_AUTH_MAXFLAVOR) {
9621 err = nfs4_lookup_root_sec(server, fhandle,
9622 info, flavor);
9623 if (!err)
9624 break;
9625 }
9626 }
9627
9628 if (flavor == RPC_AUTH_MAXFLAVOR)
9629 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009630
9631out_freepage:
9632 put_page(page);
9633 if (err == -EACCES)
9634 return -EPERM;
9635out:
9636 return err;
9637}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009638
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009639static int _nfs41_test_stateid(struct nfs_server *server,
9640 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009641 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009642{
9643 int status;
9644 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009645 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009646 };
9647 struct nfs41_test_stateid_res res;
9648 struct rpc_message msg = {
9649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9650 .rpc_argp = &args,
9651 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009652 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009653 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009654 struct rpc_clnt *rpc_client = server->client;
9655
9656 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9657 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009658
Chuck Lever38527b12012-07-11 16:30:23 -04009659 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009660 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009661 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009662 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009663 if (status != NFS_OK) {
9664 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009665 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009666 }
9667 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009668 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009669}
9670
Trond Myklebust43912bb2016-09-22 13:38:56 -04009671static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9672 int err, struct nfs4_exception *exception)
9673{
9674 exception->retry = 0;
9675 switch(err) {
9676 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009677 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009678 nfs4_handle_exception(server, err, exception);
9679 break;
9680 case -NFS4ERR_BADSESSION:
9681 case -NFS4ERR_BADSLOT:
9682 case -NFS4ERR_BAD_HIGH_SLOT:
9683 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9684 case -NFS4ERR_DEADSESSION:
9685 nfs4_do_handle_exception(server, err, exception);
9686 }
9687}
9688
Chuck Lever38527b12012-07-11 16:30:23 -04009689/**
9690 * nfs41_test_stateid - perform a TEST_STATEID operation
9691 *
9692 * @server: server / transport on which to perform the operation
9693 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009694 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009695 *
9696 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9697 * Otherwise a negative NFS4ERR value is returned if the operation
9698 * failed or the state ID is not currently valid.
9699 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009700static int nfs41_test_stateid(struct nfs_server *server,
9701 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009702 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009703{
Trond Myklebust0688e642019-04-07 13:59:09 -04009704 struct nfs4_exception exception = {
9705 .interruptible = true,
9706 };
Bryan Schumaker7d974792011-06-02 14:59:08 -04009707 int err;
9708 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009709 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009710 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009711 } while (exception.retry);
9712 return err;
9713}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009714
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009715struct nfs_free_stateid_data {
9716 struct nfs_server *server;
9717 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009718 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009719};
9720
9721static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9722{
9723 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009724 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009725 &data->args.seq_args,
9726 &data->res.seq_res,
9727 task);
9728}
9729
9730static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9731{
9732 struct nfs_free_stateid_data *data = calldata;
9733
9734 nfs41_sequence_done(task, &data->res.seq_res);
9735
9736 switch (task->tk_status) {
9737 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009738 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009739 rpc_restart_call_prepare(task);
9740 }
9741}
9742
9743static void nfs41_free_stateid_release(void *calldata)
9744{
9745 kfree(calldata);
9746}
9747
Trond Myklebust17f26b12013-08-21 15:48:42 -04009748static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009749 .rpc_call_prepare = nfs41_free_stateid_prepare,
9750 .rpc_call_done = nfs41_free_stateid_done,
9751 .rpc_release = nfs41_free_stateid_release,
9752};
9753
Anna Schumaker2f261022018-05-15 13:03:39 -04009754/**
9755 * nfs41_free_stateid - perform a FREE_STATEID operation
9756 *
9757 * @server: server / transport on which to perform the operation
9758 * @stateid: state ID to release
9759 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009760 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009761 *
9762 * Note: this function is always asynchronous.
9763 */
9764static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009765 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009766 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009767 bool privileged)
9768{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009769 struct rpc_message msg = {
9770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009771 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009772 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009773 struct rpc_task_setup task_setup = {
9774 .rpc_client = server->client,
9775 .rpc_message = &msg,
9776 .callback_ops = &nfs41_free_stateid_ops,
9777 .flags = RPC_TASK_ASYNC,
9778 };
9779 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009780 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009781
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009782 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9783 &task_setup.rpc_client, &msg);
9784
Chuck Lever38527b12012-07-11 16:30:23 -04009785 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009786 data = kmalloc(sizeof(*data), GFP_NOFS);
9787 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009788 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009789 data->server = server;
9790 nfs4_stateid_copy(&data->args.stateid, stateid);
9791
9792 task_setup.callback_data = data;
9793
9794 msg.rpc_argp = &data->args;
9795 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009796 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009797 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009798 if (IS_ERR(task))
9799 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009800 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009801 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009802}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009803
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009804static void
9805nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009806{
NeilBrowna52458b2018-12-03 11:30:31 +11009807 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009808
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009809 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009810 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009811}
9812
Trond Myklebust36281ca2012-03-04 18:13:56 -05009813static bool nfs41_match_stateid(const nfs4_stateid *s1,
9814 const nfs4_stateid *s2)
9815{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009816 if (s1->type != s2->type)
9817 return false;
9818
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009819 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009820 return false;
9821
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009822 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009823 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009824
Anna Schumaker045c5512017-01-11 16:59:48 -05009825 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009826}
9827
Andy Adamson557134a2009-04-01 09:21:53 -04009828#endif /* CONFIG_NFS_V4_1 */
9829
Trond Myklebust36281ca2012-03-04 18:13:56 -05009830static bool nfs4_match_stateid(const nfs4_stateid *s1,
9831 const nfs4_stateid *s2)
9832{
Trond Myklebustf597c532012-03-04 18:13:56 -05009833 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009834}
9835
9836
Trond Myklebust17280172012-03-11 13:11:00 -04009837static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009838 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009839 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 .recover_open = nfs4_open_reclaim,
9841 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009842 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009843 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844};
9845
Andy Adamson591d71c2009-04-01 09:22:47 -04009846#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009847static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009848 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9849 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9850 .recover_open = nfs4_open_reclaim,
9851 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009852 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009853 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009854 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009855};
9856#endif /* CONFIG_NFS_V4_1 */
9857
Trond Myklebust17280172012-03-11 13:11:00 -04009858static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009859 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009860 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009861 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009862 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009863 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864};
9865
Andy Adamson591d71c2009-04-01 09:22:47 -04009866#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009867static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009868 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9869 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009870 .recover_open = nfs41_open_expired,
9871 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009872 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009873};
9874#endif /* CONFIG_NFS_V4_1 */
9875
Trond Myklebust17280172012-03-11 13:11:00 -04009876static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009877 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009878 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009879 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009880};
9881
9882#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009883static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009884 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009885 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009886 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009887};
9888#endif
9889
Chuck Leverec011fe2013-10-17 14:12:39 -04009890static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009891 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009892 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009893};
9894
9895#if defined(CONFIG_NFS_V4_1)
9896static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009897 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009898 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009899};
9900#endif /* CONFIG_NFS_V4_1 */
9901
Trond Myklebust97dc1352010-06-16 09:52:26 -04009902static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9903 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009904 .init_caps = NFS_CAP_READDIRPLUS
9905 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009906 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009907 .init_client = nfs40_init_client,
9908 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009909 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009910 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009911 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009912 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009913 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009914 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009915 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9916 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9917 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009918 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009919};
9920
9921#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009922static struct nfs_seqid *
9923nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9924{
9925 return NULL;
9926}
9927
Trond Myklebust97dc1352010-06-16 09:52:26 -04009928static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9929 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009930 .init_caps = NFS_CAP_READDIRPLUS
9931 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009932 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009933 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009934 | NFS_CAP_ATOMIC_OPEN_V1
9935 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009936 .init_client = nfs41_init_client,
9937 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009938 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009939 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009940 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009941 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009942 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009943 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009944 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009945 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9946 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9947 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009948 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009949};
9950#endif
9951
Steve Dickson42c2c422013-05-22 12:50:38 -04009952#if defined(CONFIG_NFS_V4_2)
9953static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9954 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009955 .init_caps = NFS_CAP_READDIRPLUS
9956 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009957 | NFS_CAP_POSIX_LOCK
9958 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009959 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009960 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009961 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009962 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009963 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -04009964 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009965 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009966 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009967 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -05009968 | NFS_CAP_CLONE
9969 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009970 .init_client = nfs41_init_client,
9971 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009972 .match_stateid = nfs41_match_stateid,
9973 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009974 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009975 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009976 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009977 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009978 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009979 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9980 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9981 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009982 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009983};
9984#endif
9985
Trond Myklebust97dc1352010-06-16 09:52:26 -04009986const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9987 [0] = &nfs_v4_0_minor_ops,
9988#if defined(CONFIG_NFS_V4_1)
9989 [1] = &nfs_v4_1_minor_ops,
9990#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009991#if defined(CONFIG_NFS_V4_2)
9992 [2] = &nfs_v4_2_minor_ops,
9993#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009994};
9995
Trond Myklebust13997822016-07-24 17:10:52 -04009996static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009997{
9998 ssize_t error, error2;
9999
10000 error = generic_listxattr(dentry, list, size);
10001 if (error < 0)
10002 return error;
10003 if (list) {
10004 list += error;
10005 size -= error;
10006 }
10007
10008 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10009 if (error2 < 0)
10010 return error2;
10011 return error + error2;
10012}
10013
Trond Myklebust17f26b12013-08-21 15:48:42 -040010014static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010015 .create = nfs_create,
10016 .lookup = nfs_lookup,
10017 .atomic_open = nfs_atomic_open,
10018 .link = nfs_link,
10019 .unlink = nfs_unlink,
10020 .symlink = nfs_symlink,
10021 .mkdir = nfs_mkdir,
10022 .rmdir = nfs_rmdir,
10023 .mknod = nfs_mknod,
10024 .rename = nfs_rename,
10025 .permission = nfs_permission,
10026 .getattr = nfs_getattr,
10027 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010028 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010029};
10030
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010031static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010032 .permission = nfs_permission,
10033 .getattr = nfs_getattr,
10034 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010035 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010036};
10037
David Howells509de812006-08-22 20:06:11 -040010038const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010039 .version = 4, /* protocol version */
10040 .dentry_ops = &nfs4_dentry_operations,
10041 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010042 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010043 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010044 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010045 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010046 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010047 .getattr = nfs4_proc_getattr,
10048 .setattr = nfs4_proc_setattr,
10049 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010050 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010051 .access = nfs4_proc_access,
10052 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010053 .create = nfs4_proc_create,
10054 .remove = nfs4_proc_remove,
10055 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010056 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010057 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010058 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010059 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010060 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010061 .link = nfs4_proc_link,
10062 .symlink = nfs4_proc_symlink,
10063 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010064 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010065 .readdir = nfs4_proc_readdir,
10066 .mknod = nfs4_proc_mknod,
10067 .statfs = nfs4_proc_statfs,
10068 .fsinfo = nfs4_proc_fsinfo,
10069 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010070 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010071 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010072 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010073 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010074 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010076 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010077 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010078 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010079 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010080 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010081 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010082 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010083 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010084 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010085 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010086 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010087 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010088 .create_server = nfs4_create_server,
10089 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010090};
10091
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010092static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010093 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010094 .list = nfs4_xattr_list_nfs4_acl,
10095 .get = nfs4_xattr_get_nfs4_acl,
10096 .set = nfs4_xattr_set_nfs4_acl,
10097};
10098
10099const struct xattr_handler *nfs4_xattr_handlers[] = {
10100 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010101#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10102 &nfs4_xattr_nfs4_label_handler,
10103#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010104 NULL
10105};
10106
Linus Torvalds1da177e2005-04-16 15:20:36 -070010107/*
10108 * Local variables:
10109 * c-basic-offset: 8
10110 * End:
10111 */