blob: d1a7facfa9268092eb90ff932c7f54c85be4732e [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 Myklebust2ced46c2007-07-03 23:48:13 -04001100static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001101 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001103 struct nfs4_sequence_args *args,
1104 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001105{
Chuck Lever9915ea72013-08-09 12:48:27 -04001106 struct nfs_client *clp = server->nfs_client;
1107 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001108 .seq_server = server,
1109 .seq_args = args,
1110 .seq_res = res,
1111 };
1112 struct rpc_task_setup task_setup = {
1113 .rpc_client = clnt,
1114 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001115 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001116 .callback_data = &data
1117 };
1118
Anna Schumaker48c05852019-08-14 15:27:00 -04001119 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001120}
1121
Bryan Schumaker7c513052011-03-24 17:12:24 +00001122int nfs4_call_sync(struct rpc_clnt *clnt,
1123 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001124 struct rpc_message *msg,
1125 struct nfs4_sequence_args *args,
1126 struct nfs4_sequence_res *res,
1127 int cache_reply)
1128{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001129 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001130 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001131}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Trond Myklebust3c591172018-07-31 15:54:10 -04001133static void
1134nfs4_inc_nlink_locked(struct inode *inode)
1135{
1136 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1137 inc_nlink(inode);
1138}
1139
1140static void
1141nfs4_dec_nlink_locked(struct inode *inode)
1142{
1143 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1144 drop_nlink(inode);
1145}
1146
1147static void
1148update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001149 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 struct nfs_inode *nfsi = NFS_I(dir);
1152
Trond Myklebust16e14372018-03-20 16:53:31 -04001153 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1154 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001155 | NFS_INO_INVALID_DATA
1156 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001157 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001158 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1159 nfsi->attrtimeo_timestamp = jiffies;
1160 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001162 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001163 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1164 NFS_INO_INVALID_ACL;
1165 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001166 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001167 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001168 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001169 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001170 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001171}
1172
1173static void
1174update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001175 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001176{
1177 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001178 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 spin_unlock(&dir->i_lock);
1180}
1181
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001182struct nfs4_open_createattrs {
1183 struct nfs4_label *label;
1184 struct iattr *sattr;
1185 const __u32 verf[2];
1186};
1187
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001188static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1189 int err, struct nfs4_exception *exception)
1190{
1191 if (err != -EINVAL)
1192 return false;
1193 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1194 return false;
1195 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1196 exception->retry = 1;
1197 return true;
1198}
1199
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001200static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1201{
1202 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1203}
1204
1205static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1206{
1207 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1208
1209 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1210}
1211
Trond Myklebust6ae37332015-01-30 14:21:14 -05001212static u32
1213nfs4_map_atomic_open_share(struct nfs_server *server,
1214 fmode_t fmode, int openflags)
1215{
1216 u32 res = 0;
1217
1218 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1219 case FMODE_READ:
1220 res = NFS4_SHARE_ACCESS_READ;
1221 break;
1222 case FMODE_WRITE:
1223 res = NFS4_SHARE_ACCESS_WRITE;
1224 break;
1225 case FMODE_READ|FMODE_WRITE:
1226 res = NFS4_SHARE_ACCESS_BOTH;
1227 }
1228 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1229 goto out;
1230 /* Want no delegation if we're using O_DIRECT */
1231 if (openflags & O_DIRECT)
1232 res |= NFS4_SHARE_WANT_NO_DELEG;
1233out:
1234 return res;
1235}
1236
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001237static enum open_claim_type4
1238nfs4_map_atomic_open_claim(struct nfs_server *server,
1239 enum open_claim_type4 claim)
1240{
1241 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1242 return claim;
1243 switch (claim) {
1244 default:
1245 return claim;
1246 case NFS4_OPEN_CLAIM_FH:
1247 return NFS4_OPEN_CLAIM_NULL;
1248 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1249 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1250 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1251 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1252 }
1253}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001256{
1257 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001258 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001259 p->o_res.seqid = p->o_arg.seqid;
1260 p->c_res.seqid = p->c_arg.seqid;
1261 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001262 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001263 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001264 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265}
1266
Al Viro82a2c1b2011-06-22 18:30:55 -04001267static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001269 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001270 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001271 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001272{
Al Viro82a2c1b2011-06-22 18:30:55 -04001273 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001274 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001275 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001276 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001277 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278 struct nfs4_opendata *p;
1279
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001280 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001281 if (p == NULL)
1282 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001283
1284 p->f_label = nfs4_label_alloc(server, gfp_mask);
1285 if (IS_ERR(p->f_label))
1286 goto err_free_p;
1287
Kinglong Meea49c2692015-07-27 15:31:38 +08001288 p->a_label = nfs4_label_alloc(server, gfp_mask);
1289 if (IS_ERR(p->a_label))
1290 goto err_free_f;
1291
Trond Myklebust63f5f792015-01-23 19:19:25 -05001292 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1293 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001294 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001295 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001296 nfs_sb_active(dentry->d_sb);
1297 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001298 p->dir = parent;
1299 p->owner = sp;
1300 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001301 p->o_arg.open_flags = flags;
1302 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001303 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001304 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1305 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001306 if (flags & O_CREAT) {
1307 p->o_arg.umask = current_umask();
1308 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1309 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1310 p->o_arg.u.attrs = &p->attrs;
1311 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1312
1313 memcpy(p->o_arg.u.verifier.data, c->verf,
1314 sizeof(p->o_arg.u.verifier.data));
1315 }
1316 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001317 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1318 * will return permission denied for all bits until close */
1319 if (!(flags & O_EXCL)) {
1320 /* ask server to check for all possible rights as results
1321 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001322 switch (p->o_arg.claim) {
1323 default:
1324 break;
1325 case NFS4_OPEN_CLAIM_NULL:
1326 case NFS4_OPEN_CLAIM_FH:
1327 p->o_arg.access = NFS4_ACCESS_READ |
1328 NFS4_ACCESS_MODIFY |
1329 NFS4_ACCESS_EXTEND |
1330 NFS4_ACCESS_EXECUTE;
1331 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001332 }
David Howells7539bba2006-08-22 20:06:09 -04001333 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001334 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1335 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001336 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001337 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001338 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001339 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001340 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001341 case NFS4_OPEN_CLAIM_NULL:
1342 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1343 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1344 p->o_arg.fh = NFS_FH(dir);
1345 break;
1346 case NFS4_OPEN_CLAIM_PREVIOUS:
1347 case NFS4_OPEN_CLAIM_FH:
1348 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1349 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001350 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001351 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001352 p->c_arg.fh = &p->o_res.fh;
1353 p->c_arg.stateid = &p->o_res.stateid;
1354 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001355 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001356 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001357 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001358
1359err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001360 nfs4_label_free(p->a_label);
1361err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001362 nfs4_label_free(p->f_label);
1363err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001364 kfree(p);
1365err:
1366 dput(parent);
1367 return NULL;
1368}
1369
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001370static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001371{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001372 struct nfs4_opendata *p = container_of(kref,
1373 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001374 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001375
Fred Isaman30ae2412016-10-18 13:39:51 -04001376 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001377 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001378 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001379 if (p->state != NULL)
1380 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001381 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001382
Kinglong Meea49c2692015-07-27 15:31:38 +08001383 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001384 nfs4_label_free(p->f_label);
1385
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001386 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001387 dput(p->dentry);
1388 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001389 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001390 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001391 kfree(p);
1392}
1393
1394static void nfs4_opendata_put(struct nfs4_opendata *p)
1395{
1396 if (p != NULL)
1397 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001398}
1399
Trond Myklebust24311f82015-09-20 10:50:17 -04001400static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1401 fmode_t fmode)
1402{
1403 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1404 case FMODE_READ|FMODE_WRITE:
1405 return state->n_rdwr != 0;
1406 case FMODE_WRITE:
1407 return state->n_wronly != 0;
1408 case FMODE_READ:
1409 return state->n_rdonly != 0;
1410 }
1411 WARN_ON_ONCE(1);
1412 return false;
1413}
1414
Trond Myklebustbe189f72018-09-27 17:12:33 -04001415static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1416 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001417{
1418 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001419
Trond Myklebust536e43d2012-01-17 22:04:26 -05001420 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001421 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001422 switch (claim) {
1423 case NFS4_OPEN_CLAIM_NULL:
1424 case NFS4_OPEN_CLAIM_FH:
1425 goto out;
1426 default:
1427 break;
1428 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001429 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001430 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001431 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1432 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001433 break;
1434 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001435 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1436 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001437 break;
1438 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001439 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1440 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001441 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001442out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001443 return ret;
1444}
1445
Trond Myklebust2a606182015-08-19 22:30:00 -05001446static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1447 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001448{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001449 if (delegation == NULL)
1450 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001451 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001452 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001453 switch (claim) {
1454 case NFS4_OPEN_CLAIM_NULL:
1455 case NFS4_OPEN_CLAIM_FH:
1456 break;
1457 case NFS4_OPEN_CLAIM_PREVIOUS:
1458 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1459 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001460 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001461 default:
1462 return 0;
1463 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001464 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001465 return 1;
1466}
1467
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001468static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001469{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001470 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001471 case FMODE_WRITE:
1472 state->n_wronly++;
1473 break;
1474 case FMODE_READ:
1475 state->n_rdonly++;
1476 break;
1477 case FMODE_READ|FMODE_WRITE:
1478 state->n_rdwr++;
1479 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001480 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001481}
1482
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001483#ifdef CONFIG_NFS_V4_1
1484static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1485{
1486 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1487 return true;
1488 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1489 return true;
1490 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1491 return true;
1492 return false;
1493}
1494#endif /* CONFIG_NFS_V4_1 */
1495
Trond Myklebustc9399f22017-11-06 15:28:01 -05001496static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1497{
1498 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1499 wake_up_all(&state->waitq);
1500}
1501
1502static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1503 const nfs4_stateid *stateid)
1504{
1505 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1506 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1507
1508 if (stateid_seqid == state_seqid + 1U ||
1509 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1510 nfs_state_log_update_open_stateid(state);
1511 else
1512 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1513}
1514
Trond Myklebust4f14c192014-02-12 19:15:06 -05001515static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001516{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001517 struct nfs_client *clp = state->owner->so_server->nfs_client;
1518 bool need_recover = false;
1519
1520 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1521 need_recover = true;
1522 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1523 need_recover = true;
1524 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1525 need_recover = true;
1526 if (need_recover)
1527 nfs4_state_mark_reclaim_nograce(clp, state);
1528}
1529
Trond Myklebustc9399f22017-11-06 15:28:01 -05001530/*
1531 * Check for whether or not the caller may update the open stateid
1532 * to the value passed in by stateid.
1533 *
1534 * Note: This function relies heavily on the server implementing
1535 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1536 * correctly.
1537 * i.e. The stateid seqids have to be initialised to 1, and
1538 * are then incremented on every state transition.
1539 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001540static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001541 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001542{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001543 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1544 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1545 if (stateid->seqid == cpu_to_be32(1))
1546 nfs_state_log_update_open_stateid(state);
1547 else
1548 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001549 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001550 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001551
1552 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1553 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001554 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001555 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001556 return false;
1557}
1558
Trond Myklebustf95549c2015-01-23 18:06:09 -05001559static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1560{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001561 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1562 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001563 if (state->n_wronly)
1564 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1565 if (state->n_rdonly)
1566 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1567 if (state->n_rdwr)
1568 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001569 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001570}
1571
Trond Myklebust226056c2014-02-11 10:41:07 -05001572static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1573 nfs4_stateid *stateid, fmode_t fmode)
1574{
1575 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1576 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1577 case FMODE_WRITE:
1578 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1579 break;
1580 case FMODE_READ:
1581 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1582 break;
1583 case 0:
1584 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1585 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1586 clear_bit(NFS_OPEN_STATE, &state->flags);
1587 }
1588 if (stateid == NULL)
1589 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001590 /* Handle OPEN+OPEN_DOWNGRADE races */
1591 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1592 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001593 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001594 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001595 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001596 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001597 nfs4_stateid_copy(&state->stateid, stateid);
1598 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001599 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001600out:
1601 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001602}
1603
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001604static void nfs_clear_open_stateid(struct nfs4_state *state,
1605 nfs4_stateid *arg_stateid,
1606 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001607{
1608 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001609 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1610 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1611 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001612 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001613 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1614 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001615}
1616
Trond Myklebust1393d962016-09-22 13:39:13 -04001617static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001618 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001619 __must_hold(&state->owner->so_lock)
1620 __must_hold(&state->seqlock)
1621 __must_hold(RCU)
1622
Trond Myklebust003707c2007-07-05 18:07:55 -04001623{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001624 DEFINE_WAIT(wait);
1625 int status = 0;
1626 for (;;) {
1627
1628 if (!nfs_need_update_open_stateid(state, stateid))
1629 return;
1630 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001631 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001632 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001633 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001634 /* Rely on seqids for serialisation with NFSv4.0 */
1635 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1636 break;
1637
1638 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1639 /*
1640 * Ensure we process the state changes in the same order
1641 * in which the server processed them by delaying the
1642 * update of the stateid until we are in sequence.
1643 */
1644 write_sequnlock(&state->seqlock);
1645 spin_unlock(&state->owner->so_lock);
1646 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001647 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001648 if (!signal_pending(current)) {
1649 if (schedule_timeout(5*HZ) == 0)
1650 status = -EAGAIN;
1651 else
1652 status = 0;
1653 } else
1654 status = -EINTR;
1655 finish_wait(&state->waitq, &wait);
1656 rcu_read_lock();
1657 spin_lock(&state->owner->so_lock);
1658 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001659 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001660
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001661 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1662 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001663 nfs4_stateid_copy(freeme, &state->open_stateid);
1664 nfs_test_and_clear_all_open_stateid(state);
1665 }
1666
Trond Myklebuste999e802014-02-10 18:20:47 -05001667 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1668 nfs4_stateid_copy(&state->stateid, stateid);
1669 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001670 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001671 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001672}
1673
Trond Myklebustc9399f22017-11-06 15:28:01 -05001674static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001675 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001676 fmode_t fmode,
1677 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001679 /*
1680 * Protect the call to nfs4_state_set_mode_locked and
1681 * serialise the stateid update
1682 */
1683 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001684 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1685 switch (fmode) {
1686 case FMODE_READ:
1687 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1688 break;
1689 case FMODE_WRITE:
1690 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1691 break;
1692 case FMODE_READ|FMODE_WRITE:
1693 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001694 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001695 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001696 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001697}
1698
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001699static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1700{
1701 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1702 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1703 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1704 clear_bit(NFS_OPEN_STATE, &state->flags);
1705}
1706
Trond Myklebustc9399f22017-11-06 15:28:01 -05001707static void nfs_state_set_delegation(struct nfs4_state *state,
1708 const nfs4_stateid *deleg_stateid,
1709 fmode_t fmode)
1710{
1711 /*
1712 * Protect the call to nfs4_state_set_mode_locked and
1713 * serialise the stateid update
1714 */
1715 write_seqlock(&state->seqlock);
1716 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1717 set_bit(NFS_DELEGATED_STATE, &state->flags);
1718 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001721static void nfs_state_clear_delegation(struct nfs4_state *state)
1722{
1723 write_seqlock(&state->seqlock);
1724 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1725 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1726 write_sequnlock(&state->seqlock);
1727}
1728
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001729int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001730 const nfs4_stateid *open_stateid,
1731 const nfs4_stateid *delegation,
1732 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001733{
Trond Myklebust1393d962016-09-22 13:39:13 -04001734 struct nfs_server *server = NFS_SERVER(state->inode);
1735 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001736 struct nfs_inode *nfsi = NFS_I(state->inode);
1737 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001738 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001739 int ret = 0;
1740
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001741 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001742
1743 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001744 spin_lock(&state->owner->so_lock);
1745 if (open_stateid != NULL) {
1746 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1747 ret = 1;
1748 }
1749
Trond Myklebust333ac782019-10-22 12:12:17 -04001750 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001751 if (deleg_cur == NULL)
1752 goto no_delegation;
1753
1754 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001755 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001756 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001757 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001758 goto no_delegation_unlock;
1759
1760 if (delegation == NULL)
1761 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001762 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001763 goto no_delegation_unlock;
1764
Trond Myklebustb7391f42008-12-23 15:21:52 -05001765 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001766 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001767 ret = 1;
1768no_delegation_unlock:
1769 spin_unlock(&deleg_cur->lock);
1770no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001771 if (ret)
1772 update_open_stateflags(state, fmode);
1773 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001774 rcu_read_unlock();
1775
Trond Myklebust4f14c192014-02-12 19:15:06 -05001776 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001777 nfs4_schedule_state_manager(clp);
1778 if (freeme.type != 0)
1779 nfs4_test_and_free_stateid(server, &freeme,
1780 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001781
1782 return ret;
1783}
1784
Trond Myklebust39071e62015-01-24 15:07:56 -05001785static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1786 const nfs4_stateid *stateid)
1787{
1788 struct nfs4_state *state = lsp->ls_state;
1789 bool ret = false;
1790
1791 spin_lock(&state->state_lock);
1792 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1793 goto out_noupdate;
1794 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1795 goto out_noupdate;
1796 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1797 ret = true;
1798out_noupdate:
1799 spin_unlock(&state->state_lock);
1800 return ret;
1801}
Trond Myklebust34310432008-12-23 15:21:38 -05001802
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001803static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001804{
1805 struct nfs_delegation *delegation;
1806
Trond Myklebustf5086242018-03-20 16:43:13 -04001807 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001808 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001809 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001810 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001811 rcu_read_unlock();
1812 return;
1813 }
1814 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001815 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001816}
1817
Trond Myklebust6ee41262007-07-08 14:11:36 -04001818static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001819{
1820 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001821 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001822 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001823 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001824 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001825 nfs4_stateid stateid;
1826 int ret = -EAGAIN;
1827
Trond Myklebustaac00a82007-07-05 19:02:21 -04001828 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001829 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001830 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001831 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001832 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001833 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001834 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001835 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001836 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001837 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001838 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001839 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001840 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001841 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001842 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001843 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001844 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001845 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001846 if (!opendata->is_recover) {
1847 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1848 if (ret != 0)
1849 goto out;
1850 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001851 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001852
1853 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001854 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001855 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001857out:
1858 return ERR_PTR(ret);
1859out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001860 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001861 return state;
1862}
1863
Andy Adamsone23008e2012-10-02 21:07:32 -04001864static void
1865nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1866{
1867 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1868 struct nfs_delegation *delegation;
1869 int delegation_flags = 0;
1870
1871 rcu_read_lock();
1872 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1873 if (delegation)
1874 delegation_flags = delegation->flags;
1875 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001876 switch (data->o_arg.claim) {
1877 default:
1878 break;
1879 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1880 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001881 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1882 "returning a delegation for "
1883 "OPEN(CLAIM_DELEGATE_CUR)\n",
1884 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001885 return;
1886 }
1887 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001888 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001889 data->owner->so_cred,
1890 data->o_res.delegation_type,
1891 &data->o_res.delegation,
1892 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001893 else
1894 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001895 data->owner->so_cred,
1896 data->o_res.delegation_type,
1897 &data->o_res.delegation,
1898 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001899
1900 if (data->o_res.do_recall)
1901 nfs_async_inode_return_delegation(state->inode,
1902 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001903}
1904
1905/*
1906 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1907 * and update the nfs4_state.
1908 */
1909static struct nfs4_state *
1910_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1911{
1912 struct inode *inode = data->state->inode;
1913 struct nfs4_state *state = data->state;
1914 int ret;
1915
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001916 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001917 if (data->rpc_status)
1918 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001919 /* cached opens have already been processed */
1920 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001921 }
1922
Andy Adamsone23008e2012-10-02 21:07:32 -04001923 ret = nfs_refresh_inode(inode, &data->f_attr);
1924 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001925 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001926
1927 if (data->o_res.delegation_type != 0)
1928 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001929update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001930 if (!update_open_stateid(state, &data->o_res.stateid,
1931 NULL, data->o_arg.fmode))
1932 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001933 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001934
1935 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001936}
1937
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001938static struct inode *
1939nfs4_opendata_get_inode(struct nfs4_opendata *data)
1940{
1941 struct inode *inode;
1942
1943 switch (data->o_arg.claim) {
1944 case NFS4_OPEN_CLAIM_NULL:
1945 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1946 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1947 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1948 return ERR_PTR(-EAGAIN);
1949 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1950 &data->f_attr, data->f_label);
1951 break;
1952 default:
1953 inode = d_inode(data->dentry);
1954 ihold(inode);
1955 nfs_refresh_inode(inode, &data->f_attr);
1956 }
1957 return inode;
1958}
1959
Andy Adamsone23008e2012-10-02 21:07:32 -04001960static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001961nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1962{
1963 struct nfs4_state *state;
1964 struct inode *inode;
1965
1966 inode = nfs4_opendata_get_inode(data);
1967 if (IS_ERR(inode))
1968 return ERR_CAST(inode);
1969 if (data->state != NULL && data->state->inode == inode) {
1970 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001971 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04001972 } else
1973 state = nfs4_get_open_state(inode, data->owner);
1974 iput(inode);
1975 if (state == NULL)
1976 state = ERR_PTR(-ENOMEM);
1977 return state;
1978}
1979
Andy Adamsone23008e2012-10-02 21:07:32 -04001980static struct nfs4_state *
1981_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001982{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001983 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001984
Trond Myklebustaac00a82007-07-05 19:02:21 -04001985 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001986 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001987 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001988 goto out;
1989 }
1990
Trond Myklebust75e8c482017-08-08 10:38:07 -04001991 state = nfs4_opendata_find_nfs4_state(data);
1992 if (IS_ERR(state))
1993 goto out;
1994
Andy Adamsone23008e2012-10-02 21:07:32 -04001995 if (data->o_res.delegation_type != 0)
1996 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001997 if (!update_open_stateid(state, &data->o_res.stateid,
1998 NULL, data->o_arg.fmode)) {
1999 nfs4_put_open_state(state);
2000 state = ERR_PTR(-EAGAIN);
2001 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002002out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002003 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002004 return state;
2005}
2006
Andy Adamsone23008e2012-10-02 21:07:32 -04002007static struct nfs4_state *
2008nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2009{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002010 struct nfs4_state *ret;
2011
Andy Adamsone23008e2012-10-02 21:07:32 -04002012 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002013 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2014 else
2015 ret = _nfs4_opendata_to_nfs4_state(data);
2016 nfs4_sequence_free_slot(&data->o_res.seq_res);
2017 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002018}
2019
Trond Myklebust0de43972018-09-02 15:57:01 -04002020static struct nfs_open_context *
2021nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002022{
2023 struct nfs_inode *nfsi = NFS_I(state->inode);
2024 struct nfs_open_context *ctx;
2025
Trond Myklebust0de43972018-09-02 15:57:01 -04002026 rcu_read_lock();
2027 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002028 if (ctx->state != state)
2029 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002030 if ((ctx->mode & mode) != mode)
2031 continue;
2032 if (!get_nfs_open_context(ctx))
2033 continue;
2034 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002035 return ctx;
2036 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002037 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002038 return ERR_PTR(-ENOENT);
2039}
2040
Trond Myklebust0de43972018-09-02 15:57:01 -04002041static struct nfs_open_context *
2042nfs4_state_find_open_context(struct nfs4_state *state)
2043{
2044 struct nfs_open_context *ctx;
2045
2046 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2047 if (!IS_ERR(ctx))
2048 return ctx;
2049 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2050 if (!IS_ERR(ctx))
2051 return ctx;
2052 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2053}
2054
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002055static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2056 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002057{
2058 struct nfs4_opendata *opendata;
2059
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002060 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002061 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002062 if (opendata == NULL)
2063 return ERR_PTR(-ENOMEM);
2064 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002065 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002066 return opendata;
2067}
2068
Trond Myklebust24311f82015-09-20 10:50:17 -04002069static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2070 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002071{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002072 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002073 int ret;
2074
Trond Myklebust24311f82015-09-20 10:50:17 -04002075 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002076 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002077 opendata->o_arg.open_flags = 0;
2078 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002079 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2080 NFS_SB(opendata->dentry->d_sb),
2081 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002082 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2083 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2084 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002085 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002086 if (ret != 0)
2087 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002088 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002089 if (IS_ERR(newstate))
2090 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002091 if (newstate != opendata->state)
2092 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002093 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002094 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002095}
2096
2097static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2098{
Trond Myklebust864472e2006-01-03 09:55:15 +01002099 int ret;
2100
2101 /* memory barrier prior to reading state->n_* */
2102 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002103 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2104 if (ret != 0)
2105 return ret;
2106 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2107 if (ret != 0)
2108 return ret;
2109 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2110 if (ret != 0)
2111 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002112 /*
2113 * We may have performed cached opens for all three recoveries.
2114 * Check if we need to update the current stateid.
2115 */
2116 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002117 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002118 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002119 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002120 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002121 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002122 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002123 return 0;
2124}
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126/*
2127 * OPEN_RECLAIM:
2128 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002130static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002132 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002133 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002134 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 int status;
2136
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002137 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2138 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002139 if (IS_ERR(opendata))
2140 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002141 rcu_read_lock();
2142 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002143 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002144 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002145 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002146 opendata->o_arg.u.delegation_type = delegation_type;
2147 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002148 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return status;
2150}
2151
Trond Myklebust539cd032007-06-05 11:46:42 -04002152static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 struct nfs_server *server = NFS_SERVER(state->inode);
2155 struct nfs4_exception exception = { };
2156 int err;
2157 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002158 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002159 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002160 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2161 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002162 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002163 break;
2164 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 } while (exception.retry);
2166 return err;
2167}
2168
Trond Myklebust864472e2006-01-03 09:55:15 +01002169static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2170{
2171 struct nfs_open_context *ctx;
2172 int ret;
2173
2174 ctx = nfs4_state_find_open_context(state);
2175 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002176 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002177 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2178 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002179 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002180 put_nfs_open_context(ctx);
2181 return ret;
2182}
2183
NeilBrowndce26302017-12-13 09:57:09 +11002184static 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 -07002185{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002186 switch (err) {
2187 default:
2188 printk(KERN_ERR "NFS: %s: unhandled error "
2189 "%d.\n", __func__, err);
2190 case 0:
2191 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002192 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002193 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002194 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002195 break;
2196 case -NFS4ERR_BADSESSION:
2197 case -NFS4ERR_BADSLOT:
2198 case -NFS4ERR_BAD_HIGH_SLOT:
2199 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2200 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002201 return -EAGAIN;
2202 case -NFS4ERR_STALE_CLIENTID:
2203 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002204 /* Don't recall a delegation if it was lost */
2205 nfs4_schedule_lease_recovery(server->nfs_client);
2206 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002207 case -NFS4ERR_MOVED:
2208 nfs4_schedule_migration_recovery(server);
2209 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002210 case -NFS4ERR_LEASE_MOVED:
2211 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2212 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002213 case -NFS4ERR_DELEG_REVOKED:
2214 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002215 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002216 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002217 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002218 nfs_inode_find_state_and_recover(state->inode,
2219 stateid);
2220 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002221 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002222 case -NFS4ERR_DELAY:
2223 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002224 ssleep(1);
2225 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002226 case -ENOMEM:
2227 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002228 if (fl) {
2229 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2230 if (lsp)
2231 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2232 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002233 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return err;
2236}
2237
Trond Myklebust24311f82015-09-20 10:50:17 -04002238int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002239 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002240{
2241 struct nfs_server *server = NFS_SERVER(state->inode);
2242 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002243 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002244
2245 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2246 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2247 if (IS_ERR(opendata))
2248 return PTR_ERR(opendata);
2249 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002250 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002251 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2252 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002253 goto out;
2254 }
2255 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002256 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2257 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002258 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002259 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002260 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2261 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2262 if (err)
2263 goto out;
2264 }
2265 nfs_state_clear_delegation(state);
2266out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002267 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002268 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002269}
2270
Chuck Leverbe05c862013-08-09 12:49:47 -04002271static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2272{
2273 struct nfs4_opendata *data = calldata;
2274
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002275 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2276 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002277}
2278
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002279static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2280{
2281 struct nfs4_opendata *data = calldata;
2282
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002283 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002284
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002285 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002286 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002287 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002288 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002289 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002290 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002291 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002292}
2293
2294static void nfs4_open_confirm_release(void *calldata)
2295{
2296 struct nfs4_opendata *data = calldata;
2297 struct nfs4_state *state = NULL;
2298
2299 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002300 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002301 goto out_free;
2302 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002303 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002304 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002305 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002306 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002307 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002308out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002309 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002310}
2311
2312static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002313 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002314 .rpc_call_done = nfs4_open_confirm_done,
2315 .rpc_release = nfs4_open_confirm_release,
2316};
2317
2318/*
2319 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2320 */
2321static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2322{
David Howells2b0143b2015-03-17 22:25:59 +00002323 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002324 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002325 struct rpc_message msg = {
2326 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2327 .rpc_argp = &data->c_arg,
2328 .rpc_resp = &data->c_res,
2329 .rpc_cred = data->owner->so_cred,
2330 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002331 struct rpc_task_setup task_setup_data = {
2332 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002333 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002334 .callback_ops = &nfs4_open_confirm_ops,
2335 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002336 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002337 .flags = RPC_TASK_ASYNC,
2338 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 int status;
2340
Anna Schumakerfba83f32018-05-04 16:22:50 -04002341 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2342 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002343 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002344 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002345 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002346 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002347 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002348 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002349 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002350 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002351 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002352 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002353 smp_wmb();
2354 } else
2355 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002356 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return status;
2358}
2359
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002360static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002362 struct nfs4_opendata *data = calldata;
2363 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002364 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002365 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002366
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002367 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002368 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002369 /*
2370 * Check if we still need to send an OPEN call, or if we can use
2371 * a delegation instead.
2372 */
2373 if (data->state != NULL) {
2374 struct nfs_delegation *delegation;
2375
Trond Myklebustbe189f72018-09-27 17:12:33 -04002376 if (can_open_cached(data->state, data->o_arg.fmode,
2377 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002378 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002379 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002380 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002381 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002382 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002383 rcu_read_unlock();
2384 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002385 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002386 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002387 switch (claim) {
2388 default:
2389 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002390 case NFS4_OPEN_CLAIM_PREVIOUS:
2391 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2392 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002393 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002394 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002395 case NFS4_OPEN_CLAIM_FH:
2396 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002397 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002398 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002399 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002400 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002401 &data->o_res.seq_res,
2402 task) != 0)
2403 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002404
2405 /* Set the create mode (note dependency on the session type) */
2406 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2407 if (data->o_arg.open_flags & O_EXCL) {
2408 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2409 if (nfs4_has_persistent_session(clp))
2410 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2411 else if (clp->cl_mvops->minor_version > 0)
2412 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2413 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002414 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002415unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002416 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002417 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002418out_no_action:
2419 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002420out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002421 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002422}
2423
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002424static void nfs4_open_done(struct rpc_task *task, void *calldata)
2425{
2426 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002428 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002429
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002430 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002431 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002432
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002433 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002434 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2435 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002436 case S_IFREG:
2437 break;
2438 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002439 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002440 break;
2441 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002442 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002443 break;
2444 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002445 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002446 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002447 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002448 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002449 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2450 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002451 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002452 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002453}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002454
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002455static void nfs4_open_release(void *calldata)
2456{
2457 struct nfs4_opendata *data = calldata;
2458 struct nfs4_state *state = NULL;
2459
2460 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002461 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002462 goto out_free;
2463 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002464 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002465 goto out_free;
2466 /* In case we need an open_confirm, no cleanup! */
2467 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2468 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002469 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002470 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002471 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002472out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002473 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002474}
2475
2476static const struct rpc_call_ops nfs4_open_ops = {
2477 .rpc_call_prepare = nfs4_open_prepare,
2478 .rpc_call_done = nfs4_open_done,
2479 .rpc_release = nfs4_open_release,
2480};
2481
Fred Isaman3b65a302016-09-19 10:06:49 -04002482static int nfs4_run_open_task(struct nfs4_opendata *data,
2483 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002484{
David Howells2b0143b2015-03-17 22:25:59 +00002485 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002486 struct nfs_server *server = NFS_SERVER(dir);
2487 struct nfs_openargs *o_arg = &data->o_arg;
2488 struct nfs_openres *o_res = &data->o_res;
2489 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002490 struct rpc_message msg = {
2491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2492 .rpc_argp = o_arg,
2493 .rpc_resp = o_res,
2494 .rpc_cred = data->owner->so_cred,
2495 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002496 struct rpc_task_setup task_setup_data = {
2497 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002498 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002499 .callback_ops = &nfs4_open_ops,
2500 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002501 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002502 .flags = RPC_TASK_ASYNC,
2503 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002504 int status;
2505
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002506 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002507 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002508 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002509 data->cancelled = false;
2510 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002511 if (!ctx) {
2512 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002513 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002514 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002515 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002516 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002517 pnfs_lgopen_prepare(data, ctx);
2518 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002519 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002520 if (IS_ERR(task))
2521 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002522 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002523 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002524 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002525 smp_wmb();
2526 } else
2527 status = data->rpc_status;
2528 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002529
2530 return status;
2531}
2532
2533static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2534{
David Howells2b0143b2015-03-17 22:25:59 +00002535 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002536 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002537 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002538
Fred Isaman3b65a302016-09-19 10:06:49 -04002539 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002540 if (status != 0 || !data->rpc_done)
2541 return status;
2542
Trond Myklebust6926afd2012-01-07 13:22:46 -05002543 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2544
Anna Schumakerd7e98252017-01-11 16:13:29 -05002545 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002546 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002547
2548 return status;
2549}
2550
Trond Myklebustf3792d62014-07-10 08:54:32 -04002551/*
2552 * Additional permission checks in order to distinguish between an
2553 * open for read, and an open for execute. This works around the
2554 * fact that NFSv4 OPEN treats read and execute permissions as being
2555 * the same.
2556 * Note that in the non-execute case, we want to turn off permission
2557 * checking if we just created a new file (POSIX open() semantics).
2558 */
NeilBrowna52458b2018-12-03 11:30:31 +11002559static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002560 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002561 struct nfs4_state *state, fmode_t fmode,
2562 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002563{
2564 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002565 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002566
2567 /* access call failed or for some reason the server doesn't
2568 * support any access modes -- defer access call until later */
2569 if (opendata->o_res.access_supported == 0)
2570 return 0;
2571
2572 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002573 /*
2574 * Use openflags to check for exec, because fmode won't
2575 * always have FMODE_EXEC set when file open for exec.
2576 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002577 if (openflags & __FMODE_EXEC) {
2578 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002579 if (S_ISDIR(state->inode->i_mode))
2580 mask = NFS4_ACCESS_LOOKUP;
2581 else
2582 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002583 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002584 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002585
2586 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002587 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2588 nfs_access_add_cache(state->inode, &cache);
2589
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002590 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2591 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002592 return 0;
2593
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002594 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002595}
2596
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002597/*
2598 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2599 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002600static int _nfs4_proc_open(struct nfs4_opendata *data,
2601 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002602{
David Howells2b0143b2015-03-17 22:25:59 +00002603 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002604 struct nfs_server *server = NFS_SERVER(dir);
2605 struct nfs_openargs *o_arg = &data->o_arg;
2606 struct nfs_openres *o_res = &data->o_res;
2607 int status;
2608
Fred Isaman3b65a302016-09-19 10:06:49 -04002609 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002610 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002611 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002612 if (status != 0) {
2613 if (status == -NFS4ERR_BADNAME &&
2614 !(o_arg->open_flags & O_CREAT))
2615 return -ENOENT;
2616 return status;
2617 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002618
Trond Myklebust6926afd2012-01-07 13:22:46 -05002619 nfs_fattr_map_and_free_names(server, &data->f_attr);
2620
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002621 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002622 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002623 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002624 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002625 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002626 if (data->file_created ||
2627 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002628 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002629 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002630 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002631 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2632 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002634 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002636 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002638 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2639 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002640 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2641 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002642 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646/*
2647 * OPEN_EXPIRED:
2648 * reclaim state on the server after a network partition.
2649 * Assumes caller holds the appropriate lock
2650 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002651static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002653 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002654 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002656 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002657 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002658 if (IS_ERR(opendata))
2659 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002660 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002661 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002662 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002663 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002664 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002667static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002668{
Trond Myklebust539cd032007-06-05 11:46:42 -04002669 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002670 struct nfs4_exception exception = { };
2671 int err;
2672
2673 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002674 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002675 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002676 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2677 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002678 switch (err) {
2679 default:
2680 goto out;
2681 case -NFS4ERR_GRACE:
2682 case -NFS4ERR_DELAY:
2683 nfs4_handle_exception(server, err, &exception);
2684 err = 0;
2685 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002686 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002687out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002688 return err;
2689}
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2692{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002694 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Trond Myklebust864472e2006-01-03 09:55:15 +01002696 ctx = nfs4_state_find_open_context(state);
2697 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002698 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002699 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002700 put_nfs_open_context(ctx);
2701 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
Trond Myklebust41020b62016-09-22 13:38:58 -04002704static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2705 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002706{
Trond Myklebust41020b62016-09-22 13:38:58 -04002707 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002708 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002709}
2710
2711static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2712{
2713 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002714 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002715}
2716
2717static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2718{
2719 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2720 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002721 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002722 return nfs4_open_expired(sp, state);
2723}
2724
Trond Myklebust45870d62016-09-22 13:38:59 -04002725static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2726 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002727 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002728{
2729 return -NFS4ERR_BAD_STATEID;
2730}
2731
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002732#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002733static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2734 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002735 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002736{
2737 int status;
2738
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002739 switch (stateid->type) {
2740 default:
2741 break;
2742 case NFS4_INVALID_STATEID_TYPE:
2743 case NFS4_SPECIAL_STATEID_TYPE:
2744 return -NFS4ERR_BAD_STATEID;
2745 case NFS4_REVOKED_STATEID_TYPE:
2746 goto out_free;
2747 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002748
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002749 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002750 switch (status) {
2751 case -NFS4ERR_EXPIRED:
2752 case -NFS4ERR_ADMIN_REVOKED:
2753 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002754 break;
2755 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002756 return status;
2757 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002758out_free:
2759 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002760 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002761 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002762}
2763
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002764static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002765{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002766 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002767 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002768 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002769 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002770 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002771
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002772 /* Get the delegation credential for use by test/free_stateid */
2773 rcu_read_lock();
2774 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002775 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002776 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002777 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002778 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002779 }
2780
2781 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002782
Trond Myklebust994b15b2018-09-05 14:07:14 -04002783 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2784 &delegation->flags)) {
2785 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002786 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002787 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002788
NeilBrowna52458b2018-12-03 11:30:31 +11002789 if (delegation->cred)
2790 cred = get_cred(delegation->cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002791 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002792 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002793 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002794 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002795 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002796 else
2797 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002798
Trond Myklebust8c39a392019-07-19 13:48:44 -04002799 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002800 return ret;
2801}
2802
2803static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2804{
2805 nfs4_stateid tmp;
2806
2807 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2808 nfs4_copy_delegation_stateid(state->inode, state->state,
2809 &tmp, NULL) &&
2810 nfs4_stateid_match_other(&state->stateid, &tmp))
2811 nfs_state_set_delegation(state, &tmp, state->state);
2812 else
2813 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002814}
2815
2816/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002817 * nfs41_check_expired_locks - possibly free a lock stateid
2818 *
2819 * @state: NFSv4 state for an inode
2820 *
2821 * Returns NFS_OK if recovery for this stateid is now finished.
2822 * Otherwise a negative NFS4ERR value is returned.
2823 */
2824static int nfs41_check_expired_locks(struct nfs4_state *state)
2825{
2826 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002827 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002828 struct nfs_server *server = NFS_SERVER(state->inode);
2829
2830 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2831 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002832
2833 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002834 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2835 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002836 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002837
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002838 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002839 spin_unlock(&state->state_lock);
2840
2841 nfs4_put_lock_state(prev);
2842 prev = lsp;
2843
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002844 status = nfs41_test_and_free_expired_stateid(server,
2845 &lsp->ls_stateid,
2846 cred);
2847 trace_nfs4_test_lock_stateid(state, lsp, status);
2848 if (status == -NFS4ERR_EXPIRED ||
2849 status == -NFS4ERR_BAD_STATEID) {
2850 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002851 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002852 if (!recover_lost_locks)
2853 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2854 } else if (status != NFS_OK) {
2855 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002856 nfs4_put_lock_state(prev);
2857 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002858 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002859 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002860 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002861 }
2862 spin_unlock(&state->state_lock);
2863 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002864out:
2865 return ret;
2866}
2867
2868/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002869 * nfs41_check_open_stateid - possibly free an open stateid
2870 *
2871 * @state: NFSv4 state for an inode
2872 *
2873 * Returns NFS_OK if recovery for this stateid is now finished.
2874 * Otherwise a negative NFS4ERR value is returned.
2875 */
2876static int nfs41_check_open_stateid(struct nfs4_state *state)
2877{
2878 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002879 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002880 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002881 int status;
2882
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002883 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002884 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002885 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002886 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002887 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002888 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002889 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002890 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002891 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002892 if (nfs_open_stateid_recover_openmode(state))
2893 return -NFS4ERR_OPENMODE;
2894 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002895}
2896
2897static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2898{
Chuck Levereb64cf92012-07-11 16:30:05 -04002899 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002900
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002901 status = nfs41_check_delegation_stateid(state);
2902 if (status != NFS_OK)
2903 return status;
2904 nfs41_delegation_recover_stateid(state);
2905
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002906 status = nfs41_check_expired_locks(state);
2907 if (status != NFS_OK)
2908 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002909 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002910 if (status != NFS_OK)
2911 status = nfs4_open_expired(sp, state);
2912 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002913}
2914#endif
2915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002917 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2918 * fields corresponding to attributes that were used to store the verifier.
2919 * Make sure we clobber those fields in the later setattr call
2920 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002921static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002922 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002923{
Trond Myklebust609339c2018-03-28 16:18:17 -04002924 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2925 __u32 attrset[3];
2926 unsigned ret;
2927 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002928
Trond Myklebust609339c2018-03-28 16:18:17 -04002929 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2930 attrset[i] = opendata->o_res.attrset[i];
2931 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2932 attrset[i] &= ~bitmask[i];
2933 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002934
Trond Myklebust609339c2018-03-28 16:18:17 -04002935 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2936 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002937
Trond Myklebust609339c2018-03-28 16:18:17 -04002938 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2939 if (sattr->ia_valid & ATTR_ATIME_SET)
2940 ret |= ATTR_ATIME_SET;
2941 else
2942 ret |= ATTR_ATIME;
2943 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002944
Trond Myklebust609339c2018-03-28 16:18:17 -04002945 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2946 if (sattr->ia_valid & ATTR_MTIME_SET)
2947 ret |= ATTR_MTIME_SET;
2948 else
2949 ret |= ATTR_MTIME;
2950 }
2951
2952 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002953 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002954 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002955}
2956
Trond Myklebustc21443c2013-02-07 14:26:21 -05002957static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002958 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002959{
2960 struct nfs4_state_owner *sp = opendata->owner;
2961 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002962 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002963 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002964 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002965 unsigned int seq;
2966 int ret;
2967
2968 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2969
Fred Isaman3b65a302016-09-19 10:06:49 -04002970 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002971 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002972 goto out;
2973
Trond Myklebustae55e592018-05-22 11:17:16 -04002974 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002975 ret = PTR_ERR(state);
2976 if (IS_ERR(state))
2977 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002978 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002979 if (server->caps & NFS_CAP_POSIX_LOCK)
2980 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002981 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2982 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002983
Trond Myklebust275bb302013-05-29 13:11:28 -04002984 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002985 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002986 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002987 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002988 alias = d_exact_alias(dentry, state->inode);
2989 if (!alias)
2990 alias = d_splice_alias(igrab(state->inode), dentry);
2991 /* d_splice_alias() can't fail here - it's a non-directory */
2992 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002993 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002994 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002995 }
2996 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002997 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002998 }
2999
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003000 /* Parse layoutget results before we check for access */
3001 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3002
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003003 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3004 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003005 if (ret != 0)
3006 goto out;
3007
David Howells2b0143b2015-03-17 22:25:59 +00003008 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003009 nfs_inode_attach_open_context(ctx);
3010 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3011 nfs4_schedule_stateid_recovery(server, state);
3012 }
Fred Isaman2409a972016-10-06 12:11:21 -04003013
Trond Myklebustc21443c2013-02-07 14:26:21 -05003014out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003015 if (!opendata->cancelled)
3016 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003017 return ret;
3018}
3019
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003020/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003021 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 */
Andy Adamson82be4172012-05-23 05:02:35 -04003023static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003024 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003025 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003026 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003027 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
3029 struct nfs4_state_owner *sp;
3030 struct nfs4_state *state = NULL;
3031 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003032 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003033 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003034 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003035 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003036 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003037 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003038 struct iattr *sattr = c->sattr;
3039 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003040 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003041 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
3043 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003045 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3046 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3048 goto out_err;
3049 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003050 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003051 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003052 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003053 if (d_really_is_positive(dentry))
3054 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003055 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003056 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003057 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003058 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3059 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003060 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003061 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
David Quigley14c43f72013-05-22 12:50:43 -04003063 if (label) {
3064 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3065 if (IS_ERR(olabel)) {
3066 status = PTR_ERR(olabel);
3067 goto err_opendata_put;
3068 }
3069 }
3070
Trond Myklebuste911b812014-03-26 13:24:37 -07003071 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3072 if (!opendata->f_attr.mdsthreshold) {
3073 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3074 if (!opendata->f_attr.mdsthreshold)
3075 goto err_free_label;
3076 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003077 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003078 }
David Howells2b0143b2015-03-17 22:25:59 +00003079 if (d_really_is_positive(dentry))
3080 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003081
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003082 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003083 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003084 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003085 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003086
NeilBrownefcbc042015-07-30 13:00:56 +10003087 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003088 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003089 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003090 /*
3091 * send create attributes which was not set by open
3092 * with an extra setattr.
3093 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003094 if (attrs || label) {
3095 unsigned ia_old = sattr->ia_valid;
3096
3097 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003098 nfs_fattr_init(opendata->o_res.f_attr);
3099 status = nfs4_do_setattr(state->inode, cred,
3100 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003101 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003102 if (status == 0) {
3103 nfs_setattr_update_inode(state->inode, sattr,
3104 opendata->o_res.f_attr);
3105 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3106 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003107 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003108 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003109 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003110 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003111 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003112
Trond Myklebuste911b812014-03-26 13:24:37 -07003113 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003114 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003115 opendata->f_attr.mdsthreshold = NULL;
3116 }
Andy Adamson82be4172012-05-23 05:02:35 -04003117
David Quigley14c43f72013-05-22 12:50:43 -04003118 nfs4_label_free(olabel);
3119
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003120 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003123err_free_label:
3124 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003125err_opendata_put:
3126 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003127err_put_state_owner:
3128 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 return status;
3131}
3132
3133
Andy Adamson82be4172012-05-23 05:02:35 -04003134static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003135 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003136 int flags,
3137 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003138 struct nfs4_label *label,
3139 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003141 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003142 struct nfs4_exception exception = {
3143 .interruptible = true,
3144 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003146 struct nfs4_open_createattrs c = {
3147 .label = label,
3148 .sattr = sattr,
3149 .verf = {
3150 [0] = (__u32)jiffies,
3151 [1] = (__u32)current->pid,
3152 },
3153 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 int status;
3155
3156 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003157 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003158 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003159 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 if (status == 0)
3161 break;
3162 /* NOTE: BAD_SEQID means the server and client disagree about the
3163 * book-keeping w.r.t. state-changing operations
3164 * (OPEN/CLOSE/LOCK/LOCKU...)
3165 * It is actually a sign of a bug on the client or on the server.
3166 *
3167 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003168 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 * have unhashed the old state_owner for us, and that we can
3170 * therefore safely retry using a new one. We should still warn
3171 * the user though...
3172 */
3173 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003174 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003175 " returned a bad sequence-id error!\n",
3176 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 exception.retry = 1;
3178 continue;
3179 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003180 /*
3181 * BAD_STATEID on OPEN means that the server cancelled our
3182 * state before it received the OPEN_CONFIRM.
3183 * Recover by retrying the request as per the discussion
3184 * on Page 181 of RFC3530.
3185 */
3186 if (status == -NFS4ERR_BAD_STATEID) {
3187 exception.retry = 1;
3188 continue;
3189 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003190 if (status == -EAGAIN) {
3191 /* We must have found a delegation */
3192 exception.retry = 1;
3193 continue;
3194 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003195 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3196 continue;
3197 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 status, &exception));
3199 } while (exception.retry);
3200 return res;
3201}
3202
Trond Myklebust8487c472016-06-26 08:44:35 -04003203static int _nfs4_do_setattr(struct inode *inode,
3204 struct nfs_setattrargs *arg,
3205 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003206 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003207 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003208{
3209 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003210 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3212 .rpc_argp = arg,
3213 .rpc_resp = res,
3214 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003215 };
NeilBrowna52458b2018-12-03 11:30:31 +11003216 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003217 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003218 bool truncate;
3219 int status;
3220
3221 nfs_fattr_init(res->fattr);
3222
3223 /* Servers should only apply open mode checks for file size changes */
3224 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003225 if (!truncate)
3226 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003227
Trond Myklebust991eedb2018-04-09 11:15:30 -04003228 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003229 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003230 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003231 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003232 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003233 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003234 l_ctx = nfs_get_lock_context(ctx);
3235 if (IS_ERR(l_ctx))
3236 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003237 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3238 &arg->stateid, &delegation_cred);
3239 nfs_put_lock_context(l_ctx);
3240 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003241 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003242 } else {
3243zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003244 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003245 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003246 if (delegation_cred)
3247 msg.rpc_cred = delegation_cred;
3248
3249 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3250
NeilBrowna52458b2018-12-03 11:30:31 +11003251 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003252 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003253 renew_lease(server, timestamp);
3254 trace_nfs4_setattr(inode, &arg->stateid, status);
3255 return status;
3256}
3257
NeilBrowna52458b2018-12-03 11:30:31 +11003258static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003259 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003260 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003261 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003263 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003264 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003265 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003266 struct nfs_setattrargs arg = {
3267 .fh = NFS_FH(inode),
3268 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003270 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003271 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003272 };
3273 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003275 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003277 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003278 struct nfs4_exception exception = {
3279 .state = state,
3280 .inode = inode,
3281 .stateid = &arg.stateid,
3282 };
3283 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003286 nfs4_bitmap_copy_adjust_setattr(bitmask,
3287 nfs4_bitmask(server, olabel),
3288 inode);
3289
NeilBrown29b59f92016-10-13 15:26:47 +11003290 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003291 switch (err) {
3292 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003293 if (!(sattr->ia_valid & ATTR_SIZE)) {
3294 pr_warn_once("NFSv4: server %s is incorrectly "
3295 "applying open mode checks to "
3296 "a SETATTR that is not "
3297 "changing file size.\n",
3298 server->nfs_client->cl_hostname);
3299 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003300 if (state && !(state->state & FMODE_WRITE)) {
3301 err = -EBADF;
3302 if (sattr->ia_valid & ATTR_OPEN)
3303 err = -EACCES;
3304 goto out;
3305 }
3306 }
3307 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003309out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 return err;
3311}
3312
Peng Tao500d7012015-09-22 11:35:22 +08003313static bool
3314nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3315{
3316 if (inode == NULL || !nfs_have_layout(inode))
3317 return false;
3318
3319 return pnfs_wait_on_layoutreturn(inode, task);
3320}
3321
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003322/*
3323 * Update the seqid of an open stateid
3324 */
3325static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3326 struct nfs4_state *state)
3327{
3328 __be32 seqid_open;
3329 u32 dst_seqid;
3330 int seq;
3331
3332 for (;;) {
3333 if (!nfs4_valid_open_stateid(state))
3334 break;
3335 seq = read_seqbegin(&state->seqlock);
3336 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3337 nfs4_stateid_copy(dst, &state->open_stateid);
3338 if (read_seqretry(&state->seqlock, seq))
3339 continue;
3340 break;
3341 }
3342 seqid_open = state->open_stateid.seqid;
3343 if (read_seqretry(&state->seqlock, seq))
3344 continue;
3345
3346 dst_seqid = be32_to_cpu(dst->seqid);
3347 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3348 dst->seqid = seqid_open;
3349 break;
3350 }
3351}
3352
3353/*
3354 * Update the seqid of an open stateid after receiving
3355 * NFS4ERR_OLD_STATEID
3356 */
3357static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3358 struct nfs4_state *state)
3359{
3360 __be32 seqid_open;
3361 u32 dst_seqid;
3362 bool ret;
3363 int seq;
3364
3365 for (;;) {
3366 ret = false;
3367 if (!nfs4_valid_open_stateid(state))
3368 break;
3369 seq = read_seqbegin(&state->seqlock);
3370 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3371 if (read_seqretry(&state->seqlock, seq))
3372 continue;
3373 break;
3374 }
3375 seqid_open = state->open_stateid.seqid;
3376 if (read_seqretry(&state->seqlock, seq))
3377 continue;
3378
3379 dst_seqid = be32_to_cpu(dst->seqid);
3380 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3381 dst->seqid = cpu_to_be32(dst_seqid + 1);
3382 else
3383 dst->seqid = seqid_open;
3384 ret = true;
3385 break;
3386 }
3387
3388 return ret;
3389}
3390
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391struct nfs4_closedata {
3392 struct inode *inode;
3393 struct nfs4_state *state;
3394 struct nfs_closeargs arg;
3395 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003396 struct {
3397 struct nfs4_layoutreturn_args arg;
3398 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003399 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003400 u32 roc_barrier;
3401 bool roc;
3402 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003403 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003404 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405};
3406
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003407static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003408{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003409 struct nfs4_closedata *calldata = data;
3410 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003411 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003412
Trond Myklebustcf805162016-11-15 14:56:07 -05003413 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003414 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3415 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003416 nfs4_put_open_state(calldata->state);
3417 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003418 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003419 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003420 kfree(calldata);
3421}
3422
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003423static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003425 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003428 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003429 struct nfs4_exception exception = {
3430 .state = state,
3431 .inode = calldata->inode,
3432 .stateid = &calldata->arg.stateid,
3433 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Chuck Levera3ca5652012-03-01 17:00:40 -05003435 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003436 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3437 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003438 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003439
3440 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04003441 if (pnfs_roc_done(task, calldata->inode,
3442 &calldata->arg.lr_args,
3443 &calldata->res.lr_res,
3444 &calldata->res.lr_ret) == -EAGAIN)
3445 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003446
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003447 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 * the state_owner. we keep this around to process errors
3449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 switch (task->tk_status) {
3451 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003452 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003453 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003454 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003455 case -NFS4ERR_ACCESS:
3456 if (calldata->arg.bitmask != NULL) {
3457 calldata->arg.bitmask = NULL;
3458 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003459 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003460
3461 }
3462 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003463 case -NFS4ERR_OLD_STATEID:
3464 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003465 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003466 state))
3467 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003468 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003469 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003471 case -NFS4ERR_EXPIRED:
3472 nfs4_free_revoked_stateid(server,
3473 &calldata->arg.stateid,
3474 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003475 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003476 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003477 if (calldata->arg.fmode == 0)
3478 break;
3479 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003481 task->tk_status = nfs4_async_handle_exception(task,
3482 server, task->tk_status, &exception);
3483 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003484 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003486 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3487 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003488out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003489 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003490 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003491 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003492 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003493 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003494out_restart:
3495 task->tk_status = 0;
3496 rpc_restart_call_prepare(task);
3497 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498}
3499
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003500static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003502 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003503 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003504 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003505 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003506 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003507 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003508
Chuck Levera3ca5652012-03-01 17:00:40 -05003509 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003510 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003511 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003512
Trond Myklebust88069f72009-12-08 08:33:16 -05003513 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003514 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003515 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3516 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3517 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003518 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003519 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003520 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003521 if (state->n_rdonly == 0)
3522 call_close |= is_rdonly;
3523 else if (is_rdonly)
3524 calldata->arg.fmode |= FMODE_READ;
3525 if (state->n_wronly == 0)
3526 call_close |= is_wronly;
3527 else if (is_wronly)
3528 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003529 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3530 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003531 } else if (is_rdwr)
3532 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3533
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003534 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3535 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003536 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003537 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003538
3539 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003540 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003541 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003542 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003543
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003544 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003545 nfs_release_seqid(calldata->arg.seqid);
3546 goto out_wait;
3547 }
3548
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003549 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3550 if (lo && !pnfs_layout_is_valid(lo)) {
3551 calldata->arg.lr_args = NULL;
3552 calldata->res.lr_res = NULL;
3553 }
3554
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003555 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003556 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003557
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003558 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003559 /* Close-to-open cache consistency revalidation */
3560 if (!nfs4_have_delegation(inode, FMODE_READ))
3561 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3562 else
3563 calldata->arg.bitmask = NULL;
3564 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003565
Trond Myklebust6ae37332015-01-30 14:21:14 -05003566 calldata->arg.share_access =
3567 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3568 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003569
Trond Myklebustd8d84982016-12-19 12:14:44 -05003570 if (calldata->res.fattr == NULL)
3571 calldata->arg.bitmask = NULL;
3572 else if (calldata->arg.bitmask == NULL)
3573 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003574 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003575 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003576 &calldata->arg.seq_args,
3577 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003578 task) != 0)
3579 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003580 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003581 return;
3582out_no_action:
3583 task->tk_action = NULL;
3584out_wait:
3585 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003588static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003589 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003590 .rpc_call_done = nfs4_close_done,
3591 .rpc_release = nfs4_free_closedata,
3592};
3593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594/*
3595 * It is possible for data to be read/written from a mem-mapped file
3596 * after the sys_close call (which hits the vfs layer as a flush).
3597 * This means that we can't safely call nfsv4 close on a file until
3598 * the inode is cleared. This in turn means that we are not good
3599 * NFSv4 citizens - we do not indicate to the server to update the file's
3600 * share state even when we are done with one of the three share
3601 * stateid's in the inode.
3602 *
3603 * NOTE: Caller must be holding the sp->so_owner semaphore!
3604 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003605int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003607 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003608 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003610 struct nfs4_state_owner *sp = state->owner;
3611 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003612 struct rpc_message msg = {
3613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3614 .rpc_cred = state->owner->so_cred,
3615 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003616 struct rpc_task_setup task_setup_data = {
3617 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003618 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003619 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003620 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003621 .flags = RPC_TASK_ASYNC,
3622 };
Trond Myklebust95121352005-10-18 14:20:12 -07003623 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003625 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3626 &task_setup_data.rpc_client, &msg);
3627
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003628 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003630 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003631 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003632 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003634 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003635 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3636 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003638 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3639 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003640 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003641 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003642 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003643 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003644 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003645 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003646 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003647 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003648 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003649 calldata->lr.roc = pnfs_roc(state->inode,
3650 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3651 if (calldata->lr.roc) {
3652 calldata->arg.lr_args = &calldata->lr.arg;
3653 calldata->res.lr_res = &calldata->lr.res;
3654 }
Al Viro643168c2011-06-22 18:20:23 -04003655 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003656
Trond Myklebust1174dd12010-12-21 10:52:24 -05003657 msg.rpc_argp = &calldata->arg;
3658 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003659 task_setup_data.callback_data = calldata;
3660 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003661 if (IS_ERR(task))
3662 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003663 status = 0;
3664 if (wait)
3665 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003666 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003667 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003668out_free_calldata:
3669 kfree(calldata);
3670out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003671 nfs4_put_open_state(state);
3672 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003673 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674}
3675
Trond Myklebust2b484292010-09-17 10:56:51 -04003676static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003677nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3678 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003681 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3682
3683 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
Trond Myklebust565277f2007-10-15 18:17:53 -04003685 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003686 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003687
3688 nfs4_label_release_security(label);
3689
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003690 if (IS_ERR(state))
3691 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003692 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
Trond Myklebust1185a552009-12-03 15:54:02 -05003695static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003696{
3697 if (ctx->state == NULL)
3698 return;
3699 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003700 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003701 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003702 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003703}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Trond Myklebustb944dba2013-11-04 15:20:20 -05003705#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3706#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003707#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3710{
Kinglong Mee8c612822015-08-26 21:12:58 +08003711 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003712 struct nfs4_server_caps_arg args = {
3713 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003714 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003715 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 struct nfs4_server_caps_res res = {};
3717 struct rpc_message msg = {
3718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003719 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 .rpc_resp = &res,
3721 };
3722 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003723 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Kinglong Mee8c612822015-08-26 21:12:58 +08003725 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3726 FATTR4_WORD0_FH_EXPIRE_TYPE |
3727 FATTR4_WORD0_LINK_SUPPORT |
3728 FATTR4_WORD0_SYMLINK_SUPPORT |
3729 FATTR4_WORD0_ACLSUPPORT;
3730 if (minorversion)
3731 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3732
Bryan Schumaker7c513052011-03-24 17:12:24 +00003733 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003735 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003736 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003737 case 0:
3738 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3739 res.attr_bitmask[2] = 0;
3740 break;
3741 case 1:
3742 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3743 break;
3744 case 2:
3745 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003748 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3749 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3750 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3751 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003752 NFS_CAP_CTIME|NFS_CAP_MTIME|
3753 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003754 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3755 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 server->caps |= NFS_CAP_ACLS;
3757 if (res.has_links != 0)
3758 server->caps |= NFS_CAP_HARDLINKS;
3759 if (res.has_symlinks != 0)
3760 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003761 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3762 server->caps |= NFS_CAP_FILEID;
3763 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3764 server->caps |= NFS_CAP_MODE;
3765 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3766 server->caps |= NFS_CAP_NLINK;
3767 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3768 server->caps |= NFS_CAP_OWNER;
3769 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3770 server->caps |= NFS_CAP_OWNER_GROUP;
3771 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3772 server->caps |= NFS_CAP_ATIME;
3773 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3774 server->caps |= NFS_CAP_CTIME;
3775 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3776 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003777#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3778 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3779 server->caps |= NFS_CAP_SECURITY_LABEL;
3780#endif
3781 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3782 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003783 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003784
Trond Myklebusta65318b2009-03-11 14:10:28 -04003785 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3786 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3787 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003788 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003789
3790 /* Avoid a regression due to buggy server */
3791 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3792 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003793 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3794 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003795
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003797 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 return status;
3801}
3802
Trond Myklebust55a97592006-06-09 09:34:19 -04003803int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
Trond Myklebust0688e642019-04-07 13:59:09 -04003805 struct nfs4_exception exception = {
3806 .interruptible = true,
3807 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 int err;
3809 do {
3810 err = nfs4_handle_exception(server,
3811 _nfs4_server_capabilities(server, fhandle),
3812 &exception);
3813 } while (exception.retry);
3814 return err;
3815}
3816
3817static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3818 struct nfs_fsinfo *info)
3819{
David Quigleyaa9c2662013-05-22 12:50:44 -04003820 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003822 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 };
3824 struct nfs4_lookup_res res = {
3825 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003826 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 .fh = fhandle,
3828 };
3829 struct rpc_message msg = {
3830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3831 .rpc_argp = &args,
3832 .rpc_resp = &res,
3833 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003834
David Quigleyaa9c2662013-05-22 12:50:44 -04003835 bitmask[0] = nfs4_fattr_bitmap[0];
3836 bitmask[1] = nfs4_fattr_bitmap[1];
3837 /*
3838 * Process the label in the upcoming getfattr
3839 */
3840 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3841
Trond Myklebust0e574af2005-10-27 22:12:38 -04003842 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003843 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844}
3845
3846static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3847 struct nfs_fsinfo *info)
3848{
Trond Myklebust0688e642019-04-07 13:59:09 -04003849 struct nfs4_exception exception = {
3850 .interruptible = true,
3851 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 int err;
3853 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003854 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003855 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003856 switch (err) {
3857 case 0:
3858 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003859 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003860 default:
3861 err = nfs4_handle_exception(server, err, &exception);
3862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003864out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 return err;
3866}
3867
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003868static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3869 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3870{
Trond Myklebustc2190662013-08-26 19:23:04 -04003871 struct rpc_auth_create_args auth_args = {
3872 .pseudoflavor = flavor,
3873 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003874 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003875
Trond Myklebustc2190662013-08-26 19:23:04 -04003876 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003877 if (IS_ERR(auth))
3878 return -EACCES;
3879 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003880}
3881
Chuck Lever9a744ba2013-03-16 15:56:02 -04003882/*
3883 * Retry pseudoroot lookup with various security flavors. We do this when:
3884 *
3885 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3886 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3887 *
3888 * Returns zero on success, or a negative NFS4ERR value, or a
3889 * negative errno value.
3890 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003891static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003892 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003894 /* Per 3530bis 15.33.5 */
3895 static const rpc_authflavor_t flav_array[] = {
3896 RPC_AUTH_GSS_KRB5P,
3897 RPC_AUTH_GSS_KRB5I,
3898 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003899 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003900 RPC_AUTH_NULL,
3901 };
3902 int status = -EPERM;
3903 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003905 if (server->auth_info.flavor_len > 0) {
3906 /* try each flavor specified by user */
3907 for (i = 0; i < server->auth_info.flavor_len; i++) {
3908 status = nfs4_lookup_root_sec(server, fhandle, info,
3909 server->auth_info.flavors[i]);
3910 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3911 continue;
3912 break;
3913 }
3914 } else {
3915 /* no flavors specified by user, try default list */
3916 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3917 status = nfs4_lookup_root_sec(server, fhandle, info,
3918 flav_array[i]);
3919 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3920 continue;
3921 break;
3922 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003923 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003924
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003925 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003926 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003927 * to access the mount. It could also mean that we tried to mount
3928 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3929 * existing mount programs don't handle -EACCES very well so it should
3930 * be mapped to -EPERM instead.
3931 */
3932 if (status == -EACCES)
3933 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003934 return status;
3935}
3936
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003937/**
3938 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3939 * @server: initialized nfs_server handle
3940 * @fhandle: we fill in the pseudo-fs root file handle
3941 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003942 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003943 *
3944 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003945 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003946int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003947 struct nfs_fsinfo *info,
3948 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003949{
Andre Przywarac7757072015-04-23 17:17:40 +01003950 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003951
Andre Przywarac7757072015-04-23 17:17:40 +01003952 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003953 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003954
3955 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003956 status = server->nfs_client->cl_mvops->find_root_sec(server,
3957 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 if (status == 0)
3960 status = nfs4_server_capabilities(server, fhandle);
3961 if (status == 0)
3962 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003963
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003964 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965}
3966
Bryan Schumakerbae36242012-05-10 15:07:31 -04003967static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3968 struct nfs_fsinfo *info)
3969{
3970 int error;
3971 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003972 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003973
3974 error = nfs4_server_capabilities(server, mntfh);
3975 if (error < 0) {
3976 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3977 return error;
3978 }
3979
David Quigley14c43f72013-05-22 12:50:43 -04003980 label = nfs4_label_alloc(server, GFP_KERNEL);
3981 if (IS_ERR(label))
3982 return PTR_ERR(label);
3983
Trond Myklebusta841b542018-04-07 13:50:59 -04003984 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003985 if (error < 0) {
3986 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003987 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003988 }
3989
3990 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3991 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3992 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3993
David Quigley14c43f72013-05-22 12:50:43 -04003994err_free_label:
3995 nfs4_label_free(label);
3996
Bryan Schumakerbae36242012-05-10 15:07:31 -04003997 return error;
3998}
3999
Manoj Naik6b97fd32006-06-09 09:34:29 -04004000/*
4001 * Get locations and (maybe) other attributes of a referral.
4002 * Note that we'll actually follow the referral later when
4003 * we detect fsid mismatch in inode revalidation
4004 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004005static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4006 const struct qstr *name, struct nfs_fattr *fattr,
4007 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004008{
4009 int status = -ENOMEM;
4010 struct page *page = NULL;
4011 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004012
4013 page = alloc_page(GFP_KERNEL);
4014 if (page == NULL)
4015 goto out;
4016 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4017 if (locations == NULL)
4018 goto out;
4019
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004020 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004021 if (status != 0)
4022 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004023
4024 /*
4025 * If the fsid didn't change, this is a migration event, not a
4026 * referral. Cause us to drop into the exception handler, which
4027 * will kick off migration recovery.
4028 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004029 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004030 dprintk("%s: server did not return a different fsid for"
4031 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004032 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004033 goto out;
4034 }
Andy Adamson533eb462011-06-13 18:25:56 -04004035 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4036 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004037
Andy Adamson533eb462011-06-13 18:25:56 -04004038 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004039 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004040 memset(fhandle, 0, sizeof(struct nfs_fh));
4041out:
4042 if (page)
4043 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004044 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004045 return status;
4046}
4047
David Quigley1775fd32013-05-22 12:50:42 -04004048static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004049 struct nfs_fattr *fattr, struct nfs4_label *label,
4050 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051{
Trond Myklebust771734f2018-04-07 13:54:23 -04004052 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 struct nfs4_getattr_arg args = {
4054 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004055 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 };
4057 struct nfs4_getattr_res res = {
4058 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004059 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 .server = server,
4061 };
4062 struct rpc_message msg = {
4063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4064 .rpc_argp = &args,
4065 .rpc_resp = &res,
4066 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004067
Trond Myklebust771734f2018-04-07 13:54:23 -04004068 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004069
Trond Myklebust0e574af2005-10-27 22:12:38 -04004070 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004071 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072}
4073
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004074int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004075 struct nfs_fattr *fattr, struct nfs4_label *label,
4076 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
Trond Myklebust0688e642019-04-07 13:59:09 -04004078 struct nfs4_exception exception = {
4079 .interruptible = true,
4080 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 int err;
4082 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004083 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004084 trace_nfs4_getattr(server, fhandle, fattr, err);
4085 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 &exception);
4087 } while (exception.retry);
4088 return err;
4089}
4090
4091/*
4092 * The file is not closed if it is opened due to the a request to change
4093 * the size of the file. The open call will not be needed once the
4094 * VFS layer lookup-intents are implemented.
4095 *
4096 * Close is called when the inode is destroyed.
4097 * If we haven't opened the file for O_WRONLY, we
4098 * need to in the size_change case to obtain a stateid.
4099 *
4100 * Got race?
4101 * Because OPEN is always done by name in nfsv4, it is
4102 * possible that we opened a different file by the same
4103 * name. We can recognize this race condition, but we
4104 * can't do anything about it besides returning an error.
4105 *
4106 * This will be fixed with VFS changes (lookup-intent).
4107 */
4108static int
4109nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4110 struct iattr *sattr)
4111{
David Howells2b0143b2015-03-17 22:25:59 +00004112 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004113 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004114 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004115 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 int status;
4117
Peng Tao88ac8152014-09-12 11:04:10 +08004118 if (pnfs_ld_layoutret_on_setattr(inode) &&
4119 sattr->ia_valid & ATTR_SIZE &&
4120 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004121 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004122
Trond Myklebust0e574af2005-10-27 22:12:38 -04004123 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
Andy Adamson26699402012-05-30 16:12:24 -04004125 /* Deal with open(O_TRUNC) */
4126 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004127 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004128
4129 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004130 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004131 return 0;
4132
Trond Myklebustd5308382005-11-04 15:33:38 -05004133 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004134 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004135
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004136 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004137 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004138 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004139 }
4140
David Quigley14c43f72013-05-22 12:50:43 -04004141 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4142 if (IS_ERR(label))
4143 return PTR_ERR(label);
4144
Trond Myklebust199366f2018-03-20 16:43:18 -04004145 /* Return any delegations if we're going to change ACLs */
4146 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004147 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004148
NeilBrown29b59f92016-10-13 15:26:47 +11004149 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004150 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004151 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004152 nfs_setsecurity(inode, fattr, label);
4153 }
David Quigley14c43f72013-05-22 12:50:43 -04004154 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 return status;
4156}
4157
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004158static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4159 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004160 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004161{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004162 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004163 int status;
4164 struct nfs4_lookup_arg args = {
4165 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004166 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04004167 .name = name,
4168 };
4169 struct nfs4_lookup_res res = {
4170 .server = server,
4171 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004172 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004173 .fh = fhandle,
4174 };
4175 struct rpc_message msg = {
4176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4177 .rpc_argp = &args,
4178 .rpc_resp = &res,
4179 };
4180
David Quigleyaa9c2662013-05-22 12:50:44 -04004181 args.bitmask = nfs4_bitmask(server, label);
4182
David Howells2b3de442006-08-22 20:06:09 -04004183 nfs_fattr_init(fattr);
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004186 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 dprintk("NFS reply lookup: %d\n", status);
4188 return status;
4189}
4190
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004191static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004192{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004193 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004194 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004195 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4196 fattr->nlink = 2;
4197}
4198
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004199static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004200 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004201 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004202{
Trond Myklebust0688e642019-04-07 13:59:09 -04004203 struct nfs4_exception exception = {
4204 .interruptible = true,
4205 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004206 struct rpc_clnt *client = *clnt;
4207 int err;
4208 do {
David Quigley1775fd32013-05-22 12:50:42 -04004209 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004210 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004211 switch (err) {
4212 case -NFS4ERR_BADNAME:
4213 err = -ENOENT;
4214 goto out;
4215 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004216 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004217 if (err == -NFS4ERR_MOVED)
4218 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004219 goto out;
4220 case -NFS4ERR_WRONGSEC:
4221 err = -EPERM;
4222 if (client != *clnt)
4223 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004224 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004225 if (IS_ERR(client))
4226 return PTR_ERR(client);
4227
4228 exception.retry = 1;
4229 break;
4230 default:
4231 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4232 }
4233 } while (exception.retry);
4234
4235out:
4236 if (err == 0)
4237 *clnt = client;
4238 else if (client != *clnt)
4239 rpc_shutdown_client(client);
4240
4241 return err;
4242}
4243
Al Virobeffb8f2016-07-20 16:34:42 -04004244static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004245 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4246 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004248 int status;
4249 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004250
David Quigley1775fd32013-05-22 12:50:42 -04004251 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004252 if (client != NFS_CLIENT(dir)) {
4253 rpc_shutdown_client(client);
4254 nfs_fixup_secinfo_attributes(fattr);
4255 }
4256 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257}
4258
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004259struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004260nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004261 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4262{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004263 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004264 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004265
David Quigley1775fd32013-05-22 12:50:42 -04004266 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004267 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004268 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004269 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004270}
4271
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004272static int _nfs4_proc_lookupp(struct inode *inode,
4273 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4274 struct nfs4_label *label)
4275{
4276 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4277 struct nfs_server *server = NFS_SERVER(inode);
4278 int status;
4279 struct nfs4_lookupp_arg args = {
4280 .bitmask = server->attr_bitmask,
4281 .fh = NFS_FH(inode),
4282 };
4283 struct nfs4_lookupp_res res = {
4284 .server = server,
4285 .fattr = fattr,
4286 .label = label,
4287 .fh = fhandle,
4288 };
4289 struct rpc_message msg = {
4290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4291 .rpc_argp = &args,
4292 .rpc_resp = &res,
4293 };
4294
4295 args.bitmask = nfs4_bitmask(server, label);
4296
4297 nfs_fattr_init(fattr);
4298
4299 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4300 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4301 &res.seq_res, 0);
4302 dprintk("NFS reply lookupp: %d\n", status);
4303 return status;
4304}
4305
4306static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4307 struct nfs_fattr *fattr, struct nfs4_label *label)
4308{
Trond Myklebust0688e642019-04-07 13:59:09 -04004309 struct nfs4_exception exception = {
4310 .interruptible = true,
4311 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004312 int err;
4313 do {
4314 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4315 trace_nfs4_lookupp(inode, err);
4316 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4317 &exception);
4318 } while (exception.retry);
4319 return err;
4320}
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4323{
Trond Myklebust76b32992007-08-10 17:45:11 -04004324 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 struct nfs4_accessargs args = {
4326 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004327 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004329 struct nfs4_accessres res = {
4330 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004331 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 struct rpc_message msg = {
4333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4334 .rpc_argp = &args,
4335 .rpc_resp = &res,
4336 .rpc_cred = entry->cred,
4337 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004338 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Trond Myklebust7c672652018-06-04 15:00:53 -04004340 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004341 res.fattr = nfs_alloc_fattr();
4342 if (res.fattr == NULL)
4343 return -ENOMEM;
4344 args.bitmask = server->cache_consistency_bitmask;
4345 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004346 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004348 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004349 if (res.fattr)
4350 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004352 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 return status;
4354}
4355
4356static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4357{
Trond Myklebust0688e642019-04-07 13:59:09 -04004358 struct nfs4_exception exception = {
4359 .interruptible = true,
4360 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 int err;
4362 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004363 err = _nfs4_proc_access(inode, entry);
4364 trace_nfs4_access(inode, err);
4365 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 &exception);
4367 } while (exception.retry);
4368 return err;
4369}
4370
4371/*
4372 * TODO: For the time being, we don't try to get any attributes
4373 * along with any of the zero-copy operations READ, READDIR,
4374 * READLINK, WRITE.
4375 *
4376 * In the case of the first three, we want to put the GETATTR
4377 * after the read-type operation -- this is because it is hard
4378 * to predict the length of a GETATTR response in v4, and thus
4379 * align the READ data correctly. This means that the GETATTR
4380 * may end up partially falling into the page cache, and we should
4381 * shift it into the 'tail' of the xdr_buf before processing.
4382 * To do this efficiently, we need to know the total length
4383 * of data received, which doesn't seem to be available outside
4384 * of the RPC layer.
4385 *
4386 * In the case of WRITE, we also want to put the GETATTR after
4387 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004388 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 *
4390 * Both of these changes to the XDR layer would in fact be quite
4391 * minor, but I decided to leave them for a subsequent patch.
4392 */
4393static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4394 unsigned int pgbase, unsigned int pglen)
4395{
4396 struct nfs4_readlink args = {
4397 .fh = NFS_FH(inode),
4398 .pgbase = pgbase,
4399 .pglen = pglen,
4400 .pages = &page,
4401 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004402 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 struct rpc_message msg = {
4404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4405 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004406 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 };
4408
Bryan Schumaker7c513052011-03-24 17:12:24 +00004409 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 -07004410}
4411
4412static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4413 unsigned int pgbase, unsigned int pglen)
4414{
Trond Myklebust0688e642019-04-07 13:59:09 -04004415 struct nfs4_exception exception = {
4416 .interruptible = true,
4417 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 int err;
4419 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004420 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4421 trace_nfs4_readlink(inode, err);
4422 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 &exception);
4424 } while (exception.retry);
4425 return err;
4426}
4427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004429 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431static int
4432nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004433 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004435 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004436 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004437 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 int status = 0;
4440
NeilBrown532d4de2016-10-13 15:26:47 +11004441 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004442 if (IS_ERR(ctx))
4443 return PTR_ERR(ctx);
4444
David Quigleyaa9c2662013-05-22 12:50:44 -04004445 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4446
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004447 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4448 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004449 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 if (IS_ERR(state)) {
4451 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004452 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004455 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004456 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 return status;
4458}
4459
Trond Myklebust3c591172018-07-31 15:54:10 -04004460static int
4461_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462{
Trond Myklebust16e42952005-10-27 22:12:44 -04004463 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004464 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004466 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004468 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004469 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004470 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4473 .rpc_argp = &args,
4474 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004476 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004477 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004478
Bryan Schumaker7c513052011-03-24 17:12:24 +00004479 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004480 if (status == 0) {
4481 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004482 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004483 /* Removing a directory decrements nlink in the parent */
4484 if (ftype == NF4DIR && dir->i_nlink > 2)
4485 nfs4_dec_nlink_locked(dir);
4486 spin_unlock(&dir->i_lock);
4487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 return status;
4489}
4490
Trond Myklebust912678d2018-03-20 16:43:15 -04004491static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4492{
Trond Myklebust0688e642019-04-07 13:59:09 -04004493 struct nfs4_exception exception = {
4494 .interruptible = true,
4495 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004496 struct inode *inode = d_inode(dentry);
4497 int err;
4498
Trond Myklebustc01d3642018-03-20 16:43:20 -04004499 if (inode) {
4500 if (inode->i_nlink == 1)
4501 nfs4_inode_return_delegation(inode);
4502 else
4503 nfs4_inode_make_writeable(inode);
4504 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004505 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004506 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004507 trace_nfs4_remove(dir, &dentry->d_name, err);
4508 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4509 &exception);
4510 } while (exception.retry);
4511 return err;
4512}
4513
4514static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515{
Trond Myklebust0688e642019-04-07 13:59:09 -04004516 struct nfs4_exception exception = {
4517 .interruptible = true,
4518 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004522 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004523 trace_nfs4_remove(dir, name, err);
4524 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 &exception);
4526 } while (exception.retry);
4527 return err;
4528}
4529
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004530static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4531 struct dentry *dentry,
4532 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004534 struct nfs_removeargs *args = msg->rpc_argp;
4535 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
Trond Myklebust977fcc22018-03-20 16:43:17 -04004537 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004539 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004540
4541 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004542
4543 if (inode)
4544 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545}
4546
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004547static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4548{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004549 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004550 &data->args.seq_args,
4551 &data->res.seq_res,
4552 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553}
4554
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004555static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004557 struct nfs_unlinkdata *data = task->tk_calldata;
4558 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004559
Trond Myklebust14516c32010-07-31 14:29:06 -04004560 if (!nfs4_sequence_done(task, &res->seq_res))
4561 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004562 if (nfs4_async_handle_error(task, res->server, NULL,
4563 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004564 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004565 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004566 update_changeattr(dir, &res->cinfo,
4567 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004568 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569}
4570
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004571static void nfs4_proc_rename_setup(struct rpc_message *msg,
4572 struct dentry *old_dentry,
4573 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004574{
Jeff Laytond3d41522010-09-17 17:31:57 -04004575 struct nfs_renameargs *arg = msg->rpc_argp;
4576 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004577 struct inode *old_inode = d_inode(old_dentry);
4578 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004579
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004580 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004581 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004582 if (new_inode)
4583 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004584 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004585 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004586 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004587}
4588
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004589static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4590{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004591 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004592 &data->args.seq_args,
4593 &data->res.seq_res,
4594 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004595}
4596
4597static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4598 struct inode *new_dir)
4599{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004600 struct nfs_renamedata *data = task->tk_calldata;
4601 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004602
4603 if (!nfs4_sequence_done(task, &res->seq_res))
4604 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004605 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004606 return 0;
4607
Trond Myklebustc733c492017-01-11 12:32:26 -05004608 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004609 if (new_dir != old_dir) {
4610 /* Note: If we moved a directory, nlink will change */
4611 update_changeattr(old_dir, &res->old_cinfo,
4612 res->old_fattr->time_start,
4613 NFS_INO_INVALID_OTHER);
4614 update_changeattr(new_dir, &res->new_cinfo,
4615 res->new_fattr->time_start,
4616 NFS_INO_INVALID_OTHER);
4617 } else
4618 update_changeattr(old_dir, &res->old_cinfo,
4619 res->old_fattr->time_start,
4620 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004621 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004622 return 1;
4623}
4624
Al Virobeffb8f2016-07-20 16:34:42 -04004625static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004627 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004628 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 struct nfs4_link_arg arg = {
4630 .fh = NFS_FH(inode),
4631 .dir_fh = NFS_FH(dir),
4632 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004633 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004635 struct nfs4_link_res res = {
4636 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004637 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004638 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 struct rpc_message msg = {
4640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4641 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004642 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004644 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Trond Myklebust136f2622010-04-16 16:22:49 -04004646 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004647 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004648 goto out;
4649
David Quigley14c43f72013-05-22 12:50:43 -04004650 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4651 if (IS_ERR(res.label)) {
4652 status = PTR_ERR(res.label);
4653 goto out;
4654 }
4655
Trond Myklebustc01d3642018-03-20 16:43:20 -04004656 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004657 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004658
Bryan Schumaker7c513052011-03-24 17:12:24 +00004659 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004660 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004661 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004662 status = nfs_post_op_update_inode(inode, res.fattr);
4663 if (!status)
4664 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004665 }
David Quigley14c43f72013-05-22 12:50:43 -04004666
4667
4668 nfs4_label_free(res.label);
4669
Trond Myklebust136f2622010-04-16 16:22:49 -04004670out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004671 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 return status;
4673}
4674
Al Virobeffb8f2016-07-20 16:34:42 -04004675static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676{
Trond Myklebust0688e642019-04-07 13:59:09 -04004677 struct nfs4_exception exception = {
4678 .interruptible = true,
4679 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 int err;
4681 do {
4682 err = nfs4_handle_exception(NFS_SERVER(inode),
4683 _nfs4_proc_link(inode, dir, name),
4684 &exception);
4685 } while (exception.retry);
4686 return err;
4687}
4688
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004689struct nfs4_createdata {
4690 struct rpc_message msg;
4691 struct nfs4_create_arg arg;
4692 struct nfs4_create_res res;
4693 struct nfs_fh fh;
4694 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004695 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004696};
4697
4698static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004699 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004700{
4701 struct nfs4_createdata *data;
4702
4703 data = kzalloc(sizeof(*data), GFP_KERNEL);
4704 if (data != NULL) {
4705 struct nfs_server *server = NFS_SERVER(dir);
4706
David Quigley14c43f72013-05-22 12:50:43 -04004707 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4708 if (IS_ERR(data->label))
4709 goto out_free;
4710
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004711 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4712 data->msg.rpc_argp = &data->arg;
4713 data->msg.rpc_resp = &data->res;
4714 data->arg.dir_fh = NFS_FH(dir);
4715 data->arg.server = server;
4716 data->arg.name = name;
4717 data->arg.attrs = sattr;
4718 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004719 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004720 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004721 data->res.server = server;
4722 data->res.fh = &data->fh;
4723 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004724 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004725 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004726 }
4727 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004728out_free:
4729 kfree(data);
4730 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004731}
4732
4733static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4734{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004735 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004736 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004737 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004738 spin_lock(&dir->i_lock);
4739 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004740 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004741 /* Creating a directory bumps nlink in the parent */
4742 if (data->arg.ftype == NF4DIR)
4743 nfs4_inc_nlink_locked(dir);
4744 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004745 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004746 }
4747 return status;
4748}
4749
4750static void nfs4_free_createdata(struct nfs4_createdata *data)
4751{
David Quigley14c43f72013-05-22 12:50:43 -04004752 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004753 kfree(data);
4754}
4755
Chuck Lever4f390c12006-08-22 20:06:22 -04004756static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004757 struct page *page, unsigned int len, struct iattr *sattr,
4758 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004760 struct nfs4_createdata *data;
4761 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Chuck Lever94a6d752006-08-22 20:06:23 -04004763 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004764 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004765
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004766 status = -ENOMEM;
4767 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4768 if (data == NULL)
4769 goto out;
4770
4771 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4772 data->arg.u.symlink.pages = &page;
4773 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004774 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004776 status = nfs4_do_create(dir, dentry, data);
4777
4778 nfs4_free_createdata(data);
4779out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 return status;
4781}
4782
Chuck Lever4f390c12006-08-22 20:06:22 -04004783static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004784 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785{
Trond Myklebust0688e642019-04-07 13:59:09 -04004786 struct nfs4_exception exception = {
4787 .interruptible = true,
4788 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004789 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004791
4792 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004795 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4796 trace_nfs4_symlink(dir, &dentry->d_name, err);
4797 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 &exception);
4799 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004800
4801 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 return err;
4803}
4804
4805static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004806 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004808 struct nfs4_createdata *data;
4809 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004811 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4812 if (data == NULL)
4813 goto out;
4814
David Quigley1775fd32013-05-22 12:50:42 -04004815 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004816 status = nfs4_do_create(dir, dentry, data);
4817
4818 nfs4_free_createdata(data);
4819out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 return status;
4821}
4822
4823static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4824 struct iattr *sattr)
4825{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004826 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004827 struct nfs4_exception exception = {
4828 .interruptible = true,
4829 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004830 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004832
David Quigleyaa9c2662013-05-22 12:50:44 -04004833 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4834
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004835 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4836 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004838 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4839 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4840 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 &exception);
4842 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004843 nfs4_label_release_security(label);
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 return err;
4846}
4847
NeilBrown684f39b2018-12-03 11:30:30 +11004848static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004849 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850{
David Howells2b0143b2015-03-17 22:25:59 +00004851 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 struct nfs4_readdir_arg args = {
4853 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004854 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 .pgbase = 0,
4856 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004857 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004858 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 };
4860 struct nfs4_readdir_res res;
4861 struct rpc_message msg = {
4862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4863 .rpc_argp = &args,
4864 .rpc_resp = &res,
4865 .rpc_cred = cred,
4866 };
4867 int status;
4868
Al Viro6de14722013-09-16 10:53:17 -04004869 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4870 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004871 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004872 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004874 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 -05004875 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004876 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004877 status += args.pgbase;
4878 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004879
4880 nfs_invalidate_atime(dir);
4881
Harvey Harrison3110ff82008-05-02 13:42:44 -07004882 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 return status;
4884}
4885
NeilBrown684f39b2018-12-03 11:30:30 +11004886static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004887 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888{
Trond Myklebust0688e642019-04-07 13:59:09 -04004889 struct nfs4_exception exception = {
4890 .interruptible = true,
4891 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 int err;
4893 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004894 err = _nfs4_proc_readdir(dentry, cred, cookie,
4895 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004896 trace_nfs4_readdir(d_inode(dentry), err);
4897 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 &exception);
4899 } while (exception.retry);
4900 return err;
4901}
4902
4903static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004904 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004906 struct nfs4_createdata *data;
4907 int mode = sattr->ia_mode;
4908 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004910 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4911 if (data == NULL)
4912 goto out;
4913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004915 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004917 data->arg.ftype = NF4BLK;
4918 data->arg.u.device.specdata1 = MAJOR(rdev);
4919 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 }
4921 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004922 data->arg.ftype = NF4CHR;
4923 data->arg.u.device.specdata1 = MAJOR(rdev);
4924 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004925 } else if (!S_ISSOCK(mode)) {
4926 status = -EINVAL;
4927 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 }
David Quigley1775fd32013-05-22 12:50:42 -04004929
David Quigleyaa9c2662013-05-22 12:50:44 -04004930 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004931 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004932out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004933 nfs4_free_createdata(data);
4934out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 return status;
4936}
4937
4938static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4939 struct iattr *sattr, dev_t rdev)
4940{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004941 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004942 struct nfs4_exception exception = {
4943 .interruptible = true,
4944 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004945 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004947
David Quigleyaa9c2662013-05-22 12:50:44 -04004948 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4949
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004950 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4951 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004953 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4954 trace_nfs4_mknod(dir, &dentry->d_name, err);
4955 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 &exception);
4957 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004958
4959 nfs4_label_release_security(label);
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 return err;
4962}
4963
4964static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4965 struct nfs_fsstat *fsstat)
4966{
4967 struct nfs4_statfs_arg args = {
4968 .fh = fhandle,
4969 .bitmask = server->attr_bitmask,
4970 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004971 struct nfs4_statfs_res res = {
4972 .fsstat = fsstat,
4973 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 struct rpc_message msg = {
4975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4976 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004977 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 };
4979
Trond Myklebust0e574af2005-10-27 22:12:38 -04004980 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004981 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982}
4983
4984static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4985{
Trond Myklebust0688e642019-04-07 13:59:09 -04004986 struct nfs4_exception exception = {
4987 .interruptible = true,
4988 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 int err;
4990 do {
4991 err = nfs4_handle_exception(server,
4992 _nfs4_proc_statfs(server, fhandle, fsstat),
4993 &exception);
4994 } while (exception.retry);
4995 return err;
4996}
4997
4998static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4999 struct nfs_fsinfo *fsinfo)
5000{
5001 struct nfs4_fsinfo_arg args = {
5002 .fh = fhandle,
5003 .bitmask = server->attr_bitmask,
5004 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005005 struct nfs4_fsinfo_res res = {
5006 .fsinfo = fsinfo,
5007 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 struct rpc_message msg = {
5009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5010 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005011 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 };
5013
Bryan Schumaker7c513052011-03-24 17:12:24 +00005014 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015}
5016
5017static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5018{
Trond Myklebust0688e642019-04-07 13:59:09 -04005019 struct nfs4_exception exception = {
5020 .interruptible = true,
5021 };
Chuck Lever83ca7f52013-03-16 15:55:53 -04005022 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 int err;
5024
5025 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005026 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005027 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005028 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04005029 nfs4_set_lease_period(server->nfs_client,
5030 fsinfo->lease_time * HZ,
5031 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005032 break;
5033 }
5034 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 } while (exception.retry);
5036 return err;
5037}
5038
5039static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5040{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005041 int error;
5042
Trond Myklebust0e574af2005-10-27 22:12:38 -04005043 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005044 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005045 if (error == 0) {
5046 /* block layout checks this! */
5047 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005048 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005049 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005050
5051 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052}
5053
5054static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5055 struct nfs_pathconf *pathconf)
5056{
5057 struct nfs4_pathconf_arg args = {
5058 .fh = fhandle,
5059 .bitmask = server->attr_bitmask,
5060 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005061 struct nfs4_pathconf_res res = {
5062 .pathconf = pathconf,
5063 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 struct rpc_message msg = {
5065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5066 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005067 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 };
5069
5070 /* None of the pathconf attributes are mandatory to implement */
5071 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5072 memset(pathconf, 0, sizeof(*pathconf));
5073 return 0;
5074 }
5075
Trond Myklebust0e574af2005-10-27 22:12:38 -04005076 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005077 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078}
5079
5080static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5081 struct nfs_pathconf *pathconf)
5082{
Trond Myklebust0688e642019-04-07 13:59:09 -04005083 struct nfs4_exception exception = {
5084 .interruptible = true,
5085 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 int err;
5087
5088 do {
5089 err = nfs4_handle_exception(server,
5090 _nfs4_proc_pathconf(server, fhandle, pathconf),
5091 &exception);
5092 } while (exception.retry);
5093 return err;
5094}
5095
Trond Myklebust5521abf2013-03-16 20:54:34 -04005096int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005097 const struct nfs_open_context *ctx,
5098 const struct nfs_lock_context *l_ctx,
5099 fmode_t fmode)
5100{
NeilBrown17393472016-10-13 15:26:47 +11005101 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005102}
5103EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5104
Trond Myklebust5521abf2013-03-16 20:54:34 -04005105static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5106 const struct nfs_open_context *ctx,
5107 const struct nfs_lock_context *l_ctx,
5108 fmode_t fmode)
5109{
5110 nfs4_stateid current_stateid;
5111
Trond Myklebuste1253be2014-03-05 08:44:23 -05005112 /* If the current stateid represents a lost lock, then exit */
5113 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
5114 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005115 return nfs4_stateid_match(stateid, &current_stateid);
5116}
5117
5118static bool nfs4_error_stateid_expired(int err)
5119{
5120 switch (err) {
5121 case -NFS4ERR_DELEG_REVOKED:
5122 case -NFS4ERR_ADMIN_REVOKED:
5123 case -NFS4ERR_BAD_STATEID:
5124 case -NFS4ERR_STALE_STATEID:
5125 case -NFS4ERR_OLD_STATEID:
5126 case -NFS4ERR_OPENMODE:
5127 case -NFS4ERR_EXPIRED:
5128 return true;
5129 }
5130 return false;
5131}
5132
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005133static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005135 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005137 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005138 if (task->tk_status < 0) {
5139 struct nfs4_exception exception = {
5140 .inode = hdr->inode,
5141 .state = hdr->args.context->state,
5142 .stateid = &hdr->args.stateid,
5143 };
5144 task->tk_status = nfs4_async_handle_exception(task,
5145 server, task->tk_status, &exception);
5146 if (exception.retry) {
5147 rpc_restart_call_prepare(task);
5148 return -EAGAIN;
5149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005151
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005153 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155}
5156
Trond Myklebust5521abf2013-03-16 20:54:34 -04005157static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005158 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005159{
5160
5161 if (!nfs4_error_stateid_expired(task->tk_status) ||
5162 nfs4_stateid_is_current(&args->stateid,
5163 args->context,
5164 args->lock_context,
5165 FMODE_READ))
5166 return false;
5167 rpc_restart_call_prepare(task);
5168 return true;
5169}
5170
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005171static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005172{
5173
5174 dprintk("--> %s\n", __func__);
5175
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005176 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005177 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005178 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005179 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005180 if (task->tk_status > 0)
5181 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005182 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5183 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005184}
5185
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005186static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5187 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005189 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005190 if (!hdr->pgio_done_cb)
5191 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005192 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005193 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194}
5195
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005196static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5197 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005198{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005199 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005200 &hdr->args.seq_args,
5201 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005202 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005203 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005204 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5205 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005206 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005207 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005208 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005209 return -EIO;
5210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211}
5212
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005213static int nfs4_write_done_cb(struct rpc_task *task,
5214 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005216 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005217
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005218 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005219 if (task->tk_status < 0) {
5220 struct nfs4_exception exception = {
5221 .inode = hdr->inode,
5222 .state = hdr->args.context->state,
5223 .stateid = &hdr->args.stateid,
5224 };
5225 task->tk_status = nfs4_async_handle_exception(task,
5226 NFS_SERVER(inode), task->tk_status,
5227 &exception);
5228 if (exception.retry) {
5229 rpc_restart_call_prepare(task);
5230 return -EAGAIN;
5231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005233 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005234 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005235 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005236 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238}
5239
Trond Myklebust5521abf2013-03-16 20:54:34 -04005240static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005241 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005242{
5243
5244 if (!nfs4_error_stateid_expired(task->tk_status) ||
5245 nfs4_stateid_is_current(&args->stateid,
5246 args->context,
5247 args->lock_context,
5248 FMODE_WRITE))
5249 return false;
5250 rpc_restart_call_prepare(task);
5251 return true;
5252}
5253
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005254static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005255{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005256 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005257 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005258 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005259 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005260 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5261 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005262}
5263
Trond Myklebust5a37f852012-04-28 14:55:16 -04005264static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005265bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005266{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005267 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005268 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005269 return false;
5270 /* Otherwise, request attributes if and only if we don't hold
5271 * a delegation
5272 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005273 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005274}
Fred Isamana69aef12011-03-03 15:13:47 +00005275
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005276static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005277 struct rpc_message *msg,
5278 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005280 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005281
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005282 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5283 hdr->args.bitmask = NULL;
5284 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005285 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005286 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005287
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005288 if (!hdr->pgio_done_cb)
5289 hdr->pgio_done_cb = nfs4_write_done_cb;
5290 hdr->res.server = server;
5291 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005293 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005294 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005295 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296}
5297
Fred Isaman0b7c0152012-04-20 14:47:39 -04005298static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5299{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005300 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005301 &data->args.seq_args,
5302 &data->res.seq_res,
5303 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304}
5305
Fred Isaman0b7c0152012-04-20 14:47:39 -04005306static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005309
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005310 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005311 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5312 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005313 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005314 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317}
5318
Fred Isaman0b7c0152012-04-20 14:47:39 -04005319static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005320{
5321 if (!nfs4_sequence_done(task, &data->res.seq_res))
5322 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005323 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005324}
5325
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005326static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5327 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005329 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005330
Fred Isaman0b7c0152012-04-20 14:47:39 -04005331 if (data->commit_done_cb == NULL)
5332 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005333 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005334 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005335 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005336 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337}
5338
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005339static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5340 struct nfs_commitres *res)
5341{
5342 struct inode *dst_inode = file_inode(dst);
5343 struct nfs_server *server = NFS_SERVER(dst_inode);
5344 struct rpc_message msg = {
5345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5346 .rpc_argp = args,
5347 .rpc_resp = res,
5348 };
5349
5350 args->fh = NFS_FH(dst_inode);
5351 return nfs4_call_sync(server->client, server, &msg,
5352 &args->seq_args, &res->seq_res, 1);
5353}
5354
5355int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5356{
5357 struct nfs_commitargs args = {
5358 .offset = offset,
5359 .count = count,
5360 };
5361 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5362 struct nfs4_exception exception = { };
5363 int status;
5364
5365 do {
5366 status = _nfs4_proc_commit(dst, &args, res);
5367 status = nfs4_handle_exception(dst_server, status, &exception);
5368 } while (exception.retry);
5369
5370 return status;
5371}
5372
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005373struct nfs4_renewdata {
5374 struct nfs_client *client;
5375 unsigned long timestamp;
5376};
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378/*
5379 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5380 * standalone procedure for queueing an asynchronous RENEW.
5381 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005382static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005383{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005384 struct nfs4_renewdata *data = calldata;
5385 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005386
Elena Reshetova212bf412017-10-20 12:53:38 +03005387 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005388 nfs4_schedule_state_renewal(clp);
5389 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005390 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005391}
5392
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005393static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005395 struct nfs4_renewdata *data = calldata;
5396 struct nfs_client *clp = data->client;
5397 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005399 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005400 switch (task->tk_status) {
5401 case 0:
5402 break;
5403 case -NFS4ERR_LEASE_MOVED:
5404 nfs4_schedule_lease_moved_recovery(clp);
5405 break;
5406 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005407 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005408 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5409 return;
5410 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005411 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005412 return;
5413 }
5414 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005416 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417}
5418
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005419static const struct rpc_call_ops nfs4_renew_ops = {
5420 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005421 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005422};
5423
NeilBrowna52458b2018-12-03 11:30:31 +11005424static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425{
5426 struct rpc_message msg = {
5427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5428 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005429 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005431 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005433 if (renew_flags == 0)
5434 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005435 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005436 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005437 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005438 if (data == NULL) {
5439 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005440 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005441 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005442 data->client = clp;
5443 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005444 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005445 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446}
5447
NeilBrowna52458b2018-12-03 11:30:31 +11005448static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449{
5450 struct rpc_message msg = {
5451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5452 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005453 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 };
5455 unsigned long now = jiffies;
5456 int status;
5457
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005458 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 if (status < 0)
5460 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005461 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 return 0;
5463}
5464
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005465static inline int nfs4_server_supports_acls(struct nfs_server *server)
5466{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005467 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005468}
5469
Trond Myklebust21f498c2012-08-24 10:59:25 -04005470/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5471 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005472 * the stack.
5473 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005474#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005475
Neil Hormane9e3d722011-03-04 19:26:03 -05005476static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005477 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005478{
5479 struct page *newpage, **spages;
5480 int rc = 0;
5481 size_t len;
5482 spages = pages;
5483
5484 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005485 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005486 newpage = alloc_page(GFP_KERNEL);
5487
5488 if (newpage == NULL)
5489 goto unwind;
5490 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005491 buf += len;
5492 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005493 *pages++ = newpage;
5494 rc++;
5495 } while (buflen != 0);
5496
5497 return rc;
5498
5499unwind:
5500 for(; rc > 0; rc--)
5501 __free_page(spages[rc-1]);
5502 return -ENOMEM;
5503}
5504
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005505struct nfs4_cached_acl {
5506 int cached;
5507 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005508 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005509};
5510
5511static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005512{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005513 struct nfs_inode *nfsi = NFS_I(inode);
5514
5515 spin_lock(&inode->i_lock);
5516 kfree(nfsi->nfs4_acl);
5517 nfsi->nfs4_acl = acl;
5518 spin_unlock(&inode->i_lock);
5519}
5520
5521static void nfs4_zap_acl_attr(struct inode *inode)
5522{
5523 nfs4_set_cached_acl(inode, NULL);
5524}
5525
5526static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5527{
5528 struct nfs_inode *nfsi = NFS_I(inode);
5529 struct nfs4_cached_acl *acl;
5530 int ret = -ENOENT;
5531
5532 spin_lock(&inode->i_lock);
5533 acl = nfsi->nfs4_acl;
5534 if (acl == NULL)
5535 goto out;
5536 if (buf == NULL) /* user is just asking for length */
5537 goto out_len;
5538 if (acl->cached == 0)
5539 goto out;
5540 ret = -ERANGE; /* see getxattr(2) man page */
5541 if (acl->len > buflen)
5542 goto out;
5543 memcpy(buf, acl->data, acl->len);
5544out_len:
5545 ret = acl->len;
5546out:
5547 spin_unlock(&inode->i_lock);
5548 return ret;
5549}
5550
Sachin Prabhu5794d212012-04-17 14:36:40 +01005551static 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 +00005552{
5553 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005554 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005555
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005556 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005557 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005558 if (acl == NULL)
5559 goto out;
5560 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005561 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005562 } else {
5563 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5564 if (acl == NULL)
5565 goto out;
5566 acl->cached = 0;
5567 }
5568 acl->len = acl_len;
5569out:
5570 nfs4_set_cached_acl(inode, acl);
5571}
5572
Andy Adamsonbf118a32011-12-07 11:55:27 -05005573/*
5574 * The getxattr API returns the required buffer length when called with a
5575 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5576 * the required buf. On a NULL buf, we send a page of data to the server
5577 * guessing that the ACL request can be serviced by a page. If so, we cache
5578 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5579 * the cache. If not so, we throw away the page, and cache the required
5580 * length. The next getxattr call will then produce another round trip to
5581 * the server, this time with the input buf of the required size.
5582 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005583static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005584{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005585 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005586 struct nfs_getaclargs args = {
5587 .fh = NFS_FH(inode),
5588 .acl_pages = pages,
5589 .acl_len = buflen,
5590 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005591 struct nfs_getaclres res = {
5592 .acl_len = buflen,
5593 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005594 struct rpc_message msg = {
5595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5596 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005597 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005598 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005599 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005600 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005601
Trond Myklebust21f498c2012-08-24 10:59:25 -04005602 if (npages > ARRAY_SIZE(pages))
5603 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005604
Andy Adamsonbf118a32011-12-07 11:55:27 -05005605 for (i = 0; i < npages; i++) {
5606 pages[i] = alloc_page(GFP_KERNEL);
5607 if (!pages[i])
5608 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005609 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005610
5611 /* for decoding across pages */
5612 res.acl_scratch = alloc_page(GFP_KERNEL);
5613 if (!res.acl_scratch)
5614 goto out_free;
5615
Andy Adamsonbf118a32011-12-07 11:55:27 -05005616 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005617
Peng Taode040be2012-01-10 22:42:47 +08005618 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005619 __func__, buf, buflen, npages, args.acl_len);
5620 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5621 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005622 if (ret)
5623 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005624
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005625 /* Handle the case where the passed-in buffer is too short */
5626 if (res.acl_flags & NFS4_ACL_TRUNC) {
5627 /* Did the user only issue a request for the acl length? */
5628 if (buf == NULL)
5629 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005630 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005631 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005632 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005633 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005634 if (buf) {
5635 if (res.acl_len > buflen) {
5636 ret = -ERANGE;
5637 goto out_free;
5638 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005639 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005640 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005641out_ok:
5642 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005643out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005644 for (i = 0; i < npages; i++)
5645 if (pages[i])
5646 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005647 if (res.acl_scratch)
5648 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005649 return ret;
5650}
5651
Trond Myklebust16b42892006-08-24 12:27:15 -04005652static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5653{
Trond Myklebust0688e642019-04-07 13:59:09 -04005654 struct nfs4_exception exception = {
5655 .interruptible = true,
5656 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005657 ssize_t ret;
5658 do {
5659 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005660 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005661 if (ret >= 0)
5662 break;
5663 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5664 } while (exception.retry);
5665 return ret;
5666}
5667
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005668static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5669{
5670 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005671 int ret;
5672
5673 if (!nfs4_server_supports_acls(server))
5674 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005675 ret = nfs_revalidate_inode(server, inode);
5676 if (ret < 0)
5677 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005678 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5679 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005680 ret = nfs4_read_cached_acl(inode, buf, buflen);
5681 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005682 /* -ENOENT is returned if there is no ACL or if there is an ACL
5683 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005684 return ret;
5685 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005686}
5687
Trond Myklebust16b42892006-08-24 12:27:15 -04005688static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005689{
5690 struct nfs_server *server = NFS_SERVER(inode);
5691 struct page *pages[NFS4ACL_MAXPAGES];
5692 struct nfs_setaclargs arg = {
5693 .fh = NFS_FH(inode),
5694 .acl_pages = pages,
5695 .acl_len = buflen,
5696 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005697 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005698 struct rpc_message msg = {
5699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5700 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005701 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005702 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005703 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005704 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005705
5706 if (!nfs4_server_supports_acls(server))
5707 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005708 if (npages > ARRAY_SIZE(pages))
5709 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005710 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005711 if (i < 0)
5712 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005713 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005714 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005715
5716 /*
5717 * Free each page after tx, so the only ref left is
5718 * held by the network stack
5719 */
5720 for (; i > 0; i--)
5721 put_page(pages[i-1]);
5722
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005723 /*
5724 * Acl update can result in inode attribute update.
5725 * so mark the attribute cache invalid.
5726 */
5727 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005728 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005729 | NFS_INO_INVALID_CTIME
5730 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005731 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005732 nfs_access_zap_cache(inode);
5733 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005734 return ret;
5735}
5736
Trond Myklebust16b42892006-08-24 12:27:15 -04005737static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5738{
5739 struct nfs4_exception exception = { };
5740 int err;
5741 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005742 err = __nfs4_proc_set_acl(inode, buf, buflen);
5743 trace_nfs4_set_acl(inode, err);
5744 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005745 &exception);
5746 } while (exception.retry);
5747 return err;
5748}
5749
David Quigleyaa9c2662013-05-22 12:50:44 -04005750#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5751static int _nfs4_get_security_label(struct inode *inode, void *buf,
5752 size_t buflen)
5753{
5754 struct nfs_server *server = NFS_SERVER(inode);
5755 struct nfs_fattr fattr;
5756 struct nfs4_label label = {0, 0, buflen, buf};
5757
5758 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005759 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005760 .fh = NFS_FH(inode),
5761 .bitmask = bitmask,
5762 };
5763 struct nfs4_getattr_res res = {
5764 .fattr = &fattr,
5765 .label = &label,
5766 .server = server,
5767 };
5768 struct rpc_message msg = {
5769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005770 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005771 .rpc_resp = &res,
5772 };
5773 int ret;
5774
5775 nfs_fattr_init(&fattr);
5776
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005777 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005778 if (ret)
5779 return ret;
5780 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5781 return -ENOENT;
5782 if (buflen < label.len)
5783 return -ERANGE;
5784 return 0;
5785}
5786
5787static int nfs4_get_security_label(struct inode *inode, void *buf,
5788 size_t buflen)
5789{
Trond Myklebust0688e642019-04-07 13:59:09 -04005790 struct nfs4_exception exception = {
5791 .interruptible = true,
5792 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005793 int err;
5794
5795 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5796 return -EOPNOTSUPP;
5797
5798 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005799 err = _nfs4_get_security_label(inode, buf, buflen);
5800 trace_nfs4_get_security_label(inode, err);
5801 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005802 &exception);
5803 } while (exception.retry);
5804 return err;
5805}
5806
5807static int _nfs4_do_set_security_label(struct inode *inode,
5808 struct nfs4_label *ilabel,
5809 struct nfs_fattr *fattr,
5810 struct nfs4_label *olabel)
5811{
5812
5813 struct iattr sattr = {0};
5814 struct nfs_server *server = NFS_SERVER(inode);
5815 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005816 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005817 .fh = NFS_FH(inode),
5818 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005819 .server = server,
5820 .bitmask = bitmask,
5821 .label = ilabel,
5822 };
5823 struct nfs_setattrres res = {
5824 .fattr = fattr,
5825 .label = olabel,
5826 .server = server,
5827 };
5828 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5830 .rpc_argp = &arg,
5831 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005832 };
5833 int status;
5834
Jeff Layton12207f62013-11-01 10:49:32 -04005835 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005836
Jeff Layton12207f62013-11-01 10:49:32 -04005837 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005838 if (status)
5839 dprintk("%s failed: %d\n", __func__, status);
5840
5841 return status;
5842}
5843
5844static int nfs4_do_set_security_label(struct inode *inode,
5845 struct nfs4_label *ilabel,
5846 struct nfs_fattr *fattr,
5847 struct nfs4_label *olabel)
5848{
5849 struct nfs4_exception exception = { };
5850 int err;
5851
5852 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005853 err = _nfs4_do_set_security_label(inode, ilabel,
5854 fattr, olabel);
5855 trace_nfs4_set_security_label(inode, err);
5856 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005857 &exception);
5858 } while (exception.retry);
5859 return err;
5860}
5861
5862static int
Al Viro59301222016-05-27 10:19:30 -04005863nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005864{
5865 struct nfs4_label ilabel, *olabel = NULL;
5866 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005867 int status;
5868
5869 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5870 return -EOPNOTSUPP;
5871
5872 nfs_fattr_init(&fattr);
5873
5874 ilabel.pi = 0;
5875 ilabel.lfs = 0;
5876 ilabel.label = (char *)buf;
5877 ilabel.len = buflen;
5878
David Quigleyaa9c2662013-05-22 12:50:44 -04005879 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5880 if (IS_ERR(olabel)) {
5881 status = -PTR_ERR(olabel);
5882 goto out;
5883 }
5884
5885 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5886 if (status == 0)
5887 nfs_setsecurity(inode, &fattr, olabel);
5888
5889 nfs4_label_free(olabel);
5890out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005891 return status;
5892}
5893#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5894
5895
Chuck Leverf0920752012-05-21 22:45:41 -04005896static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5897 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005898{
5899 __be32 verf[2];
5900
Chuck Lever2c820d92012-05-21 22:45:33 -04005901 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5902 /* An impossible timestamp guarantees this value
5903 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005904 verf[0] = cpu_to_be32(U32_MAX);
5905 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005906 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005907 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005908 u64 ns = ktime_to_ns(nn->boot_time);
5909
5910 verf[0] = cpu_to_be32(ns >> 32);
5911 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005912 }
Chuck Levercd937102012-03-02 17:14:31 -05005913 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5914}
5915
Jeff Laytona3192682015-06-09 19:43:59 -04005916static int
5917nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005918{
Jeff Laytona3192682015-06-09 19:43:59 -04005919 size_t len;
5920 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005921
Trond Myklebustceb3a162015-01-03 15:16:04 -05005922 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005923 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005924
Jeff Laytona3192682015-06-09 19:43:59 -04005925 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005926 len = 14 +
5927 strlen(clp->cl_rpcclient->cl_nodename) +
5928 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005929 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005930 1;
5931 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005932 if (nfs4_client_id_uniquifier[0] != '\0')
5933 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005934 if (len > NFS4_OPAQUE_LIMIT + 1)
5935 return -EINVAL;
5936
5937 /*
5938 * Since this string is allocated at mount time, and held until the
5939 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5940 * about a memory-reclaim deadlock.
5941 */
5942 str = kmalloc(len, GFP_KERNEL);
5943 if (!str)
5944 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005945
Chuck Levere984a552012-09-14 17:24:21 -04005946 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005947 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005948 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005949 clp->cl_rpcclient->cl_nodename,
5950 nfs4_client_id_uniquifier,
5951 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005952 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005953 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005954 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005955 clp->cl_rpcclient->cl_nodename,
5956 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005957 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005958 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005959
Jeff Laytona3192682015-06-09 19:43:59 -04005960 clp->cl_owner_id = str;
5961 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005962}
5963
Jeff Layton873e3852015-06-09 19:44:00 -04005964static int
5965nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005966{
Jeff Layton873e3852015-06-09 19:44:00 -04005967 size_t len;
5968 char *str;
5969
5970 len = 10 + 10 + 1 + 10 + 1 +
5971 strlen(nfs4_client_id_uniquifier) + 1 +
5972 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5973
5974 if (len > NFS4_OPAQUE_LIMIT + 1)
5975 return -EINVAL;
5976
5977 /*
5978 * Since this string is allocated at mount time, and held until the
5979 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5980 * about a memory-reclaim deadlock.
5981 */
5982 str = kmalloc(len, GFP_KERNEL);
5983 if (!str)
5984 return -ENOMEM;
5985
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005986 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005987 clp->rpc_ops->version, clp->cl_minorversion,
5988 nfs4_client_id_uniquifier,
5989 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005990 clp->cl_owner_id = str;
5991 return 0;
5992}
5993
5994static int
5995nfs4_init_uniform_client_string(struct nfs_client *clp)
5996{
Jeff Layton873e3852015-06-09 19:44:00 -04005997 size_t len;
5998 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005999
6000 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006001 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006002
6003 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04006004 return nfs4_init_uniquifier_client_string(clp);
6005
6006 len = 10 + 10 + 1 + 10 + 1 +
6007 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6008
6009 if (len > NFS4_OPAQUE_LIMIT + 1)
6010 return -EINVAL;
6011
6012 /*
6013 * Since this string is allocated at mount time, and held until the
6014 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6015 * about a memory-reclaim deadlock.
6016 */
6017 str = kmalloc(len, GFP_KERNEL);
6018 if (!str)
6019 return -ENOMEM;
6020
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006021 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04006022 clp->rpc_ops->version, clp->cl_minorversion,
6023 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006024 clp->cl_owner_id = str;
6025 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006026}
6027
Chuck Lever706cb8d2014-03-12 12:51:47 -04006028/*
6029 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6030 * services. Advertise one based on the address family of the
6031 * clientaddr.
6032 */
6033static unsigned int
6034nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6035{
6036 if (strchr(clp->cl_ipaddr, ':') != NULL)
6037 return scnprintf(buf, len, "tcp6");
6038 else
6039 return scnprintf(buf, len, "tcp");
6040}
6041
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006042static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6043{
6044 struct nfs4_setclientid *sc = calldata;
6045
6046 if (task->tk_status == 0)
6047 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6048}
6049
6050static const struct rpc_call_ops nfs4_setclientid_ops = {
6051 .rpc_call_done = nfs4_setclientid_done,
6052};
6053
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006054/**
6055 * nfs4_proc_setclientid - Negotiate client ID
6056 * @clp: state data structure
6057 * @program: RPC program for NFSv4 callback service
6058 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006059 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006060 * @res: where to place the result
6061 *
6062 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6063 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006064int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006065 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006066 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067{
6068 nfs4_verifier sc_verifier;
6069 struct nfs4_setclientid setclientid = {
6070 .sc_verifier = &sc_verifier,
6071 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006072 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 };
6074 struct rpc_message msg = {
6075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6076 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006077 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006078 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006080 struct rpc_task_setup task_setup_data = {
6081 .rpc_client = clp->cl_rpcclient,
6082 .rpc_message = &msg,
6083 .callback_ops = &nfs4_setclientid_ops,
6084 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006085 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006086 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006087 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
Chuck Leverde734832012-07-11 16:30:50 -04006089 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006090 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006091
6092 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6093 status = nfs4_init_uniform_client_string(clp);
6094 else
Jeff Laytona3192682015-06-09 19:43:59 -04006095 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006096
6097 if (status)
6098 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006099
Chuck Leverde734832012-07-11 16:30:50 -04006100 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006101 setclientid.sc_netid_len =
6102 nfs4_init_callback_netid(clp,
6103 setclientid.sc_netid,
6104 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006105 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006106 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 clp->cl_ipaddr, port >> 8, port & 255);
6108
Jeff Layton3a6bb732015-06-09 19:43:57 -04006109 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006110 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006111 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006112
6113 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006114 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006115 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006116 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6117 put_rpccred(setclientid.sc_cred);
6118 }
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006119out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006120 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006121 dprintk("NFS reply setclientid: %d\n", status);
6122 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123}
6124
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006125/**
6126 * nfs4_proc_setclientid_confirm - Confirm client ID
6127 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006128 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006129 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006130 *
6131 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6132 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006133int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006134 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006135 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 struct rpc_message msg = {
6138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006139 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006140 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 int status;
6143
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006144 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6145 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6146 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006147 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6148 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006149 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006150 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 return status;
6152}
6153
Trond Myklebustfe650402006-01-03 09:55:18 +01006154struct nfs4_delegreturndata {
6155 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006156 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006157 struct nfs_fh fh;
6158 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006159 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006160 struct {
6161 struct nfs4_layoutreturn_args arg;
6162 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006163 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006164 u32 roc_barrier;
6165 bool roc;
6166 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006167 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006168 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006169 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006170};
6171
Trond Myklebustfe650402006-01-03 09:55:18 +01006172static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6173{
6174 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006175 struct nfs4_exception exception = {
6176 .inode = data->inode,
6177 .stateid = &data->stateid,
6178 };
Andy Adamson938e1012009-04-01 09:22:28 -04006179
Trond Myklebust14516c32010-07-31 14:29:06 -04006180 if (!nfs4_sequence_done(task, &data->res.seq_res))
6181 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006182
Trond Myklebustca8acf82013-08-13 10:36:56 -04006183 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006184
6185 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04006186 if (pnfs_roc_done(task, data->inode,
6187 &data->args.lr_args,
6188 &data->res.lr_res,
6189 &data->res.lr_ret) == -EAGAIN)
6190 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006191
Ricardo Labiaga79708862009-12-07 09:23:21 -05006192 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006193 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006194 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006195 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006196 case -NFS4ERR_ADMIN_REVOKED:
6197 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006198 case -NFS4ERR_EXPIRED:
6199 nfs4_free_revoked_stateid(data->res.server,
6200 data->args.stateid,
6201 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006202 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006203 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006204 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006205 task->tk_status = 0;
6206 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006207 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006208 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6209 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006210 if (data->args.bitmask) {
6211 data->args.bitmask = NULL;
6212 data->res.fattr = NULL;
6213 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006214 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006215 case -NFS4ERR_ACCESS:
6216 if (data->args.bitmask) {
6217 data->args.bitmask = NULL;
6218 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006219 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006220 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006221 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006222 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006223 task->tk_status = nfs4_async_handle_exception(task,
6224 data->res.server, task->tk_status,
6225 &exception);
6226 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006227 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006228 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006229 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006230 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006231 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006232out_restart:
6233 task->tk_status = 0;
6234 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006235}
6236
6237static void nfs4_delegreturn_release(void *calldata)
6238{
Peng Tao039b7562014-07-03 13:05:02 +08006239 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006240 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006241
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006242 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006243 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006244 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6245 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006246 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006247 nfs_iput_and_deactive(inode);
6248 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006249 kfree(calldata);
6250}
6251
Andy Adamson938e1012009-04-01 09:22:28 -04006252static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6253{
6254 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006255 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006256
6257 d_data = (struct nfs4_delegreturndata *)data;
6258
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006259 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08006260 return;
6261
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006262 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6263 if (lo && !pnfs_layout_is_valid(lo)) {
6264 d_data->args.lr_args = NULL;
6265 d_data->res.lr_res = NULL;
6266 }
6267
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006268 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006269 &d_data->args.seq_args,
6270 &d_data->res.seq_res,
6271 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006272}
Andy Adamson938e1012009-04-01 09:22:28 -04006273
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006274static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006275 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006276 .rpc_call_done = nfs4_delegreturn_done,
6277 .rpc_release = nfs4_delegreturn_release,
6278};
6279
NeilBrowna52458b2018-12-03 11:30:31 +11006280static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006281{
6282 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006283 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006284 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006285 struct rpc_message msg = {
6286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6287 .rpc_cred = cred,
6288 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006289 struct rpc_task_setup task_setup_data = {
6290 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006291 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006292 .callback_ops = &nfs4_delegreturn_ops,
6293 .flags = RPC_TASK_ASYNC,
6294 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006295 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006296
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006297 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006298 if (data == NULL)
6299 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006300 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006301
6302 nfs4_state_protect(server->nfs_client,
6303 NFS_SP4_MACH_CRED_CLEANUP,
6304 &task_setup_data.rpc_client, &msg);
6305
Trond Myklebustfe650402006-01-03 09:55:18 +01006306 data->args.fhandle = &data->fh;
6307 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006308 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006309 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006310 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006311 data->res.fattr = &data->fattr;
6312 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006313 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006314 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006315 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006316 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006317 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006318 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006319 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006320 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006321 if (data->lr.roc) {
6322 data->args.lr_args = &data->lr.arg;
6323 data->res.lr_res = &data->lr.res;
6324 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006325 } else if (data->lr.roc) {
6326 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6327 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006328 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006329
Trond Myklebustc970aa82007-07-14 15:39:59 -04006330 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006331 msg.rpc_argp = &data->args;
6332 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006333 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006334 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006335 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006336 if (!issync)
6337 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006338 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006339 if (status != 0)
6340 goto out;
6341 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006342out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006343 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006344 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345}
6346
NeilBrowna52458b2018-12-03 11:30:31 +11006347int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348{
6349 struct nfs_server *server = NFS_SERVER(inode);
6350 struct nfs4_exception exception = { };
6351 int err;
6352 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006353 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006354 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 switch (err) {
6356 case -NFS4ERR_STALE_STATEID:
6357 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 case 0:
6359 return 0;
6360 }
6361 err = nfs4_handle_exception(server, err, &exception);
6362 } while (exception.retry);
6363 return err;
6364}
6365
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6367{
6368 struct inode *inode = state->inode;
6369 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006370 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006371 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006373 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006375 struct nfs_lockt_res res = {
6376 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 };
6378 struct rpc_message msg = {
6379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006380 .rpc_argp = &arg,
6381 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 .rpc_cred = state->owner->so_cred,
6383 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 struct nfs4_lock_state *lsp;
6385 int status;
6386
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006387 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006388 status = nfs4_set_lock_state(state, request);
6389 if (status != 0)
6390 goto out;
6391 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006392 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006393 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006394 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006395 switch (status) {
6396 case 0:
6397 request->fl_type = F_UNLCK;
6398 break;
6399 case -NFS4ERR_DENIED:
6400 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006402 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006403 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006404out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 return status;
6406}
6407
6408static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6409{
Trond Myklebust0688e642019-04-07 13:59:09 -04006410 struct nfs4_exception exception = {
6411 .interruptible = true,
6412 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 int err;
6414
6415 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006416 err = _nfs4_proc_getlk(state, cmd, request);
6417 trace_nfs4_get_lock(request, state, cmd, err);
6418 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 &exception);
6420 } while (exception.retry);
6421 return err;
6422}
6423
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006424/*
6425 * Update the seqid of a lock stateid after receiving
6426 * NFS4ERR_OLD_STATEID
6427 */
6428static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6429 struct nfs4_lock_state *lsp)
6430{
6431 struct nfs4_state *state = lsp->ls_state;
6432 bool ret = false;
6433
6434 spin_lock(&state->state_lock);
6435 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6436 goto out;
6437 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6438 nfs4_stateid_seqid_inc(dst);
6439 else
6440 dst->seqid = lsp->ls_stateid.seqid;
6441 ret = true;
6442out:
6443 spin_unlock(&state->state_lock);
6444 return ret;
6445}
6446
6447static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6448 struct nfs4_lock_state *lsp)
6449{
6450 struct nfs4_state *state = lsp->ls_state;
6451 bool ret;
6452
6453 spin_lock(&state->state_lock);
6454 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6455 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6456 spin_unlock(&state->state_lock);
6457 return ret;
6458}
6459
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006460struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006461 struct nfs_locku_args arg;
6462 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006463 struct nfs4_lock_state *lsp;
6464 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006465 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006466 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006467 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006468 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006469};
6470
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006471static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6472 struct nfs_open_context *ctx,
6473 struct nfs4_lock_state *lsp,
6474 struct nfs_seqid *seqid)
6475{
6476 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006477 struct nfs4_state *state = lsp->ls_state;
6478 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006479
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006480 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006481 if (p == NULL)
6482 return NULL;
6483 p->arg.fh = NFS_FH(inode);
6484 p->arg.fl = &p->fl;
6485 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006486 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006487 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006488 /* Ensure we don't close file until we're done freeing locks! */
6489 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006490 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006491 locks_init_lock(&p->fl);
6492 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006493 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006494 spin_lock(&state->state_lock);
6495 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6496 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006497 return p;
6498}
6499
Trond Myklebust06f814a2006-01-03 09:55:07 +01006500static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006501{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006502 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006503 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006504 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006505 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006506 put_nfs_open_context(calldata->ctx);
6507 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006508}
6509
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006510static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006511{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006512 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006513 struct nfs4_exception exception = {
6514 .inode = calldata->lsp->ls_state->inode,
6515 .stateid = &calldata->arg.stateid,
6516 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006517
Trond Myklebust14516c32010-07-31 14:29:06 -04006518 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6519 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006520 switch (task->tk_status) {
6521 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006522 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006523 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006524 if (nfs4_update_lock_stateid(calldata->lsp,
6525 &calldata->res.stateid))
6526 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006527 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006528 case -NFS4ERR_ADMIN_REVOKED:
6529 case -NFS4ERR_EXPIRED:
6530 nfs4_free_revoked_stateid(calldata->server,
6531 &calldata->arg.stateid,
6532 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006533 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006534 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006535 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006536 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6537 calldata->lsp))
6538 rpc_restart_call_prepare(task);
6539 break;
6540 case -NFS4ERR_OLD_STATEID:
6541 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6542 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006543 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006544 break;
6545 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006546 task->tk_status = nfs4_async_handle_exception(task,
6547 calldata->server, task->tk_status,
6548 &exception);
6549 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006550 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006551 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006552 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006553}
6554
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006555static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006556{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006557 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006559 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6560 nfs_async_iocounter_wait(task, calldata->l_ctx))
6561 return;
6562
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006563 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006564 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006565 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006566 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006567 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006568 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006569 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006570 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006571 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006572 &calldata->res.seq_res,
6573 task) != 0)
6574 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006575 return;
6576out_no_action:
6577 task->tk_action = NULL;
6578out_wait:
6579 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580}
6581
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006582static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006583 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006584 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006585 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006586};
6587
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006588static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6589 struct nfs_open_context *ctx,
6590 struct nfs4_lock_state *lsp,
6591 struct nfs_seqid *seqid)
6592{
6593 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006594 struct rpc_message msg = {
6595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6596 .rpc_cred = ctx->cred,
6597 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006598 struct rpc_task_setup task_setup_data = {
6599 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006600 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006601 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006602 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006603 .flags = RPC_TASK_ASYNC,
6604 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006605
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006606 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6607 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6608
Frank Filz137d6ac2007-07-09 15:32:29 -07006609 /* Ensure this is an unlock - when canceling a lock, the
6610 * canceled lock is passed in, and it won't be an unlock.
6611 */
6612 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006613 if (fl->fl_flags & FL_CLOSE)
6614 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006615
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006616 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6617 if (data == NULL) {
6618 nfs_free_seqid(seqid);
6619 return ERR_PTR(-ENOMEM);
6620 }
6621
Anna Schumakerfba83f32018-05-04 16:22:50 -04006622 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006623 msg.rpc_argp = &data->arg;
6624 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006625 task_setup_data.callback_data = data;
6626 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006627}
6628
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6630{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006631 struct inode *inode = state->inode;
6632 struct nfs4_state_owner *sp = state->owner;
6633 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006634 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006635 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006636 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006637 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006638 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006639 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640
Trond Myklebust9b073572006-06-29 16:38:34 -04006641 status = nfs4_set_lock_state(state, request);
6642 /* Unlock _before_ we do the RPC call */
6643 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006644 /* Exclude nfs_delegation_claim_locks() */
6645 mutex_lock(&sp->so_delegreturn_mutex);
6646 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006647 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006648 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006649 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006650 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006651 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006652 }
6653 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006654 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006655 if (status != 0)
6656 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006657 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006658 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006659 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6660 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006661 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6662 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006663 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006664 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006665 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006666 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006667 status = PTR_ERR(task);
6668 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006669 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006670 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006671 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006672out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006673 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006674 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006675 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676}
6677
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006678struct nfs4_lockdata {
6679 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006680 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006681 struct nfs4_lock_state *lsp;
6682 struct nfs_open_context *ctx;
6683 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006684 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006685 int rpc_status;
6686 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006687 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006688};
6689
6690static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006691 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6692 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006693{
6694 struct nfs4_lockdata *p;
6695 struct inode *inode = lsp->ls_state->inode;
6696 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006697 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006698
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006699 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006700 if (p == NULL)
6701 return NULL;
6702
6703 p->arg.fh = NFS_FH(inode);
6704 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006705 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006706 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006707 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006708 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6709 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006710 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006711 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006712 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006713 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006714 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006715 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006716 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006717 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006718 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006719 locks_init_lock(&p->fl);
6720 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006721 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006722out_free_seqid:
6723 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006724out_free:
6725 kfree(p);
6726 return NULL;
6727}
6728
6729static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6730{
6731 struct nfs4_lockdata *data = calldata;
6732 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733
Harvey Harrison3110ff82008-05-02 13:42:44 -07006734 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006735 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006736 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006737 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006738 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006739 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006740 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006741 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006742 nfs4_stateid_copy(&data->arg.open_stateid,
6743 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006744 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006745 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006746 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006747 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006748 nfs4_stateid_copy(&data->arg.lock_stateid,
6749 &data->lsp->ls_stateid);
6750 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006751 if (!nfs4_valid_open_stateid(state)) {
6752 data->rpc_status = -EBADF;
6753 task->tk_action = NULL;
6754 goto out_release_open_seqid;
6755 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006756 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006757 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006758 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006759 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006760 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006761 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006762out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006763 nfs_release_seqid(data->arg.open_seqid);
6764out_release_lock_seqid:
6765 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006766out_wait:
6767 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006768 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006769}
6770
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006771static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6772{
6773 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006774 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006775
Harvey Harrison3110ff82008-05-02 13:42:44 -07006776 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006777
Trond Myklebust14516c32010-07-31 14:29:06 -04006778 if (!nfs4_sequence_done(task, &data->res.seq_res))
6779 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006780
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006781 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006782 switch (task->tk_status) {
6783 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006784 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006785 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006786 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006787 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006788 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006789 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006790 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006791 if (data->arg.new_lock_owner != 0) {
6792 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6793 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6794 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006795 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6796 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006797 break;
6798 case -NFS4ERR_BAD_STATEID:
6799 case -NFS4ERR_OLD_STATEID:
6800 case -NFS4ERR_STALE_STATEID:
6801 case -NFS4ERR_EXPIRED:
6802 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006803 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006804 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006805 goto out_restart;
6806 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006807 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006808 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006809 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006810out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006811 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006812 return;
6813out_restart:
6814 if (!data->cancelled)
6815 rpc_restart_call_prepare(task);
6816 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006817}
6818
6819static void nfs4_lock_release(void *calldata)
6820{
6821 struct nfs4_lockdata *data = calldata;
6822
Harvey Harrison3110ff82008-05-02 13:42:44 -07006823 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006824 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006825 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006826 struct rpc_task *task;
6827 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6828 data->arg.lock_seqid);
6829 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006830 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006831 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006832 } else
6833 nfs_free_seqid(data->arg.lock_seqid);
6834 nfs4_put_lock_state(data->lsp);
6835 put_nfs_open_context(data->ctx);
6836 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006837 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006838}
6839
6840static const struct rpc_call_ops nfs4_lock_ops = {
6841 .rpc_call_prepare = nfs4_lock_prepare,
6842 .rpc_call_done = nfs4_lock_done,
6843 .rpc_release = nfs4_lock_release,
6844};
6845
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006846static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6847{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006848 switch (error) {
6849 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006850 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006851 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006852 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006853 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006854 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006855 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006856 break;
6857 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006858 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006859 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006860 };
6861}
6862
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006863static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006864{
6865 struct nfs4_lockdata *data;
6866 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006867 struct rpc_message msg = {
6868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6869 .rpc_cred = state->owner->so_cred,
6870 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006871 struct rpc_task_setup task_setup_data = {
6872 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006873 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006874 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006875 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006876 .flags = RPC_TASK_ASYNC,
6877 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006878 int ret;
6879
Harvey Harrison3110ff82008-05-02 13:42:44 -07006880 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006881 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006882 fl->fl_u.nfs4_fl.owner,
6883 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006884 if (data == NULL)
6885 return -ENOMEM;
6886 if (IS_SETLKW(cmd))
6887 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006888 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6889 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006890 msg.rpc_argp = &data->arg;
6891 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006892 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006893 if (recovery_type > NFS_LOCK_NEW) {
6894 if (recovery_type == NFS_LOCK_RECLAIM)
6895 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006896 } else
6897 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006898 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006899 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006900 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006901 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006902 if (ret == 0) {
6903 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006904 if (ret)
6905 nfs4_handle_setlk_error(data->server, data->lsp,
6906 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006907 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006908 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006909 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006910 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006911 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006912 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913}
6914
6915static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6916{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006917 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006918 struct nfs4_exception exception = {
6919 .inode = state->inode,
6920 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006921 int err;
6922
6923 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006924 /* Cache the lock if possible... */
6925 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6926 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006927 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006928 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006929 break;
6930 nfs4_handle_exception(server, err, &exception);
6931 } while (exception.retry);
6932 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933}
6934
6935static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6936{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006937 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006938 struct nfs4_exception exception = {
6939 .inode = state->inode,
6940 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006941 int err;
6942
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006943 err = nfs4_set_lock_state(state, request);
6944 if (err != 0)
6945 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006946 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006947 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6948 return 0;
6949 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006950 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006951 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6952 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006953 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006954 switch (err) {
6955 default:
6956 goto out;
6957 case -NFS4ERR_GRACE:
6958 case -NFS4ERR_DELAY:
6959 nfs4_handle_exception(server, err, &exception);
6960 err = 0;
6961 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006962 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006963out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006964 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965}
6966
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006967#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006968static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6969{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006970 struct nfs4_lock_state *lsp;
6971 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006972
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006973 status = nfs4_set_lock_state(state, request);
6974 if (status != 0)
6975 return status;
6976 lsp = request->fl_u.nfs4_fl.owner;
6977 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6978 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6979 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006980 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006981}
6982#endif
6983
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6985{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006986 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006987 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006988 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006989 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
Trond Myklebust01c3b862006-06-29 16:38:39 -04006991 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006992 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006993 if (status < 0)
6994 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006995 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006996 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006997 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006998 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006999 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007000 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007001 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007002 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007003 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007004 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007005 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007006 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007007 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007008 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007009out:
7010 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 return status;
7012}
7013
7014static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7015{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007016 struct nfs4_exception exception = {
7017 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007018 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007019 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007020 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 int err;
7022
7023 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007024 err = _nfs4_proc_setlk(state, cmd, request);
7025 if (err == -NFS4ERR_DENIED)
7026 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007028 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 } while (exception.retry);
7030 return err;
7031}
7032
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007033#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7034#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7035
7036static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007037nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7038 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007039{
7040 int status = -ERESTARTSYS;
7041 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7042
7043 while(!signalled()) {
7044 status = nfs4_proc_setlk(state, cmd, request);
7045 if ((status != -EAGAIN) || IS_SETLK(cmd))
7046 break;
7047 freezable_schedule_timeout_interruptible(timeout);
7048 timeout *= 2;
7049 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7050 status = -ERESTARTSYS;
7051 }
7052 return status;
7053}
7054
Jeff Laytona1d617d82016-09-17 18:17:39 -04007055#ifdef CONFIG_NFS_V4_1
7056struct nfs4_lock_waiter {
7057 struct task_struct *task;
7058 struct inode *inode;
7059 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007060};
7061
7062static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007063nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007064{
7065 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007066 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007067
Jeff Layton57174592018-03-18 08:37:03 -04007068 /* NULL key means to wake up everyone */
7069 if (key) {
7070 struct cb_notify_lock_args *cbnl = key;
7071 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7072 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007073
Jeff Layton57174592018-03-18 08:37:03 -04007074 /* Only wake if the callback was for the same owner. */
7075 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7076 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007077
Jeff Layton57174592018-03-18 08:37:03 -04007078 /* Make sure it's for the right inode */
7079 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7080 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007081 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007082
7083 /* override "private" so we can use default_wake_function */
7084 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007085 ret = woken_wake_function(wait, mode, flags, key);
7086 if (ret)
7087 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007088 wait->private = waiter;
7089 return ret;
7090}
7091
7092static int
7093nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7094{
7095 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007096 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7097 struct nfs_server *server = NFS_SERVER(state->inode);
7098 struct nfs_client *clp = server->nfs_client;
7099 wait_queue_head_t *q = &clp->cl_lock_waitq;
7100 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7101 .id = lsp->ls_seqid.owner_id,
7102 .s_dev = server->s_dev };
7103 struct nfs4_lock_waiter waiter = { .task = current,
7104 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007105 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007106 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007107
7108 /* Don't bother with waitqueue if we don't expect a callback */
7109 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7110 return nfs4_retry_setlk_simple(state, cmd, request);
7111
7112 init_wait(&wait);
7113 wait.private = &waiter;
7114 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007115
7116 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007117 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007118 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007119 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7120 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007121 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007122 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007123
7124 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007125 freezer_do_not_count();
7126 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7127 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007128 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007129 }
7130
Jeff Laytona1d617d82016-09-17 18:17:39 -04007131 return status;
7132}
7133#else /* !CONFIG_NFS_V4_1 */
7134static inline int
7135nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7136{
7137 return nfs4_retry_setlk_simple(state, cmd, request);
7138}
7139#endif
7140
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141static int
7142nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7143{
7144 struct nfs_open_context *ctx;
7145 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 int status;
7147
7148 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007149 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 state = ctx->state;
7151
Trond Myklebustd9531262009-07-21 19:22:38 -04007152 if (IS_GETLK(cmd)) {
7153 if (state != NULL)
7154 return nfs4_proc_getlk(state, F_GETLK, request);
7155 return 0;
7156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157
7158 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7159 return -EINVAL;
7160
Trond Myklebustd9531262009-07-21 19:22:38 -04007161 if (request->fl_type == F_UNLCK) {
7162 if (state != NULL)
7163 return nfs4_proc_unlck(state, cmd, request);
7164 return 0;
7165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
Trond Myklebustd9531262009-07-21 19:22:38 -04007167 if (state == NULL)
7168 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007169
7170 if ((request->fl_flags & FL_POSIX) &&
7171 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7172 return -ENOLCK;
7173
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007174 /*
7175 * Don't rely on the VFS having checked the file open mode,
7176 * since it won't do this for flock() locks.
7177 */
7178 switch (request->fl_type) {
7179 case F_RDLCK:
7180 if (!(filp->f_mode & FMODE_READ))
7181 return -EBADF;
7182 break;
7183 case F_WRLCK:
7184 if (!(filp->f_mode & FMODE_WRITE))
7185 return -EBADF;
7186 }
7187
Jeff Layton1ea67db2016-09-17 18:17:37 -04007188 status = nfs4_set_lock_state(state, request);
7189 if (status != 0)
7190 return status;
7191
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007192 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193}
7194
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007195int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007196{
7197 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007198 int err;
7199
7200 err = nfs4_set_lock_state(state, fl);
7201 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007202 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007203 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007204 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007205}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007206
Trond Myklebustcf470c32012-03-07 13:49:12 -05007207struct nfs_release_lockowner_data {
7208 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007209 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007210 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007211 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007212 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007213};
7214
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007215static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7216{
7217 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007218 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007219 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7220 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007221 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007222 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007223}
7224
7225static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7226{
7227 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007228 struct nfs_server *server = data->server;
7229
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007230 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007231
7232 switch (task->tk_status) {
7233 case 0:
7234 renew_lease(server, data->timestamp);
7235 break;
7236 case -NFS4ERR_STALE_CLIENTID:
7237 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007238 nfs4_schedule_lease_recovery(server->nfs_client);
7239 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007240 case -NFS4ERR_LEASE_MOVED:
7241 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007242 if (nfs4_async_handle_error(task, server,
7243 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007244 rpc_restart_call_prepare(task);
7245 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007246}
7247
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007248static void nfs4_release_lockowner_release(void *calldata)
7249{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007250 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007251 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007252 kfree(calldata);
7253}
7254
Trond Myklebust17280172012-03-11 13:11:00 -04007255static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007256 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7257 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007258 .rpc_release = nfs4_release_lockowner_release,
7259};
7260
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007261static void
7262nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007263{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007264 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007265 struct rpc_message msg = {
7266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7267 };
7268
7269 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007270 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007271
Trond Myklebustcf470c32012-03-07 13:49:12 -05007272 data = kmalloc(sizeof(*data), GFP_NOFS);
7273 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007274 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007275 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007276 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007277 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7278 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7279 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007280
Trond Myklebustcf470c32012-03-07 13:49:12 -05007281 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007282 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007283 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007284 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007285}
7286
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007287#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7288
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007289static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007290 struct dentry *unused, struct inode *inode,
7291 const char *key, const void *buf,
7292 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007293{
Al Viro59301222016-05-27 10:19:30 -04007294 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007295}
7296
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007297static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007298 struct dentry *unused, struct inode *inode,
7299 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007300{
Al Virob2968212016-04-10 20:48:24 -04007301 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007302}
7303
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007304static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007305{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007306 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007307}
7308
David Quigleyc9bccef2013-05-22 12:50:45 -04007309#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007310
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007311static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007312 struct dentry *unused, struct inode *inode,
7313 const char *key, const void *buf,
7314 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007315{
7316 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007317 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007318
7319 return -EOPNOTSUPP;
7320}
7321
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007322static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007323 struct dentry *unused, struct inode *inode,
7324 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007325{
7326 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007327 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007328 return -EOPNOTSUPP;
7329}
7330
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007331static ssize_t
7332nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007333{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007334 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007335
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007336 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7337 len = security_inode_listsecurity(inode, list, list_len);
7338 if (list_len && len > list_len)
7339 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007340 }
7341 return len;
7342}
7343
7344static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7345 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007346 .get = nfs4_xattr_get_nfs4_label,
7347 .set = nfs4_xattr_set_nfs4_label,
7348};
David Quigleyc9bccef2013-05-22 12:50:45 -04007349
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007350#else
7351
7352static ssize_t
7353nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7354{
7355 return 0;
7356}
7357
7358#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007359
Andy Adamson533eb462011-06-13 18:25:56 -04007360/*
7361 * nfs_fhget will use either the mounted_on_fileid or the fileid
7362 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007363static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7364{
Andy Adamson533eb462011-06-13 18:25:56 -04007365 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7366 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7367 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007368 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007369 return;
7370
7371 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007372 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007373 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7374 fattr->nlink = 2;
7375}
7376
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007377static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7378 const struct qstr *name,
7379 struct nfs4_fs_locations *fs_locations,
7380 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007381{
7382 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007383 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007384 struct nfs4_fs_locations_arg args = {
7385 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007386 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007387 .page = page,
7388 .bitmask = bitmask,
7389 };
Benny Halevy22958462009-04-01 09:22:02 -04007390 struct nfs4_fs_locations_res res = {
7391 .fs_locations = fs_locations,
7392 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007393 struct rpc_message msg = {
7394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7395 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007396 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007397 };
7398 int status;
7399
Harvey Harrison3110ff82008-05-02 13:42:44 -07007400 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007401
Chuck Leverc05cefc2017-11-05 15:45:22 -05007402 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7403 bitmask[1] = nfs4_fattr_bitmap[1];
7404
Andy Adamson533eb462011-06-13 18:25:56 -04007405 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7406 * is not supported */
7407 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007408 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007409 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007410 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007411
Trond Myklebustc228fd32007-01-13 02:28:11 -05007412 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007413 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007414 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007415 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007416 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007417 return status;
7418}
7419
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007420int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7421 const struct qstr *name,
7422 struct nfs4_fs_locations *fs_locations,
7423 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007424{
Trond Myklebust0688e642019-04-07 13:59:09 -04007425 struct nfs4_exception exception = {
7426 .interruptible = true,
7427 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007428 int err;
7429 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007430 err = _nfs4_proc_fs_locations(client, dir, name,
7431 fs_locations, page);
7432 trace_nfs4_get_fs_locations(dir, name, err);
7433 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007434 &exception);
7435 } while (exception.retry);
7436 return err;
7437}
7438
Chuck Leverb03d7352013-10-17 14:12:50 -04007439/*
7440 * This operation also signals the server that this client is
7441 * performing migration recovery. The server can stop returning
7442 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7443 * appended to this compound to identify the client ID which is
7444 * performing recovery.
7445 */
7446static int _nfs40_proc_get_locations(struct inode *inode,
7447 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007448 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007449{
7450 struct nfs_server *server = NFS_SERVER(inode);
7451 struct rpc_clnt *clnt = server->client;
7452 u32 bitmask[2] = {
7453 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7454 };
7455 struct nfs4_fs_locations_arg args = {
7456 .clientid = server->nfs_client->cl_clientid,
7457 .fh = NFS_FH(inode),
7458 .page = page,
7459 .bitmask = bitmask,
7460 .migration = 1, /* skip LOOKUP */
7461 .renew = 1, /* append RENEW */
7462 };
7463 struct nfs4_fs_locations_res res = {
7464 .fs_locations = locations,
7465 .migration = 1,
7466 .renew = 1,
7467 };
7468 struct rpc_message msg = {
7469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7470 .rpc_argp = &args,
7471 .rpc_resp = &res,
7472 .rpc_cred = cred,
7473 };
7474 unsigned long now = jiffies;
7475 int status;
7476
7477 nfs_fattr_init(&locations->fattr);
7478 locations->server = server;
7479 locations->nlocations = 0;
7480
Anna Schumakerfba83f32018-05-04 16:22:50 -04007481 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007482 status = nfs4_call_sync_sequence(clnt, server, &msg,
7483 &args.seq_args, &res.seq_res);
7484 if (status)
7485 return status;
7486
7487 renew_lease(server, now);
7488 return 0;
7489}
7490
7491#ifdef CONFIG_NFS_V4_1
7492
7493/*
7494 * This operation also signals the server that this client is
7495 * performing migration recovery. The server can stop asserting
7496 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7497 * performing this operation is identified in the SEQUENCE
7498 * operation in this compound.
7499 *
7500 * When the client supports GETATTR(fs_locations_info), it can
7501 * be plumbed in here.
7502 */
7503static int _nfs41_proc_get_locations(struct inode *inode,
7504 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007505 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007506{
7507 struct nfs_server *server = NFS_SERVER(inode);
7508 struct rpc_clnt *clnt = server->client;
7509 u32 bitmask[2] = {
7510 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7511 };
7512 struct nfs4_fs_locations_arg args = {
7513 .fh = NFS_FH(inode),
7514 .page = page,
7515 .bitmask = bitmask,
7516 .migration = 1, /* skip LOOKUP */
7517 };
7518 struct nfs4_fs_locations_res res = {
7519 .fs_locations = locations,
7520 .migration = 1,
7521 };
7522 struct rpc_message msg = {
7523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7524 .rpc_argp = &args,
7525 .rpc_resp = &res,
7526 .rpc_cred = cred,
7527 };
7528 int status;
7529
7530 nfs_fattr_init(&locations->fattr);
7531 locations->server = server;
7532 locations->nlocations = 0;
7533
Anna Schumakerfba83f32018-05-04 16:22:50 -04007534 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007535 status = nfs4_call_sync_sequence(clnt, server, &msg,
7536 &args.seq_args, &res.seq_res);
7537 if (status == NFS4_OK &&
7538 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7539 status = -NFS4ERR_LEASE_MOVED;
7540 return status;
7541}
7542
7543#endif /* CONFIG_NFS_V4_1 */
7544
7545/**
7546 * nfs4_proc_get_locations - discover locations for a migrated FSID
7547 * @inode: inode on FSID that is migrating
7548 * @locations: result of query
7549 * @page: buffer
7550 * @cred: credential to use for this operation
7551 *
7552 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7553 * operation failed, or a negative errno if a local error occurred.
7554 *
7555 * On success, "locations" is filled in, but if the server has
7556 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7557 * asserted.
7558 *
7559 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7560 * from this client that require migration recovery.
7561 */
7562int nfs4_proc_get_locations(struct inode *inode,
7563 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007564 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007565{
7566 struct nfs_server *server = NFS_SERVER(inode);
7567 struct nfs_client *clp = server->nfs_client;
7568 const struct nfs4_mig_recovery_ops *ops =
7569 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007570 struct nfs4_exception exception = {
7571 .interruptible = true,
7572 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007573 int status;
7574
7575 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7576 (unsigned long long)server->fsid.major,
7577 (unsigned long long)server->fsid.minor,
7578 clp->cl_hostname);
7579 nfs_display_fhandle(NFS_FH(inode), __func__);
7580
7581 do {
7582 status = ops->get_locations(inode, locations, page, cred);
7583 if (status != -NFS4ERR_DELAY)
7584 break;
7585 nfs4_handle_exception(server, status, &exception);
7586 } while (exception.retry);
7587 return status;
7588}
7589
Chuck Lever44c99932013-10-17 14:13:30 -04007590/*
7591 * This operation also signals the server that this client is
7592 * performing "lease moved" recovery. The server can stop
7593 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7594 * is appended to this compound to identify the client ID which is
7595 * performing recovery.
7596 */
NeilBrowna52458b2018-12-03 11:30:31 +11007597static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007598{
7599 struct nfs_server *server = NFS_SERVER(inode);
7600 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7601 struct rpc_clnt *clnt = server->client;
7602 struct nfs4_fsid_present_arg args = {
7603 .fh = NFS_FH(inode),
7604 .clientid = clp->cl_clientid,
7605 .renew = 1, /* append RENEW */
7606 };
7607 struct nfs4_fsid_present_res res = {
7608 .renew = 1,
7609 };
7610 struct rpc_message msg = {
7611 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7612 .rpc_argp = &args,
7613 .rpc_resp = &res,
7614 .rpc_cred = cred,
7615 };
7616 unsigned long now = jiffies;
7617 int status;
7618
7619 res.fh = nfs_alloc_fhandle();
7620 if (res.fh == NULL)
7621 return -ENOMEM;
7622
Anna Schumakerfba83f32018-05-04 16:22:50 -04007623 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007624 status = nfs4_call_sync_sequence(clnt, server, &msg,
7625 &args.seq_args, &res.seq_res);
7626 nfs_free_fhandle(res.fh);
7627 if (status)
7628 return status;
7629
7630 do_renew_lease(clp, now);
7631 return 0;
7632}
7633
7634#ifdef CONFIG_NFS_V4_1
7635
7636/*
7637 * This operation also signals the server that this client is
7638 * performing "lease moved" recovery. The server can stop asserting
7639 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7640 * this operation is identified in the SEQUENCE operation in this
7641 * compound.
7642 */
NeilBrowna52458b2018-12-03 11:30:31 +11007643static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007644{
7645 struct nfs_server *server = NFS_SERVER(inode);
7646 struct rpc_clnt *clnt = server->client;
7647 struct nfs4_fsid_present_arg args = {
7648 .fh = NFS_FH(inode),
7649 };
7650 struct nfs4_fsid_present_res res = {
7651 };
7652 struct rpc_message msg = {
7653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7654 .rpc_argp = &args,
7655 .rpc_resp = &res,
7656 .rpc_cred = cred,
7657 };
7658 int status;
7659
7660 res.fh = nfs_alloc_fhandle();
7661 if (res.fh == NULL)
7662 return -ENOMEM;
7663
Anna Schumakerfba83f32018-05-04 16:22:50 -04007664 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007665 status = nfs4_call_sync_sequence(clnt, server, &msg,
7666 &args.seq_args, &res.seq_res);
7667 nfs_free_fhandle(res.fh);
7668 if (status == NFS4_OK &&
7669 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7670 status = -NFS4ERR_LEASE_MOVED;
7671 return status;
7672}
7673
7674#endif /* CONFIG_NFS_V4_1 */
7675
7676/**
7677 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7678 * @inode: inode on FSID to check
7679 * @cred: credential to use for this operation
7680 *
7681 * Server indicates whether the FSID is present, moved, or not
7682 * recognized. This operation is necessary to clear a LEASE_MOVED
7683 * condition for this client ID.
7684 *
7685 * Returns NFS4_OK if the FSID is present on this server,
7686 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7687 * NFS4ERR code if some error occurred on the server, or a
7688 * negative errno if a local failure occurred.
7689 */
NeilBrowna52458b2018-12-03 11:30:31 +11007690int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007691{
7692 struct nfs_server *server = NFS_SERVER(inode);
7693 struct nfs_client *clp = server->nfs_client;
7694 const struct nfs4_mig_recovery_ops *ops =
7695 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007696 struct nfs4_exception exception = {
7697 .interruptible = true,
7698 };
Chuck Lever44c99932013-10-17 14:13:30 -04007699 int status;
7700
7701 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7702 (unsigned long long)server->fsid.major,
7703 (unsigned long long)server->fsid.minor,
7704 clp->cl_hostname);
7705 nfs_display_fhandle(NFS_FH(inode), __func__);
7706
7707 do {
7708 status = ops->fsid_present(inode, cred);
7709 if (status != -NFS4ERR_DELAY)
7710 break;
7711 nfs4_handle_exception(server, status, &exception);
7712 } while (exception.retry);
7713 return status;
7714}
7715
Trond Myklebust302fad72019-02-18 13:32:38 -05007716/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007717 * If 'use_integrity' is true and the state managment nfs_client
7718 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7719 * and the machine credential as per RFC3530bis and RFC5661 Security
7720 * Considerations sections. Otherwise, just use the user cred with the
7721 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007722 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007723static 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 +00007724{
7725 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04007726 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7727 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007728 struct nfs4_secinfo_arg args = {
7729 .dir_fh = NFS_FH(dir),
7730 .name = name,
7731 };
7732 struct nfs4_secinfo_res res = {
7733 .flavors = flavors,
7734 };
7735 struct rpc_message msg = {
7736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7737 .rpc_argp = &args,
7738 .rpc_resp = &res,
7739 };
Anna Schumaker50493362019-08-14 15:30:16 -04007740 struct nfs4_call_sync_data data = {
7741 .seq_server = NFS_SERVER(dir),
7742 .seq_args = &args.seq_args,
7743 .seq_res = &res.seq_res,
7744 };
7745 struct rpc_task_setup task_setup = {
7746 .rpc_client = clnt,
7747 .rpc_message = &msg,
7748 .callback_ops = clp->cl_mvops->call_sync_ops,
7749 .callback_data = &data,
7750 .flags = RPC_TASK_NO_ROUND_ROBIN,
7751 };
NeilBrowna52458b2018-12-03 11:30:31 +11007752 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007753
7754 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04007755 clnt = clp->cl_rpcclient;
7756 task_setup.rpc_client = clnt;
7757
7758 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007759 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007760 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007761
7762 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007763
Anna Schumaker50493362019-08-14 15:30:16 -04007764 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7765 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7766 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007767
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007768 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007769
NeilBrowna52458b2018-12-03 11:30:31 +11007770 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007771 return status;
7772}
7773
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007774int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7775 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007776{
Trond Myklebust0688e642019-04-07 13:59:09 -04007777 struct nfs4_exception exception = {
7778 .interruptible = true,
7779 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007780 int err;
7781 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007782 err = -NFS4ERR_WRONGSEC;
7783
7784 /* try to use integrity protection with machine cred */
7785 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7786 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7787
7788 /*
7789 * if unable to use integrity protection, or SECINFO with
7790 * integrity protection returns NFS4ERR_WRONGSEC (which is
7791 * disallowed by spec, but exists in deployed servers) use
7792 * the current filesystem's rpc_client and the user cred.
7793 */
7794 if (err == -NFS4ERR_WRONGSEC)
7795 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7796
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007797 trace_nfs4_secinfo(dir, name, err);
7798 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007799 &exception);
7800 } while (exception.retry);
7801 return err;
7802}
7803
Andy Adamson557134a2009-04-01 09:21:53 -04007804#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007805/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007806 * Check the exchange flags returned by the server for invalid flags, having
7807 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7808 * DS flags set.
7809 */
7810static int nfs4_check_cl_exchange_flags(u32 flags)
7811{
7812 if (flags & ~EXCHGID4_FLAG_MASK_R)
7813 goto out_inval;
7814 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7815 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7816 goto out_inval;
7817 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7818 goto out_inval;
7819 return NFS_OK;
7820out_inval:
7821 return -NFS4ERR_INVAL;
7822}
7823
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007824static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007825nfs41_same_server_scope(struct nfs41_server_scope *a,
7826 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007827{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007828 if (a->server_scope_sz != b->server_scope_sz)
7829 return false;
7830 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007831}
7832
Andy Adamson02a95de2016-02-05 16:08:37 -05007833static void
7834nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7835{
Trond Myklebust5c441542019-11-13 08:34:00 +01007836 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
7837 struct nfs_client *clp = args->client;
7838
7839 switch (task->tk_status) {
7840 case -NFS4ERR_BADSESSION:
7841 case -NFS4ERR_DEADSESSION:
7842 nfs4_schedule_session_recovery(clp->cl_session,
7843 task->tk_status);
7844 }
Andy Adamson02a95de2016-02-05 16:08:37 -05007845}
7846
7847static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7848 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7849};
7850
Andy Adamson357f54d2010-12-14 10:11:57 -05007851/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007852 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007853 *
7854 * The 4.1 client currently uses the same TCP connection for the
7855 * fore and backchannel.
7856 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007857static
7858int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7859 struct rpc_xprt *xprt,
7860 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007861 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007862{
7863 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007864 struct nfs41_bind_conn_to_session_args args = {
7865 .client = clp,
7866 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7867 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007868 struct nfs41_bind_conn_to_session_res res;
7869 struct rpc_message msg = {
7870 .rpc_proc =
7871 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007872 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007873 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007874 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007875 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007876 struct rpc_task_setup task_setup_data = {
7877 .rpc_client = clnt,
7878 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007879 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007880 .rpc_message = &msg,
7881 .flags = RPC_TASK_TIMEOUT,
7882 };
7883 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007884
Trond Myklebust71a097c2015-02-18 09:27:18 -08007885 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7886 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7887 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007888
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007889 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7890 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7891 args.dir = NFS4_CDFC4_FORE;
7892
7893 task = rpc_run_task(&task_setup_data);
7894 if (!IS_ERR(task)) {
7895 status = task->tk_status;
7896 rpc_put_task(task);
7897 } else
7898 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007899 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007900 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007901 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007902 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7903 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007904 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007905 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007906 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007907 dprintk("NFS: %s: Unexpected direction from server\n",
7908 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007909 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007910 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007911 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007912 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7913 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007914 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007915 }
7916 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007917
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007918 return status;
7919}
7920
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007921struct rpc_bind_conn_calldata {
7922 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007923 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007924};
7925
7926static int
7927nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7928 struct rpc_xprt *xprt,
7929 void *calldata)
7930{
7931 struct rpc_bind_conn_calldata *p = calldata;
7932
7933 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7934}
7935
NeilBrowna52458b2018-12-03 11:30:31 +11007936int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007937{
7938 struct rpc_bind_conn_calldata data = {
7939 .clp = clp,
7940 .cred = cred,
7941 };
7942 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7943 nfs4_proc_bind_conn_to_session_callback, &data);
7944}
7945
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007946/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007947 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7948 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007949 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007950static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7951 .how = SP4_MACH_CRED,
7952 .enforce.u.words = {
7953 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7954 1 << (OP_EXCHANGE_ID - 32) |
7955 1 << (OP_CREATE_SESSION - 32) |
7956 1 << (OP_DESTROY_SESSION - 32) |
7957 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007958 },
7959 .allow.u.words = {
7960 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007961 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007962 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007963 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007964 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007965 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007966 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007967 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007968 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007969 1 << (OP_FREE_STATEID - 32) |
7970 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007971 }
7972};
7973
7974/*
7975 * Select the state protection mode for client `clp' given the server results
7976 * from exchange_id in `sp'.
7977 *
7978 * Returns 0 on success, negative errno otherwise.
7979 */
7980static int nfs4_sp4_select_mode(struct nfs_client *clp,
7981 struct nfs41_state_protection *sp)
7982{
7983 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7984 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7985 1 << (OP_EXCHANGE_ID - 32) |
7986 1 << (OP_CREATE_SESSION - 32) |
7987 1 << (OP_DESTROY_SESSION - 32) |
7988 1 << (OP_DESTROY_CLIENTID - 32)
7989 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007990 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007991 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007992 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007993
7994 if (sp->how == SP4_MACH_CRED) {
7995 /* Print state protect result */
7996 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7997 for (i = 0; i <= LAST_NFS4_OP; i++) {
7998 if (test_bit(i, sp->enforce.u.longs))
7999 dfprintk(MOUNT, " enforce op %d\n", i);
8000 if (test_bit(i, sp->allow.u.longs))
8001 dfprintk(MOUNT, " allow op %d\n", i);
8002 }
8003
8004 /* make sure nothing is on enforce list that isn't supported */
8005 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8006 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8007 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008008 ret = -EINVAL;
8009 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008010 }
8011 }
8012
8013 /*
8014 * Minimal mode - state operations are allowed to use machine
8015 * credential. Note this already happens by default, so the
8016 * client doesn't have to do anything more than the negotiation.
8017 *
8018 * NOTE: we don't care if EXCHANGE_ID is in the list -
8019 * we're already using the machine cred for exchange_id
8020 * and will never use a different cred.
8021 */
8022 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8023 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8024 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8025 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8026 dfprintk(MOUNT, "sp4_mach_cred:\n");
8027 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008028 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008029 } else {
8030 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008031 ret = -EINVAL;
8032 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008033 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008034
8035 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008036 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8037 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008038 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8039 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008040 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008041 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008042
Andrew Elble99ade3c2015-12-02 09:39:51 -05008043 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8044 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008045 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008046 }
8047
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008048 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8049 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8050 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008051 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008052 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008053
8054 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8055 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8056 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008057 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008058 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008059
8060 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8061 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008062 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008063 }
8064
8065 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8066 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008067 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008068 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008069 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008070out:
8071 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008072 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008073}
8074
Andy Adamson8d89bd72016-09-09 09:22:18 -04008075struct nfs41_exchange_id_data {
8076 struct nfs41_exchange_id_res res;
8077 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008078};
8079
Andy Adamson8d89bd72016-09-09 09:22:18 -04008080static void nfs4_exchange_id_release(void *data)
8081{
8082 struct nfs41_exchange_id_data *cdata =
8083 (struct nfs41_exchange_id_data *)data;
8084
Olga Kornievskaia63513232017-03-13 10:36:19 -04008085 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008086 kfree(cdata->res.impl_id);
8087 kfree(cdata->res.server_scope);
8088 kfree(cdata->res.server_owner);
8089 kfree(cdata);
8090}
8091
8092static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008093 .rpc_release = nfs4_exchange_id_release,
8094};
8095
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008096/*
8097 * _nfs4_proc_exchange_id()
8098 *
8099 * Wrapper for EXCHANGE_ID operation.
8100 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008101static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008102nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008103 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008104{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008105 struct rpc_message msg = {
8106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008107 .rpc_cred = cred,
8108 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008109 struct rpc_task_setup task_setup_data = {
8110 .rpc_client = clp->cl_rpcclient,
8111 .callback_ops = &nfs4_exchange_id_call_ops,
8112 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008113 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008114 };
8115 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008116 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008117
Elena Reshetova212bf412017-10-20 12:53:38 +03008118 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008119 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008120
Trond Myklebust9c760d12017-07-31 18:38:50 -04008121 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008122 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008123 if (!calldata)
8124 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008125
Trond Myklebustfd405592017-08-01 16:02:47 -04008126 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008127
8128 status = nfs4_init_uniform_client_string(clp);
8129 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008130 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008131
Andy Adamson8d89bd72016-09-09 09:22:18 -04008132 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8133 GFP_NOFS);
8134 status = -ENOMEM;
8135 if (unlikely(calldata->res.server_owner == NULL))
8136 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008137
Andy Adamson8d89bd72016-09-09 09:22:18 -04008138 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008139 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008140 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008141 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008142
Andy Adamson8d89bd72016-09-09 09:22:18 -04008143 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8144 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008145 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008146
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008147 switch (sp4_how) {
8148 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008149 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008150 break;
8151
8152 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008153 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008154 break;
8155
8156 default:
8157 /* unsupported! */
8158 WARN_ON_ONCE(1);
8159 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008160 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008161 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008162 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008163 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008164 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008165 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8166 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008167 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008168 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008169 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8170 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008171#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008172 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008173#endif
8174 msg.rpc_argp = &calldata->args;
8175 msg.rpc_resp = &calldata->res;
8176 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008177
Trond Myklebust9c760d12017-07-31 18:38:50 -04008178 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008179
8180out_impl_id:
8181 kfree(calldata->res.impl_id);
8182out_server_scope:
8183 kfree(calldata->res.server_scope);
8184out_server_owner:
8185 kfree(calldata->res.server_owner);
8186out_calldata:
8187 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008188out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008189 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008190 return ERR_PTR(status);
8191}
8192
8193/*
8194 * _nfs4_proc_exchange_id()
8195 *
8196 * Wrapper for EXCHANGE_ID operation.
8197 */
NeilBrowna52458b2018-12-03 11:30:31 +11008198static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008199 u32 sp4_how)
8200{
8201 struct rpc_task *task;
8202 struct nfs41_exchange_id_args *argp;
8203 struct nfs41_exchange_id_res *resp;
8204 int status;
8205
8206 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8207 if (IS_ERR(task))
8208 return PTR_ERR(task);
8209
8210 argp = task->tk_msg.rpc_argp;
8211 resp = task->tk_msg.rpc_resp;
8212 status = task->tk_status;
8213 if (status != 0)
8214 goto out;
8215
8216 status = nfs4_check_cl_exchange_flags(resp->flags);
8217 if (status != 0)
8218 goto out;
8219
8220 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8221 if (status != 0)
8222 goto out;
8223
8224 clp->cl_clientid = resp->clientid;
8225 clp->cl_exchange_flags = resp->flags;
8226 clp->cl_seqid = resp->seqid;
8227 /* Client ID is not confirmed */
8228 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8229 clear_bit(NFS4_SESSION_ESTABLISHED,
8230 &clp->cl_session->session_state);
8231
8232 if (clp->cl_serverscope != NULL &&
8233 !nfs41_same_server_scope(clp->cl_serverscope,
8234 resp->server_scope)) {
8235 dprintk("%s: server_scope mismatch detected\n",
8236 __func__);
8237 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8238 }
8239
8240 swap(clp->cl_serverowner, resp->server_owner);
8241 swap(clp->cl_serverscope, resp->server_scope);
8242 swap(clp->cl_implid, resp->impl_id);
8243
8244 /* Save the EXCHANGE_ID verifier session trunk tests */
8245 memcpy(clp->cl_confirm.data, argp->verifier.data,
8246 sizeof(clp->cl_confirm.data));
8247out:
8248 trace_nfs4_exchange_id(clp, status);
8249 rpc_put_task(task);
8250 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008251}
8252
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008253/*
8254 * nfs4_proc_exchange_id()
8255 *
8256 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8257 *
8258 * Since the clientid has expired, all compounds using sessions
8259 * associated with the stale clientid will be returning
8260 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8261 * be in some phase of session reset.
8262 *
8263 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8264 */
NeilBrowna52458b2018-12-03 11:30:31 +11008265int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008266{
8267 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8268 int status;
8269
8270 /* try SP4_MACH_CRED if krb5i/p */
8271 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8272 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008273 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008274 if (!status)
8275 return 0;
8276 }
8277
8278 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008279 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008280}
8281
Andy Adamson04fa2c62016-09-09 09:22:29 -04008282/**
8283 * nfs4_test_session_trunk
8284 *
8285 * This is an add_xprt_test() test function called from
8286 * rpc_clnt_setup_test_and_add_xprt.
8287 *
8288 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8289 * and is dereferrenced in nfs4_exchange_id_release
8290 *
8291 * Upon success, add the new transport to the rpc_clnt
8292 *
8293 * @clnt: struct rpc_clnt to get new transport
8294 * @xprt: the rpc_xprt to test
8295 * @data: call data for _nfs4_proc_exchange_id.
8296 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308297void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008298 void *data)
8299{
8300 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008301 struct rpc_task *task;
8302 int status;
8303
Andy Adamson04fa2c62016-09-09 09:22:29 -04008304 u32 sp4_how;
8305
8306 dprintk("--> %s try %s\n", __func__,
8307 xprt->address_strings[RPC_DISPLAY_ADDR]);
8308
8309 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8310
8311 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008312 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8313 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308314 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008315
8316 status = task->tk_status;
8317 if (status == 0)
8318 status = nfs4_detect_session_trunking(adata->clp,
8319 task->tk_msg.rpc_resp, xprt);
8320
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308321 if (status == 0)
8322 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8323
Trond Myklebust9c760d12017-07-31 18:38:50 -04008324 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008325}
8326EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8327
Trond Myklebust66245532012-05-25 17:18:09 -04008328static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008329 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008330{
8331 struct rpc_message msg = {
8332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8333 .rpc_argp = clp,
8334 .rpc_cred = cred,
8335 };
8336 int status;
8337
NeilBrown5a0c2572019-05-30 10:41:28 +10008338 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8339 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008340 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008341 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008342 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008343 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8344 return status;
8345}
8346
8347static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008348 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008349{
8350 unsigned int loop;
8351 int ret;
8352
8353 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8354 ret = _nfs4_proc_destroy_clientid(clp, cred);
8355 switch (ret) {
8356 case -NFS4ERR_DELAY:
8357 case -NFS4ERR_CLIENTID_BUSY:
8358 ssleep(1);
8359 break;
8360 default:
8361 return ret;
8362 }
8363 }
8364 return 0;
8365}
8366
8367int nfs4_destroy_clientid(struct nfs_client *clp)
8368{
NeilBrowna52458b2018-12-03 11:30:31 +11008369 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008370 int ret = 0;
8371
8372 if (clp->cl_mvops->minor_version < 1)
8373 goto out;
8374 if (clp->cl_exchange_flags == 0)
8375 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008376 if (clp->cl_preserve_clid)
8377 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008378 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008379 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008380 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008381 switch (ret) {
8382 case 0:
8383 case -NFS4ERR_STALE_CLIENTID:
8384 clp->cl_exchange_flags = 0;
8385 }
8386out:
8387 return ret;
8388}
8389
Donald Buczek0efb01b2019-07-07 21:26:08 +02008390#endif /* CONFIG_NFS_V4_1 */
8391
Andy Adamson2050f0c2009-04-01 09:22:30 -04008392struct nfs4_get_lease_time_data {
8393 struct nfs4_get_lease_time_args *args;
8394 struct nfs4_get_lease_time_res *res;
8395 struct nfs_client *clp;
8396};
8397
8398static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8399 void *calldata)
8400{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008401 struct nfs4_get_lease_time_data *data =
8402 (struct nfs4_get_lease_time_data *)calldata;
8403
8404 dprintk("--> %s\n", __func__);
8405 /* just setup sequence, do not trigger session recovery
8406 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008407 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008408 &data->args->la_seq_args,
8409 &data->res->lr_seq_res,
8410 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008411 dprintk("<-- %s\n", __func__);
8412}
8413
8414/*
8415 * Called from nfs4_state_manager thread for session setup, so don't recover
8416 * from sequence operation or clientid errors.
8417 */
8418static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8419{
8420 struct nfs4_get_lease_time_data *data =
8421 (struct nfs4_get_lease_time_data *)calldata;
8422
8423 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008424 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008425 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008426 switch (task->tk_status) {
8427 case -NFS4ERR_DELAY:
8428 case -NFS4ERR_GRACE:
8429 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8430 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8431 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008432 /* fall through */
8433 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008434 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008435 return;
8436 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008437 dprintk("<-- %s\n", __func__);
8438}
8439
Trond Myklebust17280172012-03-11 13:11:00 -04008440static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008441 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8442 .rpc_call_done = nfs4_get_lease_time_done,
8443};
8444
8445int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8446{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008447 struct nfs4_get_lease_time_args args;
8448 struct nfs4_get_lease_time_res res = {
8449 .lr_fsinfo = fsinfo,
8450 };
8451 struct nfs4_get_lease_time_data data = {
8452 .args = &args,
8453 .res = &res,
8454 .clp = clp,
8455 };
8456 struct rpc_message msg = {
8457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8458 .rpc_argp = &args,
8459 .rpc_resp = &res,
8460 };
8461 struct rpc_task_setup task_setup = {
8462 .rpc_client = clp->cl_rpcclient,
8463 .rpc_message = &msg,
8464 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008465 .callback_data = &data,
8466 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008467 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008468
Anna Schumakerfba83f32018-05-04 16:22:50 -04008469 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008470 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008471}
8472
Donald Buczek0efb01b2019-07-07 21:26:08 +02008473#ifdef CONFIG_NFS_V4_1
8474
Andy Adamsonfc931582009-04-01 09:22:31 -04008475/*
8476 * Initialize the values to be used by the client in CREATE_SESSION
8477 * If nfs4_init_session set the fore channel request and response sizes,
8478 * use them.
8479 *
8480 * Set the back channel max_resp_sz_cached to zero to force the client to
8481 * always set csa_cachethis to FALSE because the current implementation
8482 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8483 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008484static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8485 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008486{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008487 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008488 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008489 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008490
Andy Adamson18aad3d2013-06-26 12:21:49 -04008491 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8492 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8493
Andy Adamsonfc931582009-04-01 09:22:31 -04008494 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008495 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8496 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008497 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008498 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008499
8500 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008501 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008502 __func__,
8503 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008504 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008505
8506 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008507 args->bc_attrs.max_rqst_sz = max_bc_payload;
8508 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008509 args->bc_attrs.max_resp_sz_cached = 0;
8510 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008511 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008512 if (args->bc_attrs.max_reqs > max_bc_slots)
8513 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008514
8515 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8516 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8517 __func__,
8518 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8519 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8520 args->bc_attrs.max_reqs);
8521}
8522
Trond Myklebust79969dd2015-02-18 11:30:18 -08008523static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8524 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008525{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008526 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008527 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008528
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008529 if (rcvd->max_resp_sz > sent->max_resp_sz)
8530 return -EINVAL;
8531 /*
8532 * Our requested max_ops is the minimum we need; we're not
8533 * prepared to break up compounds into smaller pieces than that.
8534 * So, no point even trying to continue if the server won't
8535 * cooperate:
8536 */
8537 if (rcvd->max_ops < sent->max_ops)
8538 return -EINVAL;
8539 if (rcvd->max_reqs == 0)
8540 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008541 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8542 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008543 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008544}
8545
Trond Myklebust79969dd2015-02-18 11:30:18 -08008546static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8547 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008548{
8549 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008550 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008551
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008552 if (!(res->flags & SESSION4_BACK_CHAN))
8553 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008554 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8555 return -EINVAL;
8556 if (rcvd->max_resp_sz < sent->max_resp_sz)
8557 return -EINVAL;
8558 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8559 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008560 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008561 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008562 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008563 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008564out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008565 return 0;
8566}
Andy Adamson8d353012009-04-01 09:22:32 -04008567
Andy Adamson8d353012009-04-01 09:22:32 -04008568static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008569 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008570{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008571 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008572
Trond Myklebust79969dd2015-02-18 11:30:18 -08008573 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008574 if (ret)
8575 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008576 return nfs4_verify_back_channel_attrs(args, res);
8577}
8578
8579static void nfs4_update_session(struct nfs4_session *session,
8580 struct nfs41_create_session_res *res)
8581{
8582 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008583 /* Mark client id and session as being confirmed */
8584 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8585 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008586 session->flags = res->flags;
8587 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008588 if (res->flags & SESSION4_BACK_CHAN)
8589 memcpy(&session->bc_attrs, &res->bc_attrs,
8590 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008591}
8592
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008593static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008594 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008595{
8596 struct nfs4_session *session = clp->cl_session;
8597 struct nfs41_create_session_args args = {
8598 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008599 .clientid = clp->cl_clientid,
8600 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008601 .cb_program = NFS4_CALLBACK,
8602 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008603 struct nfs41_create_session_res res;
8604
Andy Adamsonfc931582009-04-01 09:22:31 -04008605 struct rpc_message msg = {
8606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8607 .rpc_argp = &args,
8608 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008609 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008610 };
8611 int status;
8612
Chuck Lever6b26cc82016-05-02 14:40:40 -04008613 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008614 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008615
NeilBrown5a0c2572019-05-30 10:41:28 +10008616 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8617 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008618 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008619
Trond Myklebustb519d402016-09-11 14:50:01 -04008620 switch (status) {
8621 case -NFS4ERR_STALE_CLIENTID:
8622 case -NFS4ERR_DELAY:
8623 case -ETIMEDOUT:
8624 case -EACCES:
8625 case -EAGAIN:
8626 goto out;
8627 };
8628
8629 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008630 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008631 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008632 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008633 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008634 if (status)
8635 goto out;
8636 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008637 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008638out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008639 return status;
8640}
8641
8642/*
8643 * Issues a CREATE_SESSION operation to the server.
8644 * It is the responsibility of the caller to verify the session is
8645 * expired before calling this routine.
8646 */
NeilBrowna52458b2018-12-03 11:30:31 +11008647int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008648{
8649 int status;
8650 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008651 struct nfs4_session *session = clp->cl_session;
8652
8653 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8654
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008655 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008656 if (status)
8657 goto out;
8658
Andy Adamsonaacd5532011-11-09 13:58:21 -05008659 /* Init or reset the session slot tables */
8660 status = nfs4_setup_session_slot_tables(session);
8661 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008662 if (status)
8663 goto out;
8664
8665 ptr = (unsigned *)&session->sess_id.data[0];
8666 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8667 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008668out:
8669 dprintk("<-- %s\n", __func__);
8670 return status;
8671}
8672
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008673/*
8674 * Issue the over-the-wire RPC DESTROY_SESSION.
8675 * The caller must serialize access to this routine.
8676 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008677int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008678 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008679{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008680 struct rpc_message msg = {
8681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8682 .rpc_argp = session,
8683 .rpc_cred = cred,
8684 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008685 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008686
8687 dprintk("--> nfs4_proc_destroy_session\n");
8688
8689 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008690 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8691 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008692
NeilBrown5a0c2572019-05-30 10:41:28 +10008693 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8694 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008695 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008696
8697 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008698 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008699 "Session has been destroyed regardless...\n", status);
8700
8701 dprintk("<-- nfs4_proc_destroy_session\n");
8702 return status;
8703}
8704
Trond Myklebust7b38c362012-05-23 13:23:31 -04008705/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008706 * Renew the cl_session lease.
8707 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008708struct nfs4_sequence_data {
8709 struct nfs_client *clp;
8710 struct nfs4_sequence_args args;
8711 struct nfs4_sequence_res res;
8712};
8713
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008714static void nfs41_sequence_release(void *data)
8715{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008716 struct nfs4_sequence_data *calldata = data;
8717 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008718
Elena Reshetova212bf412017-10-20 12:53:38 +03008719 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008720 nfs4_schedule_state_renewal(clp);
8721 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008722 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008723}
8724
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008725static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8726{
8727 switch(task->tk_status) {
8728 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008729 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8730 return -EAGAIN;
8731 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008732 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008733 }
8734 return 0;
8735}
8736
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008737static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008738{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008739 struct nfs4_sequence_data *calldata = data;
8740 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008741
Trond Myklebust14516c32010-07-31 14:29:06 -04008742 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8743 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008744
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008745 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008746 if (task->tk_status < 0) {
8747 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008748 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008749 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008750
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008751 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8752 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008753 return;
8754 }
8755 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008756 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008757out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008758 dprintk("<-- %s\n", __func__);
8759}
8760
8761static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8762{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008763 struct nfs4_sequence_data *calldata = data;
8764 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008765 struct nfs4_sequence_args *args;
8766 struct nfs4_sequence_res *res;
8767
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008768 args = task->tk_msg.rpc_argp;
8769 res = task->tk_msg.rpc_resp;
8770
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008771 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008772}
8773
8774static const struct rpc_call_ops nfs41_sequence_ops = {
8775 .rpc_call_done = nfs41_sequence_call_done,
8776 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008777 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008778};
8779
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008780static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008781 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008782 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008783 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008784{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008785 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008786 struct rpc_message msg = {
8787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8788 .rpc_cred = cred,
8789 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008790 struct rpc_task_setup task_setup_data = {
8791 .rpc_client = clp->cl_rpcclient,
8792 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008793 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008794 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008795 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008796 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008797
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008798 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008799 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008800 goto out_err;
8801
8802 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008803 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008804 if (calldata == NULL)
8805 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008806 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008807 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008808 msg.rpc_argp = &calldata->args;
8809 msg.rpc_resp = &calldata->res;
8810 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008811 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008812
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008813 ret = rpc_run_task(&task_setup_data);
8814 if (IS_ERR(ret))
8815 goto out_err;
8816 return ret;
8817out_put_clp:
8818 nfs_put_client(clp);
8819out_err:
8820 nfs41_release_slot(slot);
8821 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008822}
8823
NeilBrowna52458b2018-12-03 11:30:31 +11008824static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008825{
8826 struct rpc_task *task;
8827 int ret = 0;
8828
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008829 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008830 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008831 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008832 if (IS_ERR(task))
8833 ret = PTR_ERR(task);
8834 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008835 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008836 dprintk("<-- %s status=%d\n", __func__, ret);
8837 return ret;
8838}
8839
NeilBrowna52458b2018-12-03 11:30:31 +11008840static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008841{
8842 struct rpc_task *task;
8843 int ret;
8844
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008845 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008846 if (IS_ERR(task)) {
8847 ret = PTR_ERR(task);
8848 goto out;
8849 }
8850 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008851 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008852 ret = task->tk_status;
8853 rpc_put_task(task);
8854out:
8855 dprintk("<-- %s status=%d\n", __func__, ret);
8856 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008857}
8858
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008859struct nfs4_reclaim_complete_data {
8860 struct nfs_client *clp;
8861 struct nfs41_reclaim_complete_args arg;
8862 struct nfs41_reclaim_complete_res res;
8863};
8864
8865static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8866{
8867 struct nfs4_reclaim_complete_data *calldata = data;
8868
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008869 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008870 &calldata->arg.seq_args,
8871 &calldata->res.seq_res,
8872 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008873}
8874
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008875static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8876{
8877 switch(task->tk_status) {
8878 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008879 wake_up_all(&clp->cl_lock_waitq);
8880 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008881 case -NFS4ERR_COMPLETE_ALREADY:
8882 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8883 break;
8884 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008885 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008886 /* fall through */
8887 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008888 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008889 case -NFS4ERR_BADSESSION:
8890 case -NFS4ERR_DEADSESSION:
8891 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008892 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008893 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008894 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008895 }
8896 return 0;
8897}
8898
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008899static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8900{
8901 struct nfs4_reclaim_complete_data *calldata = data;
8902 struct nfs_client *clp = calldata->clp;
8903 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8904
8905 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008906 if (!nfs41_sequence_done(task, res))
8907 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008908
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008909 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008910 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8911 rpc_restart_call_prepare(task);
8912 return;
8913 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008914 dprintk("<-- %s\n", __func__);
8915}
8916
8917static void nfs4_free_reclaim_complete_data(void *data)
8918{
8919 struct nfs4_reclaim_complete_data *calldata = data;
8920
8921 kfree(calldata);
8922}
8923
8924static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8925 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8926 .rpc_call_done = nfs4_reclaim_complete_done,
8927 .rpc_release = nfs4_free_reclaim_complete_data,
8928};
8929
8930/*
8931 * Issue a global reclaim complete.
8932 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008933static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008934 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008935{
8936 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008937 struct rpc_message msg = {
8938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008939 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008940 };
8941 struct rpc_task_setup task_setup_data = {
8942 .rpc_client = clp->cl_rpcclient,
8943 .rpc_message = &msg,
8944 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04008945 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008946 };
8947 int status = -ENOMEM;
8948
8949 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008950 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008951 if (calldata == NULL)
8952 goto out;
8953 calldata->clp = clp;
8954 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008955
Anna Schumakerfba83f32018-05-04 16:22:50 -04008956 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008957 msg.rpc_argp = &calldata->arg;
8958 msg.rpc_resp = &calldata->res;
8959 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04008960 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008961out:
8962 dprintk("<-- %s status=%d\n", __func__, status);
8963 return status;
8964}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008965
8966static void
8967nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8968{
8969 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008970 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008971
8972 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008973 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008974 &lgp->res.seq_res, task);
8975 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008976}
8977
8978static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8979{
8980 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008981
8982 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008983 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008984 dprintk("<-- %s\n", __func__);
8985}
8986
8987static int
8988nfs4_layoutget_handle_exception(struct rpc_task *task,
8989 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8990{
Trond Myklebustee314c22012-10-01 17:25:48 -07008991 struct inode *inode = lgp->args.inode;
8992 struct nfs_server *server = NFS_SERVER(inode);
8993 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008994 int nfs4err = task->tk_status;
8995 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008996 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008997
Boaz Harroshed7e5422014-01-22 20:34:54 +02008998 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008999
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009000 nfs4_sequence_free_slot(&lgp->res.seq_res);
9001
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009002 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009003 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009004 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009005
9006 /*
9007 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9008 * on the file. set tk_status to -ENODATA to tell upper layer to
9009 * retry go inband.
9010 */
9011 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009012 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009013 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009014 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009015 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9016 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9017 */
9018 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009019 status = -EOVERFLOW;
9020 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009021 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009022 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009023 * (or clients) writing to the same RAID stripe except when
9024 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009025 *
9026 * Treat it like we would RECALLCONFLICT -- we retry for a little
9027 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009028 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009029 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009030 if (lgp->args.minlength == 0) {
9031 status = -EOVERFLOW;
9032 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009033 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009034 status = -EBUSY;
9035 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009036 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009037 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009038 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009039 case -NFS4ERR_DELEG_REVOKED:
9040 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009041 case -NFS4ERR_EXPIRED:
9042 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009043 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009044 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009045 lo = NFS_I(inode)->layout;
9046 /* If the open stateid was bad, then recover it. */
9047 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009048 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009049 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009050 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009051 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009052 break;
9053 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009054
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009055 /*
9056 * Mark the bad layout state as invalid, then retry
9057 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009058 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009059 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009060 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009061 pnfs_free_lseg_list(&head);
9062 status = -EAGAIN;
9063 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009064 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009065
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009066 err = nfs4_handle_exception(server, nfs4err, exception);
9067 if (!status) {
9068 if (exception->retry)
9069 status = -EAGAIN;
9070 else
9071 status = err;
9072 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009073out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009074 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009075 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009076}
9077
Fred Isamandacb4522016-09-19 17:47:09 -04009078size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009079{
9080 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9081 return nfs_page_array_len(0, max_resp_sz);
9082}
9083
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009084static void nfs4_layoutget_release(void *calldata)
9085{
9086 struct nfs4_layoutget *lgp = calldata;
9087
9088 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009089 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009090 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009091 dprintk("<-- %s\n", __func__);
9092}
9093
9094static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9095 .rpc_call_prepare = nfs4_layoutget_prepare,
9096 .rpc_call_done = nfs4_layoutget_done,
9097 .rpc_release = nfs4_layoutget_release,
9098};
9099
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009100struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009101nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009102{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009103 struct inode *inode = lgp->args.inode;
9104 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009105 struct rpc_task *task;
9106 struct rpc_message msg = {
9107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9108 .rpc_argp = &lgp->args,
9109 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009110 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009111 };
9112 struct rpc_task_setup task_setup_data = {
9113 .rpc_client = server->client,
9114 .rpc_message = &msg,
9115 .callback_ops = &nfs4_layoutget_call_ops,
9116 .callback_data = lgp,
9117 .flags = RPC_TASK_ASYNC,
9118 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009119 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009120 struct nfs4_exception exception = {
9121 .inode = inode,
9122 .timeout = *timeout,
9123 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009124 int status = 0;
9125
9126 dprintk("--> %s\n", __func__);
9127
Peng Tao4bd5a982014-11-17 11:05:17 +08009128 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9129 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9130
Anna Schumakerfba83f32018-05-04 16:22:50 -04009131 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009132
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009133 task = rpc_run_task(&task_setup_data);
9134 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009135 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05009136 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009137 if (status != 0)
9138 goto out;
9139
Trond Myklebust18c07782019-02-13 07:55:31 -05009140 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009141 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9142 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009143 } else if (lgp->res.layoutp->len == 0) {
9144 status = -EAGAIN;
9145 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009146 } else
9147 lseg = pnfs_layout_process(lgp);
9148out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009149 trace_nfs4_layoutget(lgp->args.ctx,
9150 &lgp->args.range,
9151 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009152 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009153 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009154
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009155 rpc_put_task(task);
9156 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009157 if (status)
9158 return ERR_PTR(status);
9159 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009160}
9161
Benny Halevycbe82602011-05-22 19:52:37 +03009162static void
9163nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9164{
9165 struct nfs4_layoutreturn *lrp = calldata;
9166
9167 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009168 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009169 &lrp->args.seq_args,
9170 &lrp->res.seq_res,
9171 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009172 if (!pnfs_layout_is_valid(lrp->args.layout))
9173 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009174}
9175
9176static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9177{
9178 struct nfs4_layoutreturn *lrp = calldata;
9179 struct nfs_server *server;
9180
9181 dprintk("--> %s\n", __func__);
9182
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009183 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009184 return;
9185
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009186 /*
9187 * Was there an RPC level error? Assume the call succeeded,
9188 * and that we need to release the layout
9189 */
9190 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9191 lrp->res.lrs_present = 0;
9192 return;
9193 }
9194
Benny Halevycbe82602011-05-22 19:52:37 +03009195 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009196 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009197 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009198 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009199 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009200 lrp->args.inode))
9201 goto out_restart;
9202 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009203 default:
9204 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05009205 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009206 case 0:
9207 break;
9208 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009209 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009210 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009211 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009212 }
Benny Halevycbe82602011-05-22 19:52:37 +03009213 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009214 return;
9215out_restart:
9216 task->tk_status = 0;
9217 nfs4_sequence_free_slot(&lrp->res.seq_res);
9218 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009219}
9220
9221static void nfs4_layoutreturn_release(void *calldata)
9222{
9223 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009224 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009225
9226 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009227 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009228 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009229 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009230 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9231 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009232 pnfs_put_layout_hdr(lrp->args.layout);
9233 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009234 kfree(calldata);
9235 dprintk("<-- %s\n", __func__);
9236}
9237
9238static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9239 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9240 .rpc_call_done = nfs4_layoutreturn_done,
9241 .rpc_release = nfs4_layoutreturn_release,
9242};
9243
Peng Tao6c166052014-11-17 09:30:40 +08009244int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009245{
9246 struct rpc_task *task;
9247 struct rpc_message msg = {
9248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9249 .rpc_argp = &lrp->args,
9250 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009251 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009252 };
9253 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009254 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009255 .rpc_message = &msg,
9256 .callback_ops = &nfs4_layoutreturn_call_ops,
9257 .callback_data = lrp,
9258 };
Peng Tao6c166052014-11-17 09:30:40 +08009259 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009260
Andrew Elble99ade3c2015-12-02 09:39:51 -05009261 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9262 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9263 &task_setup_data.rpc_client, &msg);
9264
Benny Halevycbe82602011-05-22 19:52:37 +03009265 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009266 if (!sync) {
9267 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9268 if (!lrp->inode) {
9269 nfs4_layoutreturn_release(lrp);
9270 return -EAGAIN;
9271 }
9272 task_setup_data.flags |= RPC_TASK_ASYNC;
9273 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009274 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009275 task = rpc_run_task(&task_setup_data);
9276 if (IS_ERR(task))
9277 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009278 if (sync)
9279 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009280 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009281 dprintk("<-- %s status=%d\n", __func__, status);
9282 rpc_put_task(task);
9283 return status;
9284}
9285
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009286static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009287_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9288 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009289 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009290{
9291 struct nfs4_getdeviceinfo_args args = {
9292 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009293 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9294 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009295 };
9296 struct nfs4_getdeviceinfo_res res = {
9297 .pdev = pdev,
9298 };
9299 struct rpc_message msg = {
9300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9301 .rpc_argp = &args,
9302 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009303 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009304 };
9305 int status;
9306
9307 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009308 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009309 if (res.notification & ~args.notify_types)
9310 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009311 if (res.notification != args.notify_types)
9312 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009313
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009314 dprintk("<-- %s status=%d\n", __func__, status);
9315
9316 return status;
9317}
9318
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009319int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9320 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009321 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009322{
9323 struct nfs4_exception exception = { };
9324 int err;
9325
9326 do {
9327 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009328 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009329 &exception);
9330 } while (exception.retry);
9331 return err;
9332}
9333EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9334
Andy Adamson863a3c62011-03-23 13:27:54 +00009335static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9336{
9337 struct nfs4_layoutcommit_data *data = calldata;
9338 struct nfs_server *server = NFS_SERVER(data->args.inode);
9339
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009340 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009341 &data->args.seq_args,
9342 &data->res.seq_res,
9343 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009344}
9345
9346static void
9347nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9348{
9349 struct nfs4_layoutcommit_data *data = calldata;
9350 struct nfs_server *server = NFS_SERVER(data->args.inode);
9351
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009352 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009353 return;
9354
9355 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009356 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9357 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9358 case -NFS4ERR_BADLAYOUT: /* no layout */
9359 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009360 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009361 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009362 break;
9363 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009364 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009365 rpc_restart_call_prepare(task);
9366 return;
9367 }
9368 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009369}
9370
9371static void nfs4_layoutcommit_release(void *calldata)
9372{
9373 struct nfs4_layoutcommit_data *data = calldata;
9374
Andy Adamsondb29c082011-07-30 20:52:38 -04009375 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009376 nfs_post_op_update_inode_force_wcc(data->args.inode,
9377 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009378 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009379 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009380 kfree(data);
9381}
9382
9383static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9384 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9385 .rpc_call_done = nfs4_layoutcommit_done,
9386 .rpc_release = nfs4_layoutcommit_release,
9387};
9388
9389int
Andy Adamsonef311532011-03-12 02:58:10 -05009390nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009391{
9392 struct rpc_message msg = {
9393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9394 .rpc_argp = &data->args,
9395 .rpc_resp = &data->res,
9396 .rpc_cred = data->cred,
9397 };
9398 struct rpc_task_setup task_setup_data = {
9399 .task = &data->task,
9400 .rpc_client = NFS_CLIENT(data->args.inode),
9401 .rpc_message = &msg,
9402 .callback_ops = &nfs4_layoutcommit_ops,
9403 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009404 };
9405 struct rpc_task *task;
9406 int status = 0;
9407
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009408 dprintk("NFS: initiating layoutcommit call. sync %d "
9409 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009410 data->args.lastbytewritten,
9411 data->args.inode->i_ino);
9412
Trond Myklebust472e2592015-02-05 16:50:30 -05009413 if (!sync) {
9414 data->inode = nfs_igrab_and_active(data->args.inode);
9415 if (data->inode == NULL) {
9416 nfs4_layoutcommit_release(data);
9417 return -EAGAIN;
9418 }
9419 task_setup_data.flags = RPC_TASK_ASYNC;
9420 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009421 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009422 task = rpc_run_task(&task_setup_data);
9423 if (IS_ERR(task))
9424 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009425 if (sync)
9426 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009427 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009428 dprintk("%s: status %d\n", __func__, status);
9429 rpc_put_task(task);
9430 return status;
9431}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009432
Trond Myklebust302fad72019-02-18 13:32:38 -05009433/*
Andy Adamson97431202013-08-08 10:57:56 -04009434 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9435 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9436 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009437static int
9438_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009439 struct nfs_fsinfo *info,
9440 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009441{
9442 struct nfs41_secinfo_no_name_args args = {
9443 .style = SECINFO_STYLE_CURRENT_FH,
9444 };
9445 struct nfs4_secinfo_res res = {
9446 .flavors = flavors,
9447 };
9448 struct rpc_message msg = {
9449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9450 .rpc_argp = &args,
9451 .rpc_resp = &res,
9452 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009453 struct rpc_clnt *clnt = server->client;
Anna Schumakercc15e242019-08-14 16:22:31 -04009454 struct nfs4_call_sync_data data = {
9455 .seq_server = server,
9456 .seq_args = &args.seq_args,
9457 .seq_res = &res.seq_res,
9458 };
9459 struct rpc_task_setup task_setup = {
9460 .rpc_client = server->client,
9461 .rpc_message = &msg,
9462 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9463 .callback_data = &data,
9464 .flags = RPC_TASK_NO_ROUND_ROBIN,
9465 };
NeilBrowna52458b2018-12-03 11:30:31 +11009466 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009467 int status;
9468
9469 if (use_integrity) {
9470 clnt = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009471 task_setup.rpc_client = clnt;
9472
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009473 cred = nfs4_get_clid_cred(server->nfs_client);
9474 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009475 }
9476
9477 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009478 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9479 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009480 dprintk("<-- %s status=%d\n", __func__, status);
9481
NeilBrowna52458b2018-12-03 11:30:31 +11009482 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009483
9484 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009485}
9486
9487static int
9488nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9489 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9490{
Trond Myklebust0688e642019-04-07 13:59:09 -04009491 struct nfs4_exception exception = {
9492 .interruptible = true,
9493 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009494 int err;
9495 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009496 /* first try using integrity protection */
9497 err = -NFS4ERR_WRONGSEC;
9498
9499 /* try to use integrity protection with machine cred */
9500 if (_nfs4_is_integrity_protected(server->nfs_client))
9501 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9502 flavors, true);
9503
9504 /*
9505 * if unable to use integrity protection, or SECINFO with
9506 * integrity protection returns NFS4ERR_WRONGSEC (which is
9507 * disallowed by spec, but exists in deployed servers) use
9508 * the current filesystem's rpc_client and the user cred.
9509 */
9510 if (err == -NFS4ERR_WRONGSEC)
9511 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9512 flavors, false);
9513
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009514 switch (err) {
9515 case 0:
9516 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009517 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009518 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009519 default:
9520 err = nfs4_handle_exception(server, err, &exception);
9521 }
9522 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009523out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009524 return err;
9525}
9526
9527static int
9528nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9529 struct nfs_fsinfo *info)
9530{
9531 int err;
9532 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009533 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009534 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009535 struct nfs4_secinfo4 *secinfo;
9536 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009537
9538 page = alloc_page(GFP_KERNEL);
9539 if (!page) {
9540 err = -ENOMEM;
9541 goto out;
9542 }
9543
9544 flavors = page_address(page);
9545 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9546
9547 /*
9548 * Fall back on "guess and check" method if
9549 * the server doesn't support SECINFO_NO_NAME
9550 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009551 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009552 err = nfs4_find_root_sec(server, fhandle, info);
9553 goto out_freepage;
9554 }
9555 if (err)
9556 goto out_freepage;
9557
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009558 for (i = 0; i < flavors->num_flavors; i++) {
9559 secinfo = &flavors->flavors[i];
9560
9561 switch (secinfo->flavor) {
9562 case RPC_AUTH_NULL:
9563 case RPC_AUTH_UNIX:
9564 case RPC_AUTH_GSS:
9565 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9566 &secinfo->flavor_info);
9567 break;
9568 default:
9569 flavor = RPC_AUTH_MAXFLAVOR;
9570 break;
9571 }
9572
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009573 if (!nfs_auth_info_match(&server->auth_info, flavor))
9574 flavor = RPC_AUTH_MAXFLAVOR;
9575
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009576 if (flavor != RPC_AUTH_MAXFLAVOR) {
9577 err = nfs4_lookup_root_sec(server, fhandle,
9578 info, flavor);
9579 if (!err)
9580 break;
9581 }
9582 }
9583
9584 if (flavor == RPC_AUTH_MAXFLAVOR)
9585 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009586
9587out_freepage:
9588 put_page(page);
9589 if (err == -EACCES)
9590 return -EPERM;
9591out:
9592 return err;
9593}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009594
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009595static int _nfs41_test_stateid(struct nfs_server *server,
9596 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009597 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009598{
9599 int status;
9600 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009601 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009602 };
9603 struct nfs41_test_stateid_res res;
9604 struct rpc_message msg = {
9605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9606 .rpc_argp = &args,
9607 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009608 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009609 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009610 struct rpc_clnt *rpc_client = server->client;
9611
9612 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9613 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009614
Chuck Lever38527b12012-07-11 16:30:23 -04009615 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009616 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009617 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009618 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009619 if (status != NFS_OK) {
9620 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009621 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009622 }
9623 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009624 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009625}
9626
Trond Myklebust43912bb2016-09-22 13:38:56 -04009627static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9628 int err, struct nfs4_exception *exception)
9629{
9630 exception->retry = 0;
9631 switch(err) {
9632 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009633 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009634 nfs4_handle_exception(server, err, exception);
9635 break;
9636 case -NFS4ERR_BADSESSION:
9637 case -NFS4ERR_BADSLOT:
9638 case -NFS4ERR_BAD_HIGH_SLOT:
9639 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9640 case -NFS4ERR_DEADSESSION:
9641 nfs4_do_handle_exception(server, err, exception);
9642 }
9643}
9644
Chuck Lever38527b12012-07-11 16:30:23 -04009645/**
9646 * nfs41_test_stateid - perform a TEST_STATEID operation
9647 *
9648 * @server: server / transport on which to perform the operation
9649 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009650 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009651 *
9652 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9653 * Otherwise a negative NFS4ERR value is returned if the operation
9654 * failed or the state ID is not currently valid.
9655 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009656static int nfs41_test_stateid(struct nfs_server *server,
9657 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009658 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009659{
Trond Myklebust0688e642019-04-07 13:59:09 -04009660 struct nfs4_exception exception = {
9661 .interruptible = true,
9662 };
Bryan Schumaker7d974792011-06-02 14:59:08 -04009663 int err;
9664 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009665 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009666 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009667 } while (exception.retry);
9668 return err;
9669}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009670
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009671struct nfs_free_stateid_data {
9672 struct nfs_server *server;
9673 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009674 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009675};
9676
9677static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9678{
9679 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009680 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009681 &data->args.seq_args,
9682 &data->res.seq_res,
9683 task);
9684}
9685
9686static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9687{
9688 struct nfs_free_stateid_data *data = calldata;
9689
9690 nfs41_sequence_done(task, &data->res.seq_res);
9691
9692 switch (task->tk_status) {
9693 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009694 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009695 rpc_restart_call_prepare(task);
9696 }
9697}
9698
9699static void nfs41_free_stateid_release(void *calldata)
9700{
9701 kfree(calldata);
9702}
9703
Trond Myklebust17f26b12013-08-21 15:48:42 -04009704static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009705 .rpc_call_prepare = nfs41_free_stateid_prepare,
9706 .rpc_call_done = nfs41_free_stateid_done,
9707 .rpc_release = nfs41_free_stateid_release,
9708};
9709
Anna Schumaker2f261022018-05-15 13:03:39 -04009710/**
9711 * nfs41_free_stateid - perform a FREE_STATEID operation
9712 *
9713 * @server: server / transport on which to perform the operation
9714 * @stateid: state ID to release
9715 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009716 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009717 *
9718 * Note: this function is always asynchronous.
9719 */
9720static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009721 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009722 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009723 bool privileged)
9724{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009725 struct rpc_message msg = {
9726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009727 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009728 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009729 struct rpc_task_setup task_setup = {
9730 .rpc_client = server->client,
9731 .rpc_message = &msg,
9732 .callback_ops = &nfs41_free_stateid_ops,
9733 .flags = RPC_TASK_ASYNC,
9734 };
9735 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009736 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009737
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009738 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9739 &task_setup.rpc_client, &msg);
9740
Chuck Lever38527b12012-07-11 16:30:23 -04009741 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009742 data = kmalloc(sizeof(*data), GFP_NOFS);
9743 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009744 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009745 data->server = server;
9746 nfs4_stateid_copy(&data->args.stateid, stateid);
9747
9748 task_setup.callback_data = data;
9749
9750 msg.rpc_argp = &data->args;
9751 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009752 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009753 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009754 if (IS_ERR(task))
9755 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009756 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009757 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009758}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009759
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009760static void
9761nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009762{
NeilBrowna52458b2018-12-03 11:30:31 +11009763 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009764
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009765 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009766 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009767}
9768
Trond Myklebust36281ca2012-03-04 18:13:56 -05009769static bool nfs41_match_stateid(const nfs4_stateid *s1,
9770 const nfs4_stateid *s2)
9771{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009772 if (s1->type != s2->type)
9773 return false;
9774
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009775 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009776 return false;
9777
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009778 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009779 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009780
Anna Schumaker045c5512017-01-11 16:59:48 -05009781 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009782}
9783
Andy Adamson557134a2009-04-01 09:21:53 -04009784#endif /* CONFIG_NFS_V4_1 */
9785
Trond Myklebust36281ca2012-03-04 18:13:56 -05009786static bool nfs4_match_stateid(const nfs4_stateid *s1,
9787 const nfs4_stateid *s2)
9788{
Trond Myklebustf597c532012-03-04 18:13:56 -05009789 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009790}
9791
9792
Trond Myklebust17280172012-03-11 13:11:00 -04009793static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009794 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009795 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796 .recover_open = nfs4_open_reclaim,
9797 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009798 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009799 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800};
9801
Andy Adamson591d71c2009-04-01 09:22:47 -04009802#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009803static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009804 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9805 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9806 .recover_open = nfs4_open_reclaim,
9807 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009808 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009809 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009810 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009811};
9812#endif /* CONFIG_NFS_V4_1 */
9813
Trond Myklebust17280172012-03-11 13:11:00 -04009814static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009815 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009816 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009817 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009818 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009819 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820};
9821
Andy Adamson591d71c2009-04-01 09:22:47 -04009822#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009823static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009824 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9825 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009826 .recover_open = nfs41_open_expired,
9827 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009828 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009829};
9830#endif /* CONFIG_NFS_V4_1 */
9831
Trond Myklebust17280172012-03-11 13:11:00 -04009832static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009833 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009834 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009835 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009836};
9837
9838#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009839static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009840 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009841 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009842 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009843};
9844#endif
9845
Chuck Leverec011fe2013-10-17 14:12:39 -04009846static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009847 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009848 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009849};
9850
9851#if defined(CONFIG_NFS_V4_1)
9852static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009853 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009854 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009855};
9856#endif /* CONFIG_NFS_V4_1 */
9857
Trond Myklebust97dc1352010-06-16 09:52:26 -04009858static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9859 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009860 .init_caps = NFS_CAP_READDIRPLUS
9861 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009862 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009863 .init_client = nfs40_init_client,
9864 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009865 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009866 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009867 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009868 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009869 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009870 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009871 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9872 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9873 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009874 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009875};
9876
9877#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009878static struct nfs_seqid *
9879nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9880{
9881 return NULL;
9882}
9883
Trond Myklebust97dc1352010-06-16 09:52:26 -04009884static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9885 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009886 .init_caps = NFS_CAP_READDIRPLUS
9887 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009888 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009889 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009890 | NFS_CAP_ATOMIC_OPEN_V1
9891 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009892 .init_client = nfs41_init_client,
9893 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009894 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009895 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009896 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009897 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009898 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009899 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009900 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009901 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9902 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9903 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009904 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009905};
9906#endif
9907
Steve Dickson42c2c422013-05-22 12:50:38 -04009908#if defined(CONFIG_NFS_V4_2)
9909static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9910 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009911 .init_caps = NFS_CAP_READDIRPLUS
9912 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009913 | NFS_CAP_POSIX_LOCK
9914 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009915 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009916 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009917 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009918 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009919 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -04009920 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009921 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009922 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009923 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -05009924 | NFS_CAP_CLONE
9925 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009926 .init_client = nfs41_init_client,
9927 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009928 .match_stateid = nfs41_match_stateid,
9929 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009930 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009931 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009932 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009933 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009934 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009935 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9936 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9937 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009938 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009939};
9940#endif
9941
Trond Myklebust97dc1352010-06-16 09:52:26 -04009942const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9943 [0] = &nfs_v4_0_minor_ops,
9944#if defined(CONFIG_NFS_V4_1)
9945 [1] = &nfs_v4_1_minor_ops,
9946#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009947#if defined(CONFIG_NFS_V4_2)
9948 [2] = &nfs_v4_2_minor_ops,
9949#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009950};
9951
Trond Myklebust13997822016-07-24 17:10:52 -04009952static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009953{
9954 ssize_t error, error2;
9955
9956 error = generic_listxattr(dentry, list, size);
9957 if (error < 0)
9958 return error;
9959 if (list) {
9960 list += error;
9961 size -= error;
9962 }
9963
9964 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9965 if (error2 < 0)
9966 return error2;
9967 return error + error2;
9968}
9969
Trond Myklebust17f26b12013-08-21 15:48:42 -04009970static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009971 .create = nfs_create,
9972 .lookup = nfs_lookup,
9973 .atomic_open = nfs_atomic_open,
9974 .link = nfs_link,
9975 .unlink = nfs_unlink,
9976 .symlink = nfs_symlink,
9977 .mkdir = nfs_mkdir,
9978 .rmdir = nfs_rmdir,
9979 .mknod = nfs_mknod,
9980 .rename = nfs_rename,
9981 .permission = nfs_permission,
9982 .getattr = nfs_getattr,
9983 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009984 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009985};
9986
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009987static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009988 .permission = nfs_permission,
9989 .getattr = nfs_getattr,
9990 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009991 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009992};
9993
David Howells509de812006-08-22 20:06:11 -04009994const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009995 .version = 4, /* protocol version */
9996 .dentry_ops = &nfs4_dentry_operations,
9997 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009998 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009999 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010000 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010001 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -040010002 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010003 .getattr = nfs4_proc_getattr,
10004 .setattr = nfs4_proc_setattr,
10005 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010006 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010007 .access = nfs4_proc_access,
10008 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010009 .create = nfs4_proc_create,
10010 .remove = nfs4_proc_remove,
10011 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010012 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010013 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010014 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010015 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010016 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010017 .link = nfs4_proc_link,
10018 .symlink = nfs4_proc_symlink,
10019 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010020 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010021 .readdir = nfs4_proc_readdir,
10022 .mknod = nfs4_proc_mknod,
10023 .statfs = nfs4_proc_statfs,
10024 .fsinfo = nfs4_proc_fsinfo,
10025 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010026 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010027 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010028 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010029 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010030 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010031 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010032 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010033 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010034 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010035 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010036 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010037 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010038 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010039 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010040 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010041 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010042 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -040010043 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010044 .create_server = nfs4_create_server,
10045 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046};
10047
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010048static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010049 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010050 .list = nfs4_xattr_list_nfs4_acl,
10051 .get = nfs4_xattr_get_nfs4_acl,
10052 .set = nfs4_xattr_set_nfs4_acl,
10053};
10054
10055const struct xattr_handler *nfs4_xattr_handlers[] = {
10056 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010057#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10058 &nfs4_xattr_nfs4_label_handler,
10059#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010060 NULL
10061};
10062
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063/*
10064 * Local variables:
10065 * c-basic-offset: 8
10066 * End:
10067 */