blob: e4f8311e506c2e8ec771a8796330bf6b5d027c0d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust30846df2018-04-07 13:44:28 -040074#define NFS4_BITMASK_SZ 3
75
Trond Myklebust2066fe82006-09-15 08:30:46 -040076#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define NFS4_POLL_RETRY_MAX (15*HZ)
78
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020079/* file attributes which can be mapped to nfs attributes */
80#define NFS4_VALID_ATTRS (ATTR_MODE \
81 | ATTR_UID \
82 | ATTR_GID \
83 | ATTR_SIZE \
84 | ATTR_ATIME \
85 | ATTR_MTIME \
86 | ATTR_CTIME \
87 | ATTR_ATIME_SET \
88 | ATTR_MTIME_SET)
89
Trond Myklebustcdd4e682006-01-03 09:55:12 +010090struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080091static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050093static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040094static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110095static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040096 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110097 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040098 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400100static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100101 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400102 struct nfs4_slot *slot,
103 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400104static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100105 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400106static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100107 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400108#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400109
110#ifdef CONFIG_NFS_V4_SECURITY_LABEL
111static inline struct nfs4_label *
112nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
113 struct iattr *sattr, struct nfs4_label *label)
114{
115 int err;
116
117 if (label == NULL)
118 return NULL;
119
120 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
121 return NULL;
122
David Quigleyaa9c2662013-05-22 12:50:44 -0400123 err = security_dentry_init_security(dentry, sattr->ia_mode,
124 &dentry->d_name, (void **)&label->label, &label->len);
125 if (err == 0)
126 return label;
127
128 return NULL;
129}
130static inline void
131nfs4_label_release_security(struct nfs4_label *label)
132{
133 if (label)
134 security_release_secctx(label->label, label->len);
135}
136static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
137{
138 if (label)
139 return server->attr_bitmask;
140
141 return server->attr_bitmask_nl;
142}
143#else
144static inline struct nfs4_label *
145nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
146 struct iattr *sattr, struct nfs4_label *l)
147{ return NULL; }
148static inline void
149nfs4_label_release_security(struct nfs4_label *label)
150{ return; }
151static inline u32 *
152nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
153{ return server->attr_bitmask; }
154#endif
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500157static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Trond Myklebust52567b02009-10-23 14:46:42 -0400159 if (err >= -1000)
160 return err;
161 switch (err) {
162 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500163 case -NFS4ERR_LAYOUTTRYLATER:
164 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000166 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400167 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000168 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800169 case -NFS4ERR_BADOWNER:
170 case -NFS4ERR_BADNAME:
171 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400172 case -NFS4ERR_SHARE_DENIED:
173 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400174 case -NFS4ERR_MINOR_VERS_MISMATCH:
175 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400176 case -NFS4ERR_FILE_OPEN:
177 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700180 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400183 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/*
187 * This is our standard bitmap for GETATTR requests.
188 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400189const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 FATTR4_WORD0_TYPE
191 | FATTR4_WORD0_CHANGE
192 | FATTR4_WORD0_SIZE
193 | FATTR4_WORD0_FSID
194 | FATTR4_WORD0_FILEID,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400205#ifdef CONFIG_NFS_V4_SECURITY_LABEL
206 FATTR4_WORD2_SECURITY_LABEL
207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Trond Myklebust1549210f2012-06-05 09:16:47 -0400210static const u32 nfs4_pnfs_open_bitmap[3] = {
211 FATTR4_WORD0_TYPE
212 | FATTR4_WORD0_CHANGE
213 | FATTR4_WORD0_SIZE
214 | FATTR4_WORD0_FSID
215 | FATTR4_WORD0_FILEID,
216 FATTR4_WORD1_MODE
217 | FATTR4_WORD1_NUMLINKS
218 | FATTR4_WORD1_OWNER
219 | FATTR4_WORD1_OWNER_GROUP
220 | FATTR4_WORD1_RAWDEV
221 | FATTR4_WORD1_SPACE_USED
222 | FATTR4_WORD1_TIME_ACCESS
223 | FATTR4_WORD1_TIME_METADATA
224 | FATTR4_WORD1_TIME_MODIFY,
225 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500226#ifdef CONFIG_NFS_V4_SECURITY_LABEL
227 | FATTR4_WORD2_SECURITY_LABEL
228#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400229};
230
Andy Adamsone23008e2012-10-02 21:07:32 -0400231static const u32 nfs4_open_noattr_bitmap[3] = {
232 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400233 | FATTR4_WORD0_FILEID,
234};
235
David Quigleya09df2c2013-05-22 12:50:41 -0400236const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 FATTR4_WORD0_FILES_AVAIL
238 | FATTR4_WORD0_FILES_FREE
239 | FATTR4_WORD0_FILES_TOTAL,
240 FATTR4_WORD1_SPACE_AVAIL
241 | FATTR4_WORD1_SPACE_FREE
242 | FATTR4_WORD1_SPACE_TOTAL
243};
244
David Quigleya09df2c2013-05-22 12:50:41 -0400245const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 FATTR4_WORD0_MAXLINK
247 | FATTR4_WORD0_MAXNAME,
248 0
249};
250
Fred Isamandae100c2011-07-30 20:52:37 -0400251const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 | FATTR4_WORD0_MAXREAD
253 | FATTR4_WORD0_MAXWRITE
254 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700255 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400256 | FATTR4_WORD1_FS_LAYOUT_TYPES,
257 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800258 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
David Quigleya09df2c2013-05-22 12:50:41 -0400261const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500262 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400263 | FATTR4_WORD0_SIZE
264 | FATTR4_WORD0_FSID
265 | FATTR4_WORD0_FILEID
266 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500267 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Trond Myklebust30846df2018-04-07 13:44:28 -0400277static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
278 struct inode *inode)
279{
280 unsigned long cache_validity;
281
282 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
283 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
284 return;
285
286 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
287 if (!(cache_validity & NFS_INO_REVAL_FORCED))
288 cache_validity &= ~(NFS_INO_INVALID_CHANGE
289 | NFS_INO_INVALID_SIZE);
290
291 if (!(cache_validity & NFS_INO_INVALID_SIZE))
292 dst[0] &= ~FATTR4_WORD0_SIZE;
293
294 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
295 dst[0] &= ~FATTR4_WORD0_CHANGE;
296}
297
298static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
299 const __u32 *src, struct inode *inode)
300{
301 nfs4_bitmap_copy_adjust(dst, src, inode);
302}
303
Al Virobc4785c2006-10-19 23:28:51 -0700304static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct nfs4_readdir_arg *readdir)
306{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400307 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700308 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000311 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
313 return;
314 }
315
316 readdir->cookie = 0;
317 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
318 if (cookie == 2)
319 return;
320
321 /*
322 * NFSv4 servers do not return entries for '.' and '..'
323 * Therefore, we fake these entries here. We let '.'
324 * have cookie 0 and '..' have cookie 1. Note that
325 * when talking to the server, we always send cookie 0
326 * instead of 1 or 2.
327 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (cookie == 0) {
331 *p++ = xdr_one; /* next */
332 *p++ = xdr_zero; /* cookie, first word */
333 *p++ = xdr_one; /* cookie, second word */
334 *p++ = xdr_one; /* entry len */
335 memcpy(p, ".\0\0\0", 4); /* entry */
336 p++;
337 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400338 *p++ = htonl(attrs); /* bitmap */
339 *p++ = htonl(12); /* attribute buffer length */
340 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000341 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
344 *p++ = xdr_one; /* next */
345 *p++ = xdr_zero; /* cookie, first word */
346 *p++ = xdr_two; /* cookie, second word */
347 *p++ = xdr_two; /* entry len */
348 memcpy(p, "..\0\0", 4); /* entry */
349 p++;
350 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400351 *p++ = htonl(attrs); /* bitmap */
352 *p++ = htonl(12); /* attribute buffer length */
353 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000354 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 readdir->pgbase = (char *)p - (char *)start;
357 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800358 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Trond Myklebust26d36302016-09-22 13:39:05 -0400361static void nfs4_test_and_free_stateid(struct nfs_server *server,
362 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100363 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400364{
365 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
366
367 ops->test_and_free_expired(server, stateid, cred);
368}
369
370static void __nfs4_free_revoked_stateid(struct nfs_server *server,
371 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100372 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400373{
374 stateid->type = NFS4_REVOKED_STATEID_TYPE;
375 nfs4_test_and_free_stateid(server, stateid, cred);
376}
377
378static void nfs4_free_revoked_stateid(struct nfs_server *server,
379 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100380 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400381{
382 nfs4_stateid tmp;
383
384 nfs4_stateid_copy(&tmp, stateid);
385 __nfs4_free_revoked_stateid(server, &tmp, cred);
386}
387
NeilBrown8478eaa2014-09-18 16:09:27 +1000388static long nfs4_update_delay(long *timeout)
389{
390 long ret;
391 if (!timeout)
392 return NFS4_POLL_RETRY_MAX;
393 if (*timeout <= 0)
394 *timeout = NFS4_POLL_RETRY_MIN;
395 if (*timeout > NFS4_POLL_RETRY_MAX)
396 *timeout = NFS4_POLL_RETRY_MAX;
397 ret = *timeout;
398 *timeout <<= 1;
399 return ret;
400}
401
Trond Myklebust0688e642019-04-07 13:59:09 -0400402static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500403{
Trond Myklebust65de8722008-12-23 15:21:44 -0500404 might_sleep();
405
NeilBrown8478eaa2014-09-18 16:09:27 +1000406 freezable_schedule_timeout_killable_unsafe(
407 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400408 if (!__fatal_signal_pending(current))
409 return 0;
410 return -EINTR;
411}
412
413static int nfs4_delay_interruptible(long *timeout)
414{
415 might_sleep();
416
417 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
418 if (!signal_pending(current))
419 return 0;
420 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
421}
422
423static int nfs4_delay(long *timeout, bool interruptible)
424{
425 if (interruptible)
426 return nfs4_delay_interruptible(timeout);
427 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500428}
429
Trond Myklebust50c80002019-07-11 19:02:18 -0400430static const nfs4_stateid *
431nfs4_recoverable_stateid(const nfs4_stateid *stateid)
432{
433 if (!stateid)
434 return NULL;
435 switch (stateid->type) {
436 case NFS4_OPEN_STATEID_TYPE:
437 case NFS4_LOCK_STATEID_TYPE:
438 case NFS4_DELEGATION_STATEID_TYPE:
439 return stateid;
440 default:
441 break;
442 }
443 return NULL;
444}
445
Trond Myklebust65de8722008-12-23 15:21:44 -0500446/* This is the error handling routine for processes that are allowed
447 * to sleep.
448 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400449static int nfs4_do_handle_exception(struct nfs_server *server,
450 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500451{
452 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500453 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400454 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500455 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500456 int ret = errorcode;
457
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400458 exception->delay = 0;
459 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500460 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400461
Trond Myklebust50c80002019-07-11 19:02:18 -0400462 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400463 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400464 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400465
Trond Myklebust65de8722008-12-23 15:21:44 -0500466 switch(errorcode) {
467 case 0:
468 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400469 case -NFS4ERR_BADHANDLE:
470 case -ESTALE:
471 if (inode != NULL && S_ISREG(inode->i_mode))
472 pnfs_destroy_layout(NFS_I(inode));
473 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400474 case -NFS4ERR_DELEG_REVOKED:
475 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400476 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400477 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400478 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400479 if (inode != NULL && stateid != NULL) {
480 nfs_inode_find_state_and_recover(inode,
481 stateid);
482 goto wait_on_recovery;
483 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500484 /* Fall through */
Trond Myklebust272289a2016-09-22 13:39:15 -0400485 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400486 if (inode) {
487 int err;
488
489 err = nfs_async_inode_return_delegation(inode,
490 stateid);
491 if (err == 0)
492 goto wait_on_recovery;
493 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
494 exception->retry = 1;
495 break;
496 }
497 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500498 if (state == NULL)
499 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400500 ret = nfs4_schedule_stateid_recovery(server, state);
501 if (ret < 0)
502 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500503 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500504 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500505 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500506 nfs4_schedule_lease_recovery(clp);
507 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400508 case -NFS4ERR_MOVED:
509 ret = nfs4_schedule_migration_recovery(server);
510 if (ret < 0)
511 break;
512 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400513 case -NFS4ERR_LEASE_MOVED:
514 nfs4_schedule_lease_moved_recovery(clp);
515 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500516#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400517 case -NFS4ERR_BADSESSION:
518 case -NFS4ERR_BADSLOT:
519 case -NFS4ERR_BAD_HIGH_SLOT:
520 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
521 case -NFS4ERR_DEADSESSION:
522 case -NFS4ERR_SEQ_FALSE_RETRY:
523 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100524 /* Handled in nfs41_sequence_process() */
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400525 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500526#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500527 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500528 if (exception->timeout > HZ) {
529 /* We have retried a decent amount, time to
530 * fail
531 */
532 ret = -EBUSY;
533 break;
534 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500535 /* Fall through */
Trond Myklebust65de8722008-12-23 15:21:44 -0500536 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400537 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500538 /* Fall through */
Trond Myklebust2598ed32015-09-20 16:10:18 -0400539 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400540 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400541 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400542 exception->delay = 1;
543 return 0;
544
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400545 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500546 case -NFS4ERR_OLD_STATEID:
547 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800548 break;
549 case -NFS4ERR_BADOWNER:
550 /* The following works around a Linux server bug! */
551 case -NFS4ERR_BADNAME:
552 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
553 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
554 exception->retry = 1;
555 printk(KERN_WARNING "NFS: v4 server %s "
556 "does not accept raw "
557 "uid/gids. "
558 "Reenabling the idmapper.\n",
559 server->nfs_client->cl_hostname);
560 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500561 }
562 /* We failed to handle the error */
563 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500564wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400565 exception->recovering = 1;
566 return 0;
567}
568
569/* This is the error handling routine for processes that are allowed
570 * to sleep.
571 */
572int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
573{
574 struct nfs_client *clp = server->nfs_client;
575 int ret;
576
577 ret = nfs4_do_handle_exception(server, errorcode, exception);
578 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400579 ret = nfs4_delay(&exception->timeout,
580 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400581 goto out_retry;
582 }
583 if (exception->recovering) {
584 ret = nfs4_wait_clnt_recover(clp);
585 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
586 return -EIO;
587 goto out_retry;
588 }
589 return ret;
590out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500591 if (ret == 0)
592 exception->retry = 1;
593 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500594}
595
Trond Myklebust037fc982015-09-20 15:51:00 -0400596static int
597nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
598 int errorcode, struct nfs4_exception *exception)
599{
600 struct nfs_client *clp = server->nfs_client;
601 int ret;
602
603 ret = nfs4_do_handle_exception(server, errorcode, exception);
604 if (exception->delay) {
605 rpc_delay(task, nfs4_update_delay(&exception->timeout));
606 goto out_retry;
607 }
608 if (exception->recovering) {
609 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
610 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
611 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
612 goto out_retry;
613 }
614 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
615 ret = -EIO;
616 return ret;
617out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500618 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400619 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500620 /*
621 * For NFS4ERR_MOVED, the client transport will need to
622 * be recomputed after migration recovery has completed.
623 */
624 if (errorcode == -NFS4ERR_MOVED)
625 rpc_task_release_transport(task);
626 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400627 return ret;
628}
629
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400630int
Trond Myklebust037fc982015-09-20 15:51:00 -0400631nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
632 struct nfs4_state *state, long *timeout)
633{
634 struct nfs4_exception exception = {
635 .state = state,
636 };
637
638 if (task->tk_status >= 0)
639 return 0;
640 if (timeout)
641 exception.timeout = *timeout;
642 task->tk_status = nfs4_async_handle_exception(task, server,
643 task->tk_status,
644 &exception);
645 if (exception.delay && timeout)
646 *timeout = exception.timeout;
647 if (exception.retry)
648 return -EAGAIN;
649 return 0;
650}
651
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400652/*
653 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
654 * or 'false' otherwise.
655 */
656static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
657{
658 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500659 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400660}
Trond Myklebust65de8722008-12-23 15:21:44 -0500661
Trond Myklebust452e9352010-07-31 14:29:06 -0400662static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 spin_lock(&clp->cl_lock);
665 if (time_before(clp->cl_last_renewal,timestamp))
666 clp->cl_last_renewal = timestamp;
667 spin_unlock(&clp->cl_lock);
668}
669
Trond Myklebust452e9352010-07-31 14:29:06 -0400670static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
671{
Trond Myklebustbe824162015-07-05 14:50:46 -0400672 struct nfs_client *clp = server->nfs_client;
673
674 if (!nfs4_has_session(clp))
675 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400676}
677
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400678struct nfs4_call_sync_data {
679 const struct nfs_server *seq_server;
680 struct nfs4_sequence_args *seq_args;
681 struct nfs4_sequence_res *seq_res;
682};
683
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800684void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400685 struct nfs4_sequence_res *res, int cache_reply,
686 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400687{
688 args->sa_slot = NULL;
689 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400690 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400691
692 res->sr_slot = NULL;
693}
694
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400695static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400696{
697 struct nfs4_slot *slot = res->sr_slot;
698 struct nfs4_slot_table *tbl;
699
Chuck Lever3bd23842013-08-09 12:49:19 -0400700 tbl = slot->table;
701 spin_lock(&tbl->slot_tbl_lock);
702 if (!nfs41_wake_and_assign_slot(tbl, slot))
703 nfs4_free_slot(tbl, slot);
704 spin_unlock(&tbl->slot_tbl_lock);
705
706 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400707}
708
709static int nfs40_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
711{
712 if (res->sr_slot != NULL)
713 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400714 return 1;
715}
716
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400717#if defined(CONFIG_NFS_V4_1)
718
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400719static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400720{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500721 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400722 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500723 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400724
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400725 if (!slot)
726 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500727 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500728 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500729
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400730 /* Bump the slot sequence number */
731 if (slot->seq_done)
732 slot->seq_nr++;
733 slot->seq_done = 0;
734
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500735 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500736 /* Be nice to the server: try to ensure that the last transmitted
737 * value for highest_user_slotid <= target_highest_slotid
738 */
739 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
740 send_new_highest_used_slotid = true;
741
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500742 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500743 send_new_highest_used_slotid = false;
744 goto out_unlock;
745 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500746 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500747
748 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
749 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500750out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500751 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500752 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400753 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400754 if (waitqueue_active(&tbl->slot_waitq))
755 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400756}
757
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400758static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
759{
760 nfs41_release_slot(res->sr_slot);
761 res->sr_slot = NULL;
762}
763
Trond Myklebust3453d572018-06-20 17:53:34 -0400764static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
765 u32 seqnr)
766{
767 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
768 slot->seq_nr_highest_sent = seqnr;
769}
770static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
771 u32 seqnr)
772{
773 slot->seq_nr_highest_sent = seqnr;
774 slot->seq_nr_last_acked = seqnr;
775}
776
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400777static int nfs41_sequence_process(struct rpc_task *task,
778 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400779{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500780 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500781 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400782 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100783 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500784 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400785
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500786 if (slot == NULL)
787 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400788 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500789 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400790 goto out;
791
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500792 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500793
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400794 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100795
796 status = res->sr_status;
797 if (task->tk_status == -NFS4ERR_DEADSESSION)
798 status = -NFS4ERR_DEADSESSION;
799
Andy Adamson691daf32009-12-04 15:55:39 -0500800 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100801 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400802 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400803 /* Mark this sequence number as having been acked */
804 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400805 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400806 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500807 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500808 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500809 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400810 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
811 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500812 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400813 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500814 case 1:
815 /*
816 * sr_status remains 1 if an RPC level error occurred.
817 * The server may or may not have processed the sequence
818 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500819 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400820 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
821 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500822 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 case -NFS4ERR_DELAY:
824 /* The server detected a resend of the RPC call and
825 * returned NFS4ERR_DELAY as per Section 2.10.6.2
826 * of RFC5661.
827 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500828 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400829 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500830 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500831 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400832 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400833 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400834 case -NFS4ERR_RETRY_UNCACHED_REP:
835 case -NFS4ERR_SEQ_FALSE_RETRY:
836 /*
837 * The server thinks we tried to replay a request.
838 * Retry the call after bumping the sequence ID.
839 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400840 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400841 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500842 case -NFS4ERR_BADSLOT:
843 /*
844 * The slot id we used was probably retired. Try again
845 * using a different slot id.
846 */
Trond Myklebust99589102018-06-09 12:50:50 -0400847 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400848 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500849 goto retry_nowait;
850 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400851 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500852 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400853 * Were one or more calls using this slot interrupted?
854 * If the server never received the request, then our
855 * transmitted slot sequence number may be too high.
Trond Myklebustac20d162012-12-15 15:36:07 -0500856 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400857 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
858 slot->seq_nr--;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500859 goto retry_nowait;
860 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400861 /*
862 * RFC5661:
863 * A retry might be sent while the original request is
864 * still in progress on the replier. The replier SHOULD
865 * deal with the issue by returning NFS4ERR_DELAY as the
866 * reply to SEQUENCE or CB_SEQUENCE operation, but
867 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
868 *
869 * Restart the search after a delay.
870 */
871 slot->seq_nr = slot->seq_nr_highest_sent;
872 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100873 case -NFS4ERR_BADSESSION:
874 case -NFS4ERR_DEADSESSION:
875 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
876 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400877 default:
878 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400879 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400880 }
881out:
882 /* The session may be reset by one of the error handlers. */
883 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500884out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500885 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400886session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100887 nfs4_schedule_session_recovery(session, status);
888 dprintk("%s ERROR: %d Reset session\n", __func__, status);
889 nfs41_sequence_free_slot(res);
890 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400891retry_new_seq:
892 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500893retry_nowait:
894 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400895 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500896 task->tk_status = 0;
897 ret = 0;
898 }
899 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400900out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400901 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400902 goto out;
903 rpc_delay(task, NFS4_POLL_RETRY_MAX);
904 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400905}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400906
907int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
908{
909 if (!nfs41_sequence_process(task, res))
910 return 0;
911 if (res->sr_slot != NULL)
912 nfs41_sequence_free_slot(res);
913 return 1;
914
915}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500916EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400917
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400918static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
919{
920 if (res->sr_slot == NULL)
921 return 1;
922 if (res->sr_slot->table->session != NULL)
923 return nfs41_sequence_process(task, res);
924 return nfs40_sequence_done(task, res);
925}
926
927static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
928{
929 if (res->sr_slot != NULL) {
930 if (res->sr_slot->table->session != NULL)
931 nfs41_sequence_free_slot(res);
932 else
933 nfs40_sequence_free_slot(res);
934 }
935}
936
Peng Tao2c4b1312014-06-11 05:24:16 +0800937int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400938{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500939 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400940 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400941 if (!res->sr_slot->table->session)
942 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400943 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400944}
Peng Tao2c4b1312014-06-11 05:24:16 +0800945EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400946
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
948{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400949 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950
Trond Myklebust035168ab2010-06-16 09:52:26 -0400951 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
952
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500953 nfs4_setup_sequence(data->seq_server->nfs_client,
954 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955}
956
Andy Adamson69ab40c2009-04-01 09:22:19 -0400957static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
958{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400959 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400960
Trond Myklebust14516c32010-07-31 14:29:06 -0400961 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400962}
963
Trond Myklebust17280172012-03-11 13:11:00 -0400964static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400965 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400966 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400967};
968
Chuck Lever3bd23842013-08-09 12:49:19 -0400969#else /* !CONFIG_NFS_V4_1 */
970
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400971static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
972{
973 return nfs40_sequence_done(task, res);
974}
975
976static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
977{
978 if (res->sr_slot != NULL)
979 nfs40_sequence_free_slot(res);
980}
981
Peng Tao2c4b1312014-06-11 05:24:16 +0800982int nfs4_sequence_done(struct rpc_task *task,
983 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400984{
Chuck Lever3bd23842013-08-09 12:49:19 -0400985 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400986}
Peng Tao2c4b1312014-06-11 05:24:16 +0800987EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400988
989#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400990
Trond Myklebustc1dffe02019-03-01 12:13:34 -0500991static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
992{
993 res->sr_timestamp = jiffies;
994 res->sr_status_flags = 0;
995 res->sr_status = 1;
996}
997
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400998static
999void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1000 struct nfs4_sequence_res *res,
1001 struct nfs4_slot *slot)
1002{
1003 if (!slot)
1004 return;
1005 slot->privileged = args->sa_privileged ? 1 : 0;
1006 args->sa_slot = slot;
1007
1008 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001009}
1010
1011int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001012 struct nfs4_sequence_args *args,
1013 struct nfs4_sequence_res *res,
1014 struct rpc_task *task)
1015{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001016 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001017 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001018 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001019
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001020 /* slot already allocated? */
1021 if (res->sr_slot != NULL)
1022 goto out_start;
1023
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001024 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001025 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001026
Trond Myklebust3453d572018-06-20 17:53:34 -04001027 spin_lock(&tbl->slot_tbl_lock);
1028 /* The state manager will wait until the slot table is empty */
1029 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1030 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001031
Trond Myklebust3453d572018-06-20 17:53:34 -04001032 slot = nfs4_alloc_slot(tbl);
1033 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001034 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001035 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001036 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001037 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001038 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001039
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001040 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001041
Anna Schumakerad05cc02017-01-11 13:37:06 -05001042 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001043out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001044 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001045 rpc_call_start(task);
1046 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001047out_sleep_timeout:
1048 /* Try again in 1/4 second */
1049 if (args->sa_privileged)
1050 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1051 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1052 else
1053 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1054 NULL, jiffies + (HZ >> 2));
1055 spin_unlock(&tbl->slot_tbl_lock);
1056 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001057out_sleep:
1058 if (args->sa_privileged)
1059 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001060 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001061 else
1062 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1063 spin_unlock(&tbl->slot_tbl_lock);
1064 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001065}
1066EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1067
Chuck Lever9915ea72013-08-09 12:48:27 -04001068static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1069{
1070 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001071 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001072 data->seq_args, data->seq_res, task);
1073}
1074
1075static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1076{
1077 struct nfs4_call_sync_data *data = calldata;
1078 nfs4_sequence_done(task, data->seq_res);
1079}
1080
1081static const struct rpc_call_ops nfs40_call_sync_ops = {
1082 .rpc_call_prepare = nfs40_call_sync_prepare,
1083 .rpc_call_done = nfs40_call_sync_done,
1084};
1085
Anna Schumaker48c05852019-08-14 15:27:00 -04001086static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1087{
1088 int ret;
1089 struct rpc_task *task;
1090
1091 task = rpc_run_task(task_setup);
1092 if (IS_ERR(task))
1093 return PTR_ERR(task);
1094
1095 ret = task->tk_status;
1096 rpc_put_task(task);
1097 return ret;
1098}
1099
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001100static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1101 struct nfs_server *server,
1102 struct rpc_message *msg,
1103 struct nfs4_sequence_args *args,
1104 struct nfs4_sequence_res *res,
1105 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001106{
Chuck Lever9915ea72013-08-09 12:48:27 -04001107 struct nfs_client *clp = server->nfs_client;
1108 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001109 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001110 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001111 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112 };
1113 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001114 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001115 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001116 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001117 .callback_data = &data,
1118 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001119 };
1120
Anna Schumaker48c05852019-08-14 15:27:00 -04001121 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001122}
1123
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001124static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1125 struct nfs_server *server,
1126 struct rpc_message *msg,
1127 struct nfs4_sequence_args *args,
1128 struct nfs4_sequence_res *res)
1129{
1130 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1131}
1132
1133
Bryan Schumaker7c513052011-03-24 17:12:24 +00001134int nfs4_call_sync(struct rpc_clnt *clnt,
1135 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001136 struct rpc_message *msg,
1137 struct nfs4_sequence_args *args,
1138 struct nfs4_sequence_res *res,
1139 int cache_reply)
1140{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001141 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001142 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001143}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Trond Myklebust3c591172018-07-31 15:54:10 -04001145static void
1146nfs4_inc_nlink_locked(struct inode *inode)
1147{
1148 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1149 inc_nlink(inode);
1150}
1151
1152static void
1153nfs4_dec_nlink_locked(struct inode *inode)
1154{
1155 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1156 drop_nlink(inode);
1157}
1158
1159static void
1160update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001161 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 struct nfs_inode *nfsi = NFS_I(dir);
1164
Trond Myklebust16e14372018-03-20 16:53:31 -04001165 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1166 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001167 | NFS_INO_INVALID_DATA
1168 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001169 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001170 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1171 nfsi->attrtimeo_timestamp = jiffies;
1172 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001174 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001175 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1176 NFS_INO_INVALID_ACL;
1177 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001178 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001179 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001180 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001181 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001182 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001183}
1184
1185static void
1186update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001187 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001188{
1189 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001190 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 spin_unlock(&dir->i_lock);
1192}
1193
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001194struct nfs4_open_createattrs {
1195 struct nfs4_label *label;
1196 struct iattr *sattr;
1197 const __u32 verf[2];
1198};
1199
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001200static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1201 int err, struct nfs4_exception *exception)
1202{
1203 if (err != -EINVAL)
1204 return false;
1205 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1206 return false;
1207 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1208 exception->retry = 1;
1209 return true;
1210}
1211
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001212static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1213{
1214 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1215}
1216
1217static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1218{
1219 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1220
1221 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1222}
1223
Trond Myklebust6ae37332015-01-30 14:21:14 -05001224static u32
1225nfs4_map_atomic_open_share(struct nfs_server *server,
1226 fmode_t fmode, int openflags)
1227{
1228 u32 res = 0;
1229
1230 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1231 case FMODE_READ:
1232 res = NFS4_SHARE_ACCESS_READ;
1233 break;
1234 case FMODE_WRITE:
1235 res = NFS4_SHARE_ACCESS_WRITE;
1236 break;
1237 case FMODE_READ|FMODE_WRITE:
1238 res = NFS4_SHARE_ACCESS_BOTH;
1239 }
1240 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1241 goto out;
1242 /* Want no delegation if we're using O_DIRECT */
1243 if (openflags & O_DIRECT)
1244 res |= NFS4_SHARE_WANT_NO_DELEG;
1245out:
1246 return res;
1247}
1248
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001249static enum open_claim_type4
1250nfs4_map_atomic_open_claim(struct nfs_server *server,
1251 enum open_claim_type4 claim)
1252{
1253 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1254 return claim;
1255 switch (claim) {
1256 default:
1257 return claim;
1258 case NFS4_OPEN_CLAIM_FH:
1259 return NFS4_OPEN_CLAIM_NULL;
1260 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1261 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1262 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1263 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1264 }
1265}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268{
1269 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001270 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 p->o_res.seqid = p->o_arg.seqid;
1272 p->c_res.seqid = p->c_arg.seqid;
1273 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001274 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001275 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001276 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001277}
1278
Al Viro82a2c1b2011-06-22 18:30:55 -04001279static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001280 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001281 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001282 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001283 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001284{
Al Viro82a2c1b2011-06-22 18:30:55 -04001285 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001286 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001287 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001288 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001289 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290 struct nfs4_opendata *p;
1291
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001292 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001293 if (p == NULL)
1294 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001295
1296 p->f_label = nfs4_label_alloc(server, gfp_mask);
1297 if (IS_ERR(p->f_label))
1298 goto err_free_p;
1299
Kinglong Meea49c2692015-07-27 15:31:38 +08001300 p->a_label = nfs4_label_alloc(server, gfp_mask);
1301 if (IS_ERR(p->a_label))
1302 goto err_free_f;
1303
Trond Myklebust63f5f792015-01-23 19:19:25 -05001304 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1305 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001306 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001307 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001308 nfs_sb_active(dentry->d_sb);
1309 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001310 p->dir = parent;
1311 p->owner = sp;
1312 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001313 p->o_arg.open_flags = flags;
1314 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001315 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001316 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1317 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001318 if (flags & O_CREAT) {
1319 p->o_arg.umask = current_umask();
1320 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1321 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1322 p->o_arg.u.attrs = &p->attrs;
1323 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1324
1325 memcpy(p->o_arg.u.verifier.data, c->verf,
1326 sizeof(p->o_arg.u.verifier.data));
1327 }
1328 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001329 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1330 * will return permission denied for all bits until close */
1331 if (!(flags & O_EXCL)) {
1332 /* ask server to check for all possible rights as results
1333 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001334 switch (p->o_arg.claim) {
1335 default:
1336 break;
1337 case NFS4_OPEN_CLAIM_NULL:
1338 case NFS4_OPEN_CLAIM_FH:
1339 p->o_arg.access = NFS4_ACCESS_READ |
1340 NFS4_ACCESS_MODIFY |
1341 NFS4_ACCESS_EXTEND |
1342 NFS4_ACCESS_EXECUTE;
1343 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001344 }
David Howells7539bba2006-08-22 20:06:09 -04001345 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001346 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1347 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001348 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001349 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001350 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001351 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001352 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001353 case NFS4_OPEN_CLAIM_NULL:
1354 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1355 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1356 p->o_arg.fh = NFS_FH(dir);
1357 break;
1358 case NFS4_OPEN_CLAIM_PREVIOUS:
1359 case NFS4_OPEN_CLAIM_FH:
1360 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1361 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001362 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001363 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001364 p->c_arg.fh = &p->o_res.fh;
1365 p->c_arg.stateid = &p->o_res.stateid;
1366 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001367 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001368 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001369 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001370
1371err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001372 nfs4_label_free(p->a_label);
1373err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001374 nfs4_label_free(p->f_label);
1375err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001376 kfree(p);
1377err:
1378 dput(parent);
1379 return NULL;
1380}
1381
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001382static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001383{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001384 struct nfs4_opendata *p = container_of(kref,
1385 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001386 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001387
Fred Isaman30ae2412016-10-18 13:39:51 -04001388 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001389 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001390 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001391 if (p->state != NULL)
1392 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001393 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001394
Kinglong Meea49c2692015-07-27 15:31:38 +08001395 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001396 nfs4_label_free(p->f_label);
1397
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001398 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001399 dput(p->dentry);
1400 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001401 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001402 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001403 kfree(p);
1404}
1405
1406static void nfs4_opendata_put(struct nfs4_opendata *p)
1407{
1408 if (p != NULL)
1409 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001410}
1411
Trond Myklebust24311f82015-09-20 10:50:17 -04001412static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1413 fmode_t fmode)
1414{
1415 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1416 case FMODE_READ|FMODE_WRITE:
1417 return state->n_rdwr != 0;
1418 case FMODE_WRITE:
1419 return state->n_wronly != 0;
1420 case FMODE_READ:
1421 return state->n_rdonly != 0;
1422 }
1423 WARN_ON_ONCE(1);
1424 return false;
1425}
1426
Trond Myklebustbe189f72018-09-27 17:12:33 -04001427static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1428 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001429{
1430 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001431
Trond Myklebust536e43d2012-01-17 22:04:26 -05001432 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001433 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001434 switch (claim) {
1435 case NFS4_OPEN_CLAIM_NULL:
1436 case NFS4_OPEN_CLAIM_FH:
1437 goto out;
1438 default:
1439 break;
1440 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001442 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001443 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1444 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001445 break;
1446 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001447 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1448 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001449 break;
1450 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001451 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1452 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001453 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001454out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001455 return ret;
1456}
1457
Trond Myklebust2a606182015-08-19 22:30:00 -05001458static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1459 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001460{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001461 if (delegation == NULL)
1462 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001463 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001464 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001465 switch (claim) {
1466 case NFS4_OPEN_CLAIM_NULL:
1467 case NFS4_OPEN_CLAIM_FH:
1468 break;
1469 case NFS4_OPEN_CLAIM_PREVIOUS:
1470 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1471 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001472 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001473 default:
1474 return 0;
1475 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001476 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001477 return 1;
1478}
1479
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001480static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001481{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001482 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001483 case FMODE_WRITE:
1484 state->n_wronly++;
1485 break;
1486 case FMODE_READ:
1487 state->n_rdonly++;
1488 break;
1489 case FMODE_READ|FMODE_WRITE:
1490 state->n_rdwr++;
1491 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001492 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001493}
1494
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001495#ifdef CONFIG_NFS_V4_1
1496static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1497{
1498 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1499 return true;
1500 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1501 return true;
1502 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1503 return true;
1504 return false;
1505}
1506#endif /* CONFIG_NFS_V4_1 */
1507
Trond Myklebustc9399f22017-11-06 15:28:01 -05001508static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1509{
1510 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1511 wake_up_all(&state->waitq);
1512}
1513
1514static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1515 const nfs4_stateid *stateid)
1516{
1517 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1518 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1519
1520 if (stateid_seqid == state_seqid + 1U ||
1521 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1522 nfs_state_log_update_open_stateid(state);
1523 else
1524 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1525}
1526
Trond Myklebust4f14c192014-02-12 19:15:06 -05001527static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001528{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001529 struct nfs_client *clp = state->owner->so_server->nfs_client;
1530 bool need_recover = false;
1531
1532 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1533 need_recover = true;
1534 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1535 need_recover = true;
1536 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1537 need_recover = true;
1538 if (need_recover)
1539 nfs4_state_mark_reclaim_nograce(clp, state);
1540}
1541
Trond Myklebustc9399f22017-11-06 15:28:01 -05001542/*
1543 * Check for whether or not the caller may update the open stateid
1544 * to the value passed in by stateid.
1545 *
1546 * Note: This function relies heavily on the server implementing
1547 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1548 * correctly.
1549 * i.e. The stateid seqids have to be initialised to 1, and
1550 * are then incremented on every state transition.
1551 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001552static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001553 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001554{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001555 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1556 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1557 if (stateid->seqid == cpu_to_be32(1))
1558 nfs_state_log_update_open_stateid(state);
1559 else
1560 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001561 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001562 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001563
1564 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1565 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001566 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001567 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001568 return false;
1569}
1570
Trond Myklebustf95549c2015-01-23 18:06:09 -05001571static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1572{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001573 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1574 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001575 if (state->n_wronly)
1576 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1577 if (state->n_rdonly)
1578 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1579 if (state->n_rdwr)
1580 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001581 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001582}
1583
Trond Myklebust226056c2014-02-11 10:41:07 -05001584static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1585 nfs4_stateid *stateid, fmode_t fmode)
1586{
1587 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1588 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1589 case FMODE_WRITE:
1590 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1591 break;
1592 case FMODE_READ:
1593 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1594 break;
1595 case 0:
1596 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1597 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1598 clear_bit(NFS_OPEN_STATE, &state->flags);
1599 }
1600 if (stateid == NULL)
1601 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001602 /* Handle OPEN+OPEN_DOWNGRADE races */
1603 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1604 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001605 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001606 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001607 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001608 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001609 nfs4_stateid_copy(&state->stateid, stateid);
1610 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001611 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001612out:
1613 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001614}
1615
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001616static void nfs_clear_open_stateid(struct nfs4_state *state,
1617 nfs4_stateid *arg_stateid,
1618 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001619{
1620 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001621 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1622 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1623 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001624 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001625 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1626 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001627}
1628
Trond Myklebust1393d962016-09-22 13:39:13 -04001629static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001630 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001631 __must_hold(&state->owner->so_lock)
1632 __must_hold(&state->seqlock)
1633 __must_hold(RCU)
1634
Trond Myklebust003707c2007-07-05 18:07:55 -04001635{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001636 DEFINE_WAIT(wait);
1637 int status = 0;
1638 for (;;) {
1639
1640 if (!nfs_need_update_open_stateid(state, stateid))
1641 return;
1642 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001643 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001644 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001645 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001646 /* Rely on seqids for serialisation with NFSv4.0 */
1647 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1648 break;
1649
1650 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1651 /*
1652 * Ensure we process the state changes in the same order
1653 * in which the server processed them by delaying the
1654 * update of the stateid until we are in sequence.
1655 */
1656 write_sequnlock(&state->seqlock);
1657 spin_unlock(&state->owner->so_lock);
1658 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001659 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001660 if (!signal_pending(current)) {
1661 if (schedule_timeout(5*HZ) == 0)
1662 status = -EAGAIN;
1663 else
1664 status = 0;
1665 } else
1666 status = -EINTR;
1667 finish_wait(&state->waitq, &wait);
1668 rcu_read_lock();
1669 spin_lock(&state->owner->so_lock);
1670 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001671 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001672
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001673 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1674 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001675 nfs4_stateid_copy(freeme, &state->open_stateid);
1676 nfs_test_and_clear_all_open_stateid(state);
1677 }
1678
Trond Myklebuste999e802014-02-10 18:20:47 -05001679 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1680 nfs4_stateid_copy(&state->stateid, stateid);
1681 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001682 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001683 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001684}
1685
Trond Myklebustc9399f22017-11-06 15:28:01 -05001686static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001687 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001688 fmode_t fmode,
1689 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001691 /*
1692 * Protect the call to nfs4_state_set_mode_locked and
1693 * serialise the stateid update
1694 */
1695 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001696 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1697 switch (fmode) {
1698 case FMODE_READ:
1699 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1700 break;
1701 case FMODE_WRITE:
1702 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1703 break;
1704 case FMODE_READ|FMODE_WRITE:
1705 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001706 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001707 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001708 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001709}
1710
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001711static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1712{
1713 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1714 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1715 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1716 clear_bit(NFS_OPEN_STATE, &state->flags);
1717}
1718
Trond Myklebustc9399f22017-11-06 15:28:01 -05001719static void nfs_state_set_delegation(struct nfs4_state *state,
1720 const nfs4_stateid *deleg_stateid,
1721 fmode_t fmode)
1722{
1723 /*
1724 * Protect the call to nfs4_state_set_mode_locked and
1725 * serialise the stateid update
1726 */
1727 write_seqlock(&state->seqlock);
1728 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1729 set_bit(NFS_DELEGATED_STATE, &state->flags);
1730 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001733static void nfs_state_clear_delegation(struct nfs4_state *state)
1734{
1735 write_seqlock(&state->seqlock);
1736 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1737 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1738 write_sequnlock(&state->seqlock);
1739}
1740
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001741int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001742 const nfs4_stateid *open_stateid,
1743 const nfs4_stateid *delegation,
1744 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001745{
Trond Myklebust1393d962016-09-22 13:39:13 -04001746 struct nfs_server *server = NFS_SERVER(state->inode);
1747 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001748 struct nfs_inode *nfsi = NFS_I(state->inode);
1749 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001750 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001751 int ret = 0;
1752
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001753 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001754
1755 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001756 spin_lock(&state->owner->so_lock);
1757 if (open_stateid != NULL) {
1758 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1759 ret = 1;
1760 }
1761
Trond Myklebust333ac782019-10-22 12:12:17 -04001762 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001763 if (deleg_cur == NULL)
1764 goto no_delegation;
1765
1766 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001767 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001768 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001769 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001770 goto no_delegation_unlock;
1771
1772 if (delegation == NULL)
1773 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001774 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001775 goto no_delegation_unlock;
1776
Trond Myklebustb7391f42008-12-23 15:21:52 -05001777 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001778 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001779 ret = 1;
1780no_delegation_unlock:
1781 spin_unlock(&deleg_cur->lock);
1782no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001783 if (ret)
1784 update_open_stateflags(state, fmode);
1785 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001786 rcu_read_unlock();
1787
Trond Myklebust4f14c192014-02-12 19:15:06 -05001788 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001789 nfs4_schedule_state_manager(clp);
1790 if (freeme.type != 0)
1791 nfs4_test_and_free_stateid(server, &freeme,
1792 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001793
1794 return ret;
1795}
1796
Trond Myklebust39071e62015-01-24 15:07:56 -05001797static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1798 const nfs4_stateid *stateid)
1799{
1800 struct nfs4_state *state = lsp->ls_state;
1801 bool ret = false;
1802
1803 spin_lock(&state->state_lock);
1804 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1805 goto out_noupdate;
1806 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1807 goto out_noupdate;
1808 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1809 ret = true;
1810out_noupdate:
1811 spin_unlock(&state->state_lock);
1812 return ret;
1813}
Trond Myklebust34310432008-12-23 15:21:38 -05001814
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001815static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001816{
1817 struct nfs_delegation *delegation;
1818
Trond Myklebustf5086242018-03-20 16:43:13 -04001819 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001820 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001821 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001822 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001823 rcu_read_unlock();
1824 return;
1825 }
1826 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001827 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001828}
1829
Trond Myklebust6ee41262007-07-08 14:11:36 -04001830static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001831{
1832 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001833 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001834 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001835 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001836 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001837 nfs4_stateid stateid;
1838 int ret = -EAGAIN;
1839
Trond Myklebustaac00a82007-07-05 19:02:21 -04001840 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001841 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001842 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001843 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001844 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001845 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001846 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001847 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001848 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001849 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001850 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001851 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001852 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001853 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001854 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001855 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001857 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001858 if (!opendata->is_recover) {
1859 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1860 if (ret != 0)
1861 goto out;
1862 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001863 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001864
1865 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001866 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001867 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001868 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001869out:
1870 return ERR_PTR(ret);
1871out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001872 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001873 return state;
1874}
1875
Andy Adamsone23008e2012-10-02 21:07:32 -04001876static void
1877nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1878{
1879 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1880 struct nfs_delegation *delegation;
1881 int delegation_flags = 0;
1882
1883 rcu_read_lock();
1884 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1885 if (delegation)
1886 delegation_flags = delegation->flags;
1887 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001888 switch (data->o_arg.claim) {
1889 default:
1890 break;
1891 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1892 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001893 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1894 "returning a delegation for "
1895 "OPEN(CLAIM_DELEGATE_CUR)\n",
1896 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001897 return;
1898 }
1899 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001900 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001901 data->owner->so_cred,
1902 data->o_res.delegation_type,
1903 &data->o_res.delegation,
1904 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001905 else
1906 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001907 data->owner->so_cred,
1908 data->o_res.delegation_type,
1909 &data->o_res.delegation,
1910 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001911
1912 if (data->o_res.do_recall)
1913 nfs_async_inode_return_delegation(state->inode,
1914 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001915}
1916
1917/*
1918 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1919 * and update the nfs4_state.
1920 */
1921static struct nfs4_state *
1922_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1923{
1924 struct inode *inode = data->state->inode;
1925 struct nfs4_state *state = data->state;
1926 int ret;
1927
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001928 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001929 if (data->rpc_status)
1930 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001931 /* cached opens have already been processed */
1932 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001933 }
1934
Andy Adamsone23008e2012-10-02 21:07:32 -04001935 ret = nfs_refresh_inode(inode, &data->f_attr);
1936 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001937 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001938
1939 if (data->o_res.delegation_type != 0)
1940 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001941update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001942 if (!update_open_stateid(state, &data->o_res.stateid,
1943 NULL, data->o_arg.fmode))
1944 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001945 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001946
1947 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001948}
1949
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001950static struct inode *
1951nfs4_opendata_get_inode(struct nfs4_opendata *data)
1952{
1953 struct inode *inode;
1954
1955 switch (data->o_arg.claim) {
1956 case NFS4_OPEN_CLAIM_NULL:
1957 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1958 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1959 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1960 return ERR_PTR(-EAGAIN);
1961 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1962 &data->f_attr, data->f_label);
1963 break;
1964 default:
1965 inode = d_inode(data->dentry);
1966 ihold(inode);
1967 nfs_refresh_inode(inode, &data->f_attr);
1968 }
1969 return inode;
1970}
1971
Andy Adamsone23008e2012-10-02 21:07:32 -04001972static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001973nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1974{
1975 struct nfs4_state *state;
1976 struct inode *inode;
1977
1978 inode = nfs4_opendata_get_inode(data);
1979 if (IS_ERR(inode))
1980 return ERR_CAST(inode);
1981 if (data->state != NULL && data->state->inode == inode) {
1982 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001983 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04001984 } else
1985 state = nfs4_get_open_state(inode, data->owner);
1986 iput(inode);
1987 if (state == NULL)
1988 state = ERR_PTR(-ENOMEM);
1989 return state;
1990}
1991
Andy Adamsone23008e2012-10-02 21:07:32 -04001992static struct nfs4_state *
1993_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001994{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001995 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001996
Trond Myklebustaac00a82007-07-05 19:02:21 -04001997 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001998 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001999 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002000 goto out;
2001 }
2002
Trond Myklebust75e8c482017-08-08 10:38:07 -04002003 state = nfs4_opendata_find_nfs4_state(data);
2004 if (IS_ERR(state))
2005 goto out;
2006
Andy Adamsone23008e2012-10-02 21:07:32 -04002007 if (data->o_res.delegation_type != 0)
2008 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002009 if (!update_open_stateid(state, &data->o_res.stateid,
2010 NULL, data->o_arg.fmode)) {
2011 nfs4_put_open_state(state);
2012 state = ERR_PTR(-EAGAIN);
2013 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002014out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002015 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002016 return state;
2017}
2018
Andy Adamsone23008e2012-10-02 21:07:32 -04002019static struct nfs4_state *
2020nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2021{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002022 struct nfs4_state *ret;
2023
Andy Adamsone23008e2012-10-02 21:07:32 -04002024 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002025 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2026 else
2027 ret = _nfs4_opendata_to_nfs4_state(data);
2028 nfs4_sequence_free_slot(&data->o_res.seq_res);
2029 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002030}
2031
Trond Myklebust0de43972018-09-02 15:57:01 -04002032static struct nfs_open_context *
2033nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002034{
2035 struct nfs_inode *nfsi = NFS_I(state->inode);
2036 struct nfs_open_context *ctx;
2037
Trond Myklebust0de43972018-09-02 15:57:01 -04002038 rcu_read_lock();
2039 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002040 if (ctx->state != state)
2041 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002042 if ((ctx->mode & mode) != mode)
2043 continue;
2044 if (!get_nfs_open_context(ctx))
2045 continue;
2046 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002047 return ctx;
2048 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002049 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002050 return ERR_PTR(-ENOENT);
2051}
2052
Trond Myklebust0de43972018-09-02 15:57:01 -04002053static struct nfs_open_context *
2054nfs4_state_find_open_context(struct nfs4_state *state)
2055{
2056 struct nfs_open_context *ctx;
2057
2058 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2059 if (!IS_ERR(ctx))
2060 return ctx;
2061 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2062 if (!IS_ERR(ctx))
2063 return ctx;
2064 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2065}
2066
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002067static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2068 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002069{
2070 struct nfs4_opendata *opendata;
2071
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002072 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002073 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002074 if (opendata == NULL)
2075 return ERR_PTR(-ENOMEM);
2076 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002077 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002078 return opendata;
2079}
2080
Trond Myklebust24311f82015-09-20 10:50:17 -04002081static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2082 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002083{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002084 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002085 int ret;
2086
Trond Myklebust24311f82015-09-20 10:50:17 -04002087 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002088 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002089 opendata->o_arg.open_flags = 0;
2090 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002091 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2092 NFS_SB(opendata->dentry->d_sb),
2093 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002094 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2095 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2096 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002097 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002098 if (ret != 0)
2099 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002100 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002101 if (IS_ERR(newstate))
2102 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002103 if (newstate != opendata->state)
2104 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002105 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002106 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002107}
2108
2109static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2110{
Trond Myklebust864472e2006-01-03 09:55:15 +01002111 int ret;
2112
2113 /* memory barrier prior to reading state->n_* */
2114 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002115 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2116 if (ret != 0)
2117 return ret;
2118 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2119 if (ret != 0)
2120 return ret;
2121 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2122 if (ret != 0)
2123 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002124 /*
2125 * We may have performed cached opens for all three recoveries.
2126 * Check if we need to update the current stateid.
2127 */
2128 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002129 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002130 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002131 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002132 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002133 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002134 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002135 return 0;
2136}
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138/*
2139 * OPEN_RECLAIM:
2140 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002142static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002144 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002145 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002146 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int status;
2148
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002149 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2150 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002151 if (IS_ERR(opendata))
2152 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002153 rcu_read_lock();
2154 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002155 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002156 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002157 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002158 opendata->o_arg.u.delegation_type = delegation_type;
2159 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002160 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return status;
2162}
2163
Trond Myklebust539cd032007-06-05 11:46:42 -04002164static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 struct nfs_server *server = NFS_SERVER(state->inode);
2167 struct nfs4_exception exception = { };
2168 int err;
2169 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002170 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002171 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002172 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2173 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002174 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002175 break;
2176 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 } while (exception.retry);
2178 return err;
2179}
2180
Trond Myklebust864472e2006-01-03 09:55:15 +01002181static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2182{
2183 struct nfs_open_context *ctx;
2184 int ret;
2185
2186 ctx = nfs4_state_find_open_context(state);
2187 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002188 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002189 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2190 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002191 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002192 put_nfs_open_context(ctx);
2193 return ret;
2194}
2195
NeilBrowndce26302017-12-13 09:57:09 +11002196static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002198 switch (err) {
2199 default:
2200 printk(KERN_ERR "NFS: %s: unhandled error "
2201 "%d.\n", __func__, err);
2202 case 0:
2203 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002204 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002205 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002206 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002207 break;
2208 case -NFS4ERR_BADSESSION:
2209 case -NFS4ERR_BADSLOT:
2210 case -NFS4ERR_BAD_HIGH_SLOT:
2211 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2212 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002213 return -EAGAIN;
2214 case -NFS4ERR_STALE_CLIENTID:
2215 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002216 /* Don't recall a delegation if it was lost */
2217 nfs4_schedule_lease_recovery(server->nfs_client);
2218 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002219 case -NFS4ERR_MOVED:
2220 nfs4_schedule_migration_recovery(server);
2221 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002222 case -NFS4ERR_LEASE_MOVED:
2223 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2224 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002225 case -NFS4ERR_DELEG_REVOKED:
2226 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002227 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002228 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002229 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002230 nfs_inode_find_state_and_recover(state->inode,
2231 stateid);
2232 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002233 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002234 case -NFS4ERR_DELAY:
2235 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002236 ssleep(1);
2237 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002238 case -ENOMEM:
2239 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002240 if (fl) {
2241 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2242 if (lsp)
2243 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2244 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002245 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return err;
2248}
2249
Trond Myklebust24311f82015-09-20 10:50:17 -04002250int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002251 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002252{
2253 struct nfs_server *server = NFS_SERVER(state->inode);
2254 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002255 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002256
2257 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2258 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2259 if (IS_ERR(opendata))
2260 return PTR_ERR(opendata);
2261 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002262 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002263 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2264 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002265 goto out;
2266 }
2267 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002268 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2269 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002270 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002271 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002272 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2273 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2274 if (err)
2275 goto out;
2276 }
2277 nfs_state_clear_delegation(state);
2278out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002279 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002280 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002281}
2282
Chuck Leverbe05c862013-08-09 12:49:47 -04002283static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2284{
2285 struct nfs4_opendata *data = calldata;
2286
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002287 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2288 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002289}
2290
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002291static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2292{
2293 struct nfs4_opendata *data = calldata;
2294
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002295 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002296
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002297 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002298 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002299 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002300 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002301 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002302 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002303 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002304}
2305
2306static void nfs4_open_confirm_release(void *calldata)
2307{
2308 struct nfs4_opendata *data = calldata;
2309 struct nfs4_state *state = NULL;
2310
2311 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002312 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002313 goto out_free;
2314 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002315 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002316 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002317 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002318 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002319 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002320out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002321 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002322}
2323
2324static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002325 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002326 .rpc_call_done = nfs4_open_confirm_done,
2327 .rpc_release = nfs4_open_confirm_release,
2328};
2329
2330/*
2331 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2332 */
2333static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2334{
David Howells2b0143b2015-03-17 22:25:59 +00002335 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002336 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002337 struct rpc_message msg = {
2338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2339 .rpc_argp = &data->c_arg,
2340 .rpc_resp = &data->c_res,
2341 .rpc_cred = data->owner->so_cred,
2342 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002343 struct rpc_task_setup task_setup_data = {
2344 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002345 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002346 .callback_ops = &nfs4_open_confirm_ops,
2347 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002348 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002349 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002350 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int status;
2352
Anna Schumakerfba83f32018-05-04 16:22:50 -04002353 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2354 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002355 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002356 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002357 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002358 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002359 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002360 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002361 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002362 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002363 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002364 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002365 smp_wmb();
2366 } else
2367 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002368 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 return status;
2370}
2371
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002372static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002374 struct nfs4_opendata *data = calldata;
2375 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002376 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002377 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002378
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002379 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002380 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002381 /*
2382 * Check if we still need to send an OPEN call, or if we can use
2383 * a delegation instead.
2384 */
2385 if (data->state != NULL) {
2386 struct nfs_delegation *delegation;
2387
Trond Myklebustbe189f72018-09-27 17:12:33 -04002388 if (can_open_cached(data->state, data->o_arg.fmode,
2389 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002390 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002391 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002392 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002393 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002394 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002395 rcu_read_unlock();
2396 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002397 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002398 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002399 switch (claim) {
2400 default:
2401 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002402 case NFS4_OPEN_CLAIM_PREVIOUS:
2403 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2404 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002405 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002406 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002407 case NFS4_OPEN_CLAIM_FH:
2408 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002409 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002410 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002411 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002412 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002413 &data->o_res.seq_res,
2414 task) != 0)
2415 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002416
2417 /* Set the create mode (note dependency on the session type) */
2418 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2419 if (data->o_arg.open_flags & O_EXCL) {
2420 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2421 if (nfs4_has_persistent_session(clp))
2422 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2423 else if (clp->cl_mvops->minor_version > 0)
2424 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2425 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002426 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002427unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002428 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002429 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002430out_no_action:
2431 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002432out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002433 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002434}
2435
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002436static void nfs4_open_done(struct rpc_task *task, void *calldata)
2437{
2438 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002440 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002441
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002442 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002443 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002444
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002445 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002446 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2447 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002448 case S_IFREG:
2449 break;
2450 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002451 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002452 break;
2453 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002454 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002455 break;
2456 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002457 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002458 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002459 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002460 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002461 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2462 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002463 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002464 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002465}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002466
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002467static void nfs4_open_release(void *calldata)
2468{
2469 struct nfs4_opendata *data = calldata;
2470 struct nfs4_state *state = NULL;
2471
2472 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002473 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002474 goto out_free;
2475 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002476 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002477 goto out_free;
2478 /* In case we need an open_confirm, no cleanup! */
2479 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2480 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002481 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002482 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002483 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002484out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002485 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002486}
2487
2488static const struct rpc_call_ops nfs4_open_ops = {
2489 .rpc_call_prepare = nfs4_open_prepare,
2490 .rpc_call_done = nfs4_open_done,
2491 .rpc_release = nfs4_open_release,
2492};
2493
Fred Isaman3b65a302016-09-19 10:06:49 -04002494static int nfs4_run_open_task(struct nfs4_opendata *data,
2495 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002496{
David Howells2b0143b2015-03-17 22:25:59 +00002497 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002498 struct nfs_server *server = NFS_SERVER(dir);
2499 struct nfs_openargs *o_arg = &data->o_arg;
2500 struct nfs_openres *o_res = &data->o_res;
2501 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002502 struct rpc_message msg = {
2503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2504 .rpc_argp = o_arg,
2505 .rpc_resp = o_res,
2506 .rpc_cred = data->owner->so_cred,
2507 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002508 struct rpc_task_setup task_setup_data = {
2509 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002510 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002511 .callback_ops = &nfs4_open_ops,
2512 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002513 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002514 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002515 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002516 int status;
2517
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002518 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002519 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002520 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002521 data->cancelled = false;
2522 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002523 if (!ctx) {
2524 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002525 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002526 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002527 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002528 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002529 pnfs_lgopen_prepare(data, ctx);
2530 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002531 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002532 if (IS_ERR(task))
2533 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002534 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002535 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002536 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002537 smp_wmb();
2538 } else
2539 status = data->rpc_status;
2540 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002541
2542 return status;
2543}
2544
2545static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2546{
David Howells2b0143b2015-03-17 22:25:59 +00002547 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002548 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002549 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002550
Fred Isaman3b65a302016-09-19 10:06:49 -04002551 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002552 if (status != 0 || !data->rpc_done)
2553 return status;
2554
Trond Myklebust6926afd12012-01-07 13:22:46 -05002555 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2556
Anna Schumakerd7e98252017-01-11 16:13:29 -05002557 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002558 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002559
2560 return status;
2561}
2562
Trond Myklebustf3792d62014-07-10 08:54:32 -04002563/*
2564 * Additional permission checks in order to distinguish between an
2565 * open for read, and an open for execute. This works around the
2566 * fact that NFSv4 OPEN treats read and execute permissions as being
2567 * the same.
2568 * Note that in the non-execute case, we want to turn off permission
2569 * checking if we just created a new file (POSIX open() semantics).
2570 */
NeilBrowna52458b2018-12-03 11:30:31 +11002571static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002572 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002573 struct nfs4_state *state, fmode_t fmode,
2574 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002575{
2576 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002577 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002578
2579 /* access call failed or for some reason the server doesn't
2580 * support any access modes -- defer access call until later */
2581 if (opendata->o_res.access_supported == 0)
2582 return 0;
2583
2584 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002585 /*
2586 * Use openflags to check for exec, because fmode won't
2587 * always have FMODE_EXEC set when file open for exec.
2588 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002589 if (openflags & __FMODE_EXEC) {
2590 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002591 if (S_ISDIR(state->inode->i_mode))
2592 mask = NFS4_ACCESS_LOOKUP;
2593 else
2594 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002595 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002596 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002597
2598 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002599 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2600 nfs_access_add_cache(state->inode, &cache);
2601
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002602 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2603 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002604 return 0;
2605
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002606 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002607}
2608
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002609/*
2610 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2611 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002612static int _nfs4_proc_open(struct nfs4_opendata *data,
2613 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002614{
David Howells2b0143b2015-03-17 22:25:59 +00002615 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002616 struct nfs_server *server = NFS_SERVER(dir);
2617 struct nfs_openargs *o_arg = &data->o_arg;
2618 struct nfs_openres *o_res = &data->o_res;
2619 int status;
2620
Fred Isaman3b65a302016-09-19 10:06:49 -04002621 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002622 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002623 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002624 if (status != 0) {
2625 if (status == -NFS4ERR_BADNAME &&
2626 !(o_arg->open_flags & O_CREAT))
2627 return -ENOENT;
2628 return status;
2629 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002630
Trond Myklebust6926afd12012-01-07 13:22:46 -05002631 nfs_fattr_map_and_free_names(server, &data->f_attr);
2632
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002633 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002634 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002635 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002636 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002637 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002638 if (data->file_created ||
2639 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002640 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002641 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002642 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002643 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2644 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002646 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002648 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002650 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2651 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002652 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2653 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002654 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658/*
2659 * OPEN_EXPIRED:
2660 * reclaim state on the server after a network partition.
2661 * Assumes caller holds the appropriate lock
2662 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002663static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002665 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002666 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002668 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002669 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002670 if (IS_ERR(opendata))
2671 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002672 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002673 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002674 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002675 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002679static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002680{
Trond Myklebust539cd032007-06-05 11:46:42 -04002681 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002682 struct nfs4_exception exception = { };
2683 int err;
2684
2685 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002686 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002687 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002688 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2689 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002690 switch (err) {
2691 default:
2692 goto out;
2693 case -NFS4ERR_GRACE:
2694 case -NFS4ERR_DELAY:
2695 nfs4_handle_exception(server, err, &exception);
2696 err = 0;
2697 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002698 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002699out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002700 return err;
2701}
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002706 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Trond Myklebust864472e2006-01-03 09:55:15 +01002708 ctx = nfs4_state_find_open_context(state);
2709 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002710 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002711 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002712 put_nfs_open_context(ctx);
2713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Trond Myklebust41020b62016-09-22 13:38:58 -04002716static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2717 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002718{
Trond Myklebust41020b62016-09-22 13:38:58 -04002719 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002720 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002721}
2722
2723static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2724{
2725 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002726 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002727}
2728
2729static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2730{
2731 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2732 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002733 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002734 return nfs4_open_expired(sp, state);
2735}
2736
Trond Myklebust45870d62016-09-22 13:38:59 -04002737static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2738 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002739 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002740{
2741 return -NFS4ERR_BAD_STATEID;
2742}
2743
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002744#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002745static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2746 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002747 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002748{
2749 int status;
2750
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002751 switch (stateid->type) {
2752 default:
2753 break;
2754 case NFS4_INVALID_STATEID_TYPE:
2755 case NFS4_SPECIAL_STATEID_TYPE:
2756 return -NFS4ERR_BAD_STATEID;
2757 case NFS4_REVOKED_STATEID_TYPE:
2758 goto out_free;
2759 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002760
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002761 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002762 switch (status) {
2763 case -NFS4ERR_EXPIRED:
2764 case -NFS4ERR_ADMIN_REVOKED:
2765 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002766 break;
2767 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002768 return status;
2769 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002770out_free:
2771 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002772 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002773 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002774}
2775
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002776static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002777{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002778 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002779 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002780 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002781 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002782 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002783
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002784 /* Get the delegation credential for use by test/free_stateid */
2785 rcu_read_lock();
2786 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002787 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002788 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002789 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002790 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002791 }
2792
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002793 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002794 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002795
Trond Myklebust994b15b2018-09-05 14:07:14 -04002796 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2797 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002798 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002799 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002800 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002801 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002802
NeilBrowna52458b2018-12-03 11:30:31 +11002803 if (delegation->cred)
2804 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002805 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002806 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002807 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002808 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002809 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002810 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002811 else
2812 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002813
Trond Myklebust8c39a392019-07-19 13:48:44 -04002814 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002815 return ret;
2816}
2817
2818static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2819{
2820 nfs4_stateid tmp;
2821
2822 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2823 nfs4_copy_delegation_stateid(state->inode, state->state,
2824 &tmp, NULL) &&
2825 nfs4_stateid_match_other(&state->stateid, &tmp))
2826 nfs_state_set_delegation(state, &tmp, state->state);
2827 else
2828 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002829}
2830
2831/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002832 * nfs41_check_expired_locks - possibly free a lock stateid
2833 *
2834 * @state: NFSv4 state for an inode
2835 *
2836 * Returns NFS_OK if recovery for this stateid is now finished.
2837 * Otherwise a negative NFS4ERR value is returned.
2838 */
2839static int nfs41_check_expired_locks(struct nfs4_state *state)
2840{
2841 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002842 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002843 struct nfs_server *server = NFS_SERVER(state->inode);
2844
2845 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2846 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002847
2848 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002849 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2850 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002851 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002852
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002853 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002854 spin_unlock(&state->state_lock);
2855
2856 nfs4_put_lock_state(prev);
2857 prev = lsp;
2858
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002859 status = nfs41_test_and_free_expired_stateid(server,
2860 &lsp->ls_stateid,
2861 cred);
2862 trace_nfs4_test_lock_stateid(state, lsp, status);
2863 if (status == -NFS4ERR_EXPIRED ||
2864 status == -NFS4ERR_BAD_STATEID) {
2865 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002866 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002867 if (!recover_lost_locks)
2868 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2869 } else if (status != NFS_OK) {
2870 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002871 nfs4_put_lock_state(prev);
2872 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002873 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002874 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002875 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002876 }
2877 spin_unlock(&state->state_lock);
2878 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002879out:
2880 return ret;
2881}
2882
2883/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002884 * nfs41_check_open_stateid - possibly free an open stateid
2885 *
2886 * @state: NFSv4 state for an inode
2887 *
2888 * Returns NFS_OK if recovery for this stateid is now finished.
2889 * Otherwise a negative NFS4ERR value is returned.
2890 */
2891static int nfs41_check_open_stateid(struct nfs4_state *state)
2892{
2893 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002894 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002895 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002896 int status;
2897
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002898 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002899 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002900 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002901 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002902 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002903 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002904 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002905 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002906 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002907 if (nfs_open_stateid_recover_openmode(state))
2908 return -NFS4ERR_OPENMODE;
2909 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002910}
2911
2912static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2913{
Chuck Levereb64cf92012-07-11 16:30:05 -04002914 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002915
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002916 status = nfs41_check_delegation_stateid(state);
2917 if (status != NFS_OK)
2918 return status;
2919 nfs41_delegation_recover_stateid(state);
2920
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002921 status = nfs41_check_expired_locks(state);
2922 if (status != NFS_OK)
2923 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002924 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002925 if (status != NFS_OK)
2926 status = nfs4_open_expired(sp, state);
2927 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002928}
2929#endif
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002932 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2933 * fields corresponding to attributes that were used to store the verifier.
2934 * Make sure we clobber those fields in the later setattr call
2935 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002936static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002937 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002938{
Trond Myklebust609339c2018-03-28 16:18:17 -04002939 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2940 __u32 attrset[3];
2941 unsigned ret;
2942 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002943
Trond Myklebust609339c2018-03-28 16:18:17 -04002944 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2945 attrset[i] = opendata->o_res.attrset[i];
2946 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2947 attrset[i] &= ~bitmask[i];
2948 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002949
Trond Myklebust609339c2018-03-28 16:18:17 -04002950 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2951 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002952
Trond Myklebust609339c2018-03-28 16:18:17 -04002953 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2954 if (sattr->ia_valid & ATTR_ATIME_SET)
2955 ret |= ATTR_ATIME_SET;
2956 else
2957 ret |= ATTR_ATIME;
2958 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002959
Trond Myklebust609339c2018-03-28 16:18:17 -04002960 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2961 if (sattr->ia_valid & ATTR_MTIME_SET)
2962 ret |= ATTR_MTIME_SET;
2963 else
2964 ret |= ATTR_MTIME;
2965 }
2966
2967 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002968 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002969 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002970}
2971
Trond Myklebustc21443c2013-02-07 14:26:21 -05002972static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002973 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002974{
2975 struct nfs4_state_owner *sp = opendata->owner;
2976 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002977 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002978 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002979 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05002980 struct inode *dir = d_inode(opendata->dir);
2981 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002982 unsigned int seq;
2983 int ret;
2984
2985 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05002986 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002987
Fred Isaman3b65a302016-09-19 10:06:49 -04002988 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002989 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002990 goto out;
2991
Trond Myklebustae55e592018-05-22 11:17:16 -04002992 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002993 ret = PTR_ERR(state);
2994 if (IS_ERR(state))
2995 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002996 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002997 if (server->caps & NFS_CAP_POSIX_LOCK)
2998 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002999 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3000 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003001
Trond Myklebust275bb302013-05-29 13:11:28 -04003002 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003003 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003004 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003005 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003006 alias = d_exact_alias(dentry, state->inode);
3007 if (!alias)
3008 alias = d_splice_alias(igrab(state->inode), dentry);
3009 /* d_splice_alias() can't fail here - it's a non-directory */
3010 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003011 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003012 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003013 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003014 }
3015
3016 switch(opendata->o_arg.claim) {
3017 default:
3018 break;
3019 case NFS4_OPEN_CLAIM_NULL:
3020 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3021 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3022 if (!opendata->rpc_done)
3023 break;
3024 if (opendata->o_res.delegation_type != 0)
3025 dir_verifier = nfs_save_change_attribute(dir);
3026 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003027 }
3028
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003029 /* Parse layoutget results before we check for access */
3030 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3031
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003032 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3033 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003034 if (ret != 0)
3035 goto out;
3036
David Howells2b0143b2015-03-17 22:25:59 +00003037 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003038 nfs_inode_attach_open_context(ctx);
3039 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3040 nfs4_schedule_stateid_recovery(server, state);
3041 }
Fred Isaman2409a972016-10-06 12:11:21 -04003042
Trond Myklebustc21443c2013-02-07 14:26:21 -05003043out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003044 if (!opendata->cancelled)
3045 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003046 return ret;
3047}
3048
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003049/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003050 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 */
Andy Adamson82be4172012-05-23 05:02:35 -04003052static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003053 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003054 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003055 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003056 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 struct nfs4_state_owner *sp;
3059 struct nfs4_state *state = NULL;
3060 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003061 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003062 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003063 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003064 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003065 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003066 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003067 struct iattr *sattr = c->sattr;
3068 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003069 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003070 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003074 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3075 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3077 goto out_err;
3078 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003079 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003080 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003081 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003082 if (d_really_is_positive(dentry))
3083 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003084 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003085 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003086 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003087 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3088 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003089 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003090 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
David Quigley14c43f72013-05-22 12:50:43 -04003092 if (label) {
3093 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3094 if (IS_ERR(olabel)) {
3095 status = PTR_ERR(olabel);
3096 goto err_opendata_put;
3097 }
3098 }
3099
Trond Myklebuste911b812014-03-26 13:24:37 -07003100 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3101 if (!opendata->f_attr.mdsthreshold) {
3102 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3103 if (!opendata->f_attr.mdsthreshold)
3104 goto err_free_label;
3105 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003106 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003107 }
David Howells2b0143b2015-03-17 22:25:59 +00003108 if (d_really_is_positive(dentry))
3109 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003110
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003111 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003112 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003113 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003114 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003115
NeilBrownefcbc042015-07-30 13:00:56 +10003116 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003117 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003118 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003119 /*
3120 * send create attributes which was not set by open
3121 * with an extra setattr.
3122 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003123 if (attrs || label) {
3124 unsigned ia_old = sattr->ia_valid;
3125
3126 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003127 nfs_fattr_init(opendata->o_res.f_attr);
3128 status = nfs4_do_setattr(state->inode, cred,
3129 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003130 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003131 if (status == 0) {
3132 nfs_setattr_update_inode(state->inode, sattr,
3133 opendata->o_res.f_attr);
3134 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3135 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003136 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003137 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003138 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003139 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003140 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003141
Trond Myklebuste911b812014-03-26 13:24:37 -07003142 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003143 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003144 opendata->f_attr.mdsthreshold = NULL;
3145 }
Andy Adamson82be4172012-05-23 05:02:35 -04003146
David Quigley14c43f72013-05-22 12:50:43 -04003147 nfs4_label_free(olabel);
3148
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003149 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003152err_free_label:
3153 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003154err_opendata_put:
3155 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003156err_put_state_owner:
3157 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return status;
3160}
3161
3162
Andy Adamson82be4172012-05-23 05:02:35 -04003163static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003164 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003165 int flags,
3166 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003167 struct nfs4_label *label,
3168 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003170 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003171 struct nfs4_exception exception = {
3172 .interruptible = true,
3173 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003175 struct nfs4_open_createattrs c = {
3176 .label = label,
3177 .sattr = sattr,
3178 .verf = {
3179 [0] = (__u32)jiffies,
3180 [1] = (__u32)current->pid,
3181 },
3182 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 int status;
3184
3185 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003186 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003187 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003188 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (status == 0)
3190 break;
3191 /* NOTE: BAD_SEQID means the server and client disagree about the
3192 * book-keeping w.r.t. state-changing operations
3193 * (OPEN/CLOSE/LOCK/LOCKU...)
3194 * It is actually a sign of a bug on the client or on the server.
3195 *
3196 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003197 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 * have unhashed the old state_owner for us, and that we can
3199 * therefore safely retry using a new one. We should still warn
3200 * the user though...
3201 */
3202 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003203 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003204 " returned a bad sequence-id error!\n",
3205 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 exception.retry = 1;
3207 continue;
3208 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003209 /*
3210 * BAD_STATEID on OPEN means that the server cancelled our
3211 * state before it received the OPEN_CONFIRM.
3212 * Recover by retrying the request as per the discussion
3213 * on Page 181 of RFC3530.
3214 */
3215 if (status == -NFS4ERR_BAD_STATEID) {
3216 exception.retry = 1;
3217 continue;
3218 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003219 if (status == -NFS4ERR_EXPIRED) {
3220 nfs4_schedule_lease_recovery(server->nfs_client);
3221 exception.retry = 1;
3222 continue;
3223 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003224 if (status == -EAGAIN) {
3225 /* We must have found a delegation */
3226 exception.retry = 1;
3227 continue;
3228 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003229 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3230 continue;
3231 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 status, &exception));
3233 } while (exception.retry);
3234 return res;
3235}
3236
Trond Myklebust8487c472016-06-26 08:44:35 -04003237static int _nfs4_do_setattr(struct inode *inode,
3238 struct nfs_setattrargs *arg,
3239 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003240 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003241 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003242{
3243 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003244 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3246 .rpc_argp = arg,
3247 .rpc_resp = res,
3248 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003249 };
NeilBrowna52458b2018-12-03 11:30:31 +11003250 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003251 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003252 bool truncate;
3253 int status;
3254
3255 nfs_fattr_init(res->fattr);
3256
3257 /* Servers should only apply open mode checks for file size changes */
3258 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003259 if (!truncate)
3260 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003261
Trond Myklebust991eedb2018-04-09 11:15:30 -04003262 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003263 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003264 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003265 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003266 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003267 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003268 l_ctx = nfs_get_lock_context(ctx);
3269 if (IS_ERR(l_ctx))
3270 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003271 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3272 &arg->stateid, &delegation_cred);
3273 nfs_put_lock_context(l_ctx);
3274 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003275 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003276 else if (status == -EAGAIN)
3277 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003278 } else {
3279zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003280 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003281 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003282 if (delegation_cred)
3283 msg.rpc_cred = delegation_cred;
3284
3285 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3286
NeilBrowna52458b2018-12-03 11:30:31 +11003287 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003288 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003289 renew_lease(server, timestamp);
3290 trace_nfs4_setattr(inode, &arg->stateid, status);
3291 return status;
3292}
3293
NeilBrowna52458b2018-12-03 11:30:31 +11003294static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003295 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003296 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003297 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003299 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003300 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003301 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003302 struct nfs_setattrargs arg = {
3303 .fh = NFS_FH(inode),
3304 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003306 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003307 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003308 };
3309 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003311 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003313 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003314 struct nfs4_exception exception = {
3315 .state = state,
3316 .inode = inode,
3317 .stateid = &arg.stateid,
3318 };
3319 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003322 nfs4_bitmap_copy_adjust_setattr(bitmask,
3323 nfs4_bitmask(server, olabel),
3324 inode);
3325
NeilBrown29b59f92016-10-13 15:26:47 +11003326 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003327 switch (err) {
3328 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003329 if (!(sattr->ia_valid & ATTR_SIZE)) {
3330 pr_warn_once("NFSv4: server %s is incorrectly "
3331 "applying open mode checks to "
3332 "a SETATTR that is not "
3333 "changing file size.\n",
3334 server->nfs_client->cl_hostname);
3335 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003336 if (state && !(state->state & FMODE_WRITE)) {
3337 err = -EBADF;
3338 if (sattr->ia_valid & ATTR_OPEN)
3339 err = -EACCES;
3340 goto out;
3341 }
3342 }
3343 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003345out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return err;
3347}
3348
Peng Tao500d7012015-09-22 11:35:22 +08003349static bool
3350nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3351{
3352 if (inode == NULL || !nfs_have_layout(inode))
3353 return false;
3354
3355 return pnfs_wait_on_layoutreturn(inode, task);
3356}
3357
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003358/*
3359 * Update the seqid of an open stateid
3360 */
3361static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3362 struct nfs4_state *state)
3363{
3364 __be32 seqid_open;
3365 u32 dst_seqid;
3366 int seq;
3367
3368 for (;;) {
3369 if (!nfs4_valid_open_stateid(state))
3370 break;
3371 seq = read_seqbegin(&state->seqlock);
3372 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3373 nfs4_stateid_copy(dst, &state->open_stateid);
3374 if (read_seqretry(&state->seqlock, seq))
3375 continue;
3376 break;
3377 }
3378 seqid_open = state->open_stateid.seqid;
3379 if (read_seqretry(&state->seqlock, seq))
3380 continue;
3381
3382 dst_seqid = be32_to_cpu(dst->seqid);
3383 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3384 dst->seqid = seqid_open;
3385 break;
3386 }
3387}
3388
3389/*
3390 * Update the seqid of an open stateid after receiving
3391 * NFS4ERR_OLD_STATEID
3392 */
3393static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3394 struct nfs4_state *state)
3395{
3396 __be32 seqid_open;
3397 u32 dst_seqid;
3398 bool ret;
3399 int seq;
3400
3401 for (;;) {
3402 ret = false;
3403 if (!nfs4_valid_open_stateid(state))
3404 break;
3405 seq = read_seqbegin(&state->seqlock);
3406 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3407 if (read_seqretry(&state->seqlock, seq))
3408 continue;
3409 break;
3410 }
3411 seqid_open = state->open_stateid.seqid;
3412 if (read_seqretry(&state->seqlock, seq))
3413 continue;
3414
3415 dst_seqid = be32_to_cpu(dst->seqid);
3416 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3417 dst->seqid = cpu_to_be32(dst_seqid + 1);
3418 else
3419 dst->seqid = seqid_open;
3420 ret = true;
3421 break;
3422 }
3423
3424 return ret;
3425}
3426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427struct nfs4_closedata {
3428 struct inode *inode;
3429 struct nfs4_state *state;
3430 struct nfs_closeargs arg;
3431 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003432 struct {
3433 struct nfs4_layoutreturn_args arg;
3434 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003435 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003436 u32 roc_barrier;
3437 bool roc;
3438 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003439 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003440 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441};
3442
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003443static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003444{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003445 struct nfs4_closedata *calldata = data;
3446 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003447 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003448
Trond Myklebustcf805162016-11-15 14:56:07 -05003449 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003450 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3451 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003452 nfs4_put_open_state(calldata->state);
3453 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003454 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003455 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003456 kfree(calldata);
3457}
3458
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003459static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003461 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003464 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003465 struct nfs4_exception exception = {
3466 .state = state,
3467 .inode = calldata->inode,
3468 .stateid = &calldata->arg.stateid,
3469 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Chuck Levera3ca5652012-03-01 17:00:40 -05003471 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003472 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3473 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003474 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003475
3476 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04003477 if (pnfs_roc_done(task, calldata->inode,
3478 &calldata->arg.lr_args,
3479 &calldata->res.lr_res,
3480 &calldata->res.lr_ret) == -EAGAIN)
3481 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003482
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003483 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 * the state_owner. we keep this around to process errors
3485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 switch (task->tk_status) {
3487 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003488 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003489 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003490 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003491 case -NFS4ERR_ACCESS:
3492 if (calldata->arg.bitmask != NULL) {
3493 calldata->arg.bitmask = NULL;
3494 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003495 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003496
3497 }
3498 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003499 case -NFS4ERR_OLD_STATEID:
3500 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003501 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003502 state))
3503 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003504 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003505 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003507 case -NFS4ERR_EXPIRED:
3508 nfs4_free_revoked_stateid(server,
3509 &calldata->arg.stateid,
3510 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003511 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003512 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003513 if (calldata->arg.fmode == 0)
3514 break;
3515 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003517 task->tk_status = nfs4_async_handle_exception(task,
3518 server, task->tk_status, &exception);
3519 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003520 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003522 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3523 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003524out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003525 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003526 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003527 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003528 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003529 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003530out_restart:
3531 task->tk_status = 0;
3532 rpc_restart_call_prepare(task);
3533 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003536static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003538 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003539 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003540 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003541 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003542 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003543 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003544
Chuck Levera3ca5652012-03-01 17:00:40 -05003545 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003546 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003547 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003548
Trond Myklebust88069f72009-12-08 08:33:16 -05003549 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003550 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003551 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3552 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3553 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003554 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003555 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003556 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003557 if (state->n_rdonly == 0)
3558 call_close |= is_rdonly;
3559 else if (is_rdonly)
3560 calldata->arg.fmode |= FMODE_READ;
3561 if (state->n_wronly == 0)
3562 call_close |= is_wronly;
3563 else if (is_wronly)
3564 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003565 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3566 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003567 } else if (is_rdwr)
3568 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3569
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003570 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3571 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003572 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003573 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003574
3575 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003576 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003577 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003578 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003579
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003580 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003581 nfs_release_seqid(calldata->arg.seqid);
3582 goto out_wait;
3583 }
3584
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003585 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3586 if (lo && !pnfs_layout_is_valid(lo)) {
3587 calldata->arg.lr_args = NULL;
3588 calldata->res.lr_res = NULL;
3589 }
3590
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003591 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003592 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003593
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003594 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003595 /* Close-to-open cache consistency revalidation */
3596 if (!nfs4_have_delegation(inode, FMODE_READ))
3597 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3598 else
3599 calldata->arg.bitmask = NULL;
3600 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003601
Trond Myklebust6ae37332015-01-30 14:21:14 -05003602 calldata->arg.share_access =
3603 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3604 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003605
Trond Myklebustd8d84982016-12-19 12:14:44 -05003606 if (calldata->res.fattr == NULL)
3607 calldata->arg.bitmask = NULL;
3608 else if (calldata->arg.bitmask == NULL)
3609 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003610 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003611 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003612 &calldata->arg.seq_args,
3613 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003614 task) != 0)
3615 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003616 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003617 return;
3618out_no_action:
3619 task->tk_action = NULL;
3620out_wait:
3621 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003624static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003625 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003626 .rpc_call_done = nfs4_close_done,
3627 .rpc_release = nfs4_free_closedata,
3628};
3629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630/*
3631 * It is possible for data to be read/written from a mem-mapped file
3632 * after the sys_close call (which hits the vfs layer as a flush).
3633 * This means that we can't safely call nfsv4 close on a file until
3634 * the inode is cleared. This in turn means that we are not good
3635 * NFSv4 citizens - we do not indicate to the server to update the file's
3636 * share state even when we are done with one of the three share
3637 * stateid's in the inode.
3638 *
3639 * NOTE: Caller must be holding the sp->so_owner semaphore!
3640 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003641int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003643 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003644 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003646 struct nfs4_state_owner *sp = state->owner;
3647 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003648 struct rpc_message msg = {
3649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3650 .rpc_cred = state->owner->so_cred,
3651 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003652 struct rpc_task_setup task_setup_data = {
3653 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003654 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003655 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003656 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003657 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003658 };
Trond Myklebust95121352005-10-18 14:20:12 -07003659 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003661 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3662 &task_setup_data.rpc_client, &msg);
3663
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003664 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003666 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003667 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003668 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003670 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003671 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3672 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003674 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3675 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003676 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003677 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003678 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003679 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003680 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003681 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003682 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003683 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003684 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003685 calldata->lr.roc = pnfs_roc(state->inode,
3686 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3687 if (calldata->lr.roc) {
3688 calldata->arg.lr_args = &calldata->lr.arg;
3689 calldata->res.lr_res = &calldata->lr.res;
3690 }
Al Viro643168c2011-06-22 18:20:23 -04003691 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003692
Trond Myklebust1174dd12010-12-21 10:52:24 -05003693 msg.rpc_argp = &calldata->arg;
3694 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003695 task_setup_data.callback_data = calldata;
3696 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003697 if (IS_ERR(task))
3698 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003699 status = 0;
3700 if (wait)
3701 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003702 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003703 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003704out_free_calldata:
3705 kfree(calldata);
3706out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003707 nfs4_put_open_state(state);
3708 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003709 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710}
3711
Trond Myklebust2b484292010-09-17 10:56:51 -04003712static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003713nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3714 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003717 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3718
3719 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Trond Myklebust565277f2007-10-15 18:17:53 -04003721 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003722 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003723
3724 nfs4_label_release_security(label);
3725
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003726 if (IS_ERR(state))
3727 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003728 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729}
3730
Trond Myklebust1185a552009-12-03 15:54:02 -05003731static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003732{
3733 if (ctx->state == NULL)
3734 return;
3735 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003736 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003737 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003738 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003739}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Trond Myklebustb944dba2013-11-04 15:20:20 -05003741#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3742#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003743#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3746{
Kinglong Mee8c612822015-08-26 21:12:58 +08003747 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003748 struct nfs4_server_caps_arg args = {
3749 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003750 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003751 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 struct nfs4_server_caps_res res = {};
3753 struct rpc_message msg = {
3754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003755 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 .rpc_resp = &res,
3757 };
3758 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003759 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Kinglong Mee8c612822015-08-26 21:12:58 +08003761 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3762 FATTR4_WORD0_FH_EXPIRE_TYPE |
3763 FATTR4_WORD0_LINK_SUPPORT |
3764 FATTR4_WORD0_SYMLINK_SUPPORT |
3765 FATTR4_WORD0_ACLSUPPORT;
3766 if (minorversion)
3767 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3768
Bryan Schumaker7c513052011-03-24 17:12:24 +00003769 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003771 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003772 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003773 case 0:
3774 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3775 res.attr_bitmask[2] = 0;
3776 break;
3777 case 1:
3778 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3779 break;
3780 case 2:
3781 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003784 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3785 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3786 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3787 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003788 NFS_CAP_CTIME|NFS_CAP_MTIME|
3789 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003790 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3791 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 server->caps |= NFS_CAP_ACLS;
3793 if (res.has_links != 0)
3794 server->caps |= NFS_CAP_HARDLINKS;
3795 if (res.has_symlinks != 0)
3796 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003797 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3798 server->caps |= NFS_CAP_FILEID;
3799 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3800 server->caps |= NFS_CAP_MODE;
3801 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3802 server->caps |= NFS_CAP_NLINK;
3803 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3804 server->caps |= NFS_CAP_OWNER;
3805 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3806 server->caps |= NFS_CAP_OWNER_GROUP;
3807 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3808 server->caps |= NFS_CAP_ATIME;
3809 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3810 server->caps |= NFS_CAP_CTIME;
3811 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3812 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003813#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3814 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3815 server->caps |= NFS_CAP_SECURITY_LABEL;
3816#endif
3817 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3818 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003819 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003820
Trond Myklebusta65318b2009-03-11 14:10:28 -04003821 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3822 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3823 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003824 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003825
3826 /* Avoid a regression due to buggy server */
3827 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3828 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003829 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3830 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003833 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 return status;
3837}
3838
Trond Myklebust55a97592006-06-09 09:34:19 -04003839int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840{
Trond Myklebust0688e642019-04-07 13:59:09 -04003841 struct nfs4_exception exception = {
3842 .interruptible = true,
3843 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 int err;
3845 do {
3846 err = nfs4_handle_exception(server,
3847 _nfs4_server_capabilities(server, fhandle),
3848 &exception);
3849 } while (exception.retry);
3850 return err;
3851}
3852
3853static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3854 struct nfs_fsinfo *info)
3855{
David Quigleyaa9c2662013-05-22 12:50:44 -04003856 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003858 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 };
3860 struct nfs4_lookup_res res = {
3861 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003862 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 .fh = fhandle,
3864 };
3865 struct rpc_message msg = {
3866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3867 .rpc_argp = &args,
3868 .rpc_resp = &res,
3869 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003870
David Quigleyaa9c2662013-05-22 12:50:44 -04003871 bitmask[0] = nfs4_fattr_bitmap[0];
3872 bitmask[1] = nfs4_fattr_bitmap[1];
3873 /*
3874 * Process the label in the upcoming getfattr
3875 */
3876 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3877
Trond Myklebust0e574af2005-10-27 22:12:38 -04003878 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003879 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880}
3881
3882static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3883 struct nfs_fsinfo *info)
3884{
Trond Myklebust0688e642019-04-07 13:59:09 -04003885 struct nfs4_exception exception = {
3886 .interruptible = true,
3887 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 int err;
3889 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003890 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003891 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003892 switch (err) {
3893 case 0:
3894 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003895 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003896 default:
3897 err = nfs4_handle_exception(server, err, &exception);
3898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003900out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return err;
3902}
3903
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003904static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3905 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3906{
Trond Myklebustc2190662013-08-26 19:23:04 -04003907 struct rpc_auth_create_args auth_args = {
3908 .pseudoflavor = flavor,
3909 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003910 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003911
Trond Myklebustc2190662013-08-26 19:23:04 -04003912 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003913 if (IS_ERR(auth))
3914 return -EACCES;
3915 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003916}
3917
Chuck Lever9a744ba2013-03-16 15:56:02 -04003918/*
3919 * Retry pseudoroot lookup with various security flavors. We do this when:
3920 *
3921 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3922 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3923 *
3924 * Returns zero on success, or a negative NFS4ERR value, or a
3925 * negative errno value.
3926 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003927static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003928 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003930 /* Per 3530bis 15.33.5 */
3931 static const rpc_authflavor_t flav_array[] = {
3932 RPC_AUTH_GSS_KRB5P,
3933 RPC_AUTH_GSS_KRB5I,
3934 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003935 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003936 RPC_AUTH_NULL,
3937 };
3938 int status = -EPERM;
3939 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003941 if (server->auth_info.flavor_len > 0) {
3942 /* try each flavor specified by user */
3943 for (i = 0; i < server->auth_info.flavor_len; i++) {
3944 status = nfs4_lookup_root_sec(server, fhandle, info,
3945 server->auth_info.flavors[i]);
3946 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3947 continue;
3948 break;
3949 }
3950 } else {
3951 /* no flavors specified by user, try default list */
3952 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3953 status = nfs4_lookup_root_sec(server, fhandle, info,
3954 flav_array[i]);
3955 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3956 continue;
3957 break;
3958 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003959 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003960
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003961 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003962 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003963 * to access the mount. It could also mean that we tried to mount
3964 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3965 * existing mount programs don't handle -EACCES very well so it should
3966 * be mapped to -EPERM instead.
3967 */
3968 if (status == -EACCES)
3969 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003970 return status;
3971}
3972
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003973/**
3974 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3975 * @server: initialized nfs_server handle
3976 * @fhandle: we fill in the pseudo-fs root file handle
3977 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003978 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003979 *
3980 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003981 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003982int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003983 struct nfs_fsinfo *info,
3984 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003985{
Andre Przywarac7757072015-04-23 17:17:40 +01003986 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003987
Andre Przywarac7757072015-04-23 17:17:40 +01003988 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003989 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003990
3991 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003992 status = server->nfs_client->cl_mvops->find_root_sec(server,
3993 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003994
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 if (status == 0)
3996 status = nfs4_server_capabilities(server, fhandle);
3997 if (status == 0)
3998 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003999
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004000 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
Bryan Schumakerbae36242012-05-10 15:07:31 -04004003static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4004 struct nfs_fsinfo *info)
4005{
4006 int error;
4007 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004008 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004009
4010 error = nfs4_server_capabilities(server, mntfh);
4011 if (error < 0) {
4012 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4013 return error;
4014 }
4015
David Quigley14c43f72013-05-22 12:50:43 -04004016 label = nfs4_label_alloc(server, GFP_KERNEL);
4017 if (IS_ERR(label))
4018 return PTR_ERR(label);
4019
Trond Myklebusta841b542018-04-07 13:50:59 -04004020 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004021 if (error < 0) {
4022 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04004023 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004024 }
4025
4026 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4027 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4028 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4029
David Quigley14c43f72013-05-22 12:50:43 -04004030err_free_label:
4031 nfs4_label_free(label);
4032
Bryan Schumakerbae36242012-05-10 15:07:31 -04004033 return error;
4034}
4035
Manoj Naik6b97fd32006-06-09 09:34:29 -04004036/*
4037 * Get locations and (maybe) other attributes of a referral.
4038 * Note that we'll actually follow the referral later when
4039 * we detect fsid mismatch in inode revalidation
4040 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004041static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4042 const struct qstr *name, struct nfs_fattr *fattr,
4043 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004044{
4045 int status = -ENOMEM;
4046 struct page *page = NULL;
4047 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004048
4049 page = alloc_page(GFP_KERNEL);
4050 if (page == NULL)
4051 goto out;
4052 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4053 if (locations == NULL)
4054 goto out;
4055
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004056 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004057 if (status != 0)
4058 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004059
4060 /*
4061 * If the fsid didn't change, this is a migration event, not a
4062 * referral. Cause us to drop into the exception handler, which
4063 * will kick off migration recovery.
4064 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004065 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004066 dprintk("%s: server did not return a different fsid for"
4067 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004068 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004069 goto out;
4070 }
Andy Adamson533eb462011-06-13 18:25:56 -04004071 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4072 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004073
Andy Adamson533eb462011-06-13 18:25:56 -04004074 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004075 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004076 memset(fhandle, 0, sizeof(struct nfs_fh));
4077out:
4078 if (page)
4079 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004080 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004081 return status;
4082}
4083
David Quigley1775fd32013-05-22 12:50:42 -04004084static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004085 struct nfs_fattr *fattr, struct nfs4_label *label,
4086 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087{
Trond Myklebust771734f2018-04-07 13:54:23 -04004088 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 struct nfs4_getattr_arg args = {
4090 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004091 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 };
4093 struct nfs4_getattr_res res = {
4094 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004095 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 .server = server,
4097 };
4098 struct rpc_message msg = {
4099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4100 .rpc_argp = &args,
4101 .rpc_resp = &res,
4102 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004103 unsigned short task_flags = 0;
4104
4105 /* Is this is an attribute revalidation, subject to softreval? */
4106 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4107 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004108
Trond Myklebust771734f2018-04-07 13:54:23 -04004109 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004110
Trond Myklebust0e574af2005-10-27 22:12:38 -04004111 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004112 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4113 return nfs4_do_call_sync(server->client, server, &msg,
4114 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115}
4116
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004117int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004118 struct nfs_fattr *fattr, struct nfs4_label *label,
4119 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120{
Trond Myklebust0688e642019-04-07 13:59:09 -04004121 struct nfs4_exception exception = {
4122 .interruptible = true,
4123 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 int err;
4125 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004126 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004127 trace_nfs4_getattr(server, fhandle, fattr, err);
4128 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 &exception);
4130 } while (exception.retry);
4131 return err;
4132}
4133
4134/*
4135 * The file is not closed if it is opened due to the a request to change
4136 * the size of the file. The open call will not be needed once the
4137 * VFS layer lookup-intents are implemented.
4138 *
4139 * Close is called when the inode is destroyed.
4140 * If we haven't opened the file for O_WRONLY, we
4141 * need to in the size_change case to obtain a stateid.
4142 *
4143 * Got race?
4144 * Because OPEN is always done by name in nfsv4, it is
4145 * possible that we opened a different file by the same
4146 * name. We can recognize this race condition, but we
4147 * can't do anything about it besides returning an error.
4148 *
4149 * This will be fixed with VFS changes (lookup-intent).
4150 */
4151static int
4152nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4153 struct iattr *sattr)
4154{
David Howells2b0143b2015-03-17 22:25:59 +00004155 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004156 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004157 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004158 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 int status;
4160
Peng Tao88ac8152014-09-12 11:04:10 +08004161 if (pnfs_ld_layoutret_on_setattr(inode) &&
4162 sattr->ia_valid & ATTR_SIZE &&
4163 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004164 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004165
Trond Myklebust0e574af2005-10-27 22:12:38 -04004166 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Andy Adamson26699402012-05-30 16:12:24 -04004168 /* Deal with open(O_TRUNC) */
4169 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004170 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004171
4172 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004173 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004174 return 0;
4175
Trond Myklebustd5308382005-11-04 15:33:38 -05004176 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004177 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004178
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004179 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004180 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004181 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004182 }
4183
David Quigley14c43f72013-05-22 12:50:43 -04004184 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4185 if (IS_ERR(label))
4186 return PTR_ERR(label);
4187
Trond Myklebust199366f2018-03-20 16:43:18 -04004188 /* Return any delegations if we're going to change ACLs */
4189 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004190 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004191
NeilBrown29b59f92016-10-13 15:26:47 +11004192 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004193 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004194 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004195 nfs_setsecurity(inode, fattr, label);
4196 }
David Quigley14c43f72013-05-22 12:50:43 -04004197 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 return status;
4199}
4200
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004201static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004202 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004203 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004204{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004205 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004206 int status;
4207 struct nfs4_lookup_arg args = {
4208 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004209 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004210 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004211 };
4212 struct nfs4_lookup_res res = {
4213 .server = server,
4214 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004215 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004216 .fh = fhandle,
4217 };
4218 struct rpc_message msg = {
4219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4220 .rpc_argp = &args,
4221 .rpc_resp = &res,
4222 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004223 unsigned short task_flags = 0;
4224
4225 /* Is this is an attribute revalidation, subject to softreval? */
4226 if (nfs_lookup_is_soft_revalidate(dentry))
4227 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004228
David Quigleyaa9c2662013-05-22 12:50:44 -04004229 args.bitmask = nfs4_bitmask(server, label);
4230
David Howells2b3de442006-08-22 20:06:09 -04004231 nfs_fattr_init(fattr);
4232
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004233 dprintk("NFS call lookup %pd2\n", dentry);
4234 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4235 status = nfs4_do_call_sync(clnt, server, &msg,
4236 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 dprintk("NFS reply lookup: %d\n", status);
4238 return status;
4239}
4240
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004241static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004242{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004243 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004244 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004245 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4246 fattr->nlink = 2;
4247}
4248
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004249static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004250 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004251 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004252{
Trond Myklebust0688e642019-04-07 13:59:09 -04004253 struct nfs4_exception exception = {
4254 .interruptible = true,
4255 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004256 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004257 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004258 int err;
4259 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004260 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004261 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004262 switch (err) {
4263 case -NFS4ERR_BADNAME:
4264 err = -ENOENT;
4265 goto out;
4266 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004267 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004268 if (err == -NFS4ERR_MOVED)
4269 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004270 goto out;
4271 case -NFS4ERR_WRONGSEC:
4272 err = -EPERM;
4273 if (client != *clnt)
4274 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004275 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004276 if (IS_ERR(client))
4277 return PTR_ERR(client);
4278
4279 exception.retry = 1;
4280 break;
4281 default:
4282 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4283 }
4284 } while (exception.retry);
4285
4286out:
4287 if (err == 0)
4288 *clnt = client;
4289 else if (client != *clnt)
4290 rpc_shutdown_client(client);
4291
4292 return err;
4293}
4294
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004295static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004296 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4297 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004299 int status;
4300 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004301
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004302 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004303 if (client != NFS_CLIENT(dir)) {
4304 rpc_shutdown_client(client);
4305 nfs_fixup_secinfo_attributes(fattr);
4306 }
4307 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308}
4309
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004310struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004311nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004312 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4313{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004314 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004315 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004316
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004317 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004318 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004319 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004320 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004321}
4322
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004323static int _nfs4_proc_lookupp(struct inode *inode,
4324 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4325 struct nfs4_label *label)
4326{
4327 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4328 struct nfs_server *server = NFS_SERVER(inode);
4329 int status;
4330 struct nfs4_lookupp_arg args = {
4331 .bitmask = server->attr_bitmask,
4332 .fh = NFS_FH(inode),
4333 };
4334 struct nfs4_lookupp_res res = {
4335 .server = server,
4336 .fattr = fattr,
4337 .label = label,
4338 .fh = fhandle,
4339 };
4340 struct rpc_message msg = {
4341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4342 .rpc_argp = &args,
4343 .rpc_resp = &res,
4344 };
4345
4346 args.bitmask = nfs4_bitmask(server, label);
4347
4348 nfs_fattr_init(fattr);
4349
4350 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4351 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4352 &res.seq_res, 0);
4353 dprintk("NFS reply lookupp: %d\n", status);
4354 return status;
4355}
4356
4357static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4358 struct nfs_fattr *fattr, struct nfs4_label *label)
4359{
Trond Myklebust0688e642019-04-07 13:59:09 -04004360 struct nfs4_exception exception = {
4361 .interruptible = true,
4362 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004363 int err;
4364 do {
4365 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4366 trace_nfs4_lookupp(inode, err);
4367 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4368 &exception);
4369 } while (exception.retry);
4370 return err;
4371}
4372
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4374{
Trond Myklebust76b32992007-08-10 17:45:11 -04004375 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 struct nfs4_accessargs args = {
4377 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004378 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004380 struct nfs4_accessres res = {
4381 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 struct rpc_message msg = {
4384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4385 .rpc_argp = &args,
4386 .rpc_resp = &res,
4387 .rpc_cred = entry->cred,
4388 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004389 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
Trond Myklebust7c672652018-06-04 15:00:53 -04004391 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004392 res.fattr = nfs_alloc_fattr();
4393 if (res.fattr == NULL)
4394 return -ENOMEM;
4395 args.bitmask = server->cache_consistency_bitmask;
4396 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004397 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004399 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004400 if (res.fattr)
4401 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004403 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 return status;
4405}
4406
4407static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4408{
Trond Myklebust0688e642019-04-07 13:59:09 -04004409 struct nfs4_exception exception = {
4410 .interruptible = true,
4411 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 int err;
4413 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004414 err = _nfs4_proc_access(inode, entry);
4415 trace_nfs4_access(inode, err);
4416 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 &exception);
4418 } while (exception.retry);
4419 return err;
4420}
4421
4422/*
4423 * TODO: For the time being, we don't try to get any attributes
4424 * along with any of the zero-copy operations READ, READDIR,
4425 * READLINK, WRITE.
4426 *
4427 * In the case of the first three, we want to put the GETATTR
4428 * after the read-type operation -- this is because it is hard
4429 * to predict the length of a GETATTR response in v4, and thus
4430 * align the READ data correctly. This means that the GETATTR
4431 * may end up partially falling into the page cache, and we should
4432 * shift it into the 'tail' of the xdr_buf before processing.
4433 * To do this efficiently, we need to know the total length
4434 * of data received, which doesn't seem to be available outside
4435 * of the RPC layer.
4436 *
4437 * In the case of WRITE, we also want to put the GETATTR after
4438 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004439 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 *
4441 * Both of these changes to the XDR layer would in fact be quite
4442 * minor, but I decided to leave them for a subsequent patch.
4443 */
4444static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4445 unsigned int pgbase, unsigned int pglen)
4446{
4447 struct nfs4_readlink args = {
4448 .fh = NFS_FH(inode),
4449 .pgbase = pgbase,
4450 .pglen = pglen,
4451 .pages = &page,
4452 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004453 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 struct rpc_message msg = {
4455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4456 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004457 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 };
4459
Bryan Schumaker7c513052011-03-24 17:12:24 +00004460 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 -07004461}
4462
4463static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4464 unsigned int pgbase, unsigned int pglen)
4465{
Trond Myklebust0688e642019-04-07 13:59:09 -04004466 struct nfs4_exception exception = {
4467 .interruptible = true,
4468 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 int err;
4470 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004471 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4472 trace_nfs4_readlink(inode, err);
4473 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 &exception);
4475 } while (exception.retry);
4476 return err;
4477}
4478
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004480 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482static int
4483nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004484 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004486 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004487 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004488 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 int status = 0;
4491
NeilBrown532d4de2016-10-13 15:26:47 +11004492 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004493 if (IS_ERR(ctx))
4494 return PTR_ERR(ctx);
4495
David Quigleyaa9c2662013-05-22 12:50:44 -04004496 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4497
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004498 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4499 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004500 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 if (IS_ERR(state)) {
4502 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004506 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004507 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 return status;
4509}
4510
Trond Myklebust3c591172018-07-31 15:54:10 -04004511static int
4512_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513{
Trond Myklebust16e42952005-10-27 22:12:44 -04004514 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004515 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004517 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004519 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004520 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004521 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4524 .rpc_argp = &args,
4525 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004527 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004528 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004529
Bryan Schumaker7c513052011-03-24 17:12:24 +00004530 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004531 if (status == 0) {
4532 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004533 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004534 /* Removing a directory decrements nlink in the parent */
4535 if (ftype == NF4DIR && dir->i_nlink > 2)
4536 nfs4_dec_nlink_locked(dir);
4537 spin_unlock(&dir->i_lock);
4538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 return status;
4540}
4541
Trond Myklebust912678d2018-03-20 16:43:15 -04004542static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4543{
Trond Myklebust0688e642019-04-07 13:59:09 -04004544 struct nfs4_exception exception = {
4545 .interruptible = true,
4546 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004547 struct inode *inode = d_inode(dentry);
4548 int err;
4549
Trond Myklebustc01d3642018-03-20 16:43:20 -04004550 if (inode) {
4551 if (inode->i_nlink == 1)
4552 nfs4_inode_return_delegation(inode);
4553 else
4554 nfs4_inode_make_writeable(inode);
4555 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004556 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004557 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004558 trace_nfs4_remove(dir, &dentry->d_name, err);
4559 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4560 &exception);
4561 } while (exception.retry);
4562 return err;
4563}
4564
4565static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566{
Trond Myklebust0688e642019-04-07 13:59:09 -04004567 struct nfs4_exception exception = {
4568 .interruptible = true,
4569 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004571
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004573 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004574 trace_nfs4_remove(dir, name, err);
4575 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 &exception);
4577 } while (exception.retry);
4578 return err;
4579}
4580
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004581static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4582 struct dentry *dentry,
4583 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004585 struct nfs_removeargs *args = msg->rpc_argp;
4586 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Trond Myklebust977fcc22018-03-20 16:43:17 -04004588 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004590 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004591
4592 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004593
4594 if (inode)
4595 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596}
4597
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004598static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4599{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004600 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004601 &data->args.seq_args,
4602 &data->res.seq_res,
4603 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
4605
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004606static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004608 struct nfs_unlinkdata *data = task->tk_calldata;
4609 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004610
Trond Myklebust14516c32010-07-31 14:29:06 -04004611 if (!nfs4_sequence_done(task, &res->seq_res))
4612 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004613 if (nfs4_async_handle_error(task, res->server, NULL,
4614 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004615 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004616 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004617 update_changeattr(dir, &res->cinfo,
4618 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004619 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620}
4621
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004622static void nfs4_proc_rename_setup(struct rpc_message *msg,
4623 struct dentry *old_dentry,
4624 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004625{
Jeff Laytond3d41522010-09-17 17:31:57 -04004626 struct nfs_renameargs *arg = msg->rpc_argp;
4627 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004628 struct inode *old_inode = d_inode(old_dentry);
4629 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004630
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004631 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004632 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004633 if (new_inode)
4634 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004635 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004636 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004637 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004638}
4639
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004640static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4641{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004642 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004643 &data->args.seq_args,
4644 &data->res.seq_res,
4645 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004646}
4647
4648static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4649 struct inode *new_dir)
4650{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004651 struct nfs_renamedata *data = task->tk_calldata;
4652 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004653
4654 if (!nfs4_sequence_done(task, &res->seq_res))
4655 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004656 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004657 return 0;
4658
Trond Myklebustc733c492017-01-11 12:32:26 -05004659 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004660 if (new_dir != old_dir) {
4661 /* Note: If we moved a directory, nlink will change */
4662 update_changeattr(old_dir, &res->old_cinfo,
4663 res->old_fattr->time_start,
4664 NFS_INO_INVALID_OTHER);
4665 update_changeattr(new_dir, &res->new_cinfo,
4666 res->new_fattr->time_start,
4667 NFS_INO_INVALID_OTHER);
4668 } else
4669 update_changeattr(old_dir, &res->old_cinfo,
4670 res->old_fattr->time_start,
4671 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004672 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004673 return 1;
4674}
4675
Al Virobeffb8f2016-07-20 16:34:42 -04004676static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004678 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004679 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 struct nfs4_link_arg arg = {
4681 .fh = NFS_FH(inode),
4682 .dir_fh = NFS_FH(dir),
4683 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004684 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004686 struct nfs4_link_res res = {
4687 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004688 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004689 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 struct rpc_message msg = {
4691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4692 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004693 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004695 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Trond Myklebust136f2622010-04-16 16:22:49 -04004697 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004698 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004699 goto out;
4700
David Quigley14c43f72013-05-22 12:50:43 -04004701 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4702 if (IS_ERR(res.label)) {
4703 status = PTR_ERR(res.label);
4704 goto out;
4705 }
4706
Trond Myklebustc01d3642018-03-20 16:43:20 -04004707 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004708 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004709
Bryan Schumaker7c513052011-03-24 17:12:24 +00004710 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004711 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004712 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004713 status = nfs_post_op_update_inode(inode, res.fattr);
4714 if (!status)
4715 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004716 }
David Quigley14c43f72013-05-22 12:50:43 -04004717
4718
4719 nfs4_label_free(res.label);
4720
Trond Myklebust136f2622010-04-16 16:22:49 -04004721out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004722 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 return status;
4724}
4725
Al Virobeffb8f2016-07-20 16:34:42 -04004726static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727{
Trond Myklebust0688e642019-04-07 13:59:09 -04004728 struct nfs4_exception exception = {
4729 .interruptible = true,
4730 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 int err;
4732 do {
4733 err = nfs4_handle_exception(NFS_SERVER(inode),
4734 _nfs4_proc_link(inode, dir, name),
4735 &exception);
4736 } while (exception.retry);
4737 return err;
4738}
4739
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004740struct nfs4_createdata {
4741 struct rpc_message msg;
4742 struct nfs4_create_arg arg;
4743 struct nfs4_create_res res;
4744 struct nfs_fh fh;
4745 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004746 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004747};
4748
4749static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004750 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004751{
4752 struct nfs4_createdata *data;
4753
4754 data = kzalloc(sizeof(*data), GFP_KERNEL);
4755 if (data != NULL) {
4756 struct nfs_server *server = NFS_SERVER(dir);
4757
David Quigley14c43f72013-05-22 12:50:43 -04004758 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4759 if (IS_ERR(data->label))
4760 goto out_free;
4761
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004762 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4763 data->msg.rpc_argp = &data->arg;
4764 data->msg.rpc_resp = &data->res;
4765 data->arg.dir_fh = NFS_FH(dir);
4766 data->arg.server = server;
4767 data->arg.name = name;
4768 data->arg.attrs = sattr;
4769 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004770 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004771 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004772 data->res.server = server;
4773 data->res.fh = &data->fh;
4774 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004775 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004776 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004777 }
4778 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004779out_free:
4780 kfree(data);
4781 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004782}
4783
4784static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4785{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004786 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004787 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004788 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004789 spin_lock(&dir->i_lock);
4790 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004791 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004792 /* Creating a directory bumps nlink in the parent */
4793 if (data->arg.ftype == NF4DIR)
4794 nfs4_inc_nlink_locked(dir);
4795 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004796 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004797 }
4798 return status;
4799}
4800
4801static void nfs4_free_createdata(struct nfs4_createdata *data)
4802{
David Quigley14c43f72013-05-22 12:50:43 -04004803 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004804 kfree(data);
4805}
4806
Chuck Lever4f390c12006-08-22 20:06:22 -04004807static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004808 struct page *page, unsigned int len, struct iattr *sattr,
4809 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004811 struct nfs4_createdata *data;
4812 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Chuck Lever94a6d752006-08-22 20:06:23 -04004814 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004815 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004816
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004817 status = -ENOMEM;
4818 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4819 if (data == NULL)
4820 goto out;
4821
4822 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4823 data->arg.u.symlink.pages = &page;
4824 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004825 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004827 status = nfs4_do_create(dir, dentry, data);
4828
4829 nfs4_free_createdata(data);
4830out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 return status;
4832}
4833
Chuck Lever4f390c12006-08-22 20:06:22 -04004834static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004835 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836{
Trond Myklebust0688e642019-04-07 13:59:09 -04004837 struct nfs4_exception exception = {
4838 .interruptible = true,
4839 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004840 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004842
4843 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004846 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4847 trace_nfs4_symlink(dir, &dentry->d_name, err);
4848 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 &exception);
4850 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004851
4852 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 return err;
4854}
4855
4856static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004857 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004859 struct nfs4_createdata *data;
4860 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004862 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4863 if (data == NULL)
4864 goto out;
4865
David Quigley1775fd32013-05-22 12:50:42 -04004866 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004867 status = nfs4_do_create(dir, dentry, data);
4868
4869 nfs4_free_createdata(data);
4870out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 return status;
4872}
4873
4874static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4875 struct iattr *sattr)
4876{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004877 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004878 struct nfs4_exception exception = {
4879 .interruptible = true,
4880 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004881 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004883
David Quigleyaa9c2662013-05-22 12:50:44 -04004884 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4885
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004886 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4887 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004889 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4890 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4891 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 &exception);
4893 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004894 nfs4_label_release_security(label);
4895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 return err;
4897}
4898
NeilBrown684f39b2018-12-03 11:30:30 +11004899static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004900 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901{
David Howells2b0143b2015-03-17 22:25:59 +00004902 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 struct nfs4_readdir_arg args = {
4904 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004905 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 .pgbase = 0,
4907 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004908 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004909 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 };
4911 struct nfs4_readdir_res res;
4912 struct rpc_message msg = {
4913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4914 .rpc_argp = &args,
4915 .rpc_resp = &res,
4916 .rpc_cred = cred,
4917 };
4918 int status;
4919
Al Viro6de14722013-09-16 10:53:17 -04004920 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4921 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004922 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004923 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004925 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 -05004926 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004927 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004928 status += args.pgbase;
4929 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004930
4931 nfs_invalidate_atime(dir);
4932
Harvey Harrison3110ff82008-05-02 13:42:44 -07004933 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 return status;
4935}
4936
NeilBrown684f39b2018-12-03 11:30:30 +11004937static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004938 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939{
Trond Myklebust0688e642019-04-07 13:59:09 -04004940 struct nfs4_exception exception = {
4941 .interruptible = true,
4942 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 int err;
4944 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004945 err = _nfs4_proc_readdir(dentry, cred, cookie,
4946 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004947 trace_nfs4_readdir(d_inode(dentry), err);
4948 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 &exception);
4950 } while (exception.retry);
4951 return err;
4952}
4953
4954static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004955 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004957 struct nfs4_createdata *data;
4958 int mode = sattr->ia_mode;
4959 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004961 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4962 if (data == NULL)
4963 goto out;
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004966 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004968 data->arg.ftype = NF4BLK;
4969 data->arg.u.device.specdata1 = MAJOR(rdev);
4970 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 }
4972 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004973 data->arg.ftype = NF4CHR;
4974 data->arg.u.device.specdata1 = MAJOR(rdev);
4975 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004976 } else if (!S_ISSOCK(mode)) {
4977 status = -EINVAL;
4978 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 }
David Quigley1775fd32013-05-22 12:50:42 -04004980
David Quigleyaa9c2662013-05-22 12:50:44 -04004981 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004982 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004983out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004984 nfs4_free_createdata(data);
4985out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 return status;
4987}
4988
4989static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4990 struct iattr *sattr, dev_t rdev)
4991{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004992 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004993 struct nfs4_exception exception = {
4994 .interruptible = true,
4995 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004996 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004998
David Quigleyaa9c2662013-05-22 12:50:44 -04004999 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5000
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005001 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5002 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005004 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5005 trace_nfs4_mknod(dir, &dentry->d_name, err);
5006 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 &exception);
5008 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005009
5010 nfs4_label_release_security(label);
5011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 return err;
5013}
5014
5015static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5016 struct nfs_fsstat *fsstat)
5017{
5018 struct nfs4_statfs_arg args = {
5019 .fh = fhandle,
5020 .bitmask = server->attr_bitmask,
5021 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005022 struct nfs4_statfs_res res = {
5023 .fsstat = fsstat,
5024 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 struct rpc_message msg = {
5026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5027 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005028 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 };
5030
Trond Myklebust0e574af2005-10-27 22:12:38 -04005031 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005032 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033}
5034
5035static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5036{
Trond Myklebust0688e642019-04-07 13:59:09 -04005037 struct nfs4_exception exception = {
5038 .interruptible = true,
5039 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 int err;
5041 do {
5042 err = nfs4_handle_exception(server,
5043 _nfs4_proc_statfs(server, fhandle, fsstat),
5044 &exception);
5045 } while (exception.retry);
5046 return err;
5047}
5048
5049static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5050 struct nfs_fsinfo *fsinfo)
5051{
5052 struct nfs4_fsinfo_arg args = {
5053 .fh = fhandle,
5054 .bitmask = server->attr_bitmask,
5055 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005056 struct nfs4_fsinfo_res res = {
5057 .fsinfo = fsinfo,
5058 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 struct rpc_message msg = {
5060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5061 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005062 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 };
5064
Bryan Schumaker7c513052011-03-24 17:12:24 +00005065 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066}
5067
5068static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5069{
Trond Myklebust0688e642019-04-07 13:59:09 -04005070 struct nfs4_exception exception = {
5071 .interruptible = true,
5072 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 int err;
5074
5075 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005076 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005077 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005078 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005079 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005080 break;
5081 }
5082 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 } while (exception.retry);
5084 return err;
5085}
5086
5087static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5088{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005089 int error;
5090
Trond Myklebust0e574af2005-10-27 22:12:38 -04005091 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005092 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005093 if (error == 0) {
5094 /* block layout checks this! */
5095 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005096 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005097 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005098
5099 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100}
5101
5102static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5103 struct nfs_pathconf *pathconf)
5104{
5105 struct nfs4_pathconf_arg args = {
5106 .fh = fhandle,
5107 .bitmask = server->attr_bitmask,
5108 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005109 struct nfs4_pathconf_res res = {
5110 .pathconf = pathconf,
5111 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 struct rpc_message msg = {
5113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5114 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005115 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 };
5117
5118 /* None of the pathconf attributes are mandatory to implement */
5119 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5120 memset(pathconf, 0, sizeof(*pathconf));
5121 return 0;
5122 }
5123
Trond Myklebust0e574af2005-10-27 22:12:38 -04005124 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005125 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126}
5127
5128static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5129 struct nfs_pathconf *pathconf)
5130{
Trond Myklebust0688e642019-04-07 13:59:09 -04005131 struct nfs4_exception exception = {
5132 .interruptible = true,
5133 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 int err;
5135
5136 do {
5137 err = nfs4_handle_exception(server,
5138 _nfs4_proc_pathconf(server, fhandle, pathconf),
5139 &exception);
5140 } while (exception.retry);
5141 return err;
5142}
5143
Trond Myklebust5521abf2013-03-16 20:54:34 -04005144int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005145 const struct nfs_open_context *ctx,
5146 const struct nfs_lock_context *l_ctx,
5147 fmode_t fmode)
5148{
NeilBrown17393472016-10-13 15:26:47 +11005149 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005150}
5151EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5152
Trond Myklebust5521abf2013-03-16 20:54:34 -04005153static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5154 const struct nfs_open_context *ctx,
5155 const struct nfs_lock_context *l_ctx,
5156 fmode_t fmode)
5157{
Ben Dooksd49dd112019-10-16 17:28:21 +01005158 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005159
Trond Myklebuste1253be2014-03-05 08:44:23 -05005160 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005161 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005162 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005163 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005164}
5165
5166static bool nfs4_error_stateid_expired(int err)
5167{
5168 switch (err) {
5169 case -NFS4ERR_DELEG_REVOKED:
5170 case -NFS4ERR_ADMIN_REVOKED:
5171 case -NFS4ERR_BAD_STATEID:
5172 case -NFS4ERR_STALE_STATEID:
5173 case -NFS4ERR_OLD_STATEID:
5174 case -NFS4ERR_OPENMODE:
5175 case -NFS4ERR_EXPIRED:
5176 return true;
5177 }
5178 return false;
5179}
5180
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005181static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005183 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005185 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005186 if (task->tk_status < 0) {
5187 struct nfs4_exception exception = {
5188 .inode = hdr->inode,
5189 .state = hdr->args.context->state,
5190 .stateid = &hdr->args.stateid,
5191 };
5192 task->tk_status = nfs4_async_handle_exception(task,
5193 server, task->tk_status, &exception);
5194 if (exception.retry) {
5195 rpc_restart_call_prepare(task);
5196 return -EAGAIN;
5197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005199
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005201 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203}
5204
Trond Myklebust5521abf2013-03-16 20:54:34 -04005205static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005206 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005207{
5208
5209 if (!nfs4_error_stateid_expired(task->tk_status) ||
5210 nfs4_stateid_is_current(&args->stateid,
5211 args->context,
5212 args->lock_context,
5213 FMODE_READ))
5214 return false;
5215 rpc_restart_call_prepare(task);
5216 return true;
5217}
5218
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005219static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005220{
5221
5222 dprintk("--> %s\n", __func__);
5223
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005224 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005225 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005226 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005227 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005228 if (task->tk_status > 0)
5229 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005230 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5231 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005232}
5233
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005234static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5235 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005237 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005238 if (!hdr->pgio_done_cb)
5239 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005240 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005241 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242}
5243
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005244static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5245 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005246{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005247 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005248 &hdr->args.seq_args,
5249 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005250 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005251 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005252 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5253 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005254 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005255 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005256 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005257 return -EIO;
5258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259}
5260
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005261static int nfs4_write_done_cb(struct rpc_task *task,
5262 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005264 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005265
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005266 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005267 if (task->tk_status < 0) {
5268 struct nfs4_exception exception = {
5269 .inode = hdr->inode,
5270 .state = hdr->args.context->state,
5271 .stateid = &hdr->args.stateid,
5272 };
5273 task->tk_status = nfs4_async_handle_exception(task,
5274 NFS_SERVER(inode), task->tk_status,
5275 &exception);
5276 if (exception.retry) {
5277 rpc_restart_call_prepare(task);
5278 return -EAGAIN;
5279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005281 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005282 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005283 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005284 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286}
5287
Trond Myklebust5521abf2013-03-16 20:54:34 -04005288static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005289 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005290{
5291
5292 if (!nfs4_error_stateid_expired(task->tk_status) ||
5293 nfs4_stateid_is_current(&args->stateid,
5294 args->context,
5295 args->lock_context,
5296 FMODE_WRITE))
5297 return false;
5298 rpc_restart_call_prepare(task);
5299 return true;
5300}
5301
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005302static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005303{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005304 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005305 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005306 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005307 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005308 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5309 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005310}
5311
Trond Myklebust5a37f852012-04-28 14:55:16 -04005312static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005313bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005314{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005315 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005316 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005317 return false;
5318 /* Otherwise, request attributes if and only if we don't hold
5319 * a delegation
5320 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005321 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005322}
Fred Isamana69aef12011-03-03 15:13:47 +00005323
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005324static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005325 struct rpc_message *msg,
5326 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005328 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005329
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005330 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5331 hdr->args.bitmask = NULL;
5332 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005333 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005334 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005335
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005336 if (!hdr->pgio_done_cb)
5337 hdr->pgio_done_cb = nfs4_write_done_cb;
5338 hdr->res.server = server;
5339 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005341 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005342 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005343 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344}
5345
Fred Isaman0b7c0152012-04-20 14:47:39 -04005346static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5347{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005348 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005349 &data->args.seq_args,
5350 &data->res.seq_res,
5351 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352}
5353
Fred Isaman0b7c0152012-04-20 14:47:39 -04005354static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005357
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005358 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005359 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5360 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005361 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005362 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365}
5366
Fred Isaman0b7c0152012-04-20 14:47:39 -04005367static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005368{
5369 if (!nfs4_sequence_done(task, &data->res.seq_res))
5370 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005371 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005372}
5373
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005374static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5375 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005377 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005378
Fred Isaman0b7c0152012-04-20 14:47:39 -04005379 if (data->commit_done_cb == NULL)
5380 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005381 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005382 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005383 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005384 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385}
5386
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005387static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5388 struct nfs_commitres *res)
5389{
5390 struct inode *dst_inode = file_inode(dst);
5391 struct nfs_server *server = NFS_SERVER(dst_inode);
5392 struct rpc_message msg = {
5393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5394 .rpc_argp = args,
5395 .rpc_resp = res,
5396 };
5397
5398 args->fh = NFS_FH(dst_inode);
5399 return nfs4_call_sync(server->client, server, &msg,
5400 &args->seq_args, &res->seq_res, 1);
5401}
5402
5403int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5404{
5405 struct nfs_commitargs args = {
5406 .offset = offset,
5407 .count = count,
5408 };
5409 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5410 struct nfs4_exception exception = { };
5411 int status;
5412
5413 do {
5414 status = _nfs4_proc_commit(dst, &args, res);
5415 status = nfs4_handle_exception(dst_server, status, &exception);
5416 } while (exception.retry);
5417
5418 return status;
5419}
5420
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005421struct nfs4_renewdata {
5422 struct nfs_client *client;
5423 unsigned long timestamp;
5424};
5425
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426/*
5427 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5428 * standalone procedure for queueing an asynchronous RENEW.
5429 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005430static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005431{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005432 struct nfs4_renewdata *data = calldata;
5433 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005434
Elena Reshetova212bf412017-10-20 12:53:38 +03005435 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005436 nfs4_schedule_state_renewal(clp);
5437 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005438 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005439}
5440
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005441static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005443 struct nfs4_renewdata *data = calldata;
5444 struct nfs_client *clp = data->client;
5445 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005447 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005448 switch (task->tk_status) {
5449 case 0:
5450 break;
5451 case -NFS4ERR_LEASE_MOVED:
5452 nfs4_schedule_lease_moved_recovery(clp);
5453 break;
5454 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005455 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005456 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5457 return;
5458 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005459 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005460 return;
5461 }
5462 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005464 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465}
5466
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005467static const struct rpc_call_ops nfs4_renew_ops = {
5468 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005469 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005470};
5471
NeilBrowna52458b2018-12-03 11:30:31 +11005472static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473{
5474 struct rpc_message msg = {
5475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5476 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005477 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005479 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005481 if (renew_flags == 0)
5482 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005483 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005484 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005485 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005486 if (data == NULL) {
5487 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005488 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005489 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005490 data->client = clp;
5491 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005492 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005493 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494}
5495
NeilBrowna52458b2018-12-03 11:30:31 +11005496static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497{
5498 struct rpc_message msg = {
5499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5500 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005501 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 };
5503 unsigned long now = jiffies;
5504 int status;
5505
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005506 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 if (status < 0)
5508 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005509 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 return 0;
5511}
5512
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005513static inline int nfs4_server_supports_acls(struct nfs_server *server)
5514{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005515 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005516}
5517
Trond Myklebust21f498c2012-08-24 10:59:25 -04005518/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5519 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005520 * the stack.
5521 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005522#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005523
Neil Hormane9e3d722011-03-04 19:26:03 -05005524static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005525 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005526{
5527 struct page *newpage, **spages;
5528 int rc = 0;
5529 size_t len;
5530 spages = pages;
5531
5532 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005533 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005534 newpage = alloc_page(GFP_KERNEL);
5535
5536 if (newpage == NULL)
5537 goto unwind;
5538 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005539 buf += len;
5540 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005541 *pages++ = newpage;
5542 rc++;
5543 } while (buflen != 0);
5544
5545 return rc;
5546
5547unwind:
5548 for(; rc > 0; rc--)
5549 __free_page(spages[rc-1]);
5550 return -ENOMEM;
5551}
5552
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005553struct nfs4_cached_acl {
5554 int cached;
5555 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005556 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005557};
5558
5559static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005560{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005561 struct nfs_inode *nfsi = NFS_I(inode);
5562
5563 spin_lock(&inode->i_lock);
5564 kfree(nfsi->nfs4_acl);
5565 nfsi->nfs4_acl = acl;
5566 spin_unlock(&inode->i_lock);
5567}
5568
5569static void nfs4_zap_acl_attr(struct inode *inode)
5570{
5571 nfs4_set_cached_acl(inode, NULL);
5572}
5573
5574static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5575{
5576 struct nfs_inode *nfsi = NFS_I(inode);
5577 struct nfs4_cached_acl *acl;
5578 int ret = -ENOENT;
5579
5580 spin_lock(&inode->i_lock);
5581 acl = nfsi->nfs4_acl;
5582 if (acl == NULL)
5583 goto out;
5584 if (buf == NULL) /* user is just asking for length */
5585 goto out_len;
5586 if (acl->cached == 0)
5587 goto out;
5588 ret = -ERANGE; /* see getxattr(2) man page */
5589 if (acl->len > buflen)
5590 goto out;
5591 memcpy(buf, acl->data, acl->len);
5592out_len:
5593 ret = acl->len;
5594out:
5595 spin_unlock(&inode->i_lock);
5596 return ret;
5597}
5598
Sachin Prabhu5794d212012-04-17 14:36:40 +01005599static 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 +00005600{
5601 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005602 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005603
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005604 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005605 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005606 if (acl == NULL)
5607 goto out;
5608 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005609 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005610 } else {
5611 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5612 if (acl == NULL)
5613 goto out;
5614 acl->cached = 0;
5615 }
5616 acl->len = acl_len;
5617out:
5618 nfs4_set_cached_acl(inode, acl);
5619}
5620
Andy Adamsonbf118a32011-12-07 11:55:27 -05005621/*
5622 * The getxattr API returns the required buffer length when called with a
5623 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5624 * the required buf. On a NULL buf, we send a page of data to the server
5625 * guessing that the ACL request can be serviced by a page. If so, we cache
5626 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5627 * the cache. If not so, we throw away the page, and cache the required
5628 * length. The next getxattr call will then produce another round trip to
5629 * the server, this time with the input buf of the required size.
5630 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005631static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005632{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005633 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005634 struct nfs_getaclargs args = {
5635 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005636 .acl_len = buflen,
5637 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005638 struct nfs_getaclres res = {
5639 .acl_len = buflen,
5640 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005641 struct rpc_message msg = {
5642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5643 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005644 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005645 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005646 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005647 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005648 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005649
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005650 if (buflen == 0)
5651 buflen = server->rsize;
5652
5653 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5654 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5655 if (!pages)
5656 return -ENOMEM;
5657
5658 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005659
Andy Adamsonbf118a32011-12-07 11:55:27 -05005660 for (i = 0; i < npages; i++) {
5661 pages[i] = alloc_page(GFP_KERNEL);
5662 if (!pages[i])
5663 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005664 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005665
5666 /* for decoding across pages */
5667 res.acl_scratch = alloc_page(GFP_KERNEL);
5668 if (!res.acl_scratch)
5669 goto out_free;
5670
Andy Adamsonbf118a32011-12-07 11:55:27 -05005671 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005672
Peng Taode040be2012-01-10 22:42:47 +08005673 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005674 __func__, buf, buflen, npages, args.acl_len);
5675 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5676 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005677 if (ret)
5678 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005679
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005680 /* Handle the case where the passed-in buffer is too short */
5681 if (res.acl_flags & NFS4_ACL_TRUNC) {
5682 /* Did the user only issue a request for the acl length? */
5683 if (buf == NULL)
5684 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005685 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005686 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005687 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005688 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005689 if (buf) {
5690 if (res.acl_len > buflen) {
5691 ret = -ERANGE;
5692 goto out_free;
5693 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005694 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005695 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005696out_ok:
5697 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005698out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005699 for (i = 0; i < npages; i++)
5700 if (pages[i])
5701 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005702 if (res.acl_scratch)
5703 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005704 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005705 return ret;
5706}
5707
Trond Myklebust16b42892006-08-24 12:27:15 -04005708static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5709{
Trond Myklebust0688e642019-04-07 13:59:09 -04005710 struct nfs4_exception exception = {
5711 .interruptible = true,
5712 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005713 ssize_t ret;
5714 do {
5715 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005716 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005717 if (ret >= 0)
5718 break;
5719 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5720 } while (exception.retry);
5721 return ret;
5722}
5723
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005724static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5725{
5726 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005727 int ret;
5728
5729 if (!nfs4_server_supports_acls(server))
5730 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005731 ret = nfs_revalidate_inode(server, inode);
5732 if (ret < 0)
5733 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005734 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5735 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005736 ret = nfs4_read_cached_acl(inode, buf, buflen);
5737 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005738 /* -ENOENT is returned if there is no ACL or if there is an ACL
5739 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005740 return ret;
5741 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005742}
5743
Trond Myklebust16b42892006-08-24 12:27:15 -04005744static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005745{
5746 struct nfs_server *server = NFS_SERVER(inode);
5747 struct page *pages[NFS4ACL_MAXPAGES];
5748 struct nfs_setaclargs arg = {
5749 .fh = NFS_FH(inode),
5750 .acl_pages = pages,
5751 .acl_len = buflen,
5752 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005753 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005754 struct rpc_message msg = {
5755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5756 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005757 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005758 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005759 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005760 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005761
5762 if (!nfs4_server_supports_acls(server))
5763 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005764 if (npages > ARRAY_SIZE(pages))
5765 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005766 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005767 if (i < 0)
5768 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005769 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005770 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005771
5772 /*
5773 * Free each page after tx, so the only ref left is
5774 * held by the network stack
5775 */
5776 for (; i > 0; i--)
5777 put_page(pages[i-1]);
5778
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005779 /*
5780 * Acl update can result in inode attribute update.
5781 * so mark the attribute cache invalid.
5782 */
5783 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005784 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005785 | NFS_INO_INVALID_CTIME
5786 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005787 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005788 nfs_access_zap_cache(inode);
5789 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005790 return ret;
5791}
5792
Trond Myklebust16b42892006-08-24 12:27:15 -04005793static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5794{
5795 struct nfs4_exception exception = { };
5796 int err;
5797 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005798 err = __nfs4_proc_set_acl(inode, buf, buflen);
5799 trace_nfs4_set_acl(inode, err);
5800 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005801 &exception);
5802 } while (exception.retry);
5803 return err;
5804}
5805
David Quigleyaa9c2662013-05-22 12:50:44 -04005806#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5807static int _nfs4_get_security_label(struct inode *inode, void *buf,
5808 size_t buflen)
5809{
5810 struct nfs_server *server = NFS_SERVER(inode);
5811 struct nfs_fattr fattr;
5812 struct nfs4_label label = {0, 0, buflen, buf};
5813
5814 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005815 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005816 .fh = NFS_FH(inode),
5817 .bitmask = bitmask,
5818 };
5819 struct nfs4_getattr_res res = {
5820 .fattr = &fattr,
5821 .label = &label,
5822 .server = server,
5823 };
5824 struct rpc_message msg = {
5825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005826 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005827 .rpc_resp = &res,
5828 };
5829 int ret;
5830
5831 nfs_fattr_init(&fattr);
5832
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005833 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005834 if (ret)
5835 return ret;
5836 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5837 return -ENOENT;
5838 if (buflen < label.len)
5839 return -ERANGE;
5840 return 0;
5841}
5842
5843static int nfs4_get_security_label(struct inode *inode, void *buf,
5844 size_t buflen)
5845{
Trond Myklebust0688e642019-04-07 13:59:09 -04005846 struct nfs4_exception exception = {
5847 .interruptible = true,
5848 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005849 int err;
5850
5851 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5852 return -EOPNOTSUPP;
5853
5854 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005855 err = _nfs4_get_security_label(inode, buf, buflen);
5856 trace_nfs4_get_security_label(inode, err);
5857 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005858 &exception);
5859 } while (exception.retry);
5860 return err;
5861}
5862
5863static int _nfs4_do_set_security_label(struct inode *inode,
5864 struct nfs4_label *ilabel,
5865 struct nfs_fattr *fattr,
5866 struct nfs4_label *olabel)
5867{
5868
5869 struct iattr sattr = {0};
5870 struct nfs_server *server = NFS_SERVER(inode);
5871 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005872 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005873 .fh = NFS_FH(inode),
5874 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005875 .server = server,
5876 .bitmask = bitmask,
5877 .label = ilabel,
5878 };
5879 struct nfs_setattrres res = {
5880 .fattr = fattr,
5881 .label = olabel,
5882 .server = server,
5883 };
5884 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5886 .rpc_argp = &arg,
5887 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005888 };
5889 int status;
5890
Jeff Layton12207f62013-11-01 10:49:32 -04005891 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005892
Jeff Layton12207f62013-11-01 10:49:32 -04005893 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005894 if (status)
5895 dprintk("%s failed: %d\n", __func__, status);
5896
5897 return status;
5898}
5899
5900static int nfs4_do_set_security_label(struct inode *inode,
5901 struct nfs4_label *ilabel,
5902 struct nfs_fattr *fattr,
5903 struct nfs4_label *olabel)
5904{
5905 struct nfs4_exception exception = { };
5906 int err;
5907
5908 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005909 err = _nfs4_do_set_security_label(inode, ilabel,
5910 fattr, olabel);
5911 trace_nfs4_set_security_label(inode, err);
5912 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005913 &exception);
5914 } while (exception.retry);
5915 return err;
5916}
5917
5918static int
Al Viro59301222016-05-27 10:19:30 -04005919nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005920{
5921 struct nfs4_label ilabel, *olabel = NULL;
5922 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005923 int status;
5924
5925 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5926 return -EOPNOTSUPP;
5927
5928 nfs_fattr_init(&fattr);
5929
5930 ilabel.pi = 0;
5931 ilabel.lfs = 0;
5932 ilabel.label = (char *)buf;
5933 ilabel.len = buflen;
5934
David Quigleyaa9c2662013-05-22 12:50:44 -04005935 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5936 if (IS_ERR(olabel)) {
5937 status = -PTR_ERR(olabel);
5938 goto out;
5939 }
5940
5941 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5942 if (status == 0)
5943 nfs_setsecurity(inode, &fattr, olabel);
5944
5945 nfs4_label_free(olabel);
5946out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005947 return status;
5948}
5949#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5950
5951
Chuck Leverf0920752012-05-21 22:45:41 -04005952static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5953 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005954{
5955 __be32 verf[2];
5956
Chuck Lever2c820d92012-05-21 22:45:33 -04005957 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5958 /* An impossible timestamp guarantees this value
5959 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005960 verf[0] = cpu_to_be32(U32_MAX);
5961 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005962 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005963 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005964 u64 ns = ktime_to_ns(nn->boot_time);
5965
5966 verf[0] = cpu_to_be32(ns >> 32);
5967 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005968 }
Chuck Levercd937102012-03-02 17:14:31 -05005969 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5970}
5971
Jeff Laytona3192682015-06-09 19:43:59 -04005972static int
5973nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005974{
Jeff Laytona3192682015-06-09 19:43:59 -04005975 size_t len;
5976 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005977
Trond Myklebustceb3a162015-01-03 15:16:04 -05005978 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005979 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005980
Jeff Laytona3192682015-06-09 19:43:59 -04005981 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005982 len = 14 +
5983 strlen(clp->cl_rpcclient->cl_nodename) +
5984 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005985 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005986 1;
5987 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005988 if (nfs4_client_id_uniquifier[0] != '\0')
5989 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005990 if (len > NFS4_OPAQUE_LIMIT + 1)
5991 return -EINVAL;
5992
5993 /*
5994 * Since this string is allocated at mount time, and held until the
5995 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5996 * about a memory-reclaim deadlock.
5997 */
5998 str = kmalloc(len, GFP_KERNEL);
5999 if (!str)
6000 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006001
Chuck Levere984a552012-09-14 17:24:21 -04006002 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006003 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04006004 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006005 clp->cl_rpcclient->cl_nodename,
6006 nfs4_client_id_uniquifier,
6007 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006008 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006009 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006010 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006011 clp->cl_rpcclient->cl_nodename,
6012 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006013 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006014 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006015
Jeff Laytona3192682015-06-09 19:43:59 -04006016 clp->cl_owner_id = str;
6017 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006018}
6019
Jeff Layton873e3852015-06-09 19:44:00 -04006020static int
6021nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006022{
Jeff Layton873e3852015-06-09 19:44:00 -04006023 size_t len;
6024 char *str;
6025
6026 len = 10 + 10 + 1 + 10 + 1 +
6027 strlen(nfs4_client_id_uniquifier) + 1 +
6028 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6029
6030 if (len > NFS4_OPAQUE_LIMIT + 1)
6031 return -EINVAL;
6032
6033 /*
6034 * Since this string is allocated at mount time, and held until the
6035 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6036 * about a memory-reclaim deadlock.
6037 */
6038 str = kmalloc(len, GFP_KERNEL);
6039 if (!str)
6040 return -ENOMEM;
6041
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006042 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04006043 clp->rpc_ops->version, clp->cl_minorversion,
6044 nfs4_client_id_uniquifier,
6045 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006046 clp->cl_owner_id = str;
6047 return 0;
6048}
6049
6050static int
6051nfs4_init_uniform_client_string(struct nfs_client *clp)
6052{
Jeff Layton873e3852015-06-09 19:44:00 -04006053 size_t len;
6054 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006055
6056 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006057 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006058
6059 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04006060 return nfs4_init_uniquifier_client_string(clp);
6061
6062 len = 10 + 10 + 1 + 10 + 1 +
6063 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6064
6065 if (len > NFS4_OPAQUE_LIMIT + 1)
6066 return -EINVAL;
6067
6068 /*
6069 * Since this string is allocated at mount time, and held until the
6070 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6071 * about a memory-reclaim deadlock.
6072 */
6073 str = kmalloc(len, GFP_KERNEL);
6074 if (!str)
6075 return -ENOMEM;
6076
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006077 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04006078 clp->rpc_ops->version, clp->cl_minorversion,
6079 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006080 clp->cl_owner_id = str;
6081 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006082}
6083
Chuck Lever706cb8d2014-03-12 12:51:47 -04006084/*
6085 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6086 * services. Advertise one based on the address family of the
6087 * clientaddr.
6088 */
6089static unsigned int
6090nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6091{
6092 if (strchr(clp->cl_ipaddr, ':') != NULL)
6093 return scnprintf(buf, len, "tcp6");
6094 else
6095 return scnprintf(buf, len, "tcp");
6096}
6097
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006098static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6099{
6100 struct nfs4_setclientid *sc = calldata;
6101
6102 if (task->tk_status == 0)
6103 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6104}
6105
6106static const struct rpc_call_ops nfs4_setclientid_ops = {
6107 .rpc_call_done = nfs4_setclientid_done,
6108};
6109
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006110/**
6111 * nfs4_proc_setclientid - Negotiate client ID
6112 * @clp: state data structure
6113 * @program: RPC program for NFSv4 callback service
6114 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006115 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006116 * @res: where to place the result
6117 *
6118 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6119 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006120int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006121 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006122 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123{
6124 nfs4_verifier sc_verifier;
6125 struct nfs4_setclientid setclientid = {
6126 .sc_verifier = &sc_verifier,
6127 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006128 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 };
6130 struct rpc_message msg = {
6131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6132 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006133 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006134 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006136 struct rpc_task_setup task_setup_data = {
6137 .rpc_client = clp->cl_rpcclient,
6138 .rpc_message = &msg,
6139 .callback_ops = &nfs4_setclientid_ops,
6140 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006141 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006142 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006143 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006144 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
Chuck Leverde734832012-07-11 16:30:50 -04006146 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006147 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006148
6149 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6150 status = nfs4_init_uniform_client_string(clp);
6151 else
Jeff Laytona3192682015-06-09 19:43:59 -04006152 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006153
6154 if (status)
6155 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006156
Chuck Leverde734832012-07-11 16:30:50 -04006157 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006158 setclientid.sc_netid_len =
6159 nfs4_init_callback_netid(clp,
6160 setclientid.sc_netid,
6161 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006162 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006163 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 clp->cl_ipaddr, port >> 8, port & 255);
6165
Jeff Layton3a6bb732015-06-09 19:43:57 -04006166 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006167 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006168 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006169
6170 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006171 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006172 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006173 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6174 put_rpccred(setclientid.sc_cred);
6175 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006176
6177 if (status == 0)
6178 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006179out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006180 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006181 dprintk("NFS reply setclientid: %d\n", status);
6182 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183}
6184
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006185/**
6186 * nfs4_proc_setclientid_confirm - Confirm client ID
6187 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006188 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006189 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006190 *
6191 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6192 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006193int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006194 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006195 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 struct rpc_message msg = {
6198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006199 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006200 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 int status;
6203
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006204 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6205 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6206 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006207 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6208 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006209 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006210 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 return status;
6212}
6213
Trond Myklebustfe650402006-01-03 09:55:18 +01006214struct nfs4_delegreturndata {
6215 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006216 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006217 struct nfs_fh fh;
6218 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006219 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006220 struct {
6221 struct nfs4_layoutreturn_args arg;
6222 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006223 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006224 u32 roc_barrier;
6225 bool roc;
6226 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006227 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006228 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006229 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006230};
6231
Trond Myklebustfe650402006-01-03 09:55:18 +01006232static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6233{
6234 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006235 struct nfs4_exception exception = {
6236 .inode = data->inode,
6237 .stateid = &data->stateid,
6238 };
Andy Adamson938e1012009-04-01 09:22:28 -04006239
Trond Myklebust14516c32010-07-31 14:29:06 -04006240 if (!nfs4_sequence_done(task, &data->res.seq_res))
6241 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006242
Trond Myklebustca8acf82013-08-13 10:36:56 -04006243 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006244
6245 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04006246 if (pnfs_roc_done(task, data->inode,
6247 &data->args.lr_args,
6248 &data->res.lr_res,
6249 &data->res.lr_ret) == -EAGAIN)
6250 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006251
Ricardo Labiaga79708862009-12-07 09:23:21 -05006252 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006253 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006254 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006255 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006256 case -NFS4ERR_ADMIN_REVOKED:
6257 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006258 case -NFS4ERR_EXPIRED:
6259 nfs4_free_revoked_stateid(data->res.server,
6260 data->args.stateid,
6261 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006262 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006263 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006264 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006265 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006266 task->tk_status = 0;
6267 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006268 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006269 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6270 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006271 if (data->args.bitmask) {
6272 data->args.bitmask = NULL;
6273 data->res.fattr = NULL;
6274 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006275 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006276 case -NFS4ERR_ACCESS:
6277 if (data->args.bitmask) {
6278 data->args.bitmask = NULL;
6279 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006280 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006281 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006282 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006283 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006284 task->tk_status = nfs4_async_handle_exception(task,
6285 data->res.server, task->tk_status,
6286 &exception);
6287 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006288 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006289 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006290 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006291 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006292 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006293out_restart:
6294 task->tk_status = 0;
6295 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006296}
6297
6298static void nfs4_delegreturn_release(void *calldata)
6299{
Peng Tao039b7562014-07-03 13:05:02 +08006300 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006301 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006302
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006303 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006304 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006305 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6306 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006307 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006308 nfs_iput_and_deactive(inode);
6309 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006310 kfree(calldata);
6311}
6312
Andy Adamson938e1012009-04-01 09:22:28 -04006313static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6314{
6315 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006316 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006317
6318 d_data = (struct nfs4_delegreturndata *)data;
6319
Trond Myklebust5326de92019-11-13 09:39:36 +01006320 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6321 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006322 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006323 }
Peng Tao500d7012015-09-22 11:35:22 +08006324
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006325 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6326 if (lo && !pnfs_layout_is_valid(lo)) {
6327 d_data->args.lr_args = NULL;
6328 d_data->res.lr_res = NULL;
6329 }
6330
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006331 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006332 &d_data->args.seq_args,
6333 &d_data->res.seq_res,
6334 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006335}
Andy Adamson938e1012009-04-01 09:22:28 -04006336
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006337static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006338 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006339 .rpc_call_done = nfs4_delegreturn_done,
6340 .rpc_release = nfs4_delegreturn_release,
6341};
6342
NeilBrowna52458b2018-12-03 11:30:31 +11006343static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006344{
6345 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006346 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006347 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006348 struct rpc_message msg = {
6349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6350 .rpc_cred = cred,
6351 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006352 struct rpc_task_setup task_setup_data = {
6353 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006354 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006355 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebust61296502020-02-07 19:38:12 -05006356 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006357 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006358 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006359
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006360 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006361 if (data == NULL)
6362 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006363 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006364
6365 nfs4_state_protect(server->nfs_client,
6366 NFS_SP4_MACH_CRED_CLEANUP,
6367 &task_setup_data.rpc_client, &msg);
6368
Trond Myklebustfe650402006-01-03 09:55:18 +01006369 data->args.fhandle = &data->fh;
6370 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006371 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006372 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006373 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006374 data->res.fattr = &data->fattr;
6375 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006376 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006377 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006378 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006379 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006380 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006381 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006382 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006383 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006384 if (data->lr.roc) {
6385 data->args.lr_args = &data->lr.arg;
6386 data->res.lr_res = &data->lr.res;
6387 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006388 } else if (data->lr.roc) {
6389 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6390 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006391 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006392
Trond Myklebustc970aa82007-07-14 15:39:59 -04006393 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006394 msg.rpc_argp = &data->args;
6395 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006396 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006397 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006398 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006399 if (!issync)
6400 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006401 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006402 if (status != 0)
6403 goto out;
6404 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006405out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006406 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006407 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408}
6409
NeilBrowna52458b2018-12-03 11:30:31 +11006410int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411{
6412 struct nfs_server *server = NFS_SERVER(inode);
6413 struct nfs4_exception exception = { };
6414 int err;
6415 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006416 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006417 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 switch (err) {
6419 case -NFS4ERR_STALE_STATEID:
6420 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 case 0:
6422 return 0;
6423 }
6424 err = nfs4_handle_exception(server, err, &exception);
6425 } while (exception.retry);
6426 return err;
6427}
6428
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6430{
6431 struct inode *inode = state->inode;
6432 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006433 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006434 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006436 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006438 struct nfs_lockt_res res = {
6439 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 };
6441 struct rpc_message msg = {
6442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006443 .rpc_argp = &arg,
6444 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 .rpc_cred = state->owner->so_cred,
6446 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 struct nfs4_lock_state *lsp;
6448 int status;
6449
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006450 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006451 status = nfs4_set_lock_state(state, request);
6452 if (status != 0)
6453 goto out;
6454 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006455 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006456 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006457 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006458 switch (status) {
6459 case 0:
6460 request->fl_type = F_UNLCK;
6461 break;
6462 case -NFS4ERR_DENIED:
6463 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006465 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006466 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006467out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 return status;
6469}
6470
6471static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6472{
Trond Myklebust0688e642019-04-07 13:59:09 -04006473 struct nfs4_exception exception = {
6474 .interruptible = true,
6475 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 int err;
6477
6478 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006479 err = _nfs4_proc_getlk(state, cmd, request);
6480 trace_nfs4_get_lock(request, state, cmd, err);
6481 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 &exception);
6483 } while (exception.retry);
6484 return err;
6485}
6486
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006487/*
6488 * Update the seqid of a lock stateid after receiving
6489 * NFS4ERR_OLD_STATEID
6490 */
6491static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6492 struct nfs4_lock_state *lsp)
6493{
6494 struct nfs4_state *state = lsp->ls_state;
6495 bool ret = false;
6496
6497 spin_lock(&state->state_lock);
6498 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6499 goto out;
6500 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6501 nfs4_stateid_seqid_inc(dst);
6502 else
6503 dst->seqid = lsp->ls_stateid.seqid;
6504 ret = true;
6505out:
6506 spin_unlock(&state->state_lock);
6507 return ret;
6508}
6509
6510static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6511 struct nfs4_lock_state *lsp)
6512{
6513 struct nfs4_state *state = lsp->ls_state;
6514 bool ret;
6515
6516 spin_lock(&state->state_lock);
6517 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6518 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6519 spin_unlock(&state->state_lock);
6520 return ret;
6521}
6522
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006523struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006524 struct nfs_locku_args arg;
6525 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006526 struct nfs4_lock_state *lsp;
6527 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006528 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006529 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006530 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006531 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006532};
6533
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006534static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6535 struct nfs_open_context *ctx,
6536 struct nfs4_lock_state *lsp,
6537 struct nfs_seqid *seqid)
6538{
6539 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006540 struct nfs4_state *state = lsp->ls_state;
6541 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006542
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006543 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006544 if (p == NULL)
6545 return NULL;
6546 p->arg.fh = NFS_FH(inode);
6547 p->arg.fl = &p->fl;
6548 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006549 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006550 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006551 /* Ensure we don't close file until we're done freeing locks! */
6552 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006553 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006554 locks_init_lock(&p->fl);
6555 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006556 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006557 spin_lock(&state->state_lock);
6558 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6559 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006560 return p;
6561}
6562
Trond Myklebust06f814a2006-01-03 09:55:07 +01006563static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006564{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006565 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006566 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006567 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006568 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006569 put_nfs_open_context(calldata->ctx);
6570 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006571}
6572
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006573static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006574{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006575 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006576 struct nfs4_exception exception = {
6577 .inode = calldata->lsp->ls_state->inode,
6578 .stateid = &calldata->arg.stateid,
6579 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006580
Trond Myklebust14516c32010-07-31 14:29:06 -04006581 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6582 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006583 switch (task->tk_status) {
6584 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006585 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006586 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006587 if (nfs4_update_lock_stateid(calldata->lsp,
6588 &calldata->res.stateid))
6589 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006590 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006591 case -NFS4ERR_ADMIN_REVOKED:
6592 case -NFS4ERR_EXPIRED:
6593 nfs4_free_revoked_stateid(calldata->server,
6594 &calldata->arg.stateid,
6595 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006596 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006597 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006598 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006599 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6600 calldata->lsp))
6601 rpc_restart_call_prepare(task);
6602 break;
6603 case -NFS4ERR_OLD_STATEID:
6604 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6605 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006606 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006607 break;
6608 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006609 task->tk_status = nfs4_async_handle_exception(task,
6610 calldata->server, task->tk_status,
6611 &exception);
6612 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006613 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006614 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006615 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006616}
6617
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006618static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006619{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006620 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006622 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6623 nfs_async_iocounter_wait(task, calldata->l_ctx))
6624 return;
6625
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006626 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006627 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006628 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006629 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006630 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006631 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006632 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006633 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006634 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006635 &calldata->res.seq_res,
6636 task) != 0)
6637 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006638 return;
6639out_no_action:
6640 task->tk_action = NULL;
6641out_wait:
6642 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643}
6644
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006645static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006646 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006647 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006648 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006649};
6650
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006651static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6652 struct nfs_open_context *ctx,
6653 struct nfs4_lock_state *lsp,
6654 struct nfs_seqid *seqid)
6655{
6656 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006657 struct rpc_message msg = {
6658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6659 .rpc_cred = ctx->cred,
6660 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006661 struct rpc_task_setup task_setup_data = {
6662 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006663 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006664 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006665 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006666 .flags = RPC_TASK_ASYNC,
6667 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006668
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006669 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6670 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6671
Frank Filz137d6ac2007-07-09 15:32:29 -07006672 /* Ensure this is an unlock - when canceling a lock, the
6673 * canceled lock is passed in, and it won't be an unlock.
6674 */
6675 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006676 if (fl->fl_flags & FL_CLOSE)
6677 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006678
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006679 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6680 if (data == NULL) {
6681 nfs_free_seqid(seqid);
6682 return ERR_PTR(-ENOMEM);
6683 }
6684
Anna Schumakerfba83f32018-05-04 16:22:50 -04006685 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006686 msg.rpc_argp = &data->arg;
6687 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006688 task_setup_data.callback_data = data;
6689 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006690}
6691
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6693{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006694 struct inode *inode = state->inode;
6695 struct nfs4_state_owner *sp = state->owner;
6696 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006697 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006698 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006699 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006700 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006701 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006702 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703
Trond Myklebust9b073572006-06-29 16:38:34 -04006704 status = nfs4_set_lock_state(state, request);
6705 /* Unlock _before_ we do the RPC call */
6706 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006707 /* Exclude nfs_delegation_claim_locks() */
6708 mutex_lock(&sp->so_delegreturn_mutex);
6709 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006710 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006711 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006712 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006713 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006714 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006715 }
6716 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006717 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006718 if (status != 0)
6719 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006720 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006721 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006722 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6723 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006724 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6725 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006726 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006727 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006728 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006729 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006730 status = PTR_ERR(task);
6731 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006732 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006733 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006734 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006735out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006736 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006737 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006738 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739}
6740
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006741struct nfs4_lockdata {
6742 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006743 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006744 struct nfs4_lock_state *lsp;
6745 struct nfs_open_context *ctx;
6746 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006747 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006748 int rpc_status;
6749 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006750 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006751};
6752
6753static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006754 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6755 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006756{
6757 struct nfs4_lockdata *p;
6758 struct inode *inode = lsp->ls_state->inode;
6759 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006760 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006761
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006762 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006763 if (p == NULL)
6764 return NULL;
6765
6766 p->arg.fh = NFS_FH(inode);
6767 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006768 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006769 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006770 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006771 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6772 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006773 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006774 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006775 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006776 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006777 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006778 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006779 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006780 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006781 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006782 locks_init_lock(&p->fl);
6783 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006784 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006785out_free_seqid:
6786 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006787out_free:
6788 kfree(p);
6789 return NULL;
6790}
6791
6792static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6793{
6794 struct nfs4_lockdata *data = calldata;
6795 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796
Harvey Harrison3110ff82008-05-02 13:42:44 -07006797 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006798 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006799 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006800 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006801 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006802 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006803 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006804 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006805 nfs4_stateid_copy(&data->arg.open_stateid,
6806 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006807 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006808 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006809 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006810 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006811 nfs4_stateid_copy(&data->arg.lock_stateid,
6812 &data->lsp->ls_stateid);
6813 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006814 if (!nfs4_valid_open_stateid(state)) {
6815 data->rpc_status = -EBADF;
6816 task->tk_action = NULL;
6817 goto out_release_open_seqid;
6818 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006819 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006820 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006821 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006822 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006823 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006824 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006825out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006826 nfs_release_seqid(data->arg.open_seqid);
6827out_release_lock_seqid:
6828 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006829out_wait:
6830 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006831 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006832}
6833
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006834static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6835{
6836 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006837 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006838
Harvey Harrison3110ff82008-05-02 13:42:44 -07006839 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006840
Trond Myklebust14516c32010-07-31 14:29:06 -04006841 if (!nfs4_sequence_done(task, &data->res.seq_res))
6842 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006843
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006844 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006845 switch (task->tk_status) {
6846 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006847 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006848 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006849 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006850 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006851 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006852 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006853 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006854 if (data->arg.new_lock_owner != 0) {
6855 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6856 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6857 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006858 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6859 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006860 break;
6861 case -NFS4ERR_BAD_STATEID:
6862 case -NFS4ERR_OLD_STATEID:
6863 case -NFS4ERR_STALE_STATEID:
6864 case -NFS4ERR_EXPIRED:
6865 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006866 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006867 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006868 goto out_restart;
6869 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006870 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006871 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006872 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006873out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006874 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006875 return;
6876out_restart:
6877 if (!data->cancelled)
6878 rpc_restart_call_prepare(task);
6879 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006880}
6881
6882static void nfs4_lock_release(void *calldata)
6883{
6884 struct nfs4_lockdata *data = calldata;
6885
Harvey Harrison3110ff82008-05-02 13:42:44 -07006886 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006887 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006888 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006889 struct rpc_task *task;
6890 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6891 data->arg.lock_seqid);
6892 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006893 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006894 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006895 } else
6896 nfs_free_seqid(data->arg.lock_seqid);
6897 nfs4_put_lock_state(data->lsp);
6898 put_nfs_open_context(data->ctx);
6899 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006900 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006901}
6902
6903static const struct rpc_call_ops nfs4_lock_ops = {
6904 .rpc_call_prepare = nfs4_lock_prepare,
6905 .rpc_call_done = nfs4_lock_done,
6906 .rpc_release = nfs4_lock_release,
6907};
6908
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006909static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6910{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006911 switch (error) {
6912 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006913 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006914 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006915 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006916 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006917 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006918 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006919 break;
6920 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006921 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006922 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08006923 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006924}
6925
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006926static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006927{
6928 struct nfs4_lockdata *data;
6929 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006930 struct rpc_message msg = {
6931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6932 .rpc_cred = state->owner->so_cred,
6933 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006934 struct rpc_task_setup task_setup_data = {
6935 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006936 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006937 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006938 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05006939 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006940 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006941 int ret;
6942
Harvey Harrison3110ff82008-05-02 13:42:44 -07006943 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006944 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006945 fl->fl_u.nfs4_fl.owner,
6946 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006947 if (data == NULL)
6948 return -ENOMEM;
6949 if (IS_SETLKW(cmd))
6950 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006951 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6952 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006953 msg.rpc_argp = &data->arg;
6954 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006955 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006956 if (recovery_type > NFS_LOCK_NEW) {
6957 if (recovery_type == NFS_LOCK_RECLAIM)
6958 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006959 } else
6960 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006961 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006962 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006963 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006964 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006965 if (ret == 0) {
6966 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006967 if (ret)
6968 nfs4_handle_setlk_error(data->server, data->lsp,
6969 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006970 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006971 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006972 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006973 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006974 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006975 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976}
6977
6978static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6979{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006980 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006981 struct nfs4_exception exception = {
6982 .inode = state->inode,
6983 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006984 int err;
6985
6986 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006987 /* Cache the lock if possible... */
6988 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6989 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006990 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006991 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006992 break;
6993 nfs4_handle_exception(server, err, &exception);
6994 } while (exception.retry);
6995 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996}
6997
6998static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6999{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007000 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007001 struct nfs4_exception exception = {
7002 .inode = state->inode,
7003 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007004 int err;
7005
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007006 err = nfs4_set_lock_state(state, request);
7007 if (err != 0)
7008 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007009 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007010 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7011 return 0;
7012 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007013 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007014 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7015 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007016 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007017 switch (err) {
7018 default:
7019 goto out;
7020 case -NFS4ERR_GRACE:
7021 case -NFS4ERR_DELAY:
7022 nfs4_handle_exception(server, err, &exception);
7023 err = 0;
7024 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007025 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007026out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007027 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028}
7029
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007030#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007031static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7032{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007033 struct nfs4_lock_state *lsp;
7034 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007035
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007036 status = nfs4_set_lock_state(state, request);
7037 if (status != 0)
7038 return status;
7039 lsp = request->fl_u.nfs4_fl.owner;
7040 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7041 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7042 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007043 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007044}
7045#endif
7046
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7048{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007049 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007050 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007051 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007052 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053
Trond Myklebust01c3b862006-06-29 16:38:39 -04007054 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007055 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007056 if (status < 0)
7057 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007058 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007059 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007060 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007061 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007062 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007063 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007064 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007065 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007066 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007067 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007068 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007069 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007070 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007071 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007072out:
7073 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 return status;
7075}
7076
7077static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7078{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007079 struct nfs4_exception exception = {
7080 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007081 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007082 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007083 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084 int err;
7085
7086 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007087 err = _nfs4_proc_setlk(state, cmd, request);
7088 if (err == -NFS4ERR_DENIED)
7089 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007091 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 } while (exception.retry);
7093 return err;
7094}
7095
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007096#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7097#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7098
7099static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007100nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7101 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007102{
7103 int status = -ERESTARTSYS;
7104 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7105
7106 while(!signalled()) {
7107 status = nfs4_proc_setlk(state, cmd, request);
7108 if ((status != -EAGAIN) || IS_SETLK(cmd))
7109 break;
7110 freezable_schedule_timeout_interruptible(timeout);
7111 timeout *= 2;
7112 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7113 status = -ERESTARTSYS;
7114 }
7115 return status;
7116}
7117
Jeff Laytona1d617d82016-09-17 18:17:39 -04007118#ifdef CONFIG_NFS_V4_1
7119struct nfs4_lock_waiter {
7120 struct task_struct *task;
7121 struct inode *inode;
7122 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007123};
7124
7125static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007126nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007127{
7128 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007129 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007130
Jeff Layton57174592018-03-18 08:37:03 -04007131 /* NULL key means to wake up everyone */
7132 if (key) {
7133 struct cb_notify_lock_args *cbnl = key;
7134 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7135 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007136
Jeff Layton57174592018-03-18 08:37:03 -04007137 /* Only wake if the callback was for the same owner. */
7138 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7139 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007140
Jeff Layton57174592018-03-18 08:37:03 -04007141 /* Make sure it's for the right inode */
7142 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7143 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007144 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007145
7146 /* override "private" so we can use default_wake_function */
7147 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007148 ret = woken_wake_function(wait, mode, flags, key);
7149 if (ret)
7150 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007151 wait->private = waiter;
7152 return ret;
7153}
7154
7155static int
7156nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7157{
7158 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007159 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7160 struct nfs_server *server = NFS_SERVER(state->inode);
7161 struct nfs_client *clp = server->nfs_client;
7162 wait_queue_head_t *q = &clp->cl_lock_waitq;
7163 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7164 .id = lsp->ls_seqid.owner_id,
7165 .s_dev = server->s_dev };
7166 struct nfs4_lock_waiter waiter = { .task = current,
7167 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007168 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007169 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007170
7171 /* Don't bother with waitqueue if we don't expect a callback */
7172 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7173 return nfs4_retry_setlk_simple(state, cmd, request);
7174
7175 init_wait(&wait);
7176 wait.private = &waiter;
7177 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007178
7179 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007180 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007181 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007182 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7183 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007184 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007185 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007186
7187 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007188 freezer_do_not_count();
7189 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7190 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007191 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007192 }
7193
Jeff Laytona1d617d82016-09-17 18:17:39 -04007194 return status;
7195}
7196#else /* !CONFIG_NFS_V4_1 */
7197static inline int
7198nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7199{
7200 return nfs4_retry_setlk_simple(state, cmd, request);
7201}
7202#endif
7203
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204static int
7205nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7206{
7207 struct nfs_open_context *ctx;
7208 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 int status;
7210
7211 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007212 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 state = ctx->state;
7214
Trond Myklebustd9531262009-07-21 19:22:38 -04007215 if (IS_GETLK(cmd)) {
7216 if (state != NULL)
7217 return nfs4_proc_getlk(state, F_GETLK, request);
7218 return 0;
7219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220
7221 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7222 return -EINVAL;
7223
Trond Myklebustd9531262009-07-21 19:22:38 -04007224 if (request->fl_type == F_UNLCK) {
7225 if (state != NULL)
7226 return nfs4_proc_unlck(state, cmd, request);
7227 return 0;
7228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229
Trond Myklebustd9531262009-07-21 19:22:38 -04007230 if (state == NULL)
7231 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007232
7233 if ((request->fl_flags & FL_POSIX) &&
7234 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7235 return -ENOLCK;
7236
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007237 /*
7238 * Don't rely on the VFS having checked the file open mode,
7239 * since it won't do this for flock() locks.
7240 */
7241 switch (request->fl_type) {
7242 case F_RDLCK:
7243 if (!(filp->f_mode & FMODE_READ))
7244 return -EBADF;
7245 break;
7246 case F_WRLCK:
7247 if (!(filp->f_mode & FMODE_WRITE))
7248 return -EBADF;
7249 }
7250
Jeff Layton1ea67db2016-09-17 18:17:37 -04007251 status = nfs4_set_lock_state(state, request);
7252 if (status != 0)
7253 return status;
7254
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007255 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256}
7257
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007258int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007259{
7260 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007261 int err;
7262
7263 err = nfs4_set_lock_state(state, fl);
7264 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007265 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007266 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007267 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007268}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007269
Trond Myklebustcf470c32012-03-07 13:49:12 -05007270struct nfs_release_lockowner_data {
7271 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007272 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007273 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007274 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007275 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007276};
7277
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007278static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7279{
7280 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007281 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007282 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7283 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007284 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007285 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007286}
7287
7288static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7289{
7290 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007291 struct nfs_server *server = data->server;
7292
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007293 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007294
7295 switch (task->tk_status) {
7296 case 0:
7297 renew_lease(server, data->timestamp);
7298 break;
7299 case -NFS4ERR_STALE_CLIENTID:
7300 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007301 nfs4_schedule_lease_recovery(server->nfs_client);
7302 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007303 case -NFS4ERR_LEASE_MOVED:
7304 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007305 if (nfs4_async_handle_error(task, server,
7306 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007307 rpc_restart_call_prepare(task);
7308 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007309}
7310
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007311static void nfs4_release_lockowner_release(void *calldata)
7312{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007313 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007314 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007315 kfree(calldata);
7316}
7317
Trond Myklebust17280172012-03-11 13:11:00 -04007318static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007319 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7320 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007321 .rpc_release = nfs4_release_lockowner_release,
7322};
7323
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007324static void
7325nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007326{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007327 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007328 struct rpc_message msg = {
7329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7330 };
7331
7332 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007333 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007334
Trond Myklebustcf470c32012-03-07 13:49:12 -05007335 data = kmalloc(sizeof(*data), GFP_NOFS);
7336 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007337 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007338 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007339 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007340 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7341 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7342 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007343
Trond Myklebustcf470c32012-03-07 13:49:12 -05007344 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007345 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007346 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007347 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007348}
7349
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007350#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7351
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007352static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007353 struct dentry *unused, struct inode *inode,
7354 const char *key, const void *buf,
7355 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007356{
Al Viro59301222016-05-27 10:19:30 -04007357 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007358}
7359
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007360static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007361 struct dentry *unused, struct inode *inode,
7362 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007363{
Al Virob2968212016-04-10 20:48:24 -04007364 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007365}
7366
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007367static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007368{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007369 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007370}
7371
David Quigleyc9bccef2013-05-22 12:50:45 -04007372#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007373
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007374static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007375 struct dentry *unused, struct inode *inode,
7376 const char *key, const void *buf,
7377 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007378{
7379 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007380 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007381
7382 return -EOPNOTSUPP;
7383}
7384
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007385static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007386 struct dentry *unused, struct inode *inode,
7387 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007388{
7389 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007390 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007391 return -EOPNOTSUPP;
7392}
7393
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007394static ssize_t
7395nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007396{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007397 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007398
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007399 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7400 len = security_inode_listsecurity(inode, list, list_len);
7401 if (list_len && len > list_len)
7402 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007403 }
7404 return len;
7405}
7406
7407static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7408 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007409 .get = nfs4_xattr_get_nfs4_label,
7410 .set = nfs4_xattr_set_nfs4_label,
7411};
David Quigleyc9bccef2013-05-22 12:50:45 -04007412
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007413#else
7414
7415static ssize_t
7416nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7417{
7418 return 0;
7419}
7420
7421#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007422
Andy Adamson533eb462011-06-13 18:25:56 -04007423/*
7424 * nfs_fhget will use either the mounted_on_fileid or the fileid
7425 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007426static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7427{
Andy Adamson533eb462011-06-13 18:25:56 -04007428 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7429 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7430 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007431 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007432 return;
7433
7434 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007435 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007436 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7437 fattr->nlink = 2;
7438}
7439
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007440static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7441 const struct qstr *name,
7442 struct nfs4_fs_locations *fs_locations,
7443 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007444{
7445 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007446 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007447 struct nfs4_fs_locations_arg args = {
7448 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007449 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007450 .page = page,
7451 .bitmask = bitmask,
7452 };
Benny Halevy22958462009-04-01 09:22:02 -04007453 struct nfs4_fs_locations_res res = {
7454 .fs_locations = fs_locations,
7455 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007456 struct rpc_message msg = {
7457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7458 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007459 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007460 };
7461 int status;
7462
Harvey Harrison3110ff82008-05-02 13:42:44 -07007463 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007464
Chuck Leverc05cefc2017-11-05 15:45:22 -05007465 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7466 bitmask[1] = nfs4_fattr_bitmap[1];
7467
Andy Adamson533eb462011-06-13 18:25:56 -04007468 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7469 * is not supported */
7470 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007471 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007472 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007473 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007474
Trond Myklebustc228fd32007-01-13 02:28:11 -05007475 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007476 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007477 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007478 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007479 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007480 return status;
7481}
7482
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007483int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7484 const struct qstr *name,
7485 struct nfs4_fs_locations *fs_locations,
7486 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007487{
Trond Myklebust0688e642019-04-07 13:59:09 -04007488 struct nfs4_exception exception = {
7489 .interruptible = true,
7490 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007491 int err;
7492 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007493 err = _nfs4_proc_fs_locations(client, dir, name,
7494 fs_locations, page);
7495 trace_nfs4_get_fs_locations(dir, name, err);
7496 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007497 &exception);
7498 } while (exception.retry);
7499 return err;
7500}
7501
Chuck Leverb03d7352013-10-17 14:12:50 -04007502/*
7503 * This operation also signals the server that this client is
7504 * performing migration recovery. The server can stop returning
7505 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7506 * appended to this compound to identify the client ID which is
7507 * performing recovery.
7508 */
7509static int _nfs40_proc_get_locations(struct inode *inode,
7510 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007511 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007512{
7513 struct nfs_server *server = NFS_SERVER(inode);
7514 struct rpc_clnt *clnt = server->client;
7515 u32 bitmask[2] = {
7516 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7517 };
7518 struct nfs4_fs_locations_arg args = {
7519 .clientid = server->nfs_client->cl_clientid,
7520 .fh = NFS_FH(inode),
7521 .page = page,
7522 .bitmask = bitmask,
7523 .migration = 1, /* skip LOOKUP */
7524 .renew = 1, /* append RENEW */
7525 };
7526 struct nfs4_fs_locations_res res = {
7527 .fs_locations = locations,
7528 .migration = 1,
7529 .renew = 1,
7530 };
7531 struct rpc_message msg = {
7532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7533 .rpc_argp = &args,
7534 .rpc_resp = &res,
7535 .rpc_cred = cred,
7536 };
7537 unsigned long now = jiffies;
7538 int status;
7539
7540 nfs_fattr_init(&locations->fattr);
7541 locations->server = server;
7542 locations->nlocations = 0;
7543
Anna Schumakerfba83f32018-05-04 16:22:50 -04007544 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007545 status = nfs4_call_sync_sequence(clnt, server, &msg,
7546 &args.seq_args, &res.seq_res);
7547 if (status)
7548 return status;
7549
7550 renew_lease(server, now);
7551 return 0;
7552}
7553
7554#ifdef CONFIG_NFS_V4_1
7555
7556/*
7557 * This operation also signals the server that this client is
7558 * performing migration recovery. The server can stop asserting
7559 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7560 * performing this operation is identified in the SEQUENCE
7561 * operation in this compound.
7562 *
7563 * When the client supports GETATTR(fs_locations_info), it can
7564 * be plumbed in here.
7565 */
7566static int _nfs41_proc_get_locations(struct inode *inode,
7567 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007568 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007569{
7570 struct nfs_server *server = NFS_SERVER(inode);
7571 struct rpc_clnt *clnt = server->client;
7572 u32 bitmask[2] = {
7573 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7574 };
7575 struct nfs4_fs_locations_arg args = {
7576 .fh = NFS_FH(inode),
7577 .page = page,
7578 .bitmask = bitmask,
7579 .migration = 1, /* skip LOOKUP */
7580 };
7581 struct nfs4_fs_locations_res res = {
7582 .fs_locations = locations,
7583 .migration = 1,
7584 };
7585 struct rpc_message msg = {
7586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7587 .rpc_argp = &args,
7588 .rpc_resp = &res,
7589 .rpc_cred = cred,
7590 };
7591 int status;
7592
7593 nfs_fattr_init(&locations->fattr);
7594 locations->server = server;
7595 locations->nlocations = 0;
7596
Anna Schumakerfba83f32018-05-04 16:22:50 -04007597 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007598 status = nfs4_call_sync_sequence(clnt, server, &msg,
7599 &args.seq_args, &res.seq_res);
7600 if (status == NFS4_OK &&
7601 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7602 status = -NFS4ERR_LEASE_MOVED;
7603 return status;
7604}
7605
7606#endif /* CONFIG_NFS_V4_1 */
7607
7608/**
7609 * nfs4_proc_get_locations - discover locations for a migrated FSID
7610 * @inode: inode on FSID that is migrating
7611 * @locations: result of query
7612 * @page: buffer
7613 * @cred: credential to use for this operation
7614 *
7615 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7616 * operation failed, or a negative errno if a local error occurred.
7617 *
7618 * On success, "locations" is filled in, but if the server has
7619 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7620 * asserted.
7621 *
7622 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7623 * from this client that require migration recovery.
7624 */
7625int nfs4_proc_get_locations(struct inode *inode,
7626 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007627 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007628{
7629 struct nfs_server *server = NFS_SERVER(inode);
7630 struct nfs_client *clp = server->nfs_client;
7631 const struct nfs4_mig_recovery_ops *ops =
7632 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007633 struct nfs4_exception exception = {
7634 .interruptible = true,
7635 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007636 int status;
7637
7638 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7639 (unsigned long long)server->fsid.major,
7640 (unsigned long long)server->fsid.minor,
7641 clp->cl_hostname);
7642 nfs_display_fhandle(NFS_FH(inode), __func__);
7643
7644 do {
7645 status = ops->get_locations(inode, locations, page, cred);
7646 if (status != -NFS4ERR_DELAY)
7647 break;
7648 nfs4_handle_exception(server, status, &exception);
7649 } while (exception.retry);
7650 return status;
7651}
7652
Chuck Lever44c99932013-10-17 14:13:30 -04007653/*
7654 * This operation also signals the server that this client is
7655 * performing "lease moved" recovery. The server can stop
7656 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7657 * is appended to this compound to identify the client ID which is
7658 * performing recovery.
7659 */
NeilBrowna52458b2018-12-03 11:30:31 +11007660static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007661{
7662 struct nfs_server *server = NFS_SERVER(inode);
7663 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7664 struct rpc_clnt *clnt = server->client;
7665 struct nfs4_fsid_present_arg args = {
7666 .fh = NFS_FH(inode),
7667 .clientid = clp->cl_clientid,
7668 .renew = 1, /* append RENEW */
7669 };
7670 struct nfs4_fsid_present_res res = {
7671 .renew = 1,
7672 };
7673 struct rpc_message msg = {
7674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7675 .rpc_argp = &args,
7676 .rpc_resp = &res,
7677 .rpc_cred = cred,
7678 };
7679 unsigned long now = jiffies;
7680 int status;
7681
7682 res.fh = nfs_alloc_fhandle();
7683 if (res.fh == NULL)
7684 return -ENOMEM;
7685
Anna Schumakerfba83f32018-05-04 16:22:50 -04007686 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007687 status = nfs4_call_sync_sequence(clnt, server, &msg,
7688 &args.seq_args, &res.seq_res);
7689 nfs_free_fhandle(res.fh);
7690 if (status)
7691 return status;
7692
7693 do_renew_lease(clp, now);
7694 return 0;
7695}
7696
7697#ifdef CONFIG_NFS_V4_1
7698
7699/*
7700 * This operation also signals the server that this client is
7701 * performing "lease moved" recovery. The server can stop asserting
7702 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7703 * this operation is identified in the SEQUENCE operation in this
7704 * compound.
7705 */
NeilBrowna52458b2018-12-03 11:30:31 +11007706static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007707{
7708 struct nfs_server *server = NFS_SERVER(inode);
7709 struct rpc_clnt *clnt = server->client;
7710 struct nfs4_fsid_present_arg args = {
7711 .fh = NFS_FH(inode),
7712 };
7713 struct nfs4_fsid_present_res res = {
7714 };
7715 struct rpc_message msg = {
7716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7717 .rpc_argp = &args,
7718 .rpc_resp = &res,
7719 .rpc_cred = cred,
7720 };
7721 int status;
7722
7723 res.fh = nfs_alloc_fhandle();
7724 if (res.fh == NULL)
7725 return -ENOMEM;
7726
Anna Schumakerfba83f32018-05-04 16:22:50 -04007727 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007728 status = nfs4_call_sync_sequence(clnt, server, &msg,
7729 &args.seq_args, &res.seq_res);
7730 nfs_free_fhandle(res.fh);
7731 if (status == NFS4_OK &&
7732 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7733 status = -NFS4ERR_LEASE_MOVED;
7734 return status;
7735}
7736
7737#endif /* CONFIG_NFS_V4_1 */
7738
7739/**
7740 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7741 * @inode: inode on FSID to check
7742 * @cred: credential to use for this operation
7743 *
7744 * Server indicates whether the FSID is present, moved, or not
7745 * recognized. This operation is necessary to clear a LEASE_MOVED
7746 * condition for this client ID.
7747 *
7748 * Returns NFS4_OK if the FSID is present on this server,
7749 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7750 * NFS4ERR code if some error occurred on the server, or a
7751 * negative errno if a local failure occurred.
7752 */
NeilBrowna52458b2018-12-03 11:30:31 +11007753int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007754{
7755 struct nfs_server *server = NFS_SERVER(inode);
7756 struct nfs_client *clp = server->nfs_client;
7757 const struct nfs4_mig_recovery_ops *ops =
7758 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007759 struct nfs4_exception exception = {
7760 .interruptible = true,
7761 };
Chuck Lever44c99932013-10-17 14:13:30 -04007762 int status;
7763
7764 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7765 (unsigned long long)server->fsid.major,
7766 (unsigned long long)server->fsid.minor,
7767 clp->cl_hostname);
7768 nfs_display_fhandle(NFS_FH(inode), __func__);
7769
7770 do {
7771 status = ops->fsid_present(inode, cred);
7772 if (status != -NFS4ERR_DELAY)
7773 break;
7774 nfs4_handle_exception(server, status, &exception);
7775 } while (exception.retry);
7776 return status;
7777}
7778
Trond Myklebust302fad72019-02-18 13:32:38 -05007779/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007780 * If 'use_integrity' is true and the state managment nfs_client
7781 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7782 * and the machine credential as per RFC3530bis and RFC5661 Security
7783 * Considerations sections. Otherwise, just use the user cred with the
7784 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007785 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007786static 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 +00007787{
7788 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04007789 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7790 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007791 struct nfs4_secinfo_arg args = {
7792 .dir_fh = NFS_FH(dir),
7793 .name = name,
7794 };
7795 struct nfs4_secinfo_res res = {
7796 .flavors = flavors,
7797 };
7798 struct rpc_message msg = {
7799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7800 .rpc_argp = &args,
7801 .rpc_resp = &res,
7802 };
Anna Schumaker50493362019-08-14 15:30:16 -04007803 struct nfs4_call_sync_data data = {
7804 .seq_server = NFS_SERVER(dir),
7805 .seq_args = &args.seq_args,
7806 .seq_res = &res.seq_res,
7807 };
7808 struct rpc_task_setup task_setup = {
7809 .rpc_client = clnt,
7810 .rpc_message = &msg,
7811 .callback_ops = clp->cl_mvops->call_sync_ops,
7812 .callback_data = &data,
7813 .flags = RPC_TASK_NO_ROUND_ROBIN,
7814 };
NeilBrowna52458b2018-12-03 11:30:31 +11007815 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007816
7817 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04007818 clnt = clp->cl_rpcclient;
7819 task_setup.rpc_client = clnt;
7820
7821 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007822 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007823 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007824
7825 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007826
Anna Schumaker50493362019-08-14 15:30:16 -04007827 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7828 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7829 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007830
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007831 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007832
NeilBrowna52458b2018-12-03 11:30:31 +11007833 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007834 return status;
7835}
7836
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007837int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7838 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007839{
Trond Myklebust0688e642019-04-07 13:59:09 -04007840 struct nfs4_exception exception = {
7841 .interruptible = true,
7842 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007843 int err;
7844 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007845 err = -NFS4ERR_WRONGSEC;
7846
7847 /* try to use integrity protection with machine cred */
7848 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7849 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7850
7851 /*
7852 * if unable to use integrity protection, or SECINFO with
7853 * integrity protection returns NFS4ERR_WRONGSEC (which is
7854 * disallowed by spec, but exists in deployed servers) use
7855 * the current filesystem's rpc_client and the user cred.
7856 */
7857 if (err == -NFS4ERR_WRONGSEC)
7858 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7859
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007860 trace_nfs4_secinfo(dir, name, err);
7861 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007862 &exception);
7863 } while (exception.retry);
7864 return err;
7865}
7866
Andy Adamson557134a2009-04-01 09:21:53 -04007867#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007868/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007869 * Check the exchange flags returned by the server for invalid flags, having
7870 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7871 * DS flags set.
7872 */
7873static int nfs4_check_cl_exchange_flags(u32 flags)
7874{
7875 if (flags & ~EXCHGID4_FLAG_MASK_R)
7876 goto out_inval;
7877 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7878 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7879 goto out_inval;
7880 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7881 goto out_inval;
7882 return NFS_OK;
7883out_inval:
7884 return -NFS4ERR_INVAL;
7885}
7886
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007887static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007888nfs41_same_server_scope(struct nfs41_server_scope *a,
7889 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007890{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007891 if (a->server_scope_sz != b->server_scope_sz)
7892 return false;
7893 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007894}
7895
Andy Adamson02a95de2016-02-05 16:08:37 -05007896static void
7897nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7898{
Trond Myklebust5c441542019-11-13 08:34:00 +01007899 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
7900 struct nfs_client *clp = args->client;
7901
7902 switch (task->tk_status) {
7903 case -NFS4ERR_BADSESSION:
7904 case -NFS4ERR_DEADSESSION:
7905 nfs4_schedule_session_recovery(clp->cl_session,
7906 task->tk_status);
7907 }
Andy Adamson02a95de2016-02-05 16:08:37 -05007908}
7909
7910static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7911 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7912};
7913
Andy Adamson357f54d2010-12-14 10:11:57 -05007914/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007915 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007916 *
7917 * The 4.1 client currently uses the same TCP connection for the
7918 * fore and backchannel.
7919 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007920static
7921int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7922 struct rpc_xprt *xprt,
7923 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007924 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007925{
7926 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007927 struct nfs41_bind_conn_to_session_args args = {
7928 .client = clp,
7929 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7930 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007931 struct nfs41_bind_conn_to_session_res res;
7932 struct rpc_message msg = {
7933 .rpc_proc =
7934 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007935 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007936 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007937 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007938 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007939 struct rpc_task_setup task_setup_data = {
7940 .rpc_client = clnt,
7941 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007942 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007943 .rpc_message = &msg,
7944 .flags = RPC_TASK_TIMEOUT,
7945 };
7946 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007947
Trond Myklebust71a097c2015-02-18 09:27:18 -08007948 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7949 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7950 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007951
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007952 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7953 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7954 args.dir = NFS4_CDFC4_FORE;
7955
7956 task = rpc_run_task(&task_setup_data);
7957 if (!IS_ERR(task)) {
7958 status = task->tk_status;
7959 rpc_put_task(task);
7960 } else
7961 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007962 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007963 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007964 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007965 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7966 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007967 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007968 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007969 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007970 dprintk("NFS: %s: Unexpected direction from server\n",
7971 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007972 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007973 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007974 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007975 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7976 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007977 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007978 }
7979 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007980
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007981 return status;
7982}
7983
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007984struct rpc_bind_conn_calldata {
7985 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007986 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007987};
7988
7989static int
7990nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7991 struct rpc_xprt *xprt,
7992 void *calldata)
7993{
7994 struct rpc_bind_conn_calldata *p = calldata;
7995
7996 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7997}
7998
NeilBrowna52458b2018-12-03 11:30:31 +11007999int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008000{
8001 struct rpc_bind_conn_calldata data = {
8002 .clp = clp,
8003 .cred = cred,
8004 };
8005 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8006 nfs4_proc_bind_conn_to_session_callback, &data);
8007}
8008
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008009/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008010 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8011 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008012 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008013static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8014 .how = SP4_MACH_CRED,
8015 .enforce.u.words = {
8016 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8017 1 << (OP_EXCHANGE_ID - 32) |
8018 1 << (OP_CREATE_SESSION - 32) |
8019 1 << (OP_DESTROY_SESSION - 32) |
8020 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008021 },
8022 .allow.u.words = {
8023 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008024 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008025 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008026 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008027 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008028 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008029 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008030 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008031 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008032 1 << (OP_FREE_STATEID - 32) |
8033 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008034 }
8035};
8036
8037/*
8038 * Select the state protection mode for client `clp' given the server results
8039 * from exchange_id in `sp'.
8040 *
8041 * Returns 0 on success, negative errno otherwise.
8042 */
8043static int nfs4_sp4_select_mode(struct nfs_client *clp,
8044 struct nfs41_state_protection *sp)
8045{
8046 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8047 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8048 1 << (OP_EXCHANGE_ID - 32) |
8049 1 << (OP_CREATE_SESSION - 32) |
8050 1 << (OP_DESTROY_SESSION - 32) |
8051 1 << (OP_DESTROY_CLIENTID - 32)
8052 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008053 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008054 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008055 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008056
8057 if (sp->how == SP4_MACH_CRED) {
8058 /* Print state protect result */
8059 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8060 for (i = 0; i <= LAST_NFS4_OP; i++) {
8061 if (test_bit(i, sp->enforce.u.longs))
8062 dfprintk(MOUNT, " enforce op %d\n", i);
8063 if (test_bit(i, sp->allow.u.longs))
8064 dfprintk(MOUNT, " allow op %d\n", i);
8065 }
8066
8067 /* make sure nothing is on enforce list that isn't supported */
8068 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8069 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8070 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008071 ret = -EINVAL;
8072 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008073 }
8074 }
8075
8076 /*
8077 * Minimal mode - state operations are allowed to use machine
8078 * credential. Note this already happens by default, so the
8079 * client doesn't have to do anything more than the negotiation.
8080 *
8081 * NOTE: we don't care if EXCHANGE_ID is in the list -
8082 * we're already using the machine cred for exchange_id
8083 * and will never use a different cred.
8084 */
8085 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8086 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8087 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8088 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8089 dfprintk(MOUNT, "sp4_mach_cred:\n");
8090 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008091 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008092 } else {
8093 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008094 ret = -EINVAL;
8095 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008096 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008097
8098 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008099 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8100 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008101 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8102 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008103 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008104 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008105
Andrew Elble99ade3c2015-12-02 09:39:51 -05008106 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8107 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008108 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008109 }
8110
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008111 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8112 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8113 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008114 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008115 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008116
8117 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8118 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8119 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008120 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008121 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008122
8123 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8124 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008125 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008126 }
8127
8128 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8129 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008130 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008131 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008132 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008133out:
8134 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008135 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008136}
8137
Andy Adamson8d89bd72016-09-09 09:22:18 -04008138struct nfs41_exchange_id_data {
8139 struct nfs41_exchange_id_res res;
8140 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008141};
8142
Andy Adamson8d89bd72016-09-09 09:22:18 -04008143static void nfs4_exchange_id_release(void *data)
8144{
8145 struct nfs41_exchange_id_data *cdata =
8146 (struct nfs41_exchange_id_data *)data;
8147
Olga Kornievskaia63513232017-03-13 10:36:19 -04008148 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008149 kfree(cdata->res.impl_id);
8150 kfree(cdata->res.server_scope);
8151 kfree(cdata->res.server_owner);
8152 kfree(cdata);
8153}
8154
8155static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008156 .rpc_release = nfs4_exchange_id_release,
8157};
8158
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008159/*
8160 * _nfs4_proc_exchange_id()
8161 *
8162 * Wrapper for EXCHANGE_ID operation.
8163 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008164static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008165nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008166 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008167{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008168 struct rpc_message msg = {
8169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008170 .rpc_cred = cred,
8171 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008172 struct rpc_task_setup task_setup_data = {
8173 .rpc_client = clp->cl_rpcclient,
8174 .callback_ops = &nfs4_exchange_id_call_ops,
8175 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008176 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008177 };
8178 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008179 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008180
Elena Reshetova212bf412017-10-20 12:53:38 +03008181 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008182 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008183
Trond Myklebust9c760d12017-07-31 18:38:50 -04008184 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008185 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008186 if (!calldata)
8187 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008188
Trond Myklebustfd405592017-08-01 16:02:47 -04008189 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008190
8191 status = nfs4_init_uniform_client_string(clp);
8192 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008193 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008194
Andy Adamson8d89bd72016-09-09 09:22:18 -04008195 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8196 GFP_NOFS);
8197 status = -ENOMEM;
8198 if (unlikely(calldata->res.server_owner == NULL))
8199 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008200
Andy Adamson8d89bd72016-09-09 09:22:18 -04008201 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008202 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008203 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008204 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008205
Andy Adamson8d89bd72016-09-09 09:22:18 -04008206 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8207 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008208 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008209
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008210 switch (sp4_how) {
8211 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008212 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008213 break;
8214
8215 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008216 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008217 break;
8218
8219 default:
8220 /* unsupported! */
8221 WARN_ON_ONCE(1);
8222 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008223 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008224 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008225 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008226 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008227 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008228 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8229 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008230 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008231 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008232 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8233 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008234#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008235 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008236#endif
8237 msg.rpc_argp = &calldata->args;
8238 msg.rpc_resp = &calldata->res;
8239 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008240
Trond Myklebust9c760d12017-07-31 18:38:50 -04008241 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008242
8243out_impl_id:
8244 kfree(calldata->res.impl_id);
8245out_server_scope:
8246 kfree(calldata->res.server_scope);
8247out_server_owner:
8248 kfree(calldata->res.server_owner);
8249out_calldata:
8250 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008251out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008252 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008253 return ERR_PTR(status);
8254}
8255
8256/*
8257 * _nfs4_proc_exchange_id()
8258 *
8259 * Wrapper for EXCHANGE_ID operation.
8260 */
NeilBrowna52458b2018-12-03 11:30:31 +11008261static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008262 u32 sp4_how)
8263{
8264 struct rpc_task *task;
8265 struct nfs41_exchange_id_args *argp;
8266 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008267 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008268 int status;
8269
8270 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8271 if (IS_ERR(task))
8272 return PTR_ERR(task);
8273
8274 argp = task->tk_msg.rpc_argp;
8275 resp = task->tk_msg.rpc_resp;
8276 status = task->tk_status;
8277 if (status != 0)
8278 goto out;
8279
8280 status = nfs4_check_cl_exchange_flags(resp->flags);
8281 if (status != 0)
8282 goto out;
8283
8284 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8285 if (status != 0)
8286 goto out;
8287
Robert Milkowski7dc29932020-01-30 09:43:25 +00008288 do_renew_lease(clp, now);
8289
Trond Myklebust9c760d12017-07-31 18:38:50 -04008290 clp->cl_clientid = resp->clientid;
8291 clp->cl_exchange_flags = resp->flags;
8292 clp->cl_seqid = resp->seqid;
8293 /* Client ID is not confirmed */
8294 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8295 clear_bit(NFS4_SESSION_ESTABLISHED,
8296 &clp->cl_session->session_state);
8297
8298 if (clp->cl_serverscope != NULL &&
8299 !nfs41_same_server_scope(clp->cl_serverscope,
8300 resp->server_scope)) {
8301 dprintk("%s: server_scope mismatch detected\n",
8302 __func__);
8303 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8304 }
8305
8306 swap(clp->cl_serverowner, resp->server_owner);
8307 swap(clp->cl_serverscope, resp->server_scope);
8308 swap(clp->cl_implid, resp->impl_id);
8309
8310 /* Save the EXCHANGE_ID verifier session trunk tests */
8311 memcpy(clp->cl_confirm.data, argp->verifier.data,
8312 sizeof(clp->cl_confirm.data));
8313out:
8314 trace_nfs4_exchange_id(clp, status);
8315 rpc_put_task(task);
8316 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008317}
8318
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008319/*
8320 * nfs4_proc_exchange_id()
8321 *
8322 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8323 *
8324 * Since the clientid has expired, all compounds using sessions
8325 * associated with the stale clientid will be returning
8326 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8327 * be in some phase of session reset.
8328 *
8329 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8330 */
NeilBrowna52458b2018-12-03 11:30:31 +11008331int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008332{
8333 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8334 int status;
8335
8336 /* try SP4_MACH_CRED if krb5i/p */
8337 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8338 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008339 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008340 if (!status)
8341 return 0;
8342 }
8343
8344 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008345 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008346}
8347
Andy Adamson04fa2c62016-09-09 09:22:29 -04008348/**
8349 * nfs4_test_session_trunk
8350 *
8351 * This is an add_xprt_test() test function called from
8352 * rpc_clnt_setup_test_and_add_xprt.
8353 *
8354 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8355 * and is dereferrenced in nfs4_exchange_id_release
8356 *
8357 * Upon success, add the new transport to the rpc_clnt
8358 *
8359 * @clnt: struct rpc_clnt to get new transport
8360 * @xprt: the rpc_xprt to test
8361 * @data: call data for _nfs4_proc_exchange_id.
8362 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308363void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008364 void *data)
8365{
8366 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008367 struct rpc_task *task;
8368 int status;
8369
Andy Adamson04fa2c62016-09-09 09:22:29 -04008370 u32 sp4_how;
8371
8372 dprintk("--> %s try %s\n", __func__,
8373 xprt->address_strings[RPC_DISPLAY_ADDR]);
8374
8375 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8376
8377 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008378 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8379 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308380 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008381
8382 status = task->tk_status;
8383 if (status == 0)
8384 status = nfs4_detect_session_trunking(adata->clp,
8385 task->tk_msg.rpc_resp, xprt);
8386
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308387 if (status == 0)
8388 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8389
Trond Myklebust9c760d12017-07-31 18:38:50 -04008390 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008391}
8392EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8393
Trond Myklebust66245532012-05-25 17:18:09 -04008394static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008395 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008396{
8397 struct rpc_message msg = {
8398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8399 .rpc_argp = clp,
8400 .rpc_cred = cred,
8401 };
8402 int status;
8403
NeilBrown5a0c2572019-05-30 10:41:28 +10008404 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8405 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008406 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008407 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008408 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008409 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8410 return status;
8411}
8412
8413static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008414 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008415{
8416 unsigned int loop;
8417 int ret;
8418
8419 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8420 ret = _nfs4_proc_destroy_clientid(clp, cred);
8421 switch (ret) {
8422 case -NFS4ERR_DELAY:
8423 case -NFS4ERR_CLIENTID_BUSY:
8424 ssleep(1);
8425 break;
8426 default:
8427 return ret;
8428 }
8429 }
8430 return 0;
8431}
8432
8433int nfs4_destroy_clientid(struct nfs_client *clp)
8434{
NeilBrowna52458b2018-12-03 11:30:31 +11008435 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008436 int ret = 0;
8437
8438 if (clp->cl_mvops->minor_version < 1)
8439 goto out;
8440 if (clp->cl_exchange_flags == 0)
8441 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008442 if (clp->cl_preserve_clid)
8443 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008444 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008445 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008446 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008447 switch (ret) {
8448 case 0:
8449 case -NFS4ERR_STALE_CLIENTID:
8450 clp->cl_exchange_flags = 0;
8451 }
8452out:
8453 return ret;
8454}
8455
Donald Buczek0efb01b2019-07-07 21:26:08 +02008456#endif /* CONFIG_NFS_V4_1 */
8457
Andy Adamson2050f0c2009-04-01 09:22:30 -04008458struct nfs4_get_lease_time_data {
8459 struct nfs4_get_lease_time_args *args;
8460 struct nfs4_get_lease_time_res *res;
8461 struct nfs_client *clp;
8462};
8463
8464static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8465 void *calldata)
8466{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008467 struct nfs4_get_lease_time_data *data =
8468 (struct nfs4_get_lease_time_data *)calldata;
8469
8470 dprintk("--> %s\n", __func__);
8471 /* just setup sequence, do not trigger session recovery
8472 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008473 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008474 &data->args->la_seq_args,
8475 &data->res->lr_seq_res,
8476 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008477 dprintk("<-- %s\n", __func__);
8478}
8479
8480/*
8481 * Called from nfs4_state_manager thread for session setup, so don't recover
8482 * from sequence operation or clientid errors.
8483 */
8484static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8485{
8486 struct nfs4_get_lease_time_data *data =
8487 (struct nfs4_get_lease_time_data *)calldata;
8488
8489 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008490 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008491 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008492 switch (task->tk_status) {
8493 case -NFS4ERR_DELAY:
8494 case -NFS4ERR_GRACE:
8495 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8496 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8497 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008498 /* fall through */
8499 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008500 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008501 return;
8502 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008503 dprintk("<-- %s\n", __func__);
8504}
8505
Trond Myklebust17280172012-03-11 13:11:00 -04008506static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008507 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8508 .rpc_call_done = nfs4_get_lease_time_done,
8509};
8510
8511int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8512{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008513 struct nfs4_get_lease_time_args args;
8514 struct nfs4_get_lease_time_res res = {
8515 .lr_fsinfo = fsinfo,
8516 };
8517 struct nfs4_get_lease_time_data data = {
8518 .args = &args,
8519 .res = &res,
8520 .clp = clp,
8521 };
8522 struct rpc_message msg = {
8523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8524 .rpc_argp = &args,
8525 .rpc_resp = &res,
8526 };
8527 struct rpc_task_setup task_setup = {
8528 .rpc_client = clp->cl_rpcclient,
8529 .rpc_message = &msg,
8530 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008531 .callback_data = &data,
8532 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008533 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008534
Anna Schumakerfba83f32018-05-04 16:22:50 -04008535 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008536 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008537}
8538
Donald Buczek0efb01b2019-07-07 21:26:08 +02008539#ifdef CONFIG_NFS_V4_1
8540
Andy Adamsonfc931582009-04-01 09:22:31 -04008541/*
8542 * Initialize the values to be used by the client in CREATE_SESSION
8543 * If nfs4_init_session set the fore channel request and response sizes,
8544 * use them.
8545 *
8546 * Set the back channel max_resp_sz_cached to zero to force the client to
8547 * always set csa_cachethis to FALSE because the current implementation
8548 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8549 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008550static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8551 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008552{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008553 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008554 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008555 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008556
Andy Adamson18aad3d2013-06-26 12:21:49 -04008557 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8558 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8559
Andy Adamsonfc931582009-04-01 09:22:31 -04008560 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008561 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8562 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008563 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008564 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008565
8566 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008567 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008568 __func__,
8569 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008570 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008571
8572 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008573 args->bc_attrs.max_rqst_sz = max_bc_payload;
8574 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008575 args->bc_attrs.max_resp_sz_cached = 0;
8576 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008577 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008578 if (args->bc_attrs.max_reqs > max_bc_slots)
8579 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008580
8581 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8582 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8583 __func__,
8584 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8585 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8586 args->bc_attrs.max_reqs);
8587}
8588
Trond Myklebust79969dd2015-02-18 11:30:18 -08008589static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8590 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008591{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008592 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008593 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008594
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008595 if (rcvd->max_resp_sz > sent->max_resp_sz)
8596 return -EINVAL;
8597 /*
8598 * Our requested max_ops is the minimum we need; we're not
8599 * prepared to break up compounds into smaller pieces than that.
8600 * So, no point even trying to continue if the server won't
8601 * cooperate:
8602 */
8603 if (rcvd->max_ops < sent->max_ops)
8604 return -EINVAL;
8605 if (rcvd->max_reqs == 0)
8606 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008607 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8608 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008609 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008610}
8611
Trond Myklebust79969dd2015-02-18 11:30:18 -08008612static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8613 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008614{
8615 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008616 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008617
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008618 if (!(res->flags & SESSION4_BACK_CHAN))
8619 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008620 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8621 return -EINVAL;
8622 if (rcvd->max_resp_sz < sent->max_resp_sz)
8623 return -EINVAL;
8624 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8625 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008626 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008627 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008628 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008629 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008630out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008631 return 0;
8632}
Andy Adamson8d353012009-04-01 09:22:32 -04008633
Andy Adamson8d353012009-04-01 09:22:32 -04008634static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008635 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008636{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008637 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008638
Trond Myklebust79969dd2015-02-18 11:30:18 -08008639 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008640 if (ret)
8641 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008642 return nfs4_verify_back_channel_attrs(args, res);
8643}
8644
8645static void nfs4_update_session(struct nfs4_session *session,
8646 struct nfs41_create_session_res *res)
8647{
8648 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008649 /* Mark client id and session as being confirmed */
8650 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8651 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008652 session->flags = res->flags;
8653 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008654 if (res->flags & SESSION4_BACK_CHAN)
8655 memcpy(&session->bc_attrs, &res->bc_attrs,
8656 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008657}
8658
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008659static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008660 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008661{
8662 struct nfs4_session *session = clp->cl_session;
8663 struct nfs41_create_session_args args = {
8664 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008665 .clientid = clp->cl_clientid,
8666 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008667 .cb_program = NFS4_CALLBACK,
8668 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008669 struct nfs41_create_session_res res;
8670
Andy Adamsonfc931582009-04-01 09:22:31 -04008671 struct rpc_message msg = {
8672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8673 .rpc_argp = &args,
8674 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008675 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008676 };
8677 int status;
8678
Chuck Lever6b26cc82016-05-02 14:40:40 -04008679 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008680 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008681
NeilBrown5a0c2572019-05-30 10:41:28 +10008682 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8683 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008684 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008685
Trond Myklebustb519d402016-09-11 14:50:01 -04008686 switch (status) {
8687 case -NFS4ERR_STALE_CLIENTID:
8688 case -NFS4ERR_DELAY:
8689 case -ETIMEDOUT:
8690 case -EACCES:
8691 case -EAGAIN:
8692 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08008693 }
Trond Myklebustb519d402016-09-11 14:50:01 -04008694
8695 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008696 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008697 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008698 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008699 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008700 if (status)
8701 goto out;
8702 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008703 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008704out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008705 return status;
8706}
8707
8708/*
8709 * Issues a CREATE_SESSION operation to the server.
8710 * It is the responsibility of the caller to verify the session is
8711 * expired before calling this routine.
8712 */
NeilBrowna52458b2018-12-03 11:30:31 +11008713int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008714{
8715 int status;
8716 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008717 struct nfs4_session *session = clp->cl_session;
8718
8719 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8720
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008721 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008722 if (status)
8723 goto out;
8724
Andy Adamsonaacd5532011-11-09 13:58:21 -05008725 /* Init or reset the session slot tables */
8726 status = nfs4_setup_session_slot_tables(session);
8727 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008728 if (status)
8729 goto out;
8730
8731 ptr = (unsigned *)&session->sess_id.data[0];
8732 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8733 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008734out:
8735 dprintk("<-- %s\n", __func__);
8736 return status;
8737}
8738
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008739/*
8740 * Issue the over-the-wire RPC DESTROY_SESSION.
8741 * The caller must serialize access to this routine.
8742 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008743int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008744 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008745{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008746 struct rpc_message msg = {
8747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8748 .rpc_argp = session,
8749 .rpc_cred = cred,
8750 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008751 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008752
8753 dprintk("--> nfs4_proc_destroy_session\n");
8754
8755 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008756 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8757 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008758
NeilBrown5a0c2572019-05-30 10:41:28 +10008759 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8760 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008761 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008762
8763 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008764 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008765 "Session has been destroyed regardless...\n", status);
8766
8767 dprintk("<-- nfs4_proc_destroy_session\n");
8768 return status;
8769}
8770
Trond Myklebust7b38c362012-05-23 13:23:31 -04008771/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008772 * Renew the cl_session lease.
8773 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008774struct nfs4_sequence_data {
8775 struct nfs_client *clp;
8776 struct nfs4_sequence_args args;
8777 struct nfs4_sequence_res res;
8778};
8779
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008780static void nfs41_sequence_release(void *data)
8781{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008782 struct nfs4_sequence_data *calldata = data;
8783 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008784
Elena Reshetova212bf412017-10-20 12:53:38 +03008785 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008786 nfs4_schedule_state_renewal(clp);
8787 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008788 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008789}
8790
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008791static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8792{
8793 switch(task->tk_status) {
8794 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008795 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8796 return -EAGAIN;
8797 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008798 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008799 }
8800 return 0;
8801}
8802
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008803static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008804{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008805 struct nfs4_sequence_data *calldata = data;
8806 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008807
Trond Myklebust14516c32010-07-31 14:29:06 -04008808 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8809 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008810
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008811 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008812 if (task->tk_status < 0) {
8813 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008814 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008815 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008816
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008817 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8818 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008819 return;
8820 }
8821 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008822 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008823out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008824 dprintk("<-- %s\n", __func__);
8825}
8826
8827static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8828{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008829 struct nfs4_sequence_data *calldata = data;
8830 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008831 struct nfs4_sequence_args *args;
8832 struct nfs4_sequence_res *res;
8833
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008834 args = task->tk_msg.rpc_argp;
8835 res = task->tk_msg.rpc_resp;
8836
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008837 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008838}
8839
8840static const struct rpc_call_ops nfs41_sequence_ops = {
8841 .rpc_call_done = nfs41_sequence_call_done,
8842 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008843 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008844};
8845
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008846static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008847 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008848 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008849 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008850{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008851 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008852 struct rpc_message msg = {
8853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8854 .rpc_cred = cred,
8855 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008856 struct rpc_task_setup task_setup_data = {
8857 .rpc_client = clp->cl_rpcclient,
8858 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008859 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008860 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008861 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008862 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008863
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008864 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008865 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008866 goto out_err;
8867
8868 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008869 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008870 if (calldata == NULL)
8871 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008872 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008873 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008874 msg.rpc_argp = &calldata->args;
8875 msg.rpc_resp = &calldata->res;
8876 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008877 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008878
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008879 ret = rpc_run_task(&task_setup_data);
8880 if (IS_ERR(ret))
8881 goto out_err;
8882 return ret;
8883out_put_clp:
8884 nfs_put_client(clp);
8885out_err:
8886 nfs41_release_slot(slot);
8887 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008888}
8889
NeilBrowna52458b2018-12-03 11:30:31 +11008890static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008891{
8892 struct rpc_task *task;
8893 int ret = 0;
8894
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008895 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008896 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008897 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008898 if (IS_ERR(task))
8899 ret = PTR_ERR(task);
8900 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008901 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008902 dprintk("<-- %s status=%d\n", __func__, ret);
8903 return ret;
8904}
8905
NeilBrowna52458b2018-12-03 11:30:31 +11008906static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008907{
8908 struct rpc_task *task;
8909 int ret;
8910
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008911 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008912 if (IS_ERR(task)) {
8913 ret = PTR_ERR(task);
8914 goto out;
8915 }
8916 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008917 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008918 ret = task->tk_status;
8919 rpc_put_task(task);
8920out:
8921 dprintk("<-- %s status=%d\n", __func__, ret);
8922 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008923}
8924
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008925struct nfs4_reclaim_complete_data {
8926 struct nfs_client *clp;
8927 struct nfs41_reclaim_complete_args arg;
8928 struct nfs41_reclaim_complete_res res;
8929};
8930
8931static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8932{
8933 struct nfs4_reclaim_complete_data *calldata = data;
8934
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008935 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008936 &calldata->arg.seq_args,
8937 &calldata->res.seq_res,
8938 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008939}
8940
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008941static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8942{
8943 switch(task->tk_status) {
8944 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008945 wake_up_all(&clp->cl_lock_waitq);
8946 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008947 case -NFS4ERR_COMPLETE_ALREADY:
8948 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8949 break;
8950 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008951 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008952 /* fall through */
8953 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008954 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008955 case -NFS4ERR_BADSESSION:
8956 case -NFS4ERR_DEADSESSION:
8957 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008958 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008959 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008960 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008961 }
8962 return 0;
8963}
8964
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008965static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8966{
8967 struct nfs4_reclaim_complete_data *calldata = data;
8968 struct nfs_client *clp = calldata->clp;
8969 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8970
8971 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008972 if (!nfs41_sequence_done(task, res))
8973 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008974
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008975 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008976 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8977 rpc_restart_call_prepare(task);
8978 return;
8979 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008980 dprintk("<-- %s\n", __func__);
8981}
8982
8983static void nfs4_free_reclaim_complete_data(void *data)
8984{
8985 struct nfs4_reclaim_complete_data *calldata = data;
8986
8987 kfree(calldata);
8988}
8989
8990static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8991 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8992 .rpc_call_done = nfs4_reclaim_complete_done,
8993 .rpc_release = nfs4_free_reclaim_complete_data,
8994};
8995
8996/*
8997 * Issue a global reclaim complete.
8998 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008999static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009000 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009001{
9002 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009003 struct rpc_message msg = {
9004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009005 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009006 };
9007 struct rpc_task_setup task_setup_data = {
9008 .rpc_client = clp->cl_rpcclient,
9009 .rpc_message = &msg,
9010 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009011 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009012 };
9013 int status = -ENOMEM;
9014
9015 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009016 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009017 if (calldata == NULL)
9018 goto out;
9019 calldata->clp = clp;
9020 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009021
Anna Schumakerfba83f32018-05-04 16:22:50 -04009022 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009023 msg.rpc_argp = &calldata->arg;
9024 msg.rpc_resp = &calldata->res;
9025 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009026 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009027out:
9028 dprintk("<-- %s status=%d\n", __func__, status);
9029 return status;
9030}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009031
9032static void
9033nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9034{
9035 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009036 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009037
9038 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009039 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009040 &lgp->res.seq_res, task);
9041 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009042}
9043
9044static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9045{
9046 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009047
9048 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009049 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009050 dprintk("<-- %s\n", __func__);
9051}
9052
9053static int
9054nfs4_layoutget_handle_exception(struct rpc_task *task,
9055 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9056{
Trond Myklebustee314c22012-10-01 17:25:48 -07009057 struct inode *inode = lgp->args.inode;
9058 struct nfs_server *server = NFS_SERVER(inode);
9059 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009060 int nfs4err = task->tk_status;
9061 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009062 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009063
Boaz Harroshed7e5422014-01-22 20:34:54 +02009064 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009065
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009066 nfs4_sequence_free_slot(&lgp->res.seq_res);
9067
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009068 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009069 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009070 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009071
9072 /*
9073 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9074 * on the file. set tk_status to -ENODATA to tell upper layer to
9075 * retry go inband.
9076 */
9077 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009078 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009079 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009080 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009081 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9082 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9083 */
9084 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009085 status = -EOVERFLOW;
9086 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009087 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009088 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009089 * (or clients) writing to the same RAID stripe except when
9090 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009091 *
9092 * Treat it like we would RECALLCONFLICT -- we retry for a little
9093 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009094 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009095 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009096 if (lgp->args.minlength == 0) {
9097 status = -EOVERFLOW;
9098 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009099 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009100 status = -EBUSY;
9101 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009102 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009103 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009104 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009105 case -NFS4ERR_DELEG_REVOKED:
9106 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009107 case -NFS4ERR_EXPIRED:
9108 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009109 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009110 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009111 lo = NFS_I(inode)->layout;
9112 /* If the open stateid was bad, then recover it. */
9113 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009114 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009115 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009116 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009117 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009118 break;
9119 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009120
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009121 /*
9122 * Mark the bad layout state as invalid, then retry
9123 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009124 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009125 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009126 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009127 pnfs_free_lseg_list(&head);
9128 status = -EAGAIN;
9129 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009130 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009131
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009132 err = nfs4_handle_exception(server, nfs4err, exception);
9133 if (!status) {
9134 if (exception->retry)
9135 status = -EAGAIN;
9136 else
9137 status = err;
9138 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009139out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009140 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009141 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009142}
9143
Fred Isamandacb4522016-09-19 17:47:09 -04009144size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009145{
9146 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9147 return nfs_page_array_len(0, max_resp_sz);
9148}
9149
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009150static void nfs4_layoutget_release(void *calldata)
9151{
9152 struct nfs4_layoutget *lgp = calldata;
9153
9154 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009155 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009156 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009157 dprintk("<-- %s\n", __func__);
9158}
9159
9160static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9161 .rpc_call_prepare = nfs4_layoutget_prepare,
9162 .rpc_call_done = nfs4_layoutget_done,
9163 .rpc_release = nfs4_layoutget_release,
9164};
9165
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009166struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009167nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009168{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009169 struct inode *inode = lgp->args.inode;
9170 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009171 struct rpc_task *task;
9172 struct rpc_message msg = {
9173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9174 .rpc_argp = &lgp->args,
9175 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009176 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009177 };
9178 struct rpc_task_setup task_setup_data = {
9179 .rpc_client = server->client,
9180 .rpc_message = &msg,
9181 .callback_ops = &nfs4_layoutget_call_ops,
9182 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009183 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009184 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009185 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009186 struct nfs4_exception exception = {
9187 .inode = inode,
9188 .timeout = *timeout,
9189 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009190 int status = 0;
9191
9192 dprintk("--> %s\n", __func__);
9193
Peng Tao4bd5a982014-11-17 11:05:17 +08009194 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9195 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9196
Anna Schumakerfba83f32018-05-04 16:22:50 -04009197 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009198
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009199 task = rpc_run_task(&task_setup_data);
9200 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009201 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05009202 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009203 if (status != 0)
9204 goto out;
9205
Trond Myklebust18c07782019-02-13 07:55:31 -05009206 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009207 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9208 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009209 } else if (lgp->res.layoutp->len == 0) {
9210 status = -EAGAIN;
9211 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009212 } else
9213 lseg = pnfs_layout_process(lgp);
9214out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009215 trace_nfs4_layoutget(lgp->args.ctx,
9216 &lgp->args.range,
9217 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009218 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009219 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009220
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009221 rpc_put_task(task);
9222 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009223 if (status)
9224 return ERR_PTR(status);
9225 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009226}
9227
Benny Halevycbe82602011-05-22 19:52:37 +03009228static void
9229nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9230{
9231 struct nfs4_layoutreturn *lrp = calldata;
9232
9233 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009234 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009235 &lrp->args.seq_args,
9236 &lrp->res.seq_res,
9237 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009238 if (!pnfs_layout_is_valid(lrp->args.layout))
9239 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009240}
9241
9242static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9243{
9244 struct nfs4_layoutreturn *lrp = calldata;
9245 struct nfs_server *server;
9246
9247 dprintk("--> %s\n", __func__);
9248
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009249 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009250 return;
9251
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009252 /*
9253 * Was there an RPC level error? Assume the call succeeded,
9254 * and that we need to release the layout
9255 */
9256 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9257 lrp->res.lrs_present = 0;
9258 return;
9259 }
9260
Benny Halevycbe82602011-05-22 19:52:37 +03009261 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009262 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009263 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009264 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009265 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009266 lrp->args.inode))
9267 goto out_restart;
9268 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009269 default:
9270 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05009271 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009272 case 0:
9273 break;
9274 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009275 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009276 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009277 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009278 }
Benny Halevycbe82602011-05-22 19:52:37 +03009279 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009280 return;
9281out_restart:
9282 task->tk_status = 0;
9283 nfs4_sequence_free_slot(&lrp->res.seq_res);
9284 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009285}
9286
9287static void nfs4_layoutreturn_release(void *calldata)
9288{
9289 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009290 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009291
9292 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009293 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009294 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009295 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009296 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9297 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009298 pnfs_put_layout_hdr(lrp->args.layout);
9299 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009300 kfree(calldata);
9301 dprintk("<-- %s\n", __func__);
9302}
9303
9304static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9305 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9306 .rpc_call_done = nfs4_layoutreturn_done,
9307 .rpc_release = nfs4_layoutreturn_release,
9308};
9309
Peng Tao6c166052014-11-17 09:30:40 +08009310int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009311{
9312 struct rpc_task *task;
9313 struct rpc_message msg = {
9314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9315 .rpc_argp = &lrp->args,
9316 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009317 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009318 };
9319 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009320 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009321 .rpc_message = &msg,
9322 .callback_ops = &nfs4_layoutreturn_call_ops,
9323 .callback_data = lrp,
9324 };
Peng Tao6c166052014-11-17 09:30:40 +08009325 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009326
Andrew Elble99ade3c2015-12-02 09:39:51 -05009327 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9328 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9329 &task_setup_data.rpc_client, &msg);
9330
Benny Halevycbe82602011-05-22 19:52:37 +03009331 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009332 if (!sync) {
9333 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9334 if (!lrp->inode) {
9335 nfs4_layoutreturn_release(lrp);
9336 return -EAGAIN;
9337 }
9338 task_setup_data.flags |= RPC_TASK_ASYNC;
9339 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009340 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009341 task = rpc_run_task(&task_setup_data);
9342 if (IS_ERR(task))
9343 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009344 if (sync)
9345 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009346 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009347 dprintk("<-- %s status=%d\n", __func__, status);
9348 rpc_put_task(task);
9349 return status;
9350}
9351
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009352static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009353_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9354 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009355 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009356{
9357 struct nfs4_getdeviceinfo_args args = {
9358 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009359 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9360 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009361 };
9362 struct nfs4_getdeviceinfo_res res = {
9363 .pdev = pdev,
9364 };
9365 struct rpc_message msg = {
9366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9367 .rpc_argp = &args,
9368 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009369 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009370 };
9371 int status;
9372
9373 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009374 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009375 if (res.notification & ~args.notify_types)
9376 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009377 if (res.notification != args.notify_types)
9378 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009379
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009380 dprintk("<-- %s status=%d\n", __func__, status);
9381
9382 return status;
9383}
9384
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009385int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9386 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009387 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009388{
9389 struct nfs4_exception exception = { };
9390 int err;
9391
9392 do {
9393 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009394 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009395 &exception);
9396 } while (exception.retry);
9397 return err;
9398}
9399EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9400
Andy Adamson863a3c62011-03-23 13:27:54 +00009401static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9402{
9403 struct nfs4_layoutcommit_data *data = calldata;
9404 struct nfs_server *server = NFS_SERVER(data->args.inode);
9405
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009406 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009407 &data->args.seq_args,
9408 &data->res.seq_res,
9409 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009410}
9411
9412static void
9413nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9414{
9415 struct nfs4_layoutcommit_data *data = calldata;
9416 struct nfs_server *server = NFS_SERVER(data->args.inode);
9417
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009418 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009419 return;
9420
9421 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009422 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9423 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9424 case -NFS4ERR_BADLAYOUT: /* no layout */
9425 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009426 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009427 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009428 break;
9429 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009430 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009431 rpc_restart_call_prepare(task);
9432 return;
9433 }
9434 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009435}
9436
9437static void nfs4_layoutcommit_release(void *calldata)
9438{
9439 struct nfs4_layoutcommit_data *data = calldata;
9440
Andy Adamsondb29c082011-07-30 20:52:38 -04009441 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009442 nfs_post_op_update_inode_force_wcc(data->args.inode,
9443 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009444 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009445 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009446 kfree(data);
9447}
9448
9449static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9450 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9451 .rpc_call_done = nfs4_layoutcommit_done,
9452 .rpc_release = nfs4_layoutcommit_release,
9453};
9454
9455int
Andy Adamsonef311532011-03-12 02:58:10 -05009456nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009457{
9458 struct rpc_message msg = {
9459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9460 .rpc_argp = &data->args,
9461 .rpc_resp = &data->res,
9462 .rpc_cred = data->cred,
9463 };
9464 struct rpc_task_setup task_setup_data = {
9465 .task = &data->task,
9466 .rpc_client = NFS_CLIENT(data->args.inode),
9467 .rpc_message = &msg,
9468 .callback_ops = &nfs4_layoutcommit_ops,
9469 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009470 };
9471 struct rpc_task *task;
9472 int status = 0;
9473
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009474 dprintk("NFS: initiating layoutcommit call. sync %d "
9475 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009476 data->args.lastbytewritten,
9477 data->args.inode->i_ino);
9478
Trond Myklebust472e2592015-02-05 16:50:30 -05009479 if (!sync) {
9480 data->inode = nfs_igrab_and_active(data->args.inode);
9481 if (data->inode == NULL) {
9482 nfs4_layoutcommit_release(data);
9483 return -EAGAIN;
9484 }
9485 task_setup_data.flags = RPC_TASK_ASYNC;
9486 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009487 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009488 task = rpc_run_task(&task_setup_data);
9489 if (IS_ERR(task))
9490 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009491 if (sync)
9492 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009493 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009494 dprintk("%s: status %d\n", __func__, status);
9495 rpc_put_task(task);
9496 return status;
9497}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009498
Trond Myklebust302fad72019-02-18 13:32:38 -05009499/*
Andy Adamson97431202013-08-08 10:57:56 -04009500 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9501 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9502 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009503static int
9504_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009505 struct nfs_fsinfo *info,
9506 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009507{
9508 struct nfs41_secinfo_no_name_args args = {
9509 .style = SECINFO_STYLE_CURRENT_FH,
9510 };
9511 struct nfs4_secinfo_res res = {
9512 .flavors = flavors,
9513 };
9514 struct rpc_message msg = {
9515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9516 .rpc_argp = &args,
9517 .rpc_resp = &res,
9518 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009519 struct rpc_clnt *clnt = server->client;
Anna Schumakercc15e242019-08-14 16:22:31 -04009520 struct nfs4_call_sync_data data = {
9521 .seq_server = server,
9522 .seq_args = &args.seq_args,
9523 .seq_res = &res.seq_res,
9524 };
9525 struct rpc_task_setup task_setup = {
9526 .rpc_client = server->client,
9527 .rpc_message = &msg,
9528 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9529 .callback_data = &data,
9530 .flags = RPC_TASK_NO_ROUND_ROBIN,
9531 };
NeilBrowna52458b2018-12-03 11:30:31 +11009532 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009533 int status;
9534
9535 if (use_integrity) {
9536 clnt = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009537 task_setup.rpc_client = clnt;
9538
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009539 cred = nfs4_get_clid_cred(server->nfs_client);
9540 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009541 }
9542
9543 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009544 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9545 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009546 dprintk("<-- %s status=%d\n", __func__, status);
9547
NeilBrowna52458b2018-12-03 11:30:31 +11009548 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009549
9550 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009551}
9552
9553static int
9554nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9555 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9556{
Trond Myklebust0688e642019-04-07 13:59:09 -04009557 struct nfs4_exception exception = {
9558 .interruptible = true,
9559 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009560 int err;
9561 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009562 /* first try using integrity protection */
9563 err = -NFS4ERR_WRONGSEC;
9564
9565 /* try to use integrity protection with machine cred */
9566 if (_nfs4_is_integrity_protected(server->nfs_client))
9567 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9568 flavors, true);
9569
9570 /*
9571 * if unable to use integrity protection, or SECINFO with
9572 * integrity protection returns NFS4ERR_WRONGSEC (which is
9573 * disallowed by spec, but exists in deployed servers) use
9574 * the current filesystem's rpc_client and the user cred.
9575 */
9576 if (err == -NFS4ERR_WRONGSEC)
9577 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9578 flavors, false);
9579
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009580 switch (err) {
9581 case 0:
9582 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009583 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009584 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009585 default:
9586 err = nfs4_handle_exception(server, err, &exception);
9587 }
9588 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009589out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009590 return err;
9591}
9592
9593static int
9594nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9595 struct nfs_fsinfo *info)
9596{
9597 int err;
9598 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009599 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009600 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009601 struct nfs4_secinfo4 *secinfo;
9602 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009603
9604 page = alloc_page(GFP_KERNEL);
9605 if (!page) {
9606 err = -ENOMEM;
9607 goto out;
9608 }
9609
9610 flavors = page_address(page);
9611 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9612
9613 /*
9614 * Fall back on "guess and check" method if
9615 * the server doesn't support SECINFO_NO_NAME
9616 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009617 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009618 err = nfs4_find_root_sec(server, fhandle, info);
9619 goto out_freepage;
9620 }
9621 if (err)
9622 goto out_freepage;
9623
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009624 for (i = 0; i < flavors->num_flavors; i++) {
9625 secinfo = &flavors->flavors[i];
9626
9627 switch (secinfo->flavor) {
9628 case RPC_AUTH_NULL:
9629 case RPC_AUTH_UNIX:
9630 case RPC_AUTH_GSS:
9631 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9632 &secinfo->flavor_info);
9633 break;
9634 default:
9635 flavor = RPC_AUTH_MAXFLAVOR;
9636 break;
9637 }
9638
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009639 if (!nfs_auth_info_match(&server->auth_info, flavor))
9640 flavor = RPC_AUTH_MAXFLAVOR;
9641
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009642 if (flavor != RPC_AUTH_MAXFLAVOR) {
9643 err = nfs4_lookup_root_sec(server, fhandle,
9644 info, flavor);
9645 if (!err)
9646 break;
9647 }
9648 }
9649
9650 if (flavor == RPC_AUTH_MAXFLAVOR)
9651 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009652
9653out_freepage:
9654 put_page(page);
9655 if (err == -EACCES)
9656 return -EPERM;
9657out:
9658 return err;
9659}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009660
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009661static int _nfs41_test_stateid(struct nfs_server *server,
9662 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009663 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009664{
9665 int status;
9666 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009667 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009668 };
9669 struct nfs41_test_stateid_res res;
9670 struct rpc_message msg = {
9671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9672 .rpc_argp = &args,
9673 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009674 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009675 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009676 struct rpc_clnt *rpc_client = server->client;
9677
9678 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9679 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009680
Chuck Lever38527b12012-07-11 16:30:23 -04009681 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009682 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009683 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009684 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009685 if (status != NFS_OK) {
9686 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009687 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009688 }
9689 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009690 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009691}
9692
Trond Myklebust43912bb2016-09-22 13:38:56 -04009693static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9694 int err, struct nfs4_exception *exception)
9695{
9696 exception->retry = 0;
9697 switch(err) {
9698 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009699 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009700 nfs4_handle_exception(server, err, exception);
9701 break;
9702 case -NFS4ERR_BADSESSION:
9703 case -NFS4ERR_BADSLOT:
9704 case -NFS4ERR_BAD_HIGH_SLOT:
9705 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9706 case -NFS4ERR_DEADSESSION:
9707 nfs4_do_handle_exception(server, err, exception);
9708 }
9709}
9710
Chuck Lever38527b12012-07-11 16:30:23 -04009711/**
9712 * nfs41_test_stateid - perform a TEST_STATEID operation
9713 *
9714 * @server: server / transport on which to perform the operation
9715 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009716 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009717 *
9718 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9719 * Otherwise a negative NFS4ERR value is returned if the operation
9720 * failed or the state ID is not currently valid.
9721 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009722static int nfs41_test_stateid(struct nfs_server *server,
9723 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009724 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009725{
Trond Myklebust0688e642019-04-07 13:59:09 -04009726 struct nfs4_exception exception = {
9727 .interruptible = true,
9728 };
Bryan Schumaker7d974792011-06-02 14:59:08 -04009729 int err;
9730 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009731 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009732 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009733 } while (exception.retry);
9734 return err;
9735}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009736
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009737struct nfs_free_stateid_data {
9738 struct nfs_server *server;
9739 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009740 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009741};
9742
9743static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9744{
9745 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009746 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009747 &data->args.seq_args,
9748 &data->res.seq_res,
9749 task);
9750}
9751
9752static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9753{
9754 struct nfs_free_stateid_data *data = calldata;
9755
9756 nfs41_sequence_done(task, &data->res.seq_res);
9757
9758 switch (task->tk_status) {
9759 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009760 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009761 rpc_restart_call_prepare(task);
9762 }
9763}
9764
9765static void nfs41_free_stateid_release(void *calldata)
9766{
9767 kfree(calldata);
9768}
9769
Trond Myklebust17f26b12013-08-21 15:48:42 -04009770static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009771 .rpc_call_prepare = nfs41_free_stateid_prepare,
9772 .rpc_call_done = nfs41_free_stateid_done,
9773 .rpc_release = nfs41_free_stateid_release,
9774};
9775
Anna Schumaker2f261022018-05-15 13:03:39 -04009776/**
9777 * nfs41_free_stateid - perform a FREE_STATEID operation
9778 *
9779 * @server: server / transport on which to perform the operation
9780 * @stateid: state ID to release
9781 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009782 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009783 *
9784 * Note: this function is always asynchronous.
9785 */
9786static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009787 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009788 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009789 bool privileged)
9790{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009791 struct rpc_message msg = {
9792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009793 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009794 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009795 struct rpc_task_setup task_setup = {
9796 .rpc_client = server->client,
9797 .rpc_message = &msg,
9798 .callback_ops = &nfs41_free_stateid_ops,
9799 .flags = RPC_TASK_ASYNC,
9800 };
9801 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009802 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009803
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009804 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9805 &task_setup.rpc_client, &msg);
9806
Chuck Lever38527b12012-07-11 16:30:23 -04009807 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009808 data = kmalloc(sizeof(*data), GFP_NOFS);
9809 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009810 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009811 data->server = server;
9812 nfs4_stateid_copy(&data->args.stateid, stateid);
9813
9814 task_setup.callback_data = data;
9815
9816 msg.rpc_argp = &data->args;
9817 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009818 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009819 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009820 if (IS_ERR(task))
9821 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009822 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009823 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009824}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009825
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009826static void
9827nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009828{
NeilBrowna52458b2018-12-03 11:30:31 +11009829 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009830
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009831 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009832 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009833}
9834
Trond Myklebust36281ca2012-03-04 18:13:56 -05009835static bool nfs41_match_stateid(const nfs4_stateid *s1,
9836 const nfs4_stateid *s2)
9837{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009838 if (s1->type != s2->type)
9839 return false;
9840
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009841 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009842 return false;
9843
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009844 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009845 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009846
Anna Schumaker045c5512017-01-11 16:59:48 -05009847 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009848}
9849
Andy Adamson557134a2009-04-01 09:21:53 -04009850#endif /* CONFIG_NFS_V4_1 */
9851
Trond Myklebust36281ca2012-03-04 18:13:56 -05009852static bool nfs4_match_stateid(const nfs4_stateid *s1,
9853 const nfs4_stateid *s2)
9854{
Trond Myklebustf597c532012-03-04 18:13:56 -05009855 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009856}
9857
9858
Trond Myklebust17280172012-03-11 13:11:00 -04009859static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009860 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009861 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009862 .recover_open = nfs4_open_reclaim,
9863 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009864 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009865 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009866};
9867
Andy Adamson591d71c2009-04-01 09:22:47 -04009868#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009869static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009870 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9871 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9872 .recover_open = nfs4_open_reclaim,
9873 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009874 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009875 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009876 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009877};
9878#endif /* CONFIG_NFS_V4_1 */
9879
Trond Myklebust17280172012-03-11 13:11:00 -04009880static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009881 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009882 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009883 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009884 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009885 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886};
9887
Andy Adamson591d71c2009-04-01 09:22:47 -04009888#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009889static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009890 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9891 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009892 .recover_open = nfs41_open_expired,
9893 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009894 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009895};
9896#endif /* CONFIG_NFS_V4_1 */
9897
Trond Myklebust17280172012-03-11 13:11:00 -04009898static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009899 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009900 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009901 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009902};
9903
9904#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009905static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009906 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009907 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009908 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009909};
9910#endif
9911
Chuck Leverec011fe2013-10-17 14:12:39 -04009912static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009913 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009914 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009915};
9916
9917#if defined(CONFIG_NFS_V4_1)
9918static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009919 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009920 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009921};
9922#endif /* CONFIG_NFS_V4_1 */
9923
Trond Myklebust97dc1352010-06-16 09:52:26 -04009924static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9925 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009926 .init_caps = NFS_CAP_READDIRPLUS
9927 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009928 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009929 .init_client = nfs40_init_client,
9930 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009931 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009932 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009933 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009934 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009935 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009936 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009937 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9938 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9939 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009940 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009941};
9942
9943#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009944static struct nfs_seqid *
9945nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9946{
9947 return NULL;
9948}
9949
Trond Myklebust97dc1352010-06-16 09:52:26 -04009950static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9951 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009952 .init_caps = NFS_CAP_READDIRPLUS
9953 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009954 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009955 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009956 | NFS_CAP_ATOMIC_OPEN_V1
9957 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009958 .init_client = nfs41_init_client,
9959 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009960 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009961 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009962 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009963 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009964 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009965 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009966 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009967 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9968 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9969 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009970 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009971};
9972#endif
9973
Steve Dickson42c2c422013-05-22 12:50:38 -04009974#if defined(CONFIG_NFS_V4_2)
9975static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9976 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009977 .init_caps = NFS_CAP_READDIRPLUS
9978 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009979 | NFS_CAP_POSIX_LOCK
9980 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009981 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009982 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009983 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009984 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009985 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -04009986 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009987 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009988 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009989 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -05009990 | NFS_CAP_CLONE
9991 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009992 .init_client = nfs41_init_client,
9993 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009994 .match_stateid = nfs41_match_stateid,
9995 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009996 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009997 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009998 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009999 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010000 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010001 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10002 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10003 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010004 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010005};
10006#endif
10007
Trond Myklebust97dc1352010-06-16 09:52:26 -040010008const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10009 [0] = &nfs_v4_0_minor_ops,
10010#if defined(CONFIG_NFS_V4_1)
10011 [1] = &nfs_v4_1_minor_ops,
10012#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010013#if defined(CONFIG_NFS_V4_2)
10014 [2] = &nfs_v4_2_minor_ops,
10015#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010016};
10017
Trond Myklebust13997822016-07-24 17:10:52 -040010018static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010019{
10020 ssize_t error, error2;
10021
10022 error = generic_listxattr(dentry, list, size);
10023 if (error < 0)
10024 return error;
10025 if (list) {
10026 list += error;
10027 size -= error;
10028 }
10029
10030 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10031 if (error2 < 0)
10032 return error2;
10033 return error + error2;
10034}
10035
Trond Myklebust17f26b12013-08-21 15:48:42 -040010036static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010037 .create = nfs_create,
10038 .lookup = nfs_lookup,
10039 .atomic_open = nfs_atomic_open,
10040 .link = nfs_link,
10041 .unlink = nfs_unlink,
10042 .symlink = nfs_symlink,
10043 .mkdir = nfs_mkdir,
10044 .rmdir = nfs_rmdir,
10045 .mknod = nfs_mknod,
10046 .rename = nfs_rename,
10047 .permission = nfs_permission,
10048 .getattr = nfs_getattr,
10049 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010050 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010051};
10052
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010053static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010054 .permission = nfs_permission,
10055 .getattr = nfs_getattr,
10056 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010057 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010058};
10059
David Howells509de812006-08-22 20:06:11 -040010060const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010061 .version = 4, /* protocol version */
10062 .dentry_ops = &nfs4_dentry_operations,
10063 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010064 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010065 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010066 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010067 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010068 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010069 .getattr = nfs4_proc_getattr,
10070 .setattr = nfs4_proc_setattr,
10071 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010072 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010073 .access = nfs4_proc_access,
10074 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075 .create = nfs4_proc_create,
10076 .remove = nfs4_proc_remove,
10077 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010078 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010079 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010080 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010081 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010082 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010083 .link = nfs4_proc_link,
10084 .symlink = nfs4_proc_symlink,
10085 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010086 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010087 .readdir = nfs4_proc_readdir,
10088 .mknod = nfs4_proc_mknod,
10089 .statfs = nfs4_proc_statfs,
10090 .fsinfo = nfs4_proc_fsinfo,
10091 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010092 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010093 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010094 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010095 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010096 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010098 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010100 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010101 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010103 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010104 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010105 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010106 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010107 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010108 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010109 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010110 .create_server = nfs4_create_server,
10111 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112};
10113
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010114static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010115 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010116 .list = nfs4_xattr_list_nfs4_acl,
10117 .get = nfs4_xattr_get_nfs4_acl,
10118 .set = nfs4_xattr_set_nfs4_acl,
10119};
10120
10121const struct xattr_handler *nfs4_xattr_handlers[] = {
10122 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010123#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10124 &nfs4_xattr_nfs4_label_handler,
10125#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010126 NULL
10127};
10128
Linus Torvalds1da177e2005-04-16 15:20:36 -070010129/*
10130 * Local variables:
10131 * c-basic-offset: 8
10132 * End:
10133 */