blob: 1dbdc85e68855f0fed79187bb76e0e8cefae94df [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 *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95static 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 +110096static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040097 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110098 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040099 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400100#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400101static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100102 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400103 struct nfs4_slot *slot,
104 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400105static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100106 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400107static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100108 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400109#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400110
111#ifdef CONFIG_NFS_V4_SECURITY_LABEL
112static inline struct nfs4_label *
113nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
115{
116 int err;
117
118 if (label == NULL)
119 return NULL;
120
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
122 return NULL;
123
David Quigleyaa9c2662013-05-22 12:50:44 -0400124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
126 if (err == 0)
127 return label;
128
129 return NULL;
130}
131static inline void
132nfs4_label_release_security(struct nfs4_label *label)
133{
134 if (label)
135 security_release_secctx(label->label, label->len);
136}
137static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138{
139 if (label)
140 return server->attr_bitmask;
141
142 return server->attr_bitmask_nl;
143}
144#else
145static inline struct nfs4_label *
146nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
148{ return NULL; }
149static inline void
150nfs4_label_release_security(struct nfs4_label *label)
151{ return; }
152static inline u32 *
153nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154{ return server->attr_bitmask; }
155#endif
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500158static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 if (err >= -1000)
161 return err;
162 switch (err) {
163 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400166 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000167 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400168 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000169 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
172 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400173 case -NFS4ERR_SHARE_DENIED:
174 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400177 case -NFS4ERR_FILE_OPEN:
178 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700181 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400184 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/*
188 * This is our standard bitmap for GETATTR requests.
189 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400190const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 FATTR4_WORD0_TYPE
192 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_SIZE
194 | FATTR4_WORD0_FSID
195 | FATTR4_WORD0_FILEID,
196 FATTR4_WORD1_MODE
197 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400206#ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
Trond Myklebust1549210f2012-06-05 09:16:47 -0400211static const u32 nfs4_pnfs_open_bitmap[3] = {
212 FATTR4_WORD0_TYPE
213 | FATTR4_WORD0_CHANGE
214 | FATTR4_WORD0_SIZE
215 | FATTR4_WORD0_FSID
216 | FATTR4_WORD0_FILEID,
217 FATTR4_WORD1_MODE
218 | FATTR4_WORD1_NUMLINKS
219 | FATTR4_WORD1_OWNER
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500227#ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
229#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400230};
231
Andy Adamsone23008e2012-10-02 21:07:32 -0400232static const u32 nfs4_open_noattr_bitmap[3] = {
233 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400234 | FATTR4_WORD0_FILEID,
235};
236
David Quigleya09df2c2013-05-22 12:50:41 -0400237const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 FATTR4_WORD0_MAXLINK
248 | FATTR4_WORD0_MAXNAME,
249 0
250};
251
Fred Isamandae100c2011-07-30 20:52:37 -0400252const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700256 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800259 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
David Quigleya09df2c2013-05-22 12:50:41 -0400262const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500263 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400264 | FATTR4_WORD0_SIZE
265 | FATTR4_WORD0_FSID
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500268 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400276};
277
Trond Myklebust30846df2018-04-07 13:44:28 -0400278static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
279 struct inode *inode)
280{
281 unsigned long cache_validity;
282
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
285 return;
286
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
291
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
294
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
297}
298
299static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
301{
302 nfs4_bitmap_copy_adjust(dst, src, inode);
303}
304
Al Virobc4785c2006-10-19 23:28:51 -0700305static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct nfs4_readdir_arg *readdir)
307{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700309 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000312 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
314 return;
315 }
316
317 readdir->cookie = 0;
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
319 if (cookie == 2)
320 return;
321
322 /*
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
327 * instead of 1 or 2.
328 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800329 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (cookie == 0) {
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
337 p++;
338 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
350 p++;
351 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800359 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Trond Myklebust26d36302016-09-22 13:39:05 -0400362static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100364 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400365{
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
367
368 ops->test_and_free_expired(server, stateid, cred);
369}
370
371static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100373 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400374{
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
377}
378
379static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100381 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400382{
383 nfs4_stateid tmp;
384
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
387}
388
NeilBrown8478eaa2014-09-18 16:09:27 +1000389static long nfs4_update_delay(long *timeout)
390{
391 long ret;
392 if (!timeout)
393 return NFS4_POLL_RETRY_MAX;
394 if (*timeout <= 0)
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
398 ret = *timeout;
399 *timeout <<= 1;
400 return ret;
401}
402
Trond Myklebust65de8722008-12-23 15:21:44 -0500403static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
404{
405 int res = 0;
406
407 might_sleep();
408
NeilBrown8478eaa2014-09-18 16:09:27 +1000409 freezable_schedule_timeout_killable_unsafe(
410 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500411 if (fatal_signal_pending(current))
412 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 return res;
414}
415
416/* This is the error handling routine for processes that are allowed
417 * to sleep.
418 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400419static int nfs4_do_handle_exception(struct nfs_server *server,
420 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500421{
422 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500423 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400424 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500425 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500426 int ret = errorcode;
427
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400428 exception->delay = 0;
429 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500430 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400431
432 if (stateid == NULL && state != NULL)
433 stateid = &state->stateid;
434
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 switch(errorcode) {
436 case 0:
437 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400438 case -NFS4ERR_BADHANDLE:
439 case -ESTALE:
440 if (inode != NULL && S_ISREG(inode->i_mode))
441 pnfs_destroy_layout(NFS_I(inode));
442 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400443 case -NFS4ERR_DELEG_REVOKED:
444 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400445 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400446 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400447 if (inode != NULL && stateid != NULL) {
448 nfs_inode_find_state_and_recover(inode,
449 stateid);
450 goto wait_on_recovery;
451 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500452 /* Fall through */
Trond Myklebust272289a2016-09-22 13:39:15 -0400453 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400454 if (inode) {
455 int err;
456
457 err = nfs_async_inode_return_delegation(inode,
458 stateid);
459 if (err == 0)
460 goto wait_on_recovery;
461 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
462 exception->retry = 1;
463 break;
464 }
465 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500466 if (state == NULL)
467 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400468 ret = nfs4_schedule_stateid_recovery(server, state);
469 if (ret < 0)
470 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500471 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500472 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500473 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500474 nfs4_schedule_lease_recovery(clp);
475 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400476 case -NFS4ERR_MOVED:
477 ret = nfs4_schedule_migration_recovery(server);
478 if (ret < 0)
479 break;
480 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400481 case -NFS4ERR_LEASE_MOVED:
482 nfs4_schedule_lease_moved_recovery(clp);
483 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500484#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400485 case -NFS4ERR_BADSESSION:
486 case -NFS4ERR_BADSLOT:
487 case -NFS4ERR_BAD_HIGH_SLOT:
488 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
489 case -NFS4ERR_DEADSESSION:
490 case -NFS4ERR_SEQ_FALSE_RETRY:
491 case -NFS4ERR_SEQ_MISORDERED:
492 dprintk("%s ERROR: %d Reset session\n", __func__,
493 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400494 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400495 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500496#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500497 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500498 if (exception->timeout > HZ) {
499 /* We have retried a decent amount, time to
500 * fail
501 */
502 ret = -EBUSY;
503 break;
504 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500505 /* Fall through */
Trond Myklebust65de8722008-12-23 15:21:44 -0500506 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400507 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500508 /* Fall through */
Trond Myklebust2598ed32015-09-20 16:10:18 -0400509 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400510 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400511 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400512 exception->delay = 1;
513 return 0;
514
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400515 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500516 case -NFS4ERR_OLD_STATEID:
517 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800518 break;
519 case -NFS4ERR_BADOWNER:
520 /* The following works around a Linux server bug! */
521 case -NFS4ERR_BADNAME:
522 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
523 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
524 exception->retry = 1;
525 printk(KERN_WARNING "NFS: v4 server %s "
526 "does not accept raw "
527 "uid/gids. "
528 "Reenabling the idmapper.\n",
529 server->nfs_client->cl_hostname);
530 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500531 }
532 /* We failed to handle the error */
533 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500534wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400535 exception->recovering = 1;
536 return 0;
537}
538
539/* This is the error handling routine for processes that are allowed
540 * to sleep.
541 */
542int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
543{
544 struct nfs_client *clp = server->nfs_client;
545 int ret;
546
547 ret = nfs4_do_handle_exception(server, errorcode, exception);
548 if (exception->delay) {
549 ret = nfs4_delay(server->client, &exception->timeout);
550 goto out_retry;
551 }
552 if (exception->recovering) {
553 ret = nfs4_wait_clnt_recover(clp);
554 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
555 return -EIO;
556 goto out_retry;
557 }
558 return ret;
559out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500560 if (ret == 0)
561 exception->retry = 1;
562 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500563}
564
Trond Myklebust037fc982015-09-20 15:51:00 -0400565static int
566nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
567 int errorcode, struct nfs4_exception *exception)
568{
569 struct nfs_client *clp = server->nfs_client;
570 int ret;
571
572 ret = nfs4_do_handle_exception(server, errorcode, exception);
573 if (exception->delay) {
574 rpc_delay(task, nfs4_update_delay(&exception->timeout));
575 goto out_retry;
576 }
577 if (exception->recovering) {
578 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
579 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
580 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
581 goto out_retry;
582 }
583 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
584 ret = -EIO;
585 return ret;
586out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500587 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400588 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500589 /*
590 * For NFS4ERR_MOVED, the client transport will need to
591 * be recomputed after migration recovery has completed.
592 */
593 if (errorcode == -NFS4ERR_MOVED)
594 rpc_task_release_transport(task);
595 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400596 return ret;
597}
598
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400599int
Trond Myklebust037fc982015-09-20 15:51:00 -0400600nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
601 struct nfs4_state *state, long *timeout)
602{
603 struct nfs4_exception exception = {
604 .state = state,
605 };
606
607 if (task->tk_status >= 0)
608 return 0;
609 if (timeout)
610 exception.timeout = *timeout;
611 task->tk_status = nfs4_async_handle_exception(task, server,
612 task->tk_status,
613 &exception);
614 if (exception.delay && timeout)
615 *timeout = exception.timeout;
616 if (exception.retry)
617 return -EAGAIN;
618 return 0;
619}
620
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400621/*
622 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
623 * or 'false' otherwise.
624 */
625static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
626{
627 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500628 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400629}
Trond Myklebust65de8722008-12-23 15:21:44 -0500630
Trond Myklebust452e9352010-07-31 14:29:06 -0400631static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 spin_lock(&clp->cl_lock);
634 if (time_before(clp->cl_last_renewal,timestamp))
635 clp->cl_last_renewal = timestamp;
636 spin_unlock(&clp->cl_lock);
637}
638
Trond Myklebust452e9352010-07-31 14:29:06 -0400639static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
640{
Trond Myklebustbe824162015-07-05 14:50:46 -0400641 struct nfs_client *clp = server->nfs_client;
642
643 if (!nfs4_has_session(clp))
644 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400645}
646
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400647struct nfs4_call_sync_data {
648 const struct nfs_server *seq_server;
649 struct nfs4_sequence_args *seq_args;
650 struct nfs4_sequence_res *seq_res;
651};
652
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800653void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400654 struct nfs4_sequence_res *res, int cache_reply,
655 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400656{
657 args->sa_slot = NULL;
658 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400659 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400660
661 res->sr_slot = NULL;
662}
663
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400664static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400665{
666 struct nfs4_slot *slot = res->sr_slot;
667 struct nfs4_slot_table *tbl;
668
Chuck Lever3bd23842013-08-09 12:49:19 -0400669 tbl = slot->table;
670 spin_lock(&tbl->slot_tbl_lock);
671 if (!nfs41_wake_and_assign_slot(tbl, slot))
672 nfs4_free_slot(tbl, slot);
673 spin_unlock(&tbl->slot_tbl_lock);
674
675 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400676}
677
678static int nfs40_sequence_done(struct rpc_task *task,
679 struct nfs4_sequence_res *res)
680{
681 if (res->sr_slot != NULL)
682 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400683 return 1;
684}
685
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400686#if defined(CONFIG_NFS_V4_1)
687
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400688static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400689{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500690 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400691 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500692 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400693
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400694 if (!slot)
695 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500696 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500698
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400699 /* Bump the slot sequence number */
700 if (slot->seq_done)
701 slot->seq_nr++;
702 slot->seq_done = 0;
703
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500704 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500705 /* Be nice to the server: try to ensure that the last transmitted
706 * value for highest_user_slotid <= target_highest_slotid
707 */
708 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
709 send_new_highest_used_slotid = true;
710
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500711 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500712 send_new_highest_used_slotid = false;
713 goto out_unlock;
714 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500716
717 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
718 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500719out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500720 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500721 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400722 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400723 if (waitqueue_active(&tbl->slot_waitq))
724 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400725}
726
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400727static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
728{
729 nfs41_release_slot(res->sr_slot);
730 res->sr_slot = NULL;
731}
732
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400733static int nfs41_sequence_process(struct rpc_task *task,
734 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400735{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500736 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500737 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400738 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500739 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500740 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400741
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500742 if (slot == NULL)
743 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400744 /* don't increment the sequence number if the task wasn't sent */
745 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400746 goto out;
747
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500748 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500749
Trond Myklebustac20d162012-12-15 15:36:07 -0500750 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400751 if (res->sr_status != -NFS4ERR_DELAY)
752 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500753 interrupted = true;
754 }
755
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400756 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500757 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400758 switch (res->sr_status) {
759 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400760 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400761 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500762 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500763 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500764 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400765 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
766 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500767 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400768 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500769 case 1:
770 /*
771 * sr_status remains 1 if an RPC level error occurred.
772 * The server may or may not have processed the sequence
773 * operation..
774 * Mark the slot as having hosted an interrupted RPC call.
775 */
776 slot->interrupted = 1;
777 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400778 case -NFS4ERR_DELAY:
779 /* The server detected a resend of the RPC call and
780 * returned NFS4ERR_DELAY as per Section 2.10.6.2
781 * of RFC5661.
782 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500783 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400784 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500785 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500786 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400787 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400788 case -NFS4ERR_RETRY_UNCACHED_REP:
789 case -NFS4ERR_SEQ_FALSE_RETRY:
790 /*
791 * The server thinks we tried to replay a request.
792 * Retry the call after bumping the sequence ID.
793 */
794 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500795 case -NFS4ERR_BADSLOT:
796 /*
797 * The slot id we used was probably retired. Try again
798 * using a different slot id.
799 */
Trond Myklebust99589102018-06-09 12:50:50 -0400800 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400801 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500802 goto retry_nowait;
803 case -NFS4ERR_SEQ_MISORDERED:
804 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500805 * Was the last operation on this sequence interrupted?
806 * If so, retry after bumping the sequence number.
807 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400808 if (interrupted)
809 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500810 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500811 * Could this slot have been previously retired?
812 * If so, then the server may be expecting seq_nr = 1!
813 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500814 if (slot->seq_nr != 1) {
815 slot->seq_nr = 1;
816 goto retry_nowait;
817 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400818 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400819 default:
820 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400821 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400822 }
823out:
824 /* The session may be reset by one of the error handlers. */
825 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500826out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500827 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400828session_recover:
829 nfs4_schedule_session_recovery(session, res->sr_status);
830 goto retry_nowait;
831retry_new_seq:
832 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500833retry_nowait:
834 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400835 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500836 task->tk_status = 0;
837 ret = 0;
838 }
839 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400840out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400841 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400842 goto out;
843 rpc_delay(task, NFS4_POLL_RETRY_MAX);
844 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400845}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400846
847int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
848{
849 if (!nfs41_sequence_process(task, res))
850 return 0;
851 if (res->sr_slot != NULL)
852 nfs41_sequence_free_slot(res);
853 return 1;
854
855}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500856EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400857
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400858static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
859{
860 if (res->sr_slot == NULL)
861 return 1;
862 if (res->sr_slot->table->session != NULL)
863 return nfs41_sequence_process(task, res);
864 return nfs40_sequence_done(task, res);
865}
866
867static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
868{
869 if (res->sr_slot != NULL) {
870 if (res->sr_slot->table->session != NULL)
871 nfs41_sequence_free_slot(res);
872 else
873 nfs40_sequence_free_slot(res);
874 }
875}
876
Peng Tao2c4b1312014-06-11 05:24:16 +0800877int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400878{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500879 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400880 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400881 if (!res->sr_slot->table->session)
882 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400883 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400884}
Peng Tao2c4b1312014-06-11 05:24:16 +0800885EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400886
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
888{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400889 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400890
Trond Myklebust035168ab2010-06-16 09:52:26 -0400891 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
892
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500893 nfs4_setup_sequence(data->seq_server->nfs_client,
894 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895}
896
Andy Adamson69ab40c2009-04-01 09:22:19 -0400897static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
898{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400899 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400900
Trond Myklebust14516c32010-07-31 14:29:06 -0400901 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400902}
903
Trond Myklebust17280172012-03-11 13:11:00 -0400904static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400905 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400906 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400907};
908
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400909static void
910nfs4_sequence_process_interrupted(struct nfs_client *client,
NeilBrowna52458b2018-12-03 11:30:31 +1100911 struct nfs4_slot *slot, const struct cred *cred)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400912{
913 struct rpc_task *task;
914
915 task = _nfs41_proc_sequence(client, cred, slot, true);
916 if (!IS_ERR(task))
917 rpc_put_task_async(task);
918}
919
Chuck Lever3bd23842013-08-09 12:49:19 -0400920#else /* !CONFIG_NFS_V4_1 */
921
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400922static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
923{
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 nfs40_sequence_free_slot(res);
931}
932
Peng Tao2c4b1312014-06-11 05:24:16 +0800933int nfs4_sequence_done(struct rpc_task *task,
934 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400935{
Chuck Lever3bd23842013-08-09 12:49:19 -0400936 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400937}
Peng Tao2c4b1312014-06-11 05:24:16 +0800938EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400939
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400940static void
941nfs4_sequence_process_interrupted(struct nfs_client *client,
NeilBrowna52458b2018-12-03 11:30:31 +1100942 struct nfs4_slot *slot, const struct cred *cred)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400943{
944 WARN_ON_ONCE(1);
945 slot->interrupted = 0;
946}
947
Chuck Lever3bd23842013-08-09 12:49:19 -0400948#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400949
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400950static
951void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
952 struct nfs4_sequence_res *res,
953 struct nfs4_slot *slot)
954{
955 if (!slot)
956 return;
957 slot->privileged = args->sa_privileged ? 1 : 0;
958 args->sa_slot = slot;
959
960 res->sr_slot = slot;
961 res->sr_timestamp = jiffies;
962 res->sr_status_flags = 0;
963 res->sr_status = 1;
964
965}
966
967int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500968 struct nfs4_sequence_args *args,
969 struct nfs4_sequence_res *res,
970 struct rpc_task *task)
971{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500972 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500973 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500974 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500975
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500976 /* slot already allocated? */
977 if (res->sr_slot != NULL)
978 goto out_start;
979
Anna Schumaker76ee0352017-01-10 16:49:31 -0500980 if (session) {
981 tbl = &session->fc_slot_table;
982 task->tk_timeout = 0;
983 }
984
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400985 for (;;) {
986 spin_lock(&tbl->slot_tbl_lock);
987 /* The state manager will wait until the slot table is empty */
988 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
989 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500990
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400991 slot = nfs4_alloc_slot(tbl);
992 if (IS_ERR(slot)) {
993 /* Try again in 1/4 second */
994 if (slot == ERR_PTR(-ENOMEM))
995 task->tk_timeout = HZ >> 2;
996 goto out_sleep;
997 }
998 spin_unlock(&tbl->slot_tbl_lock);
999
1000 if (likely(!slot->interrupted))
1001 break;
1002 nfs4_sequence_process_interrupted(client,
1003 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -05001004 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001005
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001006 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001007
Anna Schumakerad05cc02017-01-11 13:37:06 -05001008 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001009out_start:
1010 rpc_call_start(task);
1011 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001012
1013out_sleep:
1014 if (args->sa_privileged)
1015 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1016 NULL, RPC_PRIORITY_PRIVILEGED);
1017 else
1018 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1019 spin_unlock(&tbl->slot_tbl_lock);
1020 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001021}
1022EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1023
Chuck Lever9915ea72013-08-09 12:48:27 -04001024static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1025{
1026 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001027 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001028 data->seq_args, data->seq_res, task);
1029}
1030
1031static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1032{
1033 struct nfs4_call_sync_data *data = calldata;
1034 nfs4_sequence_done(task, data->seq_res);
1035}
1036
1037static const struct rpc_call_ops nfs40_call_sync_ops = {
1038 .rpc_call_prepare = nfs40_call_sync_prepare,
1039 .rpc_call_done = nfs40_call_sync_done,
1040};
1041
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001042static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001043 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001045 struct nfs4_sequence_args *args,
1046 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001047{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048 int ret;
1049 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001050 struct nfs_client *clp = server->nfs_client;
1051 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 .seq_server = server,
1053 .seq_args = args,
1054 .seq_res = res,
1055 };
1056 struct rpc_task_setup task_setup = {
1057 .rpc_client = clnt,
1058 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001059 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001060 .callback_data = &data
1061 };
1062
1063 task = rpc_run_task(&task_setup);
1064 if (IS_ERR(task))
1065 ret = PTR_ERR(task);
1066 else {
1067 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001068 rpc_put_task(task);
1069 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001070 return ret;
1071}
1072
Bryan Schumaker7c513052011-03-24 17:12:24 +00001073int nfs4_call_sync(struct rpc_clnt *clnt,
1074 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001075 struct rpc_message *msg,
1076 struct nfs4_sequence_args *args,
1077 struct nfs4_sequence_res *res,
1078 int cache_reply)
1079{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001080 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001081 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Trond Myklebust3c591172018-07-31 15:54:10 -04001084static void
1085nfs4_inc_nlink_locked(struct inode *inode)
1086{
1087 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1088 inc_nlink(inode);
1089}
1090
1091static void
1092nfs4_dec_nlink_locked(struct inode *inode)
1093{
1094 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1095 drop_nlink(inode);
1096}
1097
1098static void
1099update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001100 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct nfs_inode *nfsi = NFS_I(dir);
1103
Trond Myklebust16e14372018-03-20 16:53:31 -04001104 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1105 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001106 | NFS_INO_INVALID_DATA
1107 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001108 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001109 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1110 nfsi->attrtimeo_timestamp = jiffies;
1111 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001113 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001114 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1115 NFS_INO_INVALID_ACL;
1116 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001117 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001118 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001119 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001120 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001121 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001122}
1123
1124static void
1125update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001126 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001127{
1128 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001129 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 spin_unlock(&dir->i_lock);
1131}
1132
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001133struct nfs4_open_createattrs {
1134 struct nfs4_label *label;
1135 struct iattr *sattr;
1136 const __u32 verf[2];
1137};
1138
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001139static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1140 int err, struct nfs4_exception *exception)
1141{
1142 if (err != -EINVAL)
1143 return false;
1144 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1145 return false;
1146 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1147 exception->retry = 1;
1148 return true;
1149}
1150
Trond Myklebust6ae37332015-01-30 14:21:14 -05001151static u32
1152nfs4_map_atomic_open_share(struct nfs_server *server,
1153 fmode_t fmode, int openflags)
1154{
1155 u32 res = 0;
1156
1157 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1158 case FMODE_READ:
1159 res = NFS4_SHARE_ACCESS_READ;
1160 break;
1161 case FMODE_WRITE:
1162 res = NFS4_SHARE_ACCESS_WRITE;
1163 break;
1164 case FMODE_READ|FMODE_WRITE:
1165 res = NFS4_SHARE_ACCESS_BOTH;
1166 }
1167 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1168 goto out;
1169 /* Want no delegation if we're using O_DIRECT */
1170 if (openflags & O_DIRECT)
1171 res |= NFS4_SHARE_WANT_NO_DELEG;
1172out:
1173 return res;
1174}
1175
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001176static enum open_claim_type4
1177nfs4_map_atomic_open_claim(struct nfs_server *server,
1178 enum open_claim_type4 claim)
1179{
1180 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1181 return claim;
1182 switch (claim) {
1183 default:
1184 return claim;
1185 case NFS4_OPEN_CLAIM_FH:
1186 return NFS4_OPEN_CLAIM_NULL;
1187 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1188 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1189 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1190 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1191 }
1192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195{
1196 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001197 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 p->o_res.seqid = p->o_arg.seqid;
1199 p->c_res.seqid = p->c_arg.seqid;
1200 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001201 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001203 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001204}
1205
Al Viro82a2c1b2011-06-22 18:30:55 -04001206static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001207 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001208 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001209 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001210 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211{
Al Viro82a2c1b2011-06-22 18:30:55 -04001212 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001213 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001214 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001215 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001216 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001217 struct nfs4_opendata *p;
1218
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001219 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 if (p == NULL)
1221 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001222
1223 p->f_label = nfs4_label_alloc(server, gfp_mask);
1224 if (IS_ERR(p->f_label))
1225 goto err_free_p;
1226
Kinglong Meea49c2692015-07-27 15:31:38 +08001227 p->a_label = nfs4_label_alloc(server, gfp_mask);
1228 if (IS_ERR(p->a_label))
1229 goto err_free_f;
1230
Trond Myklebust63f5f792015-01-23 19:19:25 -05001231 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1232 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001233 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001234 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001235 nfs_sb_active(dentry->d_sb);
1236 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001237 p->dir = parent;
1238 p->owner = sp;
1239 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001240 p->o_arg.open_flags = flags;
1241 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001242 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001243 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001244 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1245 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001246 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1247 * will return permission denied for all bits until close */
1248 if (!(flags & O_EXCL)) {
1249 /* ask server to check for all possible rights as results
1250 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001251 switch (p->o_arg.claim) {
1252 default:
1253 break;
1254 case NFS4_OPEN_CLAIM_NULL:
1255 case NFS4_OPEN_CLAIM_FH:
1256 p->o_arg.access = NFS4_ACCESS_READ |
1257 NFS4_ACCESS_MODIFY |
1258 NFS4_ACCESS_EXTEND |
1259 NFS4_ACCESS_EXECUTE;
1260 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001261 }
David Howells7539bba2006-08-22 20:06:09 -04001262 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001263 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1264 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001265 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001267 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001268 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001269 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001270 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001271 case NFS4_OPEN_CLAIM_NULL:
1272 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1273 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1274 p->o_arg.fh = NFS_FH(dir);
1275 break;
1276 case NFS4_OPEN_CLAIM_PREVIOUS:
1277 case NFS4_OPEN_CLAIM_FH:
1278 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1279 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001280 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001281 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001282 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001284 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001285
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001286 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001287 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001288 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001289 p->c_arg.fh = &p->o_res.fh;
1290 p->c_arg.stateid = &p->o_res.stateid;
1291 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001292 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001293 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001294 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001295
1296err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001297 nfs4_label_free(p->a_label);
1298err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001299 nfs4_label_free(p->f_label);
1300err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 kfree(p);
1302err:
1303 dput(parent);
1304 return NULL;
1305}
1306
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001307static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001308{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309 struct nfs4_opendata *p = container_of(kref,
1310 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001311 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001312
Fred Isaman30ae2412016-10-18 13:39:51 -04001313 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001314 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001315 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001316 if (p->state != NULL)
1317 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001318 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001319
Kinglong Meea49c2692015-07-27 15:31:38 +08001320 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001321 nfs4_label_free(p->f_label);
1322
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001323 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001324 dput(p->dentry);
1325 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001326 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001327 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001328 kfree(p);
1329}
1330
1331static void nfs4_opendata_put(struct nfs4_opendata *p)
1332{
1333 if (p != NULL)
1334 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001335}
1336
Trond Myklebust24311f82015-09-20 10:50:17 -04001337static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1338 fmode_t fmode)
1339{
1340 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1341 case FMODE_READ|FMODE_WRITE:
1342 return state->n_rdwr != 0;
1343 case FMODE_WRITE:
1344 return state->n_wronly != 0;
1345 case FMODE_READ:
1346 return state->n_rdonly != 0;
1347 }
1348 WARN_ON_ONCE(1);
1349 return false;
1350}
1351
Trond Myklebustbe189f72018-09-27 17:12:33 -04001352static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1353 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001354{
1355 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001356
Trond Myklebust536e43d2012-01-17 22:04:26 -05001357 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001358 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001359 switch (claim) {
1360 case NFS4_OPEN_CLAIM_NULL:
1361 case NFS4_OPEN_CLAIM_FH:
1362 goto out;
1363 default:
1364 break;
1365 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001366 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001367 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001368 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1369 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001370 break;
1371 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001372 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1373 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001374 break;
1375 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001376 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1377 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001378 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001379out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001380 return ret;
1381}
1382
Trond Myklebust2a606182015-08-19 22:30:00 -05001383static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1384 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001385{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001386 if (delegation == NULL)
1387 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001388 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001389 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001390 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1391 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001392 switch (claim) {
1393 case NFS4_OPEN_CLAIM_NULL:
1394 case NFS4_OPEN_CLAIM_FH:
1395 break;
1396 case NFS4_OPEN_CLAIM_PREVIOUS:
1397 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1398 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001399 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001400 default:
1401 return 0;
1402 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001403 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001404 return 1;
1405}
1406
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001407static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001408{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001409 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001410 case FMODE_WRITE:
1411 state->n_wronly++;
1412 break;
1413 case FMODE_READ:
1414 state->n_rdonly++;
1415 break;
1416 case FMODE_READ|FMODE_WRITE:
1417 state->n_rdwr++;
1418 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001419 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001420}
1421
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001422#ifdef CONFIG_NFS_V4_1
1423static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1424{
1425 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1426 return true;
1427 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1428 return true;
1429 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1430 return true;
1431 return false;
1432}
1433#endif /* CONFIG_NFS_V4_1 */
1434
Trond Myklebustc9399f22017-11-06 15:28:01 -05001435static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1436{
1437 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1438 wake_up_all(&state->waitq);
1439}
1440
1441static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1442 const nfs4_stateid *stateid)
1443{
1444 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1445 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1446
1447 if (stateid_seqid == state_seqid + 1U ||
1448 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1449 nfs_state_log_update_open_stateid(state);
1450 else
1451 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1452}
1453
Trond Myklebust4f14c192014-02-12 19:15:06 -05001454static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001455{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001456 struct nfs_client *clp = state->owner->so_server->nfs_client;
1457 bool need_recover = false;
1458
1459 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1460 need_recover = true;
1461 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1462 need_recover = true;
1463 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1464 need_recover = true;
1465 if (need_recover)
1466 nfs4_state_mark_reclaim_nograce(clp, state);
1467}
1468
Trond Myklebustc9399f22017-11-06 15:28:01 -05001469/*
1470 * Check for whether or not the caller may update the open stateid
1471 * to the value passed in by stateid.
1472 *
1473 * Note: This function relies heavily on the server implementing
1474 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1475 * correctly.
1476 * i.e. The stateid seqids have to be initialised to 1, and
1477 * are then incremented on every state transition.
1478 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001479static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001480 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001481{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001482 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1483 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1484 if (stateid->seqid == cpu_to_be32(1))
1485 nfs_state_log_update_open_stateid(state);
1486 else
1487 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001488 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001489 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001490
1491 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1492 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001493 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001494 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001495 return false;
1496}
1497
Trond Myklebustf95549c2015-01-23 18:06:09 -05001498static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1499{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001500 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1501 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001502 if (state->n_wronly)
1503 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1504 if (state->n_rdonly)
1505 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1506 if (state->n_rdwr)
1507 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001508 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001509}
1510
Trond Myklebust226056c2014-02-11 10:41:07 -05001511static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1512 nfs4_stateid *stateid, fmode_t fmode)
1513{
1514 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1515 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1516 case FMODE_WRITE:
1517 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1518 break;
1519 case FMODE_READ:
1520 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1521 break;
1522 case 0:
1523 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1524 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1525 clear_bit(NFS_OPEN_STATE, &state->flags);
1526 }
1527 if (stateid == NULL)
1528 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001529 /* Handle OPEN+OPEN_DOWNGRADE races */
1530 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1531 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001532 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001533 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001534 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001535 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001536 nfs4_stateid_copy(&state->stateid, stateid);
1537 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001538 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001539out:
1540 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001541}
1542
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001543static void nfs_clear_open_stateid(struct nfs4_state *state,
1544 nfs4_stateid *arg_stateid,
1545 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001546{
1547 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001548 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1549 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1550 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001551 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001552 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1553 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001554}
1555
Trond Myklebust1393d962016-09-22 13:39:13 -04001556static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001557 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001558 __must_hold(&state->owner->so_lock)
1559 __must_hold(&state->seqlock)
1560 __must_hold(RCU)
1561
Trond Myklebust003707c2007-07-05 18:07:55 -04001562{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001563 DEFINE_WAIT(wait);
1564 int status = 0;
1565 for (;;) {
1566
1567 if (!nfs_need_update_open_stateid(state, stateid))
1568 return;
1569 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001570 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001571 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001572 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001573 /* Rely on seqids for serialisation with NFSv4.0 */
1574 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1575 break;
1576
1577 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1578 /*
1579 * Ensure we process the state changes in the same order
1580 * in which the server processed them by delaying the
1581 * update of the stateid until we are in sequence.
1582 */
1583 write_sequnlock(&state->seqlock);
1584 spin_unlock(&state->owner->so_lock);
1585 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001586 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001587 if (!signal_pending(current)) {
1588 if (schedule_timeout(5*HZ) == 0)
1589 status = -EAGAIN;
1590 else
1591 status = 0;
1592 } else
1593 status = -EINTR;
1594 finish_wait(&state->waitq, &wait);
1595 rcu_read_lock();
1596 spin_lock(&state->owner->so_lock);
1597 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001598 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001599
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001600 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1601 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001602 nfs4_stateid_copy(freeme, &state->open_stateid);
1603 nfs_test_and_clear_all_open_stateid(state);
1604 }
1605
Trond Myklebuste999e802014-02-10 18:20:47 -05001606 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1607 nfs4_stateid_copy(&state->stateid, stateid);
1608 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001609 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001610 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001611}
1612
Trond Myklebustc9399f22017-11-06 15:28:01 -05001613static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001614 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001615 fmode_t fmode,
1616 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001618 /*
1619 * Protect the call to nfs4_state_set_mode_locked and
1620 * serialise the stateid update
1621 */
1622 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001623 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1624 switch (fmode) {
1625 case FMODE_READ:
1626 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1627 break;
1628 case FMODE_WRITE:
1629 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1630 break;
1631 case FMODE_READ|FMODE_WRITE:
1632 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001633 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001634 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001635 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001636}
1637
1638static void nfs_state_set_delegation(struct nfs4_state *state,
1639 const nfs4_stateid *deleg_stateid,
1640 fmode_t fmode)
1641{
1642 /*
1643 * Protect the call to nfs4_state_set_mode_locked and
1644 * serialise the stateid update
1645 */
1646 write_seqlock(&state->seqlock);
1647 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1648 set_bit(NFS_DELEGATED_STATE, &state->flags);
1649 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001652static void nfs_state_clear_delegation(struct nfs4_state *state)
1653{
1654 write_seqlock(&state->seqlock);
1655 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1656 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1657 write_sequnlock(&state->seqlock);
1658}
1659
Trond Myklebust1393d962016-09-22 13:39:13 -04001660static int update_open_stateid(struct nfs4_state *state,
1661 const nfs4_stateid *open_stateid,
1662 const nfs4_stateid *delegation,
1663 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001664{
Trond Myklebust1393d962016-09-22 13:39:13 -04001665 struct nfs_server *server = NFS_SERVER(state->inode);
1666 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001667 struct nfs_inode *nfsi = NFS_I(state->inode);
1668 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001669 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001670 int ret = 0;
1671
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001672 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001673
1674 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001675 spin_lock(&state->owner->so_lock);
1676 if (open_stateid != NULL) {
1677 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1678 ret = 1;
1679 }
1680
Trond Myklebust34310432008-12-23 15:21:38 -05001681 deleg_cur = rcu_dereference(nfsi->delegation);
1682 if (deleg_cur == NULL)
1683 goto no_delegation;
1684
1685 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001686 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001687 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001688 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001689 goto no_delegation_unlock;
1690
1691 if (delegation == NULL)
1692 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001693 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001694 goto no_delegation_unlock;
1695
Trond Myklebustb7391f42008-12-23 15:21:52 -05001696 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001697 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001698 ret = 1;
1699no_delegation_unlock:
1700 spin_unlock(&deleg_cur->lock);
1701no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001702 if (ret)
1703 update_open_stateflags(state, fmode);
1704 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001705 rcu_read_unlock();
1706
Trond Myklebust4f14c192014-02-12 19:15:06 -05001707 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001708 nfs4_schedule_state_manager(clp);
1709 if (freeme.type != 0)
1710 nfs4_test_and_free_stateid(server, &freeme,
1711 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001712
1713 return ret;
1714}
1715
Trond Myklebust39071e62015-01-24 15:07:56 -05001716static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1717 const nfs4_stateid *stateid)
1718{
1719 struct nfs4_state *state = lsp->ls_state;
1720 bool ret = false;
1721
1722 spin_lock(&state->state_lock);
1723 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1724 goto out_noupdate;
1725 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1726 goto out_noupdate;
1727 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1728 ret = true;
1729out_noupdate:
1730 spin_unlock(&state->state_lock);
1731 return ret;
1732}
Trond Myklebust34310432008-12-23 15:21:38 -05001733
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001734static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001735{
1736 struct nfs_delegation *delegation;
1737
Trond Myklebustf5086242018-03-20 16:43:13 -04001738 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001739 rcu_read_lock();
1740 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001741 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001742 rcu_read_unlock();
1743 return;
1744 }
1745 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001746 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001747}
1748
Trond Myklebust6ee41262007-07-08 14:11:36 -04001749static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001750{
1751 struct nfs4_state *state = opendata->state;
1752 struct nfs_inode *nfsi = NFS_I(state->inode);
1753 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001754 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001755 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001756 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001757 nfs4_stateid stateid;
1758 int ret = -EAGAIN;
1759
Trond Myklebustaac00a82007-07-05 19:02:21 -04001760 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001761 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001762 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001763 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001764 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001765 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001766 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001767 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001768 rcu_read_lock();
1769 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001770 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001771 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001772 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001773 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001774 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001775 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001776 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001777 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001778 if (!opendata->is_recover) {
1779 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1780 if (ret != 0)
1781 goto out;
1782 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001783 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001784
1785 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001786 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001787 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001788 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001789out:
1790 return ERR_PTR(ret);
1791out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001792 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001793 return state;
1794}
1795
Andy Adamsone23008e2012-10-02 21:07:32 -04001796static void
1797nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1798{
1799 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1800 struct nfs_delegation *delegation;
1801 int delegation_flags = 0;
1802
1803 rcu_read_lock();
1804 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1805 if (delegation)
1806 delegation_flags = delegation->flags;
1807 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001808 switch (data->o_arg.claim) {
1809 default:
1810 break;
1811 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1812 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001813 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1814 "returning a delegation for "
1815 "OPEN(CLAIM_DELEGATE_CUR)\n",
1816 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001817 return;
1818 }
1819 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001820 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001821 data->owner->so_cred,
1822 data->o_res.delegation_type,
1823 &data->o_res.delegation,
1824 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001825 else
1826 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001827 data->owner->so_cred,
1828 data->o_res.delegation_type,
1829 &data->o_res.delegation,
1830 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001831
1832 if (data->o_res.do_recall)
1833 nfs_async_inode_return_delegation(state->inode,
1834 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001835}
1836
1837/*
1838 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1839 * and update the nfs4_state.
1840 */
1841static struct nfs4_state *
1842_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1843{
1844 struct inode *inode = data->state->inode;
1845 struct nfs4_state *state = data->state;
1846 int ret;
1847
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001848 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001849 if (data->rpc_status)
1850 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001851 /* cached opens have already been processed */
1852 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001853 }
1854
Andy Adamsone23008e2012-10-02 21:07:32 -04001855 ret = nfs_refresh_inode(inode, &data->f_attr);
1856 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001857 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001858
1859 if (data->o_res.delegation_type != 0)
1860 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001861update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001862 update_open_stateid(state, &data->o_res.stateid, NULL,
1863 data->o_arg.fmode);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001864 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001865
1866 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001867}
1868
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001869static struct inode *
1870nfs4_opendata_get_inode(struct nfs4_opendata *data)
1871{
1872 struct inode *inode;
1873
1874 switch (data->o_arg.claim) {
1875 case NFS4_OPEN_CLAIM_NULL:
1876 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1877 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1878 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1879 return ERR_PTR(-EAGAIN);
1880 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1881 &data->f_attr, data->f_label);
1882 break;
1883 default:
1884 inode = d_inode(data->dentry);
1885 ihold(inode);
1886 nfs_refresh_inode(inode, &data->f_attr);
1887 }
1888 return inode;
1889}
1890
Andy Adamsone23008e2012-10-02 21:07:32 -04001891static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001892nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1893{
1894 struct nfs4_state *state;
1895 struct inode *inode;
1896
1897 inode = nfs4_opendata_get_inode(data);
1898 if (IS_ERR(inode))
1899 return ERR_CAST(inode);
1900 if (data->state != NULL && data->state->inode == inode) {
1901 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001902 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04001903 } else
1904 state = nfs4_get_open_state(inode, data->owner);
1905 iput(inode);
1906 if (state == NULL)
1907 state = ERR_PTR(-ENOMEM);
1908 return state;
1909}
1910
Andy Adamsone23008e2012-10-02 21:07:32 -04001911static struct nfs4_state *
1912_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001913{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001914 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001915
Trond Myklebustaac00a82007-07-05 19:02:21 -04001916 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001917 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001918 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001919 goto out;
1920 }
1921
Trond Myklebust75e8c482017-08-08 10:38:07 -04001922 state = nfs4_opendata_find_nfs4_state(data);
1923 if (IS_ERR(state))
1924 goto out;
1925
Andy Adamsone23008e2012-10-02 21:07:32 -04001926 if (data->o_res.delegation_type != 0)
1927 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001928 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001929 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001930out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001931 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001932 return state;
1933}
1934
Andy Adamsone23008e2012-10-02 21:07:32 -04001935static struct nfs4_state *
1936nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1937{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001938 struct nfs4_state *ret;
1939
Andy Adamsone23008e2012-10-02 21:07:32 -04001940 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001941 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1942 else
1943 ret = _nfs4_opendata_to_nfs4_state(data);
1944 nfs4_sequence_free_slot(&data->o_res.seq_res);
1945 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001946}
1947
Trond Myklebust0de43972018-09-02 15:57:01 -04001948static struct nfs_open_context *
1949nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001950{
1951 struct nfs_inode *nfsi = NFS_I(state->inode);
1952 struct nfs_open_context *ctx;
1953
Trond Myklebust0de43972018-09-02 15:57:01 -04001954 rcu_read_lock();
1955 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01001956 if (ctx->state != state)
1957 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04001958 if ((ctx->mode & mode) != mode)
1959 continue;
1960 if (!get_nfs_open_context(ctx))
1961 continue;
1962 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001963 return ctx;
1964 }
Trond Myklebust0de43972018-09-02 15:57:01 -04001965 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001966 return ERR_PTR(-ENOENT);
1967}
1968
Trond Myklebust0de43972018-09-02 15:57:01 -04001969static struct nfs_open_context *
1970nfs4_state_find_open_context(struct nfs4_state *state)
1971{
1972 struct nfs_open_context *ctx;
1973
1974 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
1975 if (!IS_ERR(ctx))
1976 return ctx;
1977 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
1978 if (!IS_ERR(ctx))
1979 return ctx;
1980 return nfs4_state_find_open_context_mode(state, FMODE_READ);
1981}
1982
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001983static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1984 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001985{
1986 struct nfs4_opendata *opendata;
1987
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001988 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001989 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001990 if (opendata == NULL)
1991 return ERR_PTR(-ENOMEM);
1992 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001993 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001994 return opendata;
1995}
1996
Trond Myklebust24311f82015-09-20 10:50:17 -04001997static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1998 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001999{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002000 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002001 int ret;
2002
Trond Myklebust24311f82015-09-20 10:50:17 -04002003 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002004 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002005 opendata->o_arg.open_flags = 0;
2006 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002007 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2008 NFS_SB(opendata->dentry->d_sb),
2009 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002010 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2011 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2012 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002013 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002014 if (ret != 0)
2015 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002016 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002017 if (IS_ERR(newstate))
2018 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002019 if (newstate != opendata->state)
2020 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002021 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002022 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002023}
2024
2025static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2026{
Trond Myklebust864472e2006-01-03 09:55:15 +01002027 int ret;
2028
Trond Myklebust4f14c192014-02-12 19:15:06 -05002029 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2030 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2031 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2032 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002033 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002034 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002035 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002036 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002037 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2038 if (ret != 0)
2039 return ret;
2040 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2041 if (ret != 0)
2042 return ret;
2043 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2044 if (ret != 0)
2045 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002046 /*
2047 * We may have performed cached opens for all three recoveries.
2048 * Check if we need to update the current stateid.
2049 */
2050 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002051 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002052 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002053 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002054 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002055 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002056 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002057 return 0;
2058}
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060/*
2061 * OPEN_RECLAIM:
2062 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002064static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002066 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002067 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002068 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 int status;
2070
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002071 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2072 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002073 if (IS_ERR(opendata))
2074 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002075 rcu_read_lock();
2076 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002077 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002078 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002079 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002080 opendata->o_arg.u.delegation_type = delegation_type;
2081 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002082 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return status;
2084}
2085
Trond Myklebust539cd032007-06-05 11:46:42 -04002086static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 struct nfs_server *server = NFS_SERVER(state->inode);
2089 struct nfs4_exception exception = { };
2090 int err;
2091 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002092 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002093 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002094 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2095 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002096 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002097 break;
2098 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 } while (exception.retry);
2100 return err;
2101}
2102
Trond Myklebust864472e2006-01-03 09:55:15 +01002103static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2104{
2105 struct nfs_open_context *ctx;
2106 int ret;
2107
2108 ctx = nfs4_state_find_open_context(state);
2109 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002110 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002111 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002112 put_nfs_open_context(ctx);
2113 return ret;
2114}
2115
NeilBrowndce26302017-12-13 09:57:09 +11002116static 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 -07002117{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002118 switch (err) {
2119 default:
2120 printk(KERN_ERR "NFS: %s: unhandled error "
2121 "%d.\n", __func__, err);
2122 case 0:
2123 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002124 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002125 case -ESTALE:
2126 break;
2127 case -NFS4ERR_BADSESSION:
2128 case -NFS4ERR_BADSLOT:
2129 case -NFS4ERR_BAD_HIGH_SLOT:
2130 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2131 case -NFS4ERR_DEADSESSION:
2132 set_bit(NFS_DELEGATED_STATE, &state->flags);
2133 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2134 return -EAGAIN;
2135 case -NFS4ERR_STALE_CLIENTID:
2136 case -NFS4ERR_STALE_STATEID:
2137 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002138 /* Don't recall a delegation if it was lost */
2139 nfs4_schedule_lease_recovery(server->nfs_client);
2140 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002141 case -NFS4ERR_MOVED:
2142 nfs4_schedule_migration_recovery(server);
2143 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002144 case -NFS4ERR_LEASE_MOVED:
2145 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2146 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002147 case -NFS4ERR_DELEG_REVOKED:
2148 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002149 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002150 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002151 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002152 nfs_inode_find_state_and_recover(state->inode,
2153 stateid);
2154 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002155 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002156 case -NFS4ERR_DELAY:
2157 case -NFS4ERR_GRACE:
2158 set_bit(NFS_DELEGATED_STATE, &state->flags);
2159 ssleep(1);
2160 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002161 case -ENOMEM:
2162 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002163 if (fl) {
2164 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2165 if (lsp)
2166 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2167 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002168 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return err;
2171}
2172
Trond Myklebust24311f82015-09-20 10:50:17 -04002173int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2174 struct nfs4_state *state, const nfs4_stateid *stateid,
2175 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002176{
2177 struct nfs_server *server = NFS_SERVER(state->inode);
2178 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002179 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002180
2181 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2182 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2183 if (IS_ERR(opendata))
2184 return PTR_ERR(opendata);
2185 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002186 nfs_state_clear_delegation(state);
Trond Myklebust24311f82015-09-20 10:50:17 -04002187 switch (type & (FMODE_READ|FMODE_WRITE)) {
2188 case FMODE_READ|FMODE_WRITE:
2189 case FMODE_WRITE:
2190 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2191 if (err)
2192 break;
2193 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2194 if (err)
2195 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002196 /* Fall through */
Trond Myklebust24311f82015-09-20 10:50:17 -04002197 case FMODE_READ:
2198 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2199 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002200 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002201 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002202}
2203
Chuck Leverbe05c862013-08-09 12:49:47 -04002204static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2205{
2206 struct nfs4_opendata *data = calldata;
2207
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002208 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2209 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002210}
2211
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002212static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2213{
2214 struct nfs4_opendata *data = calldata;
2215
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002216 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002217
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002218 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002219 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002220 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002221 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002222 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002223 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002224 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002225}
2226
2227static void nfs4_open_confirm_release(void *calldata)
2228{
2229 struct nfs4_opendata *data = calldata;
2230 struct nfs4_state *state = NULL;
2231
2232 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002233 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002234 goto out_free;
2235 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002236 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002237 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002238 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002239 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002240 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002241out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002242 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002243}
2244
2245static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002246 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002247 .rpc_call_done = nfs4_open_confirm_done,
2248 .rpc_release = nfs4_open_confirm_release,
2249};
2250
2251/*
2252 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2253 */
2254static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2255{
David Howells2b0143b2015-03-17 22:25:59 +00002256 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002257 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002258 struct rpc_message msg = {
2259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2260 .rpc_argp = &data->c_arg,
2261 .rpc_resp = &data->c_res,
2262 .rpc_cred = data->owner->so_cred,
2263 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002264 struct rpc_task_setup task_setup_data = {
2265 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002266 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002267 .callback_ops = &nfs4_open_confirm_ops,
2268 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002269 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002270 .flags = RPC_TASK_ASYNC,
2271 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 int status;
2273
Anna Schumakerfba83f32018-05-04 16:22:50 -04002274 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2275 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002276 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002277 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002278 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002279 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002280 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002281 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002282 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002283 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002284 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002285 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002286 smp_wmb();
2287 } else
2288 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002289 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return status;
2291}
2292
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002293static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002295 struct nfs4_opendata *data = calldata;
2296 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002297 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002298 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002299
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002300 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002301 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002302 /*
2303 * Check if we still need to send an OPEN call, or if we can use
2304 * a delegation instead.
2305 */
2306 if (data->state != NULL) {
2307 struct nfs_delegation *delegation;
2308
Trond Myklebustbe189f72018-09-27 17:12:33 -04002309 if (can_open_cached(data->state, data->o_arg.fmode,
2310 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002311 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002312 rcu_read_lock();
2313 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002314 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002315 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002316 rcu_read_unlock();
2317 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002318 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002319 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002320 switch (claim) {
2321 default:
2322 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002323 case NFS4_OPEN_CLAIM_PREVIOUS:
2324 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2325 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002326 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002327 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002328 case NFS4_OPEN_CLAIM_FH:
2329 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002330 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002331 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002332 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002333 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002334 &data->o_res.seq_res,
2335 task) != 0)
2336 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002337
2338 /* Set the create mode (note dependency on the session type) */
2339 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2340 if (data->o_arg.open_flags & O_EXCL) {
2341 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2342 if (nfs4_has_persistent_session(clp))
2343 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2344 else if (clp->cl_mvops->minor_version > 0)
2345 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2346 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002347 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002348unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002349 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002350 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002351out_no_action:
2352 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002353out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002354 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002355}
2356
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002357static void nfs4_open_done(struct rpc_task *task, void *calldata)
2358{
2359 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002361 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002362
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002363 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002364 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002365
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002366 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002367 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2368 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002369 case S_IFREG:
2370 break;
2371 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002372 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002373 break;
2374 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002375 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002376 break;
2377 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002378 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002379 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002380 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002381 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002382 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2383 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002384 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002385 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002386}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002387
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002388static void nfs4_open_release(void *calldata)
2389{
2390 struct nfs4_opendata *data = calldata;
2391 struct nfs4_state *state = NULL;
2392
2393 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002394 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002395 goto out_free;
2396 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002397 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002398 goto out_free;
2399 /* In case we need an open_confirm, no cleanup! */
2400 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2401 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002402 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002403 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002404 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002405out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002406 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002407}
2408
2409static const struct rpc_call_ops nfs4_open_ops = {
2410 .rpc_call_prepare = nfs4_open_prepare,
2411 .rpc_call_done = nfs4_open_done,
2412 .rpc_release = nfs4_open_release,
2413};
2414
Fred Isaman3b65a302016-09-19 10:06:49 -04002415static int nfs4_run_open_task(struct nfs4_opendata *data,
2416 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002417{
David Howells2b0143b2015-03-17 22:25:59 +00002418 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002419 struct nfs_server *server = NFS_SERVER(dir);
2420 struct nfs_openargs *o_arg = &data->o_arg;
2421 struct nfs_openres *o_res = &data->o_res;
2422 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002423 struct rpc_message msg = {
2424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2425 .rpc_argp = o_arg,
2426 .rpc_resp = o_res,
2427 .rpc_cred = data->owner->so_cred,
2428 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002429 struct rpc_task_setup task_setup_data = {
2430 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002431 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002432 .callback_ops = &nfs4_open_ops,
2433 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002434 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002435 .flags = RPC_TASK_ASYNC,
2436 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002437 int status;
2438
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002439 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002440 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002441 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002442 data->cancelled = false;
2443 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002444 if (!ctx) {
2445 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002446 data->is_recover = true;
Fred Isaman2409a972016-10-06 12:11:21 -04002447 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002448 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002449 pnfs_lgopen_prepare(data, ctx);
2450 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002451 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002452 if (IS_ERR(task))
2453 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002454 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002455 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002456 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002457 smp_wmb();
2458 } else
2459 status = data->rpc_status;
2460 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002461
2462 return status;
2463}
2464
2465static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2466{
David Howells2b0143b2015-03-17 22:25:59 +00002467 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002468 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002469 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002470
Fred Isaman3b65a302016-09-19 10:06:49 -04002471 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002472 if (status != 0 || !data->rpc_done)
2473 return status;
2474
Trond Myklebust6926afd12012-01-07 13:22:46 -05002475 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2476
Anna Schumakerd7e98252017-01-11 16:13:29 -05002477 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002478 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002479
2480 return status;
2481}
2482
Trond Myklebustf3792d62014-07-10 08:54:32 -04002483/*
2484 * Additional permission checks in order to distinguish between an
2485 * open for read, and an open for execute. This works around the
2486 * fact that NFSv4 OPEN treats read and execute permissions as being
2487 * the same.
2488 * Note that in the non-execute case, we want to turn off permission
2489 * checking if we just created a new file (POSIX open() semantics).
2490 */
NeilBrowna52458b2018-12-03 11:30:31 +11002491static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002492 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002493 struct nfs4_state *state, fmode_t fmode,
2494 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002495{
2496 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002497 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002498
2499 /* access call failed or for some reason the server doesn't
2500 * support any access modes -- defer access call until later */
2501 if (opendata->o_res.access_supported == 0)
2502 return 0;
2503
2504 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002505 /*
2506 * Use openflags to check for exec, because fmode won't
2507 * always have FMODE_EXEC set when file open for exec.
2508 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002509 if (openflags & __FMODE_EXEC) {
2510 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002511 if (S_ISDIR(state->inode->i_mode))
2512 mask = NFS4_ACCESS_LOOKUP;
2513 else
2514 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002515 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002516 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002517
2518 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002519 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2520 nfs_access_add_cache(state->inode, &cache);
2521
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002522 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2523 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002524 return 0;
2525
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002526 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002527}
2528
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002529/*
2530 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2531 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002532static int _nfs4_proc_open(struct nfs4_opendata *data,
2533 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002534{
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_server *server = NFS_SERVER(dir);
2537 struct nfs_openargs *o_arg = &data->o_arg;
2538 struct nfs_openres *o_res = &data->o_res;
2539 int status;
2540
Fred Isaman3b65a302016-09-19 10:06:49 -04002541 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002542 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002543 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002544 if (status != 0) {
2545 if (status == -NFS4ERR_BADNAME &&
2546 !(o_arg->open_flags & O_CREAT))
2547 return -ENOENT;
2548 return status;
2549 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002550
Trond Myklebust6926afd12012-01-07 13:22:46 -05002551 nfs_fattr_map_and_free_names(server, &data->f_attr);
2552
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002553 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002554 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002555 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002556 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002557 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002558 if (data->file_created ||
2559 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002560 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002561 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002562 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002563 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2564 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002566 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002568 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002570 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2571 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002572 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2573 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002574 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002575 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578/*
2579 * OPEN_EXPIRED:
2580 * reclaim state on the server after a network partition.
2581 * Assumes caller holds the appropriate lock
2582 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002583static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002585 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002586 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002588 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002589 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002590 if (IS_ERR(opendata))
2591 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002592 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002593 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002594 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002595 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002599static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002600{
Trond Myklebust539cd032007-06-05 11:46:42 -04002601 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002602 struct nfs4_exception exception = { };
2603 int err;
2604
2605 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002606 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002607 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002608 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2609 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002610 switch (err) {
2611 default:
2612 goto out;
2613 case -NFS4ERR_GRACE:
2614 case -NFS4ERR_DELAY:
2615 nfs4_handle_exception(server, err, &exception);
2616 err = 0;
2617 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002618 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002619out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002620 return err;
2621}
2622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2624{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002626 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Trond Myklebust864472e2006-01-03 09:55:15 +01002628 ctx = nfs4_state_find_open_context(state);
2629 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002630 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002631 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002632 put_nfs_open_context(ctx);
2633 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
Trond Myklebust41020b62016-09-22 13:38:58 -04002636static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2637 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002638{
Trond Myklebust41020b62016-09-22 13:38:58 -04002639 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002640 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002641}
2642
2643static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2644{
2645 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002646 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002647}
2648
2649static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2650{
2651 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2652 nfs40_clear_delegation_stateid(state);
2653 return nfs4_open_expired(sp, state);
2654}
2655
Trond Myklebust45870d62016-09-22 13:38:59 -04002656static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2657 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002658 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002659{
2660 return -NFS4ERR_BAD_STATEID;
2661}
2662
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002663#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002664static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2665 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002666 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002667{
2668 int status;
2669
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002670 switch (stateid->type) {
2671 default:
2672 break;
2673 case NFS4_INVALID_STATEID_TYPE:
2674 case NFS4_SPECIAL_STATEID_TYPE:
2675 return -NFS4ERR_BAD_STATEID;
2676 case NFS4_REVOKED_STATEID_TYPE:
2677 goto out_free;
2678 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002679
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002680 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002681 switch (status) {
2682 case -NFS4ERR_EXPIRED:
2683 case -NFS4ERR_ADMIN_REVOKED:
2684 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002685 break;
2686 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002687 return status;
2688 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002689out_free:
2690 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002691 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002692 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002693}
2694
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002695static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002696{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002697 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002698 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002699 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002700 const struct cred *cred = NULL;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002701 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002702
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002703 /* Get the delegation credential for use by test/free_stateid */
2704 rcu_read_lock();
2705 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002706 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002707 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002708 nfs_state_clear_delegation(state);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002709 return;
2710 }
2711
2712 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002713 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002714 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002715 nfs_state_clear_delegation(state);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002716 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002717 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002718
Trond Myklebust994b15b2018-09-05 14:07:14 -04002719 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2720 &delegation->flags)) {
2721 rcu_read_unlock();
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002722 return;
2723 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002724
NeilBrowna52458b2018-12-03 11:30:31 +11002725 if (delegation->cred)
2726 cred = get_cred(delegation->cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002727 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002728 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002729 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002730 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002731 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002732
NeilBrowna52458b2018-12-03 11:30:31 +11002733 if (delegation->cred)
2734 put_cred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002735}
2736
2737/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002738 * nfs41_check_expired_locks - possibly free a lock stateid
2739 *
2740 * @state: NFSv4 state for an inode
2741 *
2742 * Returns NFS_OK if recovery for this stateid is now finished.
2743 * Otherwise a negative NFS4ERR value is returned.
2744 */
2745static int nfs41_check_expired_locks(struct nfs4_state *state)
2746{
2747 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002748 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002749 struct nfs_server *server = NFS_SERVER(state->inode);
2750
2751 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2752 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002753
2754 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002755 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2756 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002757 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002758
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002759 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002760 spin_unlock(&state->state_lock);
2761
2762 nfs4_put_lock_state(prev);
2763 prev = lsp;
2764
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002765 status = nfs41_test_and_free_expired_stateid(server,
2766 &lsp->ls_stateid,
2767 cred);
2768 trace_nfs4_test_lock_stateid(state, lsp, status);
2769 if (status == -NFS4ERR_EXPIRED ||
2770 status == -NFS4ERR_BAD_STATEID) {
2771 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002772 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002773 if (!recover_lost_locks)
2774 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2775 } else if (status != NFS_OK) {
2776 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002777 nfs4_put_lock_state(prev);
2778 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002779 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002780 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002781 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002782 }
2783 spin_unlock(&state->state_lock);
2784 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002785out:
2786 return ret;
2787}
2788
2789/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002790 * nfs41_check_open_stateid - possibly free an open stateid
2791 *
2792 * @state: NFSv4 state for an inode
2793 *
2794 * Returns NFS_OK if recovery for this stateid is now finished.
2795 * Otherwise a negative NFS4ERR value is returned.
2796 */
2797static int nfs41_check_open_stateid(struct nfs4_state *state)
2798{
2799 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002800 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002801 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002802 int status;
2803
Trond Myklebustb134fc42016-09-22 13:39:16 -04002804 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002805 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2806 if (nfs4_have_delegation(state->inode, state->state))
2807 return NFS_OK;
2808 return -NFS4ERR_OPENMODE;
2809 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002810 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002811 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002812 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002813 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002814 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002815 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2816 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2817 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002818 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002819 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002820 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002821 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002822 if (nfs_open_stateid_recover_openmode(state))
2823 return -NFS4ERR_OPENMODE;
2824 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002825}
2826
2827static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2828{
Chuck Levereb64cf92012-07-11 16:30:05 -04002829 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002830
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002831 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002832 status = nfs41_check_expired_locks(state);
2833 if (status != NFS_OK)
2834 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002835 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002836 if (status != NFS_OK)
2837 status = nfs4_open_expired(sp, state);
2838 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002839}
2840#endif
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002843 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2844 * fields corresponding to attributes that were used to store the verifier.
2845 * Make sure we clobber those fields in the later setattr call
2846 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002847static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002848 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002849{
Trond Myklebust609339c2018-03-28 16:18:17 -04002850 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2851 __u32 attrset[3];
2852 unsigned ret;
2853 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002854
Trond Myklebust609339c2018-03-28 16:18:17 -04002855 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2856 attrset[i] = opendata->o_res.attrset[i];
2857 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2858 attrset[i] &= ~bitmask[i];
2859 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002860
Trond Myklebust609339c2018-03-28 16:18:17 -04002861 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2862 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002863
Trond Myklebust609339c2018-03-28 16:18:17 -04002864 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2865 if (sattr->ia_valid & ATTR_ATIME_SET)
2866 ret |= ATTR_ATIME_SET;
2867 else
2868 ret |= ATTR_ATIME;
2869 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002870
Trond Myklebust609339c2018-03-28 16:18:17 -04002871 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2872 if (sattr->ia_valid & ATTR_MTIME_SET)
2873 ret |= ATTR_MTIME_SET;
2874 else
2875 ret |= ATTR_MTIME;
2876 }
2877
2878 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002879 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002880 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002881}
2882
Trond Myklebustc21443c2013-02-07 14:26:21 -05002883static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2884 fmode_t fmode,
2885 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002886 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002887{
2888 struct nfs4_state_owner *sp = opendata->owner;
2889 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002890 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002891 struct nfs4_state *state;
2892 unsigned int seq;
2893 int ret;
2894
2895 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2896
Fred Isaman3b65a302016-09-19 10:06:49 -04002897 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002898 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002899 goto out;
2900
Trond Myklebustae55e592018-05-22 11:17:16 -04002901 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002902 ret = PTR_ERR(state);
2903 if (IS_ERR(state))
2904 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002905 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002906 if (server->caps & NFS_CAP_POSIX_LOCK)
2907 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002908 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2909 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002910
Trond Myklebust275bb302013-05-29 13:11:28 -04002911 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002912 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002913 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002914 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002915 alias = d_exact_alias(dentry, state->inode);
2916 if (!alias)
2917 alias = d_splice_alias(igrab(state->inode), dentry);
2918 /* d_splice_alias() can't fail here - it's a non-directory */
2919 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002920 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002921 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002922 }
2923 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002924 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002925 }
2926
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04002927 /* Parse layoutget results before we check for access */
2928 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2929
Trond Myklebustc21443c2013-02-07 14:26:21 -05002930 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2931 if (ret != 0)
2932 goto out;
2933
David Howells2b0143b2015-03-17 22:25:59 +00002934 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002935 nfs_inode_attach_open_context(ctx);
2936 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2937 nfs4_schedule_stateid_recovery(server, state);
2938 }
Fred Isaman2409a972016-10-06 12:11:21 -04002939
Trond Myklebustc21443c2013-02-07 14:26:21 -05002940out:
Trond Myklebustae55e592018-05-22 11:17:16 -04002941 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002942 return ret;
2943}
2944
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002945/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002946 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 */
Andy Adamson82be4172012-05-23 05:02:35 -04002948static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002949 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002950 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002951 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002952 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 struct nfs4_state_owner *sp;
2955 struct nfs4_state *state = NULL;
2956 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002957 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002958 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11002959 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04002960 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2961 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002962 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002963 struct iattr *sattr = c->sattr;
2964 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002965 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002966 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002970 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2971 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2973 goto out_err;
2974 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002975 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002976 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002977 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002978 if (d_really_is_positive(dentry))
2979 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002980 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002981 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002982 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002983 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2984 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002985 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002986 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
David Quigley14c43f72013-05-22 12:50:43 -04002988 if (label) {
2989 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2990 if (IS_ERR(olabel)) {
2991 status = PTR_ERR(olabel);
2992 goto err_opendata_put;
2993 }
2994 }
2995
Trond Myklebuste911b812014-03-26 13:24:37 -07002996 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2997 if (!opendata->f_attr.mdsthreshold) {
2998 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2999 if (!opendata->f_attr.mdsthreshold)
3000 goto err_free_label;
3001 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003002 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003003 }
David Howells2b0143b2015-03-17 22:25:59 +00003004 if (d_really_is_positive(dentry))
3005 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003006
Trond Myklebust3efb9722013-05-29 13:17:04 -04003007 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003008 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003009 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003010 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003011
NeilBrownefcbc042015-07-30 13:00:56 +10003012 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003013 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003014 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003015 /*
3016 * send create attributes which was not set by open
3017 * with an extra setattr.
3018 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003019 if (attrs || label) {
3020 unsigned ia_old = sattr->ia_valid;
3021
3022 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003023 nfs_fattr_init(opendata->o_res.f_attr);
3024 status = nfs4_do_setattr(state->inode, cred,
3025 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003026 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003027 if (status == 0) {
3028 nfs_setattr_update_inode(state->inode, sattr,
3029 opendata->o_res.f_attr);
3030 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3031 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003032 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003033 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003034 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003035 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003036 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003037
Trond Myklebuste911b812014-03-26 13:24:37 -07003038 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003039 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003040 opendata->f_attr.mdsthreshold = NULL;
3041 }
Andy Adamson82be4172012-05-23 05:02:35 -04003042
David Quigley14c43f72013-05-22 12:50:43 -04003043 nfs4_label_free(olabel);
3044
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003045 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003048err_free_label:
3049 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003050err_opendata_put:
3051 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003052err_put_state_owner:
3053 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return status;
3056}
3057
3058
Andy Adamson82be4172012-05-23 05:02:35 -04003059static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003060 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003061 int flags,
3062 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003063 struct nfs4_label *label,
3064 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003066 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 struct nfs4_exception exception = { };
3068 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003069 struct nfs4_open_createattrs c = {
3070 .label = label,
3071 .sattr = sattr,
3072 .verf = {
3073 [0] = (__u32)jiffies,
3074 [1] = (__u32)current->pid,
3075 },
3076 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 int status;
3078
3079 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003080 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003081 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003082 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 if (status == 0)
3084 break;
3085 /* NOTE: BAD_SEQID means the server and client disagree about the
3086 * book-keeping w.r.t. state-changing operations
3087 * (OPEN/CLOSE/LOCK/LOCKU...)
3088 * It is actually a sign of a bug on the client or on the server.
3089 *
3090 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003091 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 * have unhashed the old state_owner for us, and that we can
3093 * therefore safely retry using a new one. We should still warn
3094 * the user though...
3095 */
3096 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003097 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003098 " returned a bad sequence-id error!\n",
3099 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 exception.retry = 1;
3101 continue;
3102 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003103 /*
3104 * BAD_STATEID on OPEN means that the server cancelled our
3105 * state before it received the OPEN_CONFIRM.
3106 * Recover by retrying the request as per the discussion
3107 * on Page 181 of RFC3530.
3108 */
3109 if (status == -NFS4ERR_BAD_STATEID) {
3110 exception.retry = 1;
3111 continue;
3112 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003113 if (status == -EAGAIN) {
3114 /* We must have found a delegation */
3115 exception.retry = 1;
3116 continue;
3117 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003118 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3119 continue;
3120 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 status, &exception));
3122 } while (exception.retry);
3123 return res;
3124}
3125
Trond Myklebust8487c472016-06-26 08:44:35 -04003126static int _nfs4_do_setattr(struct inode *inode,
3127 struct nfs_setattrargs *arg,
3128 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003129 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003130 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003131{
3132 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003133 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3135 .rpc_argp = arg,
3136 .rpc_resp = res,
3137 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003138 };
NeilBrowna52458b2018-12-03 11:30:31 +11003139 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003140 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003141 bool truncate;
3142 int status;
3143
3144 nfs_fattr_init(res->fattr);
3145
3146 /* Servers should only apply open mode checks for file size changes */
3147 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003148 if (!truncate)
3149 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003150
Trond Myklebust991eedb2018-04-09 11:15:30 -04003151 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003152 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003153 } else if (ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003154 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003155 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003156 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003157 l_ctx = nfs_get_lock_context(ctx);
3158 if (IS_ERR(l_ctx))
3159 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003160 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3161 &arg->stateid, &delegation_cred);
3162 nfs_put_lock_context(l_ctx);
3163 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003164 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003165 } else {
3166zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003167 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003168 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003169 if (delegation_cred)
3170 msg.rpc_cred = delegation_cred;
3171
3172 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3173
NeilBrowna52458b2018-12-03 11:30:31 +11003174 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003175 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003176 renew_lease(server, timestamp);
3177 trace_nfs4_setattr(inode, &arg->stateid, status);
3178 return status;
3179}
3180
NeilBrowna52458b2018-12-03 11:30:31 +11003181static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003182 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003183 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003184 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003186 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003187 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003188 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003189 struct nfs_setattrargs arg = {
3190 .fh = NFS_FH(inode),
3191 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003193 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003194 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003195 };
3196 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003198 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003200 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003201 struct nfs4_exception exception = {
3202 .state = state,
3203 .inode = inode,
3204 .stateid = &arg.stateid,
3205 };
3206 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003209 nfs4_bitmap_copy_adjust_setattr(bitmask,
3210 nfs4_bitmask(server, olabel),
3211 inode);
3212
NeilBrown29b59f92016-10-13 15:26:47 +11003213 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003214 switch (err) {
3215 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003216 if (!(sattr->ia_valid & ATTR_SIZE)) {
3217 pr_warn_once("NFSv4: server %s is incorrectly "
3218 "applying open mode checks to "
3219 "a SETATTR that is not "
3220 "changing file size.\n",
3221 server->nfs_client->cl_hostname);
3222 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003223 if (state && !(state->state & FMODE_WRITE)) {
3224 err = -EBADF;
3225 if (sattr->ia_valid & ATTR_OPEN)
3226 err = -EACCES;
3227 goto out;
3228 }
3229 }
3230 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003232out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 return err;
3234}
3235
Peng Tao500d7012015-09-22 11:35:22 +08003236static bool
3237nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3238{
3239 if (inode == NULL || !nfs_have_layout(inode))
3240 return false;
3241
3242 return pnfs_wait_on_layoutreturn(inode, task);
3243}
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245struct nfs4_closedata {
3246 struct inode *inode;
3247 struct nfs4_state *state;
3248 struct nfs_closeargs arg;
3249 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003250 struct {
3251 struct nfs4_layoutreturn_args arg;
3252 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003253 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003254 u32 roc_barrier;
3255 bool roc;
3256 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003257 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003258 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259};
3260
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003261static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003262{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003263 struct nfs4_closedata *calldata = data;
3264 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003265 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003266
Trond Myklebustcf805162016-11-15 14:56:07 -05003267 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003268 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3269 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003270 nfs4_put_open_state(calldata->state);
3271 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003272 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003273 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003274 kfree(calldata);
3275}
3276
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003277static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003279 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003282 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003283 struct nfs4_exception exception = {
3284 .state = state,
3285 .inode = calldata->inode,
3286 .stateid = &calldata->arg.stateid,
3287 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
Chuck Levera3ca5652012-03-01 17:00:40 -05003289 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003290 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3291 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003292 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003293
3294 /* Handle Layoutreturn errors */
3295 if (calldata->arg.lr_args && task->tk_status != 0) {
3296 switch (calldata->res.lr_ret) {
3297 default:
3298 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3299 break;
3300 case 0:
3301 calldata->arg.lr_args = NULL;
3302 calldata->res.lr_res = NULL;
3303 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003304 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04003305 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04003306 &calldata->arg.lr_args->range,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003307 calldata->inode))
3308 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003309 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003310 case -NFS4ERR_ADMIN_REVOKED:
3311 case -NFS4ERR_DELEG_REVOKED:
3312 case -NFS4ERR_EXPIRED:
3313 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003314 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3315 case -NFS4ERR_WRONG_CRED:
3316 calldata->arg.lr_args = NULL;
3317 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003318 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003319 }
3320 }
3321
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003322 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 * the state_owner. we keep this around to process errors
3324 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 switch (task->tk_status) {
3326 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003327 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003328 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003329 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003330 case -NFS4ERR_ACCESS:
3331 if (calldata->arg.bitmask != NULL) {
3332 calldata->arg.bitmask = NULL;
3333 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003334 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003335
3336 }
3337 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003338 case -NFS4ERR_OLD_STATEID:
3339 /* Did we race with OPEN? */
3340 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003341 state))
3342 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003343 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003344 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003346 case -NFS4ERR_EXPIRED:
3347 nfs4_free_revoked_stateid(server,
3348 &calldata->arg.stateid,
3349 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003350 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003351 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003354 task->tk_status = nfs4_async_handle_exception(task,
3355 server, task->tk_status, &exception);
3356 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003357 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003359 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3360 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003361out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003362 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003363 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003364 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003365 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003366 return;
3367lr_restart:
3368 calldata->res.lr_ret = 0;
3369out_restart:
3370 task->tk_status = 0;
3371 rpc_restart_call_prepare(task);
3372 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373}
3374
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003375static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003377 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003378 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003379 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003380 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003381 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003382 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003383
Chuck Levera3ca5652012-03-01 17:00:40 -05003384 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003385 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003386 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003387
Trond Myklebust88069f72009-12-08 08:33:16 -05003388 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003389 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003390 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3391 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3392 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003393 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003394 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003395 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003396 if (state->n_rdonly == 0)
3397 call_close |= is_rdonly;
3398 else if (is_rdonly)
3399 calldata->arg.fmode |= FMODE_READ;
3400 if (state->n_wronly == 0)
3401 call_close |= is_wronly;
3402 else if (is_wronly)
3403 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003404 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3405 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003406 } else if (is_rdwr)
3407 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3408
Trond Myklebust5cc78612016-11-14 11:19:56 -05003409 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003410 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003411 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003412 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003413
3414 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003415 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003416 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003417 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003418
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003419 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003420 nfs_release_seqid(calldata->arg.seqid);
3421 goto out_wait;
3422 }
3423
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003424 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3425 if (lo && !pnfs_layout_is_valid(lo)) {
3426 calldata->arg.lr_args = NULL;
3427 calldata->res.lr_res = NULL;
3428 }
3429
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003430 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003431 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003432
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003433 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003434 /* Close-to-open cache consistency revalidation */
3435 if (!nfs4_have_delegation(inode, FMODE_READ))
3436 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3437 else
3438 calldata->arg.bitmask = NULL;
3439 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003440
Trond Myklebust6ae37332015-01-30 14:21:14 -05003441 calldata->arg.share_access =
3442 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3443 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003444
Trond Myklebustd8d84982016-12-19 12:14:44 -05003445 if (calldata->res.fattr == NULL)
3446 calldata->arg.bitmask = NULL;
3447 else if (calldata->arg.bitmask == NULL)
3448 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003449 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003450 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003451 &calldata->arg.seq_args,
3452 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003453 task) != 0)
3454 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003455 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003456 return;
3457out_no_action:
3458 task->tk_action = NULL;
3459out_wait:
3460 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003463static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003464 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003465 .rpc_call_done = nfs4_close_done,
3466 .rpc_release = nfs4_free_closedata,
3467};
3468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469/*
3470 * It is possible for data to be read/written from a mem-mapped file
3471 * after the sys_close call (which hits the vfs layer as a flush).
3472 * This means that we can't safely call nfsv4 close on a file until
3473 * the inode is cleared. This in turn means that we are not good
3474 * NFSv4 citizens - we do not indicate to the server to update the file's
3475 * share state even when we are done with one of the three share
3476 * stateid's in the inode.
3477 *
3478 * NOTE: Caller must be holding the sp->so_owner semaphore!
3479 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003480int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003482 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003483 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003485 struct nfs4_state_owner *sp = state->owner;
3486 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003487 struct rpc_message msg = {
3488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3489 .rpc_cred = state->owner->so_cred,
3490 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003491 struct rpc_task_setup task_setup_data = {
3492 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003493 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003494 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003495 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003496 .flags = RPC_TASK_ASYNC,
3497 };
Trond Myklebust95121352005-10-18 14:20:12 -07003498 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003500 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3501 &task_setup_data.rpc_client, &msg);
3502
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003503 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003505 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003506 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003507 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003509 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003510 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3511 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003513 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3514 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003515 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003516 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003517 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003518 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003519 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003520 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003521 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003522 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003523 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003524 calldata->lr.roc = pnfs_roc(state->inode,
3525 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3526 if (calldata->lr.roc) {
3527 calldata->arg.lr_args = &calldata->lr.arg;
3528 calldata->res.lr_res = &calldata->lr.res;
3529 }
Al Viro643168c2011-06-22 18:20:23 -04003530 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003531
Trond Myklebust1174dd12010-12-21 10:52:24 -05003532 msg.rpc_argp = &calldata->arg;
3533 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003534 task_setup_data.callback_data = calldata;
3535 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003536 if (IS_ERR(task))
3537 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003538 status = 0;
3539 if (wait)
3540 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003541 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003542 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003543out_free_calldata:
3544 kfree(calldata);
3545out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003546 nfs4_put_open_state(state);
3547 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003548 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549}
3550
Trond Myklebust2b484292010-09-17 10:56:51 -04003551static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003552nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3553 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003556 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3557
3558 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Trond Myklebust565277f2007-10-15 18:17:53 -04003560 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003561 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003562
3563 nfs4_label_release_security(label);
3564
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003565 if (IS_ERR(state))
3566 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003567 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Trond Myklebust1185a552009-12-03 15:54:02 -05003570static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003571{
3572 if (ctx->state == NULL)
3573 return;
3574 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003575 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003576 else
Al Viro643168c2011-06-22 18:20:23 -04003577 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579
Trond Myklebustb944dba2013-11-04 15:20:20 -05003580#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3581#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003582#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3585{
Kinglong Mee8c612822015-08-26 21:12:58 +08003586 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003587 struct nfs4_server_caps_arg args = {
3588 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003589 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003590 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 struct nfs4_server_caps_res res = {};
3592 struct rpc_message msg = {
3593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003594 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 .rpc_resp = &res,
3596 };
3597 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003598 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
Kinglong Mee8c612822015-08-26 21:12:58 +08003600 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3601 FATTR4_WORD0_FH_EXPIRE_TYPE |
3602 FATTR4_WORD0_LINK_SUPPORT |
3603 FATTR4_WORD0_SYMLINK_SUPPORT |
3604 FATTR4_WORD0_ACLSUPPORT;
3605 if (minorversion)
3606 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3607
Bryan Schumaker7c513052011-03-24 17:12:24 +00003608 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003610 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003611 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003612 case 0:
3613 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3614 res.attr_bitmask[2] = 0;
3615 break;
3616 case 1:
3617 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3618 break;
3619 case 2:
3620 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003623 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3624 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3625 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3626 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003627 NFS_CAP_CTIME|NFS_CAP_MTIME|
3628 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003629 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3630 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 server->caps |= NFS_CAP_ACLS;
3632 if (res.has_links != 0)
3633 server->caps |= NFS_CAP_HARDLINKS;
3634 if (res.has_symlinks != 0)
3635 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003636 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3637 server->caps |= NFS_CAP_FILEID;
3638 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3639 server->caps |= NFS_CAP_MODE;
3640 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3641 server->caps |= NFS_CAP_NLINK;
3642 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3643 server->caps |= NFS_CAP_OWNER;
3644 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3645 server->caps |= NFS_CAP_OWNER_GROUP;
3646 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3647 server->caps |= NFS_CAP_ATIME;
3648 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3649 server->caps |= NFS_CAP_CTIME;
3650 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3651 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003652#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3653 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3654 server->caps |= NFS_CAP_SECURITY_LABEL;
3655#endif
3656 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3657 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003658 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003659
Trond Myklebusta65318b2009-03-11 14:10:28 -04003660 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3661 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3662 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003663 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003664
3665 /* Avoid a regression due to buggy server */
3666 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3667 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003668 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3669 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003672 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return status;
3676}
3677
Trond Myklebust55a97592006-06-09 09:34:19 -04003678int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
3680 struct nfs4_exception exception = { };
3681 int err;
3682 do {
3683 err = nfs4_handle_exception(server,
3684 _nfs4_server_capabilities(server, fhandle),
3685 &exception);
3686 } while (exception.retry);
3687 return err;
3688}
3689
3690static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3691 struct nfs_fsinfo *info)
3692{
David Quigleyaa9c2662013-05-22 12:50:44 -04003693 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003695 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 };
3697 struct nfs4_lookup_res res = {
3698 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003699 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 .fh = fhandle,
3701 };
3702 struct rpc_message msg = {
3703 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3704 .rpc_argp = &args,
3705 .rpc_resp = &res,
3706 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003707
David Quigleyaa9c2662013-05-22 12:50:44 -04003708 bitmask[0] = nfs4_fattr_bitmap[0];
3709 bitmask[1] = nfs4_fattr_bitmap[1];
3710 /*
3711 * Process the label in the upcoming getfattr
3712 */
3713 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3714
Trond Myklebust0e574af2005-10-27 22:12:38 -04003715 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003716 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
3719static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3720 struct nfs_fsinfo *info)
3721{
3722 struct nfs4_exception exception = { };
3723 int err;
3724 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003725 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003726 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003727 switch (err) {
3728 case 0:
3729 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003730 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003731 default:
3732 err = nfs4_handle_exception(server, err, &exception);
3733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003735out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 return err;
3737}
3738
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003739static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3740 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3741{
Trond Myklebustc2190662013-08-26 19:23:04 -04003742 struct rpc_auth_create_args auth_args = {
3743 .pseudoflavor = flavor,
3744 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003745 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003746
Trond Myklebustc2190662013-08-26 19:23:04 -04003747 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003748 if (IS_ERR(auth))
3749 return -EACCES;
3750 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003751}
3752
Chuck Lever9a744ba2013-03-16 15:56:02 -04003753/*
3754 * Retry pseudoroot lookup with various security flavors. We do this when:
3755 *
3756 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3757 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3758 *
3759 * Returns zero on success, or a negative NFS4ERR value, or a
3760 * negative errno value.
3761 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003762static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003763 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003765 /* Per 3530bis 15.33.5 */
3766 static const rpc_authflavor_t flav_array[] = {
3767 RPC_AUTH_GSS_KRB5P,
3768 RPC_AUTH_GSS_KRB5I,
3769 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003770 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003771 RPC_AUTH_NULL,
3772 };
3773 int status = -EPERM;
3774 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003776 if (server->auth_info.flavor_len > 0) {
3777 /* try each flavor specified by user */
3778 for (i = 0; i < server->auth_info.flavor_len; i++) {
3779 status = nfs4_lookup_root_sec(server, fhandle, info,
3780 server->auth_info.flavors[i]);
3781 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3782 continue;
3783 break;
3784 }
3785 } else {
3786 /* no flavors specified by user, try default list */
3787 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3788 status = nfs4_lookup_root_sec(server, fhandle, info,
3789 flav_array[i]);
3790 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3791 continue;
3792 break;
3793 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003794 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003795
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003796 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003797 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003798 * to access the mount. It could also mean that we tried to mount
3799 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3800 * existing mount programs don't handle -EACCES very well so it should
3801 * be mapped to -EPERM instead.
3802 */
3803 if (status == -EACCES)
3804 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003805 return status;
3806}
3807
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003808/**
3809 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3810 * @server: initialized nfs_server handle
3811 * @fhandle: we fill in the pseudo-fs root file handle
3812 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003813 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003814 *
3815 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003816 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003817int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003818 struct nfs_fsinfo *info,
3819 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003820{
Andre Przywarac7757072015-04-23 17:17:40 +01003821 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003822
Andre Przywarac7757072015-04-23 17:17:40 +01003823 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003824 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003825
3826 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003827 status = server->nfs_client->cl_mvops->find_root_sec(server,
3828 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003829
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 if (status == 0)
3831 status = nfs4_server_capabilities(server, fhandle);
3832 if (status == 0)
3833 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003834
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003835 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836}
3837
Bryan Schumakerbae36242012-05-10 15:07:31 -04003838static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3839 struct nfs_fsinfo *info)
3840{
3841 int error;
3842 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003843 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003844
3845 error = nfs4_server_capabilities(server, mntfh);
3846 if (error < 0) {
3847 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3848 return error;
3849 }
3850
David Quigley14c43f72013-05-22 12:50:43 -04003851 label = nfs4_label_alloc(server, GFP_KERNEL);
3852 if (IS_ERR(label))
3853 return PTR_ERR(label);
3854
Trond Myklebusta841b542018-04-07 13:50:59 -04003855 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003856 if (error < 0) {
3857 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003858 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003859 }
3860
3861 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3862 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3863 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3864
David Quigley14c43f72013-05-22 12:50:43 -04003865err_free_label:
3866 nfs4_label_free(label);
3867
Bryan Schumakerbae36242012-05-10 15:07:31 -04003868 return error;
3869}
3870
Manoj Naik6b97fd32006-06-09 09:34:29 -04003871/*
3872 * Get locations and (maybe) other attributes of a referral.
3873 * Note that we'll actually follow the referral later when
3874 * we detect fsid mismatch in inode revalidation
3875 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003876static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3877 const struct qstr *name, struct nfs_fattr *fattr,
3878 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003879{
3880 int status = -ENOMEM;
3881 struct page *page = NULL;
3882 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003883
3884 page = alloc_page(GFP_KERNEL);
3885 if (page == NULL)
3886 goto out;
3887 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3888 if (locations == NULL)
3889 goto out;
3890
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003891 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003892 if (status != 0)
3893 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003894
3895 /*
3896 * If the fsid didn't change, this is a migration event, not a
3897 * referral. Cause us to drop into the exception handler, which
3898 * will kick off migration recovery.
3899 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003900 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003901 dprintk("%s: server did not return a different fsid for"
3902 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003903 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003904 goto out;
3905 }
Andy Adamson533eb462011-06-13 18:25:56 -04003906 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3907 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003908
Andy Adamson533eb462011-06-13 18:25:56 -04003909 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003910 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003911 memset(fhandle, 0, sizeof(struct nfs_fh));
3912out:
3913 if (page)
3914 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003915 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003916 return status;
3917}
3918
David Quigley1775fd32013-05-22 12:50:42 -04003919static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003920 struct nfs_fattr *fattr, struct nfs4_label *label,
3921 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
Trond Myklebust771734f2018-04-07 13:54:23 -04003923 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 struct nfs4_getattr_arg args = {
3925 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04003926 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 };
3928 struct nfs4_getattr_res res = {
3929 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003930 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 .server = server,
3932 };
3933 struct rpc_message msg = {
3934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3935 .rpc_argp = &args,
3936 .rpc_resp = &res,
3937 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003938
Trond Myklebust771734f2018-04-07 13:54:23 -04003939 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04003940
Trond Myklebust0e574af2005-10-27 22:12:38 -04003941 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003942 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943}
3944
David Quigley1775fd32013-05-22 12:50:42 -04003945static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003946 struct nfs_fattr *fattr, struct nfs4_label *label,
3947 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
3949 struct nfs4_exception exception = { };
3950 int err;
3951 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04003952 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003953 trace_nfs4_getattr(server, fhandle, fattr, err);
3954 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 &exception);
3956 } while (exception.retry);
3957 return err;
3958}
3959
3960/*
3961 * The file is not closed if it is opened due to the a request to change
3962 * the size of the file. The open call will not be needed once the
3963 * VFS layer lookup-intents are implemented.
3964 *
3965 * Close is called when the inode is destroyed.
3966 * If we haven't opened the file for O_WRONLY, we
3967 * need to in the size_change case to obtain a stateid.
3968 *
3969 * Got race?
3970 * Because OPEN is always done by name in nfsv4, it is
3971 * possible that we opened a different file by the same
3972 * name. We can recognize this race condition, but we
3973 * can't do anything about it besides returning an error.
3974 *
3975 * This will be fixed with VFS changes (lookup-intent).
3976 */
3977static int
3978nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3979 struct iattr *sattr)
3980{
David Howells2b0143b2015-03-17 22:25:59 +00003981 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11003982 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003983 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003984 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 int status;
3986
Peng Tao88ac8152014-09-12 11:04:10 +08003987 if (pnfs_ld_layoutret_on_setattr(inode) &&
3988 sattr->ia_valid & ATTR_SIZE &&
3989 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003990 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003991
Trond Myklebust0e574af2005-10-27 22:12:38 -04003992 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Andy Adamson26699402012-05-30 16:12:24 -04003994 /* Deal with open(O_TRUNC) */
3995 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003996 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003997
3998 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003999 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004000 return 0;
4001
Trond Myklebustd5308382005-11-04 15:33:38 -05004002 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004003 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004004
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004005 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004006 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004007 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004008 }
4009
David Quigley14c43f72013-05-22 12:50:43 -04004010 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4011 if (IS_ERR(label))
4012 return PTR_ERR(label);
4013
Trond Myklebust199366f2018-03-20 16:43:18 -04004014 /* Return any delegations if we're going to change ACLs */
4015 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004016 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004017
NeilBrown29b59f92016-10-13 15:26:47 +11004018 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004019 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004020 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004021 nfs_setsecurity(inode, fattr, label);
4022 }
David Quigley14c43f72013-05-22 12:50:43 -04004023 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 return status;
4025}
4026
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004027static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4028 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004029 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004030{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004031 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004032 int status;
4033 struct nfs4_lookup_arg args = {
4034 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004035 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04004036 .name = name,
4037 };
4038 struct nfs4_lookup_res res = {
4039 .server = server,
4040 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004041 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004042 .fh = fhandle,
4043 };
4044 struct rpc_message msg = {
4045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4046 .rpc_argp = &args,
4047 .rpc_resp = &res,
4048 };
4049
David Quigleyaa9c2662013-05-22 12:50:44 -04004050 args.bitmask = nfs4_bitmask(server, label);
4051
David Howells2b3de442006-08-22 20:06:09 -04004052 nfs_fattr_init(fattr);
4053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004055 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 dprintk("NFS reply lookup: %d\n", status);
4057 return status;
4058}
4059
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004060static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004061{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004062 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004063 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004064 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4065 fattr->nlink = 2;
4066}
4067
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004068static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004069 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004070 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004071{
4072 struct nfs4_exception exception = { };
4073 struct rpc_clnt *client = *clnt;
4074 int err;
4075 do {
David Quigley1775fd32013-05-22 12:50:42 -04004076 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004077 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004078 switch (err) {
4079 case -NFS4ERR_BADNAME:
4080 err = -ENOENT;
4081 goto out;
4082 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004083 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004084 if (err == -NFS4ERR_MOVED)
4085 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004086 goto out;
4087 case -NFS4ERR_WRONGSEC:
4088 err = -EPERM;
4089 if (client != *clnt)
4090 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004091 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004092 if (IS_ERR(client))
4093 return PTR_ERR(client);
4094
4095 exception.retry = 1;
4096 break;
4097 default:
4098 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4099 }
4100 } while (exception.retry);
4101
4102out:
4103 if (err == 0)
4104 *clnt = client;
4105 else if (client != *clnt)
4106 rpc_shutdown_client(client);
4107
4108 return err;
4109}
4110
Al Virobeffb8f2016-07-20 16:34:42 -04004111static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004112 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4113 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004115 int status;
4116 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004117
David Quigley1775fd32013-05-22 12:50:42 -04004118 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004119 if (client != NFS_CLIENT(dir)) {
4120 rpc_shutdown_client(client);
4121 nfs_fixup_secinfo_attributes(fattr);
4122 }
4123 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124}
4125
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004126struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004127nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004128 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4129{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004130 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004131 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004132
David Quigley1775fd32013-05-22 12:50:42 -04004133 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004134 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004135 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004136 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004137}
4138
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004139static int _nfs4_proc_lookupp(struct inode *inode,
4140 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4141 struct nfs4_label *label)
4142{
4143 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4144 struct nfs_server *server = NFS_SERVER(inode);
4145 int status;
4146 struct nfs4_lookupp_arg args = {
4147 .bitmask = server->attr_bitmask,
4148 .fh = NFS_FH(inode),
4149 };
4150 struct nfs4_lookupp_res res = {
4151 .server = server,
4152 .fattr = fattr,
4153 .label = label,
4154 .fh = fhandle,
4155 };
4156 struct rpc_message msg = {
4157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4158 .rpc_argp = &args,
4159 .rpc_resp = &res,
4160 };
4161
4162 args.bitmask = nfs4_bitmask(server, label);
4163
4164 nfs_fattr_init(fattr);
4165
4166 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4167 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4168 &res.seq_res, 0);
4169 dprintk("NFS reply lookupp: %d\n", status);
4170 return status;
4171}
4172
4173static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4174 struct nfs_fattr *fattr, struct nfs4_label *label)
4175{
4176 struct nfs4_exception exception = { };
4177 int err;
4178 do {
4179 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4180 trace_nfs4_lookupp(inode, err);
4181 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4182 &exception);
4183 } while (exception.retry);
4184 return err;
4185}
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4188{
Trond Myklebust76b32992007-08-10 17:45:11 -04004189 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 struct nfs4_accessargs args = {
4191 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004192 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004194 struct nfs4_accessres res = {
4195 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004196 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 struct rpc_message msg = {
4198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4199 .rpc_argp = &args,
4200 .rpc_resp = &res,
4201 .rpc_cred = entry->cred,
4202 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004203 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
Trond Myklebust7c672652018-06-04 15:00:53 -04004205 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004206 res.fattr = nfs_alloc_fattr();
4207 if (res.fattr == NULL)
4208 return -ENOMEM;
4209 args.bitmask = server->cache_consistency_bitmask;
4210 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004211 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004213 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004214 if (res.fattr)
4215 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004217 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 return status;
4219}
4220
4221static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4222{
4223 struct nfs4_exception exception = { };
4224 int err;
4225 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004226 err = _nfs4_proc_access(inode, entry);
4227 trace_nfs4_access(inode, err);
4228 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 &exception);
4230 } while (exception.retry);
4231 return err;
4232}
4233
4234/*
4235 * TODO: For the time being, we don't try to get any attributes
4236 * along with any of the zero-copy operations READ, READDIR,
4237 * READLINK, WRITE.
4238 *
4239 * In the case of the first three, we want to put the GETATTR
4240 * after the read-type operation -- this is because it is hard
4241 * to predict the length of a GETATTR response in v4, and thus
4242 * align the READ data correctly. This means that the GETATTR
4243 * may end up partially falling into the page cache, and we should
4244 * shift it into the 'tail' of the xdr_buf before processing.
4245 * To do this efficiently, we need to know the total length
4246 * of data received, which doesn't seem to be available outside
4247 * of the RPC layer.
4248 *
4249 * In the case of WRITE, we also want to put the GETATTR after
4250 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004251 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 *
4253 * Both of these changes to the XDR layer would in fact be quite
4254 * minor, but I decided to leave them for a subsequent patch.
4255 */
4256static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4257 unsigned int pgbase, unsigned int pglen)
4258{
4259 struct nfs4_readlink args = {
4260 .fh = NFS_FH(inode),
4261 .pgbase = pgbase,
4262 .pglen = pglen,
4263 .pages = &page,
4264 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004265 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 struct rpc_message msg = {
4267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4268 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004269 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 };
4271
Bryan Schumaker7c513052011-03-24 17:12:24 +00004272 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 -07004273}
4274
4275static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4276 unsigned int pgbase, unsigned int pglen)
4277{
4278 struct nfs4_exception exception = { };
4279 int err;
4280 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004281 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4282 trace_nfs4_readlink(inode, err);
4283 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 &exception);
4285 } while (exception.retry);
4286 return err;
4287}
4288
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004290 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292static int
4293nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004294 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004296 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004297 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004298 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 int status = 0;
4301
NeilBrown532d4de2016-10-13 15:26:47 +11004302 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004303 if (IS_ERR(ctx))
4304 return PTR_ERR(ctx);
4305
David Quigleyaa9c2662013-05-22 12:50:44 -04004306 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4307
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004308 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4309 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004310 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 if (IS_ERR(state)) {
4312 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004313 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004316 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004317 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 return status;
4319}
4320
Trond Myklebust3c591172018-07-31 15:54:10 -04004321static int
4322_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
Trond Myklebust16e42952005-10-27 22:12:44 -04004324 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004325 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004327 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004329 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004330 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004331 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4334 .rpc_argp = &args,
4335 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004337 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004338 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004339
Bryan Schumaker7c513052011-03-24 17:12:24 +00004340 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004341 if (status == 0) {
4342 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004343 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004344 /* Removing a directory decrements nlink in the parent */
4345 if (ftype == NF4DIR && dir->i_nlink > 2)
4346 nfs4_dec_nlink_locked(dir);
4347 spin_unlock(&dir->i_lock);
4348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return status;
4350}
4351
Trond Myklebust912678d2018-03-20 16:43:15 -04004352static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4353{
4354 struct nfs4_exception exception = { };
4355 struct inode *inode = d_inode(dentry);
4356 int err;
4357
Trond Myklebustc01d3642018-03-20 16:43:20 -04004358 if (inode) {
4359 if (inode->i_nlink == 1)
4360 nfs4_inode_return_delegation(inode);
4361 else
4362 nfs4_inode_make_writeable(inode);
4363 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004364 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004365 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004366 trace_nfs4_remove(dir, &dentry->d_name, err);
4367 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4368 &exception);
4369 } while (exception.retry);
4370 return err;
4371}
4372
4373static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
4375 struct nfs4_exception exception = { };
4376 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004377
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004379 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004380 trace_nfs4_remove(dir, name, err);
4381 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 &exception);
4383 } while (exception.retry);
4384 return err;
4385}
4386
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004387static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4388 struct dentry *dentry,
4389 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004391 struct nfs_removeargs *args = msg->rpc_argp;
4392 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Trond Myklebust977fcc22018-03-20 16:43:17 -04004394 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004396 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004397
4398 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004399
4400 if (inode)
4401 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402}
4403
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004404static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4405{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004406 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004407 &data->args.seq_args,
4408 &data->res.seq_res,
4409 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410}
4411
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004412static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004414 struct nfs_unlinkdata *data = task->tk_calldata;
4415 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004416
Trond Myklebust14516c32010-07-31 14:29:06 -04004417 if (!nfs4_sequence_done(task, &res->seq_res))
4418 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004419 if (nfs4_async_handle_error(task, res->server, NULL,
4420 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004421 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004422 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004423 update_changeattr(dir, &res->cinfo,
4424 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004425 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426}
4427
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004428static void nfs4_proc_rename_setup(struct rpc_message *msg,
4429 struct dentry *old_dentry,
4430 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004431{
Jeff Laytond3d41522010-09-17 17:31:57 -04004432 struct nfs_renameargs *arg = msg->rpc_argp;
4433 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004434 struct inode *old_inode = d_inode(old_dentry);
4435 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004436
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004437 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004438 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004439 if (new_inode)
4440 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004441 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004442 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004443 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004444}
4445
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004446static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4447{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004448 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004449 &data->args.seq_args,
4450 &data->res.seq_res,
4451 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004452}
4453
4454static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4455 struct inode *new_dir)
4456{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004457 struct nfs_renamedata *data = task->tk_calldata;
4458 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004459
4460 if (!nfs4_sequence_done(task, &res->seq_res))
4461 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004462 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004463 return 0;
4464
Trond Myklebustc733c492017-01-11 12:32:26 -05004465 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004466 if (new_dir != old_dir) {
4467 /* Note: If we moved a directory, nlink will change */
4468 update_changeattr(old_dir, &res->old_cinfo,
4469 res->old_fattr->time_start,
4470 NFS_INO_INVALID_OTHER);
4471 update_changeattr(new_dir, &res->new_cinfo,
4472 res->new_fattr->time_start,
4473 NFS_INO_INVALID_OTHER);
4474 } else
4475 update_changeattr(old_dir, &res->old_cinfo,
4476 res->old_fattr->time_start,
4477 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004478 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004479 return 1;
4480}
4481
Al Virobeffb8f2016-07-20 16:34:42 -04004482static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004484 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004485 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 struct nfs4_link_arg arg = {
4487 .fh = NFS_FH(inode),
4488 .dir_fh = NFS_FH(dir),
4489 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004490 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004492 struct nfs4_link_res res = {
4493 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004494 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004495 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 struct rpc_message msg = {
4497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4498 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004499 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004501 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Trond Myklebust136f2622010-04-16 16:22:49 -04004503 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004504 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004505 goto out;
4506
David Quigley14c43f72013-05-22 12:50:43 -04004507 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4508 if (IS_ERR(res.label)) {
4509 status = PTR_ERR(res.label);
4510 goto out;
4511 }
4512
Trond Myklebustc01d3642018-03-20 16:43:20 -04004513 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004514 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004515
Bryan Schumaker7c513052011-03-24 17:12:24 +00004516 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004517 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004518 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004519 status = nfs_post_op_update_inode(inode, res.fattr);
4520 if (!status)
4521 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004522 }
David Quigley14c43f72013-05-22 12:50:43 -04004523
4524
4525 nfs4_label_free(res.label);
4526
Trond Myklebust136f2622010-04-16 16:22:49 -04004527out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004528 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return status;
4530}
4531
Al Virobeffb8f2016-07-20 16:34:42 -04004532static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533{
4534 struct nfs4_exception exception = { };
4535 int err;
4536 do {
4537 err = nfs4_handle_exception(NFS_SERVER(inode),
4538 _nfs4_proc_link(inode, dir, name),
4539 &exception);
4540 } while (exception.retry);
4541 return err;
4542}
4543
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004544struct nfs4_createdata {
4545 struct rpc_message msg;
4546 struct nfs4_create_arg arg;
4547 struct nfs4_create_res res;
4548 struct nfs_fh fh;
4549 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004550 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004551};
4552
4553static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004554 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004555{
4556 struct nfs4_createdata *data;
4557
4558 data = kzalloc(sizeof(*data), GFP_KERNEL);
4559 if (data != NULL) {
4560 struct nfs_server *server = NFS_SERVER(dir);
4561
David Quigley14c43f72013-05-22 12:50:43 -04004562 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4563 if (IS_ERR(data->label))
4564 goto out_free;
4565
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004566 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4567 data->msg.rpc_argp = &data->arg;
4568 data->msg.rpc_resp = &data->res;
4569 data->arg.dir_fh = NFS_FH(dir);
4570 data->arg.server = server;
4571 data->arg.name = name;
4572 data->arg.attrs = sattr;
4573 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004574 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004575 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004576 data->res.server = server;
4577 data->res.fh = &data->fh;
4578 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004579 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004580 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004581 }
4582 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004583out_free:
4584 kfree(data);
4585 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004586}
4587
4588static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4589{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004590 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004591 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004592 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004593 spin_lock(&dir->i_lock);
4594 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004595 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004596 /* Creating a directory bumps nlink in the parent */
4597 if (data->arg.ftype == NF4DIR)
4598 nfs4_inc_nlink_locked(dir);
4599 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004600 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004601 }
4602 return status;
4603}
4604
4605static void nfs4_free_createdata(struct nfs4_createdata *data)
4606{
David Quigley14c43f72013-05-22 12:50:43 -04004607 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004608 kfree(data);
4609}
4610
Chuck Lever4f390c12006-08-22 20:06:22 -04004611static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004612 struct page *page, unsigned int len, struct iattr *sattr,
4613 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004615 struct nfs4_createdata *data;
4616 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
Chuck Lever94a6d752006-08-22 20:06:23 -04004618 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004619 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004620
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004621 status = -ENOMEM;
4622 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4623 if (data == NULL)
4624 goto out;
4625
4626 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4627 data->arg.u.symlink.pages = &page;
4628 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004629 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004631 status = nfs4_do_create(dir, dentry, data);
4632
4633 nfs4_free_createdata(data);
4634out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 return status;
4636}
4637
Chuck Lever4f390c12006-08-22 20:06:22 -04004638static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004639 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640{
4641 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004642 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004644
4645 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004648 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4649 trace_nfs4_symlink(dir, &dentry->d_name, err);
4650 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 &exception);
4652 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004653
4654 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 return err;
4656}
4657
4658static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004659 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004661 struct nfs4_createdata *data;
4662 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004664 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4665 if (data == NULL)
4666 goto out;
4667
David Quigley1775fd32013-05-22 12:50:42 -04004668 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004669 status = nfs4_do_create(dir, dentry, data);
4670
4671 nfs4_free_createdata(data);
4672out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 return status;
4674}
4675
4676static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4677 struct iattr *sattr)
4678{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004679 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004681 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004683
David Quigleyaa9c2662013-05-22 12:50:44 -04004684 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4685
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004686 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4687 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004689 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4690 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4691 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 &exception);
4693 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004694 nfs4_label_release_security(label);
4695
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 return err;
4697}
4698
NeilBrown684f39b2018-12-03 11:30:30 +11004699static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004700 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701{
David Howells2b0143b2015-03-17 22:25:59 +00004702 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 struct nfs4_readdir_arg args = {
4704 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004705 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 .pgbase = 0,
4707 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004708 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004709 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 };
4711 struct nfs4_readdir_res res;
4712 struct rpc_message msg = {
4713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4714 .rpc_argp = &args,
4715 .rpc_resp = &res,
4716 .rpc_cred = cred,
4717 };
4718 int status;
4719
Al Viro6de14722013-09-16 10:53:17 -04004720 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4721 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004722 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004723 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004725 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 -05004726 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004727 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004728 status += args.pgbase;
4729 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004730
4731 nfs_invalidate_atime(dir);
4732
Harvey Harrison3110ff82008-05-02 13:42:44 -07004733 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 return status;
4735}
4736
NeilBrown684f39b2018-12-03 11:30:30 +11004737static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004738 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
4740 struct nfs4_exception exception = { };
4741 int err;
4742 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004743 err = _nfs4_proc_readdir(dentry, cred, cookie,
4744 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004745 trace_nfs4_readdir(d_inode(dentry), err);
4746 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 &exception);
4748 } while (exception.retry);
4749 return err;
4750}
4751
4752static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004753 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004755 struct nfs4_createdata *data;
4756 int mode = sattr->ia_mode;
4757 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004759 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4760 if (data == NULL)
4761 goto out;
4762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004764 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004766 data->arg.ftype = NF4BLK;
4767 data->arg.u.device.specdata1 = MAJOR(rdev);
4768 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 }
4770 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004771 data->arg.ftype = NF4CHR;
4772 data->arg.u.device.specdata1 = MAJOR(rdev);
4773 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004774 } else if (!S_ISSOCK(mode)) {
4775 status = -EINVAL;
4776 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 }
David Quigley1775fd32013-05-22 12:50:42 -04004778
David Quigleyaa9c2662013-05-22 12:50:44 -04004779 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004780 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004781out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004782 nfs4_free_createdata(data);
4783out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 return status;
4785}
4786
4787static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4788 struct iattr *sattr, dev_t rdev)
4789{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004790 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004792 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004794
David Quigleyaa9c2662013-05-22 12:50:44 -04004795 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4796
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004797 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4798 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004800 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4801 trace_nfs4_mknod(dir, &dentry->d_name, err);
4802 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 &exception);
4804 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004805
4806 nfs4_label_release_security(label);
4807
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 return err;
4809}
4810
4811static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4812 struct nfs_fsstat *fsstat)
4813{
4814 struct nfs4_statfs_arg args = {
4815 .fh = fhandle,
4816 .bitmask = server->attr_bitmask,
4817 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004818 struct nfs4_statfs_res res = {
4819 .fsstat = fsstat,
4820 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 struct rpc_message msg = {
4822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4823 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004824 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 };
4826
Trond Myklebust0e574af2005-10-27 22:12:38 -04004827 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004828 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829}
4830
4831static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4832{
4833 struct nfs4_exception exception = { };
4834 int err;
4835 do {
4836 err = nfs4_handle_exception(server,
4837 _nfs4_proc_statfs(server, fhandle, fsstat),
4838 &exception);
4839 } while (exception.retry);
4840 return err;
4841}
4842
4843static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4844 struct nfs_fsinfo *fsinfo)
4845{
4846 struct nfs4_fsinfo_arg args = {
4847 .fh = fhandle,
4848 .bitmask = server->attr_bitmask,
4849 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004850 struct nfs4_fsinfo_res res = {
4851 .fsinfo = fsinfo,
4852 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 struct rpc_message msg = {
4854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4855 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004856 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 };
4858
Bryan Schumaker7c513052011-03-24 17:12:24 +00004859 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860}
4861
4862static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4863{
4864 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004865 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 int err;
4867
4868 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004869 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004870 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004871 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004872 nfs4_set_lease_period(server->nfs_client,
4873 fsinfo->lease_time * HZ,
4874 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004875 break;
4876 }
4877 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 } while (exception.retry);
4879 return err;
4880}
4881
4882static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4883{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004884 int error;
4885
Trond Myklebust0e574af2005-10-27 22:12:38 -04004886 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004887 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004888 if (error == 0) {
4889 /* block layout checks this! */
4890 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004891 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004892 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004893
4894 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895}
4896
4897static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4898 struct nfs_pathconf *pathconf)
4899{
4900 struct nfs4_pathconf_arg args = {
4901 .fh = fhandle,
4902 .bitmask = server->attr_bitmask,
4903 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004904 struct nfs4_pathconf_res res = {
4905 .pathconf = pathconf,
4906 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 struct rpc_message msg = {
4908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4909 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004910 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 };
4912
4913 /* None of the pathconf attributes are mandatory to implement */
4914 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4915 memset(pathconf, 0, sizeof(*pathconf));
4916 return 0;
4917 }
4918
Trond Myklebust0e574af2005-10-27 22:12:38 -04004919 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004920 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921}
4922
4923static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4924 struct nfs_pathconf *pathconf)
4925{
4926 struct nfs4_exception exception = { };
4927 int err;
4928
4929 do {
4930 err = nfs4_handle_exception(server,
4931 _nfs4_proc_pathconf(server, fhandle, pathconf),
4932 &exception);
4933 } while (exception.retry);
4934 return err;
4935}
4936
Trond Myklebust5521abf2013-03-16 20:54:34 -04004937int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004938 const struct nfs_open_context *ctx,
4939 const struct nfs_lock_context *l_ctx,
4940 fmode_t fmode)
4941{
NeilBrown17393472016-10-13 15:26:47 +11004942 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004943}
4944EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4945
Trond Myklebust5521abf2013-03-16 20:54:34 -04004946static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4947 const struct nfs_open_context *ctx,
4948 const struct nfs_lock_context *l_ctx,
4949 fmode_t fmode)
4950{
4951 nfs4_stateid current_stateid;
4952
Trond Myklebuste1253be2014-03-05 08:44:23 -05004953 /* If the current stateid represents a lost lock, then exit */
4954 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4955 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004956 return nfs4_stateid_match(stateid, &current_stateid);
4957}
4958
4959static bool nfs4_error_stateid_expired(int err)
4960{
4961 switch (err) {
4962 case -NFS4ERR_DELEG_REVOKED:
4963 case -NFS4ERR_ADMIN_REVOKED:
4964 case -NFS4ERR_BAD_STATEID:
4965 case -NFS4ERR_STALE_STATEID:
4966 case -NFS4ERR_OLD_STATEID:
4967 case -NFS4ERR_OPENMODE:
4968 case -NFS4ERR_EXPIRED:
4969 return true;
4970 }
4971 return false;
4972}
4973
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004974static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004976 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004978 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004979 if (task->tk_status < 0) {
4980 struct nfs4_exception exception = {
4981 .inode = hdr->inode,
4982 .state = hdr->args.context->state,
4983 .stateid = &hdr->args.stateid,
4984 };
4985 task->tk_status = nfs4_async_handle_exception(task,
4986 server, task->tk_status, &exception);
4987 if (exception.retry) {
4988 rpc_restart_call_prepare(task);
4989 return -EAGAIN;
4990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004992
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004994 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
4997
Trond Myklebust5521abf2013-03-16 20:54:34 -04004998static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004999 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005000{
5001
5002 if (!nfs4_error_stateid_expired(task->tk_status) ||
5003 nfs4_stateid_is_current(&args->stateid,
5004 args->context,
5005 args->lock_context,
5006 FMODE_READ))
5007 return false;
5008 rpc_restart_call_prepare(task);
5009 return true;
5010}
5011
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005012static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005013{
5014
5015 dprintk("--> %s\n", __func__);
5016
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005017 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005018 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005019 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005020 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005021 if (task->tk_status > 0)
5022 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005023 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5024 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005025}
5026
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005027static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5028 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005030 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005031 if (!hdr->pgio_done_cb)
5032 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005033 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005034 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035}
5036
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005037static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5038 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005039{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005040 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005041 &hdr->args.seq_args,
5042 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005043 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005044 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005045 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5046 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005047 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005048 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005049 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005050 return -EIO;
5051 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052}
5053
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005054static int nfs4_write_done_cb(struct rpc_task *task,
5055 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005057 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005058
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005059 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005060 if (task->tk_status < 0) {
5061 struct nfs4_exception exception = {
5062 .inode = hdr->inode,
5063 .state = hdr->args.context->state,
5064 .stateid = &hdr->args.stateid,
5065 };
5066 task->tk_status = nfs4_async_handle_exception(task,
5067 NFS_SERVER(inode), task->tk_status,
5068 &exception);
5069 if (exception.retry) {
5070 rpc_restart_call_prepare(task);
5071 return -EAGAIN;
5072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005074 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005075 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005076 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005077 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079}
5080
Trond Myklebust5521abf2013-03-16 20:54:34 -04005081static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005082 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005083{
5084
5085 if (!nfs4_error_stateid_expired(task->tk_status) ||
5086 nfs4_stateid_is_current(&args->stateid,
5087 args->context,
5088 args->lock_context,
5089 FMODE_WRITE))
5090 return false;
5091 rpc_restart_call_prepare(task);
5092 return true;
5093}
5094
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005095static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005096{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005097 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005098 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005099 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005100 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005101 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5102 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005103}
5104
Trond Myklebust5a37f852012-04-28 14:55:16 -04005105static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005106bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005107{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005108 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005109 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005110 return false;
5111 /* Otherwise, request attributes if and only if we don't hold
5112 * a delegation
5113 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005114 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005115}
Fred Isamana69aef12011-03-03 15:13:47 +00005116
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005117static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005118 struct rpc_message *msg,
5119 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005121 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005122
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005123 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5124 hdr->args.bitmask = NULL;
5125 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005126 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005127 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005128
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005129 if (!hdr->pgio_done_cb)
5130 hdr->pgio_done_cb = nfs4_write_done_cb;
5131 hdr->res.server = server;
5132 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005134 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005135 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005136 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137}
5138
Fred Isaman0b7c0152012-04-20 14:47:39 -04005139static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5140{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005141 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005142 &data->args.seq_args,
5143 &data->res.seq_res,
5144 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145}
5146
Fred Isaman0b7c0152012-04-20 14:47:39 -04005147static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005150
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005151 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005152 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5153 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005154 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005155 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158}
5159
Fred Isaman0b7c0152012-04-20 14:47:39 -04005160static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005161{
5162 if (!nfs4_sequence_done(task, &data->res.seq_res))
5163 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005164 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005165}
5166
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005167static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5168 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005170 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005171
Fred Isaman0b7c0152012-04-20 14:47:39 -04005172 if (data->commit_done_cb == NULL)
5173 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005174 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005175 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005176 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005177 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178}
5179
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005180static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5181 struct nfs_commitres *res)
5182{
5183 struct inode *dst_inode = file_inode(dst);
5184 struct nfs_server *server = NFS_SERVER(dst_inode);
5185 struct rpc_message msg = {
5186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5187 .rpc_argp = args,
5188 .rpc_resp = res,
5189 };
5190
5191 args->fh = NFS_FH(dst_inode);
5192 return nfs4_call_sync(server->client, server, &msg,
5193 &args->seq_args, &res->seq_res, 1);
5194}
5195
5196int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5197{
5198 struct nfs_commitargs args = {
5199 .offset = offset,
5200 .count = count,
5201 };
5202 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5203 struct nfs4_exception exception = { };
5204 int status;
5205
5206 do {
5207 status = _nfs4_proc_commit(dst, &args, res);
5208 status = nfs4_handle_exception(dst_server, status, &exception);
5209 } while (exception.retry);
5210
5211 return status;
5212}
5213
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005214struct nfs4_renewdata {
5215 struct nfs_client *client;
5216 unsigned long timestamp;
5217};
5218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219/*
5220 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5221 * standalone procedure for queueing an asynchronous RENEW.
5222 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005223static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005224{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005225 struct nfs4_renewdata *data = calldata;
5226 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005227
Elena Reshetova212bf412017-10-20 12:53:38 +03005228 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005229 nfs4_schedule_state_renewal(clp);
5230 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005231 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005232}
5233
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005234static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005236 struct nfs4_renewdata *data = calldata;
5237 struct nfs_client *clp = data->client;
5238 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005240 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005241 switch (task->tk_status) {
5242 case 0:
5243 break;
5244 case -NFS4ERR_LEASE_MOVED:
5245 nfs4_schedule_lease_moved_recovery(clp);
5246 break;
5247 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005248 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005249 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5250 return;
5251 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005252 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005253 return;
5254 }
5255 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005257 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258}
5259
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005260static const struct rpc_call_ops nfs4_renew_ops = {
5261 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005262 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005263};
5264
NeilBrowna52458b2018-12-03 11:30:31 +11005265static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266{
5267 struct rpc_message msg = {
5268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5269 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005270 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005272 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005274 if (renew_flags == 0)
5275 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005276 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005277 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005278 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005279 if (data == NULL) {
5280 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005281 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005282 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005283 data->client = clp;
5284 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005285 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005286 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287}
5288
NeilBrowna52458b2018-12-03 11:30:31 +11005289static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290{
5291 struct rpc_message msg = {
5292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5293 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005294 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 };
5296 unsigned long now = jiffies;
5297 int status;
5298
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005299 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 if (status < 0)
5301 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005302 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 return 0;
5304}
5305
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005306static inline int nfs4_server_supports_acls(struct nfs_server *server)
5307{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005308 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005309}
5310
Trond Myklebust21f498c2012-08-24 10:59:25 -04005311/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5312 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005313 * the stack.
5314 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005315#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005316
Neil Hormane9e3d722011-03-04 19:26:03 -05005317static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005318 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005319{
5320 struct page *newpage, **spages;
5321 int rc = 0;
5322 size_t len;
5323 spages = pages;
5324
5325 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005326 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005327 newpage = alloc_page(GFP_KERNEL);
5328
5329 if (newpage == NULL)
5330 goto unwind;
5331 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005332 buf += len;
5333 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005334 *pages++ = newpage;
5335 rc++;
5336 } while (buflen != 0);
5337
5338 return rc;
5339
5340unwind:
5341 for(; rc > 0; rc--)
5342 __free_page(spages[rc-1]);
5343 return -ENOMEM;
5344}
5345
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005346struct nfs4_cached_acl {
5347 int cached;
5348 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005349 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005350};
5351
5352static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005353{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005354 struct nfs_inode *nfsi = NFS_I(inode);
5355
5356 spin_lock(&inode->i_lock);
5357 kfree(nfsi->nfs4_acl);
5358 nfsi->nfs4_acl = acl;
5359 spin_unlock(&inode->i_lock);
5360}
5361
5362static void nfs4_zap_acl_attr(struct inode *inode)
5363{
5364 nfs4_set_cached_acl(inode, NULL);
5365}
5366
5367static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5368{
5369 struct nfs_inode *nfsi = NFS_I(inode);
5370 struct nfs4_cached_acl *acl;
5371 int ret = -ENOENT;
5372
5373 spin_lock(&inode->i_lock);
5374 acl = nfsi->nfs4_acl;
5375 if (acl == NULL)
5376 goto out;
5377 if (buf == NULL) /* user is just asking for length */
5378 goto out_len;
5379 if (acl->cached == 0)
5380 goto out;
5381 ret = -ERANGE; /* see getxattr(2) man page */
5382 if (acl->len > buflen)
5383 goto out;
5384 memcpy(buf, acl->data, acl->len);
5385out_len:
5386 ret = acl->len;
5387out:
5388 spin_unlock(&inode->i_lock);
5389 return ret;
5390}
5391
Sachin Prabhu5794d212012-04-17 14:36:40 +01005392static 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 +00005393{
5394 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005395 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005396
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005397 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005398 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005399 if (acl == NULL)
5400 goto out;
5401 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005402 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005403 } else {
5404 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5405 if (acl == NULL)
5406 goto out;
5407 acl->cached = 0;
5408 }
5409 acl->len = acl_len;
5410out:
5411 nfs4_set_cached_acl(inode, acl);
5412}
5413
Andy Adamsonbf118a32011-12-07 11:55:27 -05005414/*
5415 * The getxattr API returns the required buffer length when called with a
5416 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5417 * the required buf. On a NULL buf, we send a page of data to the server
5418 * guessing that the ACL request can be serviced by a page. If so, we cache
5419 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5420 * the cache. If not so, we throw away the page, and cache the required
5421 * length. The next getxattr call will then produce another round trip to
5422 * the server, this time with the input buf of the required size.
5423 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005424static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005425{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005426 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005427 struct nfs_getaclargs args = {
5428 .fh = NFS_FH(inode),
5429 .acl_pages = pages,
5430 .acl_len = buflen,
5431 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005432 struct nfs_getaclres res = {
5433 .acl_len = buflen,
5434 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005435 struct rpc_message msg = {
5436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5437 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005438 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005439 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005440 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005441 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005442
Trond Myklebust21f498c2012-08-24 10:59:25 -04005443 if (npages > ARRAY_SIZE(pages))
5444 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005445
Andy Adamsonbf118a32011-12-07 11:55:27 -05005446 for (i = 0; i < npages; i++) {
5447 pages[i] = alloc_page(GFP_KERNEL);
5448 if (!pages[i])
5449 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005450 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005451
5452 /* for decoding across pages */
5453 res.acl_scratch = alloc_page(GFP_KERNEL);
5454 if (!res.acl_scratch)
5455 goto out_free;
5456
Andy Adamsonbf118a32011-12-07 11:55:27 -05005457 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005458
Peng Taode040be2012-01-10 22:42:47 +08005459 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005460 __func__, buf, buflen, npages, args.acl_len);
5461 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5462 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005463 if (ret)
5464 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005465
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005466 /* Handle the case where the passed-in buffer is too short */
5467 if (res.acl_flags & NFS4_ACL_TRUNC) {
5468 /* Did the user only issue a request for the acl length? */
5469 if (buf == NULL)
5470 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005471 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005472 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005473 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005474 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005475 if (buf) {
5476 if (res.acl_len > buflen) {
5477 ret = -ERANGE;
5478 goto out_free;
5479 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005480 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005481 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005482out_ok:
5483 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005484out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005485 for (i = 0; i < npages; i++)
5486 if (pages[i])
5487 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005488 if (res.acl_scratch)
5489 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005490 return ret;
5491}
5492
Trond Myklebust16b42892006-08-24 12:27:15 -04005493static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5494{
5495 struct nfs4_exception exception = { };
5496 ssize_t ret;
5497 do {
5498 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005499 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005500 if (ret >= 0)
5501 break;
5502 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5503 } while (exception.retry);
5504 return ret;
5505}
5506
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005507static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5508{
5509 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005510 int ret;
5511
5512 if (!nfs4_server_supports_acls(server))
5513 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005514 ret = nfs_revalidate_inode(server, inode);
5515 if (ret < 0)
5516 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005517 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5518 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005519 ret = nfs4_read_cached_acl(inode, buf, buflen);
5520 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005521 /* -ENOENT is returned if there is no ACL or if there is an ACL
5522 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005523 return ret;
5524 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005525}
5526
Trond Myklebust16b42892006-08-24 12:27:15 -04005527static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005528{
5529 struct nfs_server *server = NFS_SERVER(inode);
5530 struct page *pages[NFS4ACL_MAXPAGES];
5531 struct nfs_setaclargs arg = {
5532 .fh = NFS_FH(inode),
5533 .acl_pages = pages,
5534 .acl_len = buflen,
5535 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005536 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005537 struct rpc_message msg = {
5538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5539 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005540 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005541 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005542 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005543 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005544
5545 if (!nfs4_server_supports_acls(server))
5546 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005547 if (npages > ARRAY_SIZE(pages))
5548 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005549 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005550 if (i < 0)
5551 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005552 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005553 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005554
5555 /*
5556 * Free each page after tx, so the only ref left is
5557 * held by the network stack
5558 */
5559 for (; i > 0; i--)
5560 put_page(pages[i-1]);
5561
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005562 /*
5563 * Acl update can result in inode attribute update.
5564 * so mark the attribute cache invalid.
5565 */
5566 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005567 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005568 | NFS_INO_INVALID_CTIME
5569 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005570 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005571 nfs_access_zap_cache(inode);
5572 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005573 return ret;
5574}
5575
Trond Myklebust16b42892006-08-24 12:27:15 -04005576static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5577{
5578 struct nfs4_exception exception = { };
5579 int err;
5580 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005581 err = __nfs4_proc_set_acl(inode, buf, buflen);
5582 trace_nfs4_set_acl(inode, err);
5583 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005584 &exception);
5585 } while (exception.retry);
5586 return err;
5587}
5588
David Quigleyaa9c2662013-05-22 12:50:44 -04005589#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5590static int _nfs4_get_security_label(struct inode *inode, void *buf,
5591 size_t buflen)
5592{
5593 struct nfs_server *server = NFS_SERVER(inode);
5594 struct nfs_fattr fattr;
5595 struct nfs4_label label = {0, 0, buflen, buf};
5596
5597 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005598 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005599 .fh = NFS_FH(inode),
5600 .bitmask = bitmask,
5601 };
5602 struct nfs4_getattr_res res = {
5603 .fattr = &fattr,
5604 .label = &label,
5605 .server = server,
5606 };
5607 struct rpc_message msg = {
5608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005609 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005610 .rpc_resp = &res,
5611 };
5612 int ret;
5613
5614 nfs_fattr_init(&fattr);
5615
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005616 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005617 if (ret)
5618 return ret;
5619 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5620 return -ENOENT;
5621 if (buflen < label.len)
5622 return -ERANGE;
5623 return 0;
5624}
5625
5626static int nfs4_get_security_label(struct inode *inode, void *buf,
5627 size_t buflen)
5628{
5629 struct nfs4_exception exception = { };
5630 int err;
5631
5632 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5633 return -EOPNOTSUPP;
5634
5635 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005636 err = _nfs4_get_security_label(inode, buf, buflen);
5637 trace_nfs4_get_security_label(inode, err);
5638 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005639 &exception);
5640 } while (exception.retry);
5641 return err;
5642}
5643
5644static int _nfs4_do_set_security_label(struct inode *inode,
5645 struct nfs4_label *ilabel,
5646 struct nfs_fattr *fattr,
5647 struct nfs4_label *olabel)
5648{
5649
5650 struct iattr sattr = {0};
5651 struct nfs_server *server = NFS_SERVER(inode);
5652 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005653 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005654 .fh = NFS_FH(inode),
5655 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005656 .server = server,
5657 .bitmask = bitmask,
5658 .label = ilabel,
5659 };
5660 struct nfs_setattrres res = {
5661 .fattr = fattr,
5662 .label = olabel,
5663 .server = server,
5664 };
5665 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5667 .rpc_argp = &arg,
5668 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005669 };
5670 int status;
5671
Jeff Layton12207f62013-11-01 10:49:32 -04005672 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005673
Jeff Layton12207f62013-11-01 10:49:32 -04005674 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005675 if (status)
5676 dprintk("%s failed: %d\n", __func__, status);
5677
5678 return status;
5679}
5680
5681static int nfs4_do_set_security_label(struct inode *inode,
5682 struct nfs4_label *ilabel,
5683 struct nfs_fattr *fattr,
5684 struct nfs4_label *olabel)
5685{
5686 struct nfs4_exception exception = { };
5687 int err;
5688
5689 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005690 err = _nfs4_do_set_security_label(inode, ilabel,
5691 fattr, olabel);
5692 trace_nfs4_set_security_label(inode, err);
5693 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005694 &exception);
5695 } while (exception.retry);
5696 return err;
5697}
5698
5699static int
Al Viro59301222016-05-27 10:19:30 -04005700nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005701{
5702 struct nfs4_label ilabel, *olabel = NULL;
5703 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005704 int status;
5705
5706 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5707 return -EOPNOTSUPP;
5708
5709 nfs_fattr_init(&fattr);
5710
5711 ilabel.pi = 0;
5712 ilabel.lfs = 0;
5713 ilabel.label = (char *)buf;
5714 ilabel.len = buflen;
5715
David Quigleyaa9c2662013-05-22 12:50:44 -04005716 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5717 if (IS_ERR(olabel)) {
5718 status = -PTR_ERR(olabel);
5719 goto out;
5720 }
5721
5722 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5723 if (status == 0)
5724 nfs_setsecurity(inode, &fattr, olabel);
5725
5726 nfs4_label_free(olabel);
5727out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005728 return status;
5729}
5730#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5731
5732
Chuck Leverf0920752012-05-21 22:45:41 -04005733static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5734 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005735{
5736 __be32 verf[2];
5737
Chuck Lever2c820d92012-05-21 22:45:33 -04005738 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5739 /* An impossible timestamp guarantees this value
5740 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005741 verf[0] = cpu_to_be32(U32_MAX);
5742 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005743 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005744 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005745 u64 ns = ktime_to_ns(nn->boot_time);
5746
5747 verf[0] = cpu_to_be32(ns >> 32);
5748 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005749 }
Chuck Levercd937102012-03-02 17:14:31 -05005750 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5751}
5752
Jeff Laytona3192682015-06-09 19:43:59 -04005753static int
5754nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005755{
Jeff Laytona3192682015-06-09 19:43:59 -04005756 size_t len;
5757 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005758
Trond Myklebustceb3a162015-01-03 15:16:04 -05005759 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005760 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005761
Jeff Laytona3192682015-06-09 19:43:59 -04005762 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005763 len = 14 +
5764 strlen(clp->cl_rpcclient->cl_nodename) +
5765 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005766 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005767 1;
5768 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005769 if (nfs4_client_id_uniquifier[0] != '\0')
5770 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005771 if (len > NFS4_OPAQUE_LIMIT + 1)
5772 return -EINVAL;
5773
5774 /*
5775 * Since this string is allocated at mount time, and held until the
5776 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5777 * about a memory-reclaim deadlock.
5778 */
5779 str = kmalloc(len, GFP_KERNEL);
5780 if (!str)
5781 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005782
Chuck Levere984a552012-09-14 17:24:21 -04005783 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005784 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005785 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005786 clp->cl_rpcclient->cl_nodename,
5787 nfs4_client_id_uniquifier,
5788 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005789 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005790 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005791 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005792 clp->cl_rpcclient->cl_nodename,
5793 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005794 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005795 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005796
Jeff Laytona3192682015-06-09 19:43:59 -04005797 clp->cl_owner_id = str;
5798 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005799}
5800
Jeff Layton873e3852015-06-09 19:44:00 -04005801static int
5802nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005803{
Jeff Layton873e3852015-06-09 19:44:00 -04005804 size_t len;
5805 char *str;
5806
5807 len = 10 + 10 + 1 + 10 + 1 +
5808 strlen(nfs4_client_id_uniquifier) + 1 +
5809 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5810
5811 if (len > NFS4_OPAQUE_LIMIT + 1)
5812 return -EINVAL;
5813
5814 /*
5815 * Since this string is allocated at mount time, and held until the
5816 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5817 * about a memory-reclaim deadlock.
5818 */
5819 str = kmalloc(len, GFP_KERNEL);
5820 if (!str)
5821 return -ENOMEM;
5822
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005823 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005824 clp->rpc_ops->version, clp->cl_minorversion,
5825 nfs4_client_id_uniquifier,
5826 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005827 clp->cl_owner_id = str;
5828 return 0;
5829}
5830
5831static int
5832nfs4_init_uniform_client_string(struct nfs_client *clp)
5833{
Jeff Layton873e3852015-06-09 19:44:00 -04005834 size_t len;
5835 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005836
5837 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005838 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005839
5840 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005841 return nfs4_init_uniquifier_client_string(clp);
5842
5843 len = 10 + 10 + 1 + 10 + 1 +
5844 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5845
5846 if (len > NFS4_OPAQUE_LIMIT + 1)
5847 return -EINVAL;
5848
5849 /*
5850 * Since this string is allocated at mount time, and held until the
5851 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5852 * about a memory-reclaim deadlock.
5853 */
5854 str = kmalloc(len, GFP_KERNEL);
5855 if (!str)
5856 return -ENOMEM;
5857
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005858 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005859 clp->rpc_ops->version, clp->cl_minorversion,
5860 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005861 clp->cl_owner_id = str;
5862 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005863}
5864
Chuck Lever706cb8d2014-03-12 12:51:47 -04005865/*
5866 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5867 * services. Advertise one based on the address family of the
5868 * clientaddr.
5869 */
5870static unsigned int
5871nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5872{
5873 if (strchr(clp->cl_ipaddr, ':') != NULL)
5874 return scnprintf(buf, len, "tcp6");
5875 else
5876 return scnprintf(buf, len, "tcp");
5877}
5878
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005879static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5880{
5881 struct nfs4_setclientid *sc = calldata;
5882
5883 if (task->tk_status == 0)
5884 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5885}
5886
5887static const struct rpc_call_ops nfs4_setclientid_ops = {
5888 .rpc_call_done = nfs4_setclientid_done,
5889};
5890
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005891/**
5892 * nfs4_proc_setclientid - Negotiate client ID
5893 * @clp: state data structure
5894 * @program: RPC program for NFSv4 callback service
5895 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11005896 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005897 * @res: where to place the result
5898 *
5899 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5900 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005901int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11005902 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005903 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904{
5905 nfs4_verifier sc_verifier;
5906 struct nfs4_setclientid setclientid = {
5907 .sc_verifier = &sc_verifier,
5908 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005909 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 };
5911 struct rpc_message msg = {
5912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5913 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005914 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005915 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005917 struct rpc_task *task;
5918 struct rpc_task_setup task_setup_data = {
5919 .rpc_client = clp->cl_rpcclient,
5920 .rpc_message = &msg,
5921 .callback_ops = &nfs4_setclientid_ops,
5922 .callback_data = &setclientid,
5923 .flags = RPC_TASK_TIMEOUT,
5924 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005925 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
Chuck Leverde734832012-07-11 16:30:50 -04005927 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005928 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005929
5930 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5931 status = nfs4_init_uniform_client_string(clp);
5932 else
Jeff Laytona3192682015-06-09 19:43:59 -04005933 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005934
5935 if (status)
5936 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005937
Chuck Leverde734832012-07-11 16:30:50 -04005938 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005939 setclientid.sc_netid_len =
5940 nfs4_init_callback_netid(clp,
5941 setclientid.sc_netid,
5942 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005943 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005944 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 clp->cl_ipaddr, port >> 8, port & 255);
5946
Jeff Layton3a6bb732015-06-09 19:43:57 -04005947 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005948 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005949 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005950 task = rpc_run_task(&task_setup_data);
5951 if (IS_ERR(task)) {
5952 status = PTR_ERR(task);
5953 goto out;
5954 }
5955 status = task->tk_status;
5956 if (setclientid.sc_cred) {
5957 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5958 put_rpccred(setclientid.sc_cred);
5959 }
5960 rpc_put_task(task);
5961out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005962 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005963 dprintk("NFS reply setclientid: %d\n", status);
5964 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965}
5966
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005967/**
5968 * nfs4_proc_setclientid_confirm - Confirm client ID
5969 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05005970 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11005971 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005972 *
5973 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5974 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005975int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005976 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11005977 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 struct rpc_message msg = {
5980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005981 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005982 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 int status;
5985
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005986 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5987 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5988 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005989 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005990 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005991 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 return status;
5993}
5994
Trond Myklebustfe650402006-01-03 09:55:18 +01005995struct nfs4_delegreturndata {
5996 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005997 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005998 struct nfs_fh fh;
5999 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006000 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006001 struct {
6002 struct nfs4_layoutreturn_args arg;
6003 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006004 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006005 u32 roc_barrier;
6006 bool roc;
6007 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006008 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006009 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006010 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006011};
6012
Trond Myklebustfe650402006-01-03 09:55:18 +01006013static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6014{
6015 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006016 struct nfs4_exception exception = {
6017 .inode = data->inode,
6018 .stateid = &data->stateid,
6019 };
Andy Adamson938e1012009-04-01 09:22:28 -04006020
Trond Myklebust14516c32010-07-31 14:29:06 -04006021 if (!nfs4_sequence_done(task, &data->res.seq_res))
6022 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006023
Trond Myklebustca8acf82013-08-13 10:36:56 -04006024 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006025
6026 /* Handle Layoutreturn errors */
6027 if (data->args.lr_args && task->tk_status != 0) {
6028 switch(data->res.lr_ret) {
6029 default:
6030 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6031 break;
6032 case 0:
6033 data->args.lr_args = NULL;
6034 data->res.lr_res = NULL;
6035 break;
Trond Myklebust73800202017-11-06 15:28:07 -05006036 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04006037 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04006038 &data->args.lr_args->range,
Trond Myklebust140087fd2017-11-06 15:28:10 -05006039 data->inode))
6040 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05006041 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05006042 case -NFS4ERR_ADMIN_REVOKED:
6043 case -NFS4ERR_DELEG_REVOKED:
6044 case -NFS4ERR_EXPIRED:
6045 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05006046 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6047 case -NFS4ERR_WRONG_CRED:
6048 data->args.lr_args = NULL;
6049 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006050 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006051 }
6052 }
6053
Ricardo Labiaga79708862009-12-07 09:23:21 -05006054 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006055 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006056 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006057 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006058 case -NFS4ERR_ADMIN_REVOKED:
6059 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006060 case -NFS4ERR_EXPIRED:
6061 nfs4_free_revoked_stateid(data->res.server,
6062 data->args.stateid,
6063 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006064 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006065 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006066 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006067 task->tk_status = 0;
6068 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006069 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05006070 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6071 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006072 task->tk_status = 0;
6073 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006074 case -NFS4ERR_ACCESS:
6075 if (data->args.bitmask) {
6076 data->args.bitmask = NULL;
6077 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006078 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006079 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006080 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006081 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006082 task->tk_status = nfs4_async_handle_exception(task,
6083 data->res.server, task->tk_status,
6084 &exception);
6085 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006086 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006087 }
6088 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006089 return;
6090lr_restart:
6091 data->res.lr_ret = 0;
6092out_restart:
6093 task->tk_status = 0;
6094 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006095}
6096
6097static void nfs4_delegreturn_release(void *calldata)
6098{
Peng Tao039b7562014-07-03 13:05:02 +08006099 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006100 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006101
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006102 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006103 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006104 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6105 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006106 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006107 nfs_iput_and_deactive(inode);
6108 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006109 kfree(calldata);
6110}
6111
Andy Adamson938e1012009-04-01 09:22:28 -04006112static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6113{
6114 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006115 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006116
6117 d_data = (struct nfs4_delegreturndata *)data;
6118
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006119 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08006120 return;
6121
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006122 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6123 if (lo && !pnfs_layout_is_valid(lo)) {
6124 d_data->args.lr_args = NULL;
6125 d_data->res.lr_res = NULL;
6126 }
6127
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006128 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006129 &d_data->args.seq_args,
6130 &d_data->res.seq_res,
6131 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006132}
Andy Adamson938e1012009-04-01 09:22:28 -04006133
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006134static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006135 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006136 .rpc_call_done = nfs4_delegreturn_done,
6137 .rpc_release = nfs4_delegreturn_release,
6138};
6139
NeilBrowna52458b2018-12-03 11:30:31 +11006140static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006141{
6142 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006143 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006144 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006145 struct rpc_message msg = {
6146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6147 .rpc_cred = cred,
6148 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006149 struct rpc_task_setup task_setup_data = {
6150 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006151 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006152 .callback_ops = &nfs4_delegreturn_ops,
6153 .flags = RPC_TASK_ASYNC,
6154 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006155 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006156
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006157 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006158 if (data == NULL)
6159 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006160 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006161
6162 nfs4_state_protect(server->nfs_client,
6163 NFS_SP4_MACH_CRED_CLEANUP,
6164 &task_setup_data.rpc_client, &msg);
6165
Trond Myklebustfe650402006-01-03 09:55:18 +01006166 data->args.fhandle = &data->fh;
6167 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006168 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006169 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006170 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006171 data->res.fattr = &data->fattr;
6172 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006173 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006174 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006175 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006176 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006177 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006178 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006179 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006180 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006181 if (data->lr.roc) {
6182 data->args.lr_args = &data->lr.arg;
6183 data->res.lr_res = &data->lr.res;
6184 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006185 } else if (data->lr.roc) {
6186 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6187 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006188 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006189
Trond Myklebustc970aa82007-07-14 15:39:59 -04006190 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006191 msg.rpc_argp = &data->args;
6192 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006193 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006194 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006195 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006196 if (!issync)
6197 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006198 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006199 if (status != 0)
6200 goto out;
6201 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006202out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006203 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006204 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205}
6206
NeilBrowna52458b2018-12-03 11:30:31 +11006207int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208{
6209 struct nfs_server *server = NFS_SERVER(inode);
6210 struct nfs4_exception exception = { };
6211 int err;
6212 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006213 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006214 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 switch (err) {
6216 case -NFS4ERR_STALE_STATEID:
6217 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 case 0:
6219 return 0;
6220 }
6221 err = nfs4_handle_exception(server, err, &exception);
6222 } while (exception.retry);
6223 return err;
6224}
6225
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6227{
6228 struct inode *inode = state->inode;
6229 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006230 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006231 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006233 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006235 struct nfs_lockt_res res = {
6236 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 };
6238 struct rpc_message msg = {
6239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006240 .rpc_argp = &arg,
6241 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 .rpc_cred = state->owner->so_cred,
6243 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 struct nfs4_lock_state *lsp;
6245 int status;
6246
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006247 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006248 status = nfs4_set_lock_state(state, request);
6249 if (status != 0)
6250 goto out;
6251 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006252 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006253 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006254 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006255 switch (status) {
6256 case 0:
6257 request->fl_type = F_UNLCK;
6258 break;
6259 case -NFS4ERR_DENIED:
6260 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006262 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006263 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006264out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 return status;
6266}
6267
6268static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6269{
6270 struct nfs4_exception exception = { };
6271 int err;
6272
6273 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006274 err = _nfs4_proc_getlk(state, cmd, request);
6275 trace_nfs4_get_lock(request, state, cmd, err);
6276 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 &exception);
6278 } while (exception.retry);
6279 return err;
6280}
6281
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006282struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006283 struct nfs_locku_args arg;
6284 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006285 struct nfs4_lock_state *lsp;
6286 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006287 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006288 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006289 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006290 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006291};
6292
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006293static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6294 struct nfs_open_context *ctx,
6295 struct nfs4_lock_state *lsp,
6296 struct nfs_seqid *seqid)
6297{
6298 struct nfs4_unlockdata *p;
6299 struct inode *inode = lsp->ls_state->inode;
6300
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006301 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006302 if (p == NULL)
6303 return NULL;
6304 p->arg.fh = NFS_FH(inode);
6305 p->arg.fl = &p->fl;
6306 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006307 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006308 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006309 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006310 /* Ensure we don't close file until we're done freeing locks! */
6311 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006312 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006313 locks_init_lock(&p->fl);
6314 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006315 p->server = NFS_SERVER(inode);
6316 return p;
6317}
6318
Trond Myklebust06f814a2006-01-03 09:55:07 +01006319static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006320{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006321 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006322 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006323 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006324 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006325 put_nfs_open_context(calldata->ctx);
6326 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006327}
6328
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006329static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006330{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006331 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006332 struct nfs4_exception exception = {
6333 .inode = calldata->lsp->ls_state->inode,
6334 .stateid = &calldata->arg.stateid,
6335 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006336
Trond Myklebust14516c32010-07-31 14:29:06 -04006337 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6338 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006339 switch (task->tk_status) {
6340 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006341 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006342 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006343 if (nfs4_update_lock_stateid(calldata->lsp,
6344 &calldata->res.stateid))
6345 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006346 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006347 case -NFS4ERR_ADMIN_REVOKED:
6348 case -NFS4ERR_EXPIRED:
6349 nfs4_free_revoked_stateid(calldata->server,
6350 &calldata->arg.stateid,
6351 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006352 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006353 case -NFS4ERR_BAD_STATEID:
6354 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006355 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006356 if (!nfs4_stateid_match(&calldata->arg.stateid,
6357 &calldata->lsp->ls_stateid))
6358 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006359 break;
6360 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006361 task->tk_status = nfs4_async_handle_exception(task,
6362 calldata->server, task->tk_status,
6363 &exception);
6364 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006365 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006366 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006367 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006368}
6369
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006370static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006371{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006372 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006374 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6375 nfs_async_iocounter_wait(task, calldata->l_ctx))
6376 return;
6377
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006378 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006379 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006380 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006381 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006382 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006383 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006384 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006385 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006386 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006387 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006388 &calldata->res.seq_res,
6389 task) != 0)
6390 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006391 return;
6392out_no_action:
6393 task->tk_action = NULL;
6394out_wait:
6395 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396}
6397
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006398static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006399 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006400 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006401 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006402};
6403
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006404static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6405 struct nfs_open_context *ctx,
6406 struct nfs4_lock_state *lsp,
6407 struct nfs_seqid *seqid)
6408{
6409 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006410 struct rpc_message msg = {
6411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6412 .rpc_cred = ctx->cred,
6413 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006414 struct rpc_task_setup task_setup_data = {
6415 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006416 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006417 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006418 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006419 .flags = RPC_TASK_ASYNC,
6420 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006421
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006422 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6423 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6424
Frank Filz137d6ac2007-07-09 15:32:29 -07006425 /* Ensure this is an unlock - when canceling a lock, the
6426 * canceled lock is passed in, and it won't be an unlock.
6427 */
6428 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006429 if (fl->fl_flags & FL_CLOSE)
6430 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006431
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006432 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6433 if (data == NULL) {
6434 nfs_free_seqid(seqid);
6435 return ERR_PTR(-ENOMEM);
6436 }
6437
Anna Schumakerfba83f32018-05-04 16:22:50 -04006438 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006439 msg.rpc_argp = &data->arg;
6440 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006441 task_setup_data.callback_data = data;
6442 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006443}
6444
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6446{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006447 struct inode *inode = state->inode;
6448 struct nfs4_state_owner *sp = state->owner;
6449 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006450 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006451 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006452 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006453 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006454 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006455 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456
Trond Myklebust9b073572006-06-29 16:38:34 -04006457 status = nfs4_set_lock_state(state, request);
6458 /* Unlock _before_ we do the RPC call */
6459 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006460 /* Exclude nfs_delegation_claim_locks() */
6461 mutex_lock(&sp->so_delegreturn_mutex);
6462 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006463 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006464 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006465 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006466 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006467 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006468 }
6469 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006470 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006471 if (status != 0)
6472 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006473 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006474 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006475 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6476 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006477 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6478 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006479 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006480 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006481 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006482 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006483 status = PTR_ERR(task);
6484 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006485 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006486 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006487 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006488out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006489 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006490 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006491 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492}
6493
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006494struct nfs4_lockdata {
6495 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006496 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006497 struct nfs4_lock_state *lsp;
6498 struct nfs_open_context *ctx;
6499 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006500 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006501 int rpc_status;
6502 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006503 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006504};
6505
6506static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006507 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6508 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006509{
6510 struct nfs4_lockdata *p;
6511 struct inode *inode = lsp->ls_state->inode;
6512 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006513 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006514
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006515 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006516 if (p == NULL)
6517 return NULL;
6518
6519 p->arg.fh = NFS_FH(inode);
6520 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006521 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006522 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006523 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006524 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6525 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006526 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006527 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006528 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006529 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006530 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006531 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006532 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006533 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006534 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006535 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006536 locks_init_lock(&p->fl);
6537 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006538 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006539out_free_seqid:
6540 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006541out_free:
6542 kfree(p);
6543 return NULL;
6544}
6545
6546static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6547{
6548 struct nfs4_lockdata *data = calldata;
6549 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550
Harvey Harrison3110ff82008-05-02 13:42:44 -07006551 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006552 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006553 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006554 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006555 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006556 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006557 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006558 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006559 nfs4_stateid_copy(&data->arg.open_stateid,
6560 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006561 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006562 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006563 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006564 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006565 nfs4_stateid_copy(&data->arg.lock_stateid,
6566 &data->lsp->ls_stateid);
6567 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006568 if (!nfs4_valid_open_stateid(state)) {
6569 data->rpc_status = -EBADF;
6570 task->tk_action = NULL;
6571 goto out_release_open_seqid;
6572 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006573 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006574 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006575 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006576 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006577 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006578 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006579out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006580 nfs_release_seqid(data->arg.open_seqid);
6581out_release_lock_seqid:
6582 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006583out_wait:
6584 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006585 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006586}
6587
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006588static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6589{
6590 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006591 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006592
Harvey Harrison3110ff82008-05-02 13:42:44 -07006593 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006594
Trond Myklebust14516c32010-07-31 14:29:06 -04006595 if (!nfs4_sequence_done(task, &data->res.seq_res))
6596 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006597
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006598 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006599 switch (task->tk_status) {
6600 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006601 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006602 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006603 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006604 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006605 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006606 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006607 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006608 if (data->arg.new_lock_owner != 0) {
6609 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6610 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6611 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006612 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6613 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006614 break;
6615 case -NFS4ERR_BAD_STATEID:
6616 case -NFS4ERR_OLD_STATEID:
6617 case -NFS4ERR_STALE_STATEID:
6618 case -NFS4ERR_EXPIRED:
6619 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006620 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006621 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006622 goto out_restart;
6623 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006624 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006625 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006626 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006627out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006628 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006629 return;
6630out_restart:
6631 if (!data->cancelled)
6632 rpc_restart_call_prepare(task);
6633 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006634}
6635
6636static void nfs4_lock_release(void *calldata)
6637{
6638 struct nfs4_lockdata *data = calldata;
6639
Harvey Harrison3110ff82008-05-02 13:42:44 -07006640 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006641 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006642 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006643 struct rpc_task *task;
6644 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6645 data->arg.lock_seqid);
6646 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006647 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006648 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006649 } else
6650 nfs_free_seqid(data->arg.lock_seqid);
6651 nfs4_put_lock_state(data->lsp);
6652 put_nfs_open_context(data->ctx);
6653 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006654 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006655}
6656
6657static const struct rpc_call_ops nfs4_lock_ops = {
6658 .rpc_call_prepare = nfs4_lock_prepare,
6659 .rpc_call_done = nfs4_lock_done,
6660 .rpc_release = nfs4_lock_release,
6661};
6662
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006663static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6664{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006665 switch (error) {
6666 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006667 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006668 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006669 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006670 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006671 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006672 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006673 break;
6674 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006675 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006676 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006677 };
6678}
6679
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006680static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006681{
6682 struct nfs4_lockdata *data;
6683 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006684 struct rpc_message msg = {
6685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6686 .rpc_cred = state->owner->so_cred,
6687 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006688 struct rpc_task_setup task_setup_data = {
6689 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006690 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006691 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006692 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006693 .flags = RPC_TASK_ASYNC,
6694 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006695 int ret;
6696
Harvey Harrison3110ff82008-05-02 13:42:44 -07006697 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006698 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006699 fl->fl_u.nfs4_fl.owner,
6700 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006701 if (data == NULL)
6702 return -ENOMEM;
6703 if (IS_SETLKW(cmd))
6704 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006705 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6706 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006707 msg.rpc_argp = &data->arg;
6708 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006709 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006710 if (recovery_type > NFS_LOCK_NEW) {
6711 if (recovery_type == NFS_LOCK_RECLAIM)
6712 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006713 } else
6714 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006715 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006716 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006717 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006718 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006719 if (ret == 0) {
6720 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006721 if (ret)
6722 nfs4_handle_setlk_error(data->server, data->lsp,
6723 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006724 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006725 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006726 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006727 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006728 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006729 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730}
6731
6732static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6733{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006734 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006735 struct nfs4_exception exception = {
6736 .inode = state->inode,
6737 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006738 int err;
6739
6740 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006741 /* Cache the lock if possible... */
6742 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6743 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006744 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006745 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006746 break;
6747 nfs4_handle_exception(server, err, &exception);
6748 } while (exception.retry);
6749 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750}
6751
6752static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6753{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006754 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006755 struct nfs4_exception exception = {
6756 .inode = state->inode,
6757 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006758 int err;
6759
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006760 err = nfs4_set_lock_state(state, request);
6761 if (err != 0)
6762 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006763 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006764 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6765 return 0;
6766 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006767 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006768 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6769 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006770 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006771 switch (err) {
6772 default:
6773 goto out;
6774 case -NFS4ERR_GRACE:
6775 case -NFS4ERR_DELAY:
6776 nfs4_handle_exception(server, err, &exception);
6777 err = 0;
6778 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006779 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006780out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006781 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782}
6783
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006784#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006785static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6786{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006787 struct nfs4_lock_state *lsp;
6788 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006789
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006790 status = nfs4_set_lock_state(state, request);
6791 if (status != 0)
6792 return status;
6793 lsp = request->fl_u.nfs4_fl.owner;
6794 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6795 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6796 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006797 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006798}
6799#endif
6800
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6802{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006803 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006804 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006805 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006806 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807
Trond Myklebust01c3b862006-06-29 16:38:39 -04006808 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006809 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006810 if (status < 0)
6811 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006812 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006813 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006814 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006815 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006816 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006817 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006818 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006819 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006820 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006821 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006822 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006823 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006824 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006825 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006826out:
6827 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 return status;
6829}
6830
6831static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6832{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006833 struct nfs4_exception exception = {
6834 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006835 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006836 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 int err;
6838
6839 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006840 err = _nfs4_proc_setlk(state, cmd, request);
6841 if (err == -NFS4ERR_DENIED)
6842 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006844 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 } while (exception.retry);
6846 return err;
6847}
6848
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006849#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6850#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6851
6852static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006853nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6854 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006855{
6856 int status = -ERESTARTSYS;
6857 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6858
6859 while(!signalled()) {
6860 status = nfs4_proc_setlk(state, cmd, request);
6861 if ((status != -EAGAIN) || IS_SETLK(cmd))
6862 break;
6863 freezable_schedule_timeout_interruptible(timeout);
6864 timeout *= 2;
6865 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6866 status = -ERESTARTSYS;
6867 }
6868 return status;
6869}
6870
Jeff Laytona1d617d82016-09-17 18:17:39 -04006871#ifdef CONFIG_NFS_V4_1
6872struct nfs4_lock_waiter {
6873 struct task_struct *task;
6874 struct inode *inode;
6875 struct nfs_lowner *owner;
6876 bool notified;
6877};
6878
6879static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006880nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006881{
6882 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006883 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006884
Jeff Layton57174592018-03-18 08:37:03 -04006885 /* NULL key means to wake up everyone */
6886 if (key) {
6887 struct cb_notify_lock_args *cbnl = key;
6888 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6889 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006890
Jeff Layton57174592018-03-18 08:37:03 -04006891 /* Only wake if the callback was for the same owner. */
6892 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6893 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006894
Jeff Layton57174592018-03-18 08:37:03 -04006895 /* Make sure it's for the right inode */
6896 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6897 return 0;
6898
6899 waiter->notified = true;
6900 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006901
6902 /* override "private" so we can use default_wake_function */
6903 wait->private = waiter->task;
6904 ret = autoremove_wake_function(wait, mode, flags, key);
6905 wait->private = waiter;
6906 return ret;
6907}
6908
6909static int
6910nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6911{
6912 int status = -ERESTARTSYS;
6913 unsigned long flags;
6914 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6915 struct nfs_server *server = NFS_SERVER(state->inode);
6916 struct nfs_client *clp = server->nfs_client;
6917 wait_queue_head_t *q = &clp->cl_lock_waitq;
6918 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6919 .id = lsp->ls_seqid.owner_id,
6920 .s_dev = server->s_dev };
6921 struct nfs4_lock_waiter waiter = { .task = current,
6922 .inode = state->inode,
6923 .owner = &owner,
6924 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006925 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006926
6927 /* Don't bother with waitqueue if we don't expect a callback */
6928 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6929 return nfs4_retry_setlk_simple(state, cmd, request);
6930
6931 init_wait(&wait);
6932 wait.private = &waiter;
6933 wait.func = nfs4_wake_lock_waiter;
6934 add_wait_queue(q, &wait);
6935
6936 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006937 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006938 status = nfs4_proc_setlk(state, cmd, request);
6939 if ((status != -EAGAIN) || IS_SETLK(cmd))
6940 break;
6941
6942 status = -ERESTARTSYS;
6943 spin_lock_irqsave(&q->lock, flags);
6944 if (waiter.notified) {
6945 spin_unlock_irqrestore(&q->lock, flags);
6946 continue;
6947 }
6948 set_current_state(TASK_INTERRUPTIBLE);
6949 spin_unlock_irqrestore(&q->lock, flags);
6950
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006951 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006952 }
6953
6954 finish_wait(q, &wait);
6955 return status;
6956}
6957#else /* !CONFIG_NFS_V4_1 */
6958static inline int
6959nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6960{
6961 return nfs4_retry_setlk_simple(state, cmd, request);
6962}
6963#endif
6964
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965static int
6966nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6967{
6968 struct nfs_open_context *ctx;
6969 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 int status;
6971
6972 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006973 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974 state = ctx->state;
6975
Trond Myklebustd9531262009-07-21 19:22:38 -04006976 if (IS_GETLK(cmd)) {
6977 if (state != NULL)
6978 return nfs4_proc_getlk(state, F_GETLK, request);
6979 return 0;
6980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
6982 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6983 return -EINVAL;
6984
Trond Myklebustd9531262009-07-21 19:22:38 -04006985 if (request->fl_type == F_UNLCK) {
6986 if (state != NULL)
6987 return nfs4_proc_unlck(state, cmd, request);
6988 return 0;
6989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
Trond Myklebustd9531262009-07-21 19:22:38 -04006991 if (state == NULL)
6992 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006993
6994 if ((request->fl_flags & FL_POSIX) &&
6995 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6996 return -ENOLCK;
6997
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006998 /*
6999 * Don't rely on the VFS having checked the file open mode,
7000 * since it won't do this for flock() locks.
7001 */
7002 switch (request->fl_type) {
7003 case F_RDLCK:
7004 if (!(filp->f_mode & FMODE_READ))
7005 return -EBADF;
7006 break;
7007 case F_WRLCK:
7008 if (!(filp->f_mode & FMODE_WRITE))
7009 return -EBADF;
7010 }
7011
Jeff Layton1ea67db2016-09-17 18:17:37 -04007012 status = nfs4_set_lock_state(state, request);
7013 if (status != 0)
7014 return status;
7015
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007016 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017}
7018
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007019int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007020{
7021 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007022 int err;
7023
7024 err = nfs4_set_lock_state(state, fl);
7025 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007026 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007027 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007028 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007029}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007030
Trond Myklebustcf470c32012-03-07 13:49:12 -05007031struct nfs_release_lockowner_data {
7032 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007033 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007034 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007035 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007036 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007037};
7038
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007039static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7040{
7041 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007042 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007043 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7044 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007045 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007046 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007047}
7048
7049static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7050{
7051 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007052 struct nfs_server *server = data->server;
7053
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007054 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007055
7056 switch (task->tk_status) {
7057 case 0:
7058 renew_lease(server, data->timestamp);
7059 break;
7060 case -NFS4ERR_STALE_CLIENTID:
7061 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007062 nfs4_schedule_lease_recovery(server->nfs_client);
7063 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007064 case -NFS4ERR_LEASE_MOVED:
7065 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007066 if (nfs4_async_handle_error(task, server,
7067 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007068 rpc_restart_call_prepare(task);
7069 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007070}
7071
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007072static void nfs4_release_lockowner_release(void *calldata)
7073{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007074 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007075 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007076 kfree(calldata);
7077}
7078
Trond Myklebust17280172012-03-11 13:11:00 -04007079static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007080 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7081 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007082 .rpc_release = nfs4_release_lockowner_release,
7083};
7084
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007085static void
7086nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007087{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007088 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007089 struct rpc_message msg = {
7090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7091 };
7092
7093 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007094 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007095
Trond Myklebustcf470c32012-03-07 13:49:12 -05007096 data = kmalloc(sizeof(*data), GFP_NOFS);
7097 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007098 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007099 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007100 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007101 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7102 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7103 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007104
Trond Myklebustcf470c32012-03-07 13:49:12 -05007105 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007106 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007107 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007108 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007109}
7110
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007111#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7112
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007113static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007114 struct dentry *unused, struct inode *inode,
7115 const char *key, const void *buf,
7116 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007117{
Al Viro59301222016-05-27 10:19:30 -04007118 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007119}
7120
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007121static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007122 struct dentry *unused, struct inode *inode,
7123 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007124{
Al Virob2968212016-04-10 20:48:24 -04007125 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007126}
7127
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007128static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007129{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007130 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007131}
7132
David Quigleyc9bccef2013-05-22 12:50:45 -04007133#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007134
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007135static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007136 struct dentry *unused, struct inode *inode,
7137 const char *key, const void *buf,
7138 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007139{
7140 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007141 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007142
7143 return -EOPNOTSUPP;
7144}
7145
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007146static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007147 struct dentry *unused, struct inode *inode,
7148 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007149{
7150 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007151 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007152 return -EOPNOTSUPP;
7153}
7154
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007155static ssize_t
7156nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007157{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007158 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007159
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007160 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7161 len = security_inode_listsecurity(inode, list, list_len);
7162 if (list_len && len > list_len)
7163 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007164 }
7165 return len;
7166}
7167
7168static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7169 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007170 .get = nfs4_xattr_get_nfs4_label,
7171 .set = nfs4_xattr_set_nfs4_label,
7172};
David Quigleyc9bccef2013-05-22 12:50:45 -04007173
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007174#else
7175
7176static ssize_t
7177nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7178{
7179 return 0;
7180}
7181
7182#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007183
Andy Adamson533eb462011-06-13 18:25:56 -04007184/*
7185 * nfs_fhget will use either the mounted_on_fileid or the fileid
7186 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007187static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7188{
Andy Adamson533eb462011-06-13 18:25:56 -04007189 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7190 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7191 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007192 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007193 return;
7194
7195 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007196 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007197 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7198 fattr->nlink = 2;
7199}
7200
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007201static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7202 const struct qstr *name,
7203 struct nfs4_fs_locations *fs_locations,
7204 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007205{
7206 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007207 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007208 struct nfs4_fs_locations_arg args = {
7209 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007210 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007211 .page = page,
7212 .bitmask = bitmask,
7213 };
Benny Halevy22958462009-04-01 09:22:02 -04007214 struct nfs4_fs_locations_res res = {
7215 .fs_locations = fs_locations,
7216 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007217 struct rpc_message msg = {
7218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7219 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007220 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007221 };
7222 int status;
7223
Harvey Harrison3110ff82008-05-02 13:42:44 -07007224 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007225
Chuck Leverc05cefc2017-11-05 15:45:22 -05007226 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7227 bitmask[1] = nfs4_fattr_bitmap[1];
7228
Andy Adamson533eb462011-06-13 18:25:56 -04007229 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7230 * is not supported */
7231 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007232 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007233 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007234 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007235
Trond Myklebustc228fd32007-01-13 02:28:11 -05007236 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007237 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007238 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007239 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007240 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007241 return status;
7242}
7243
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007244int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7245 const struct qstr *name,
7246 struct nfs4_fs_locations *fs_locations,
7247 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007248{
7249 struct nfs4_exception exception = { };
7250 int err;
7251 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007252 err = _nfs4_proc_fs_locations(client, dir, name,
7253 fs_locations, page);
7254 trace_nfs4_get_fs_locations(dir, name, err);
7255 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007256 &exception);
7257 } while (exception.retry);
7258 return err;
7259}
7260
Chuck Leverb03d7352013-10-17 14:12:50 -04007261/*
7262 * This operation also signals the server that this client is
7263 * performing migration recovery. The server can stop returning
7264 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7265 * appended to this compound to identify the client ID which is
7266 * performing recovery.
7267 */
7268static int _nfs40_proc_get_locations(struct inode *inode,
7269 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007270 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007271{
7272 struct nfs_server *server = NFS_SERVER(inode);
7273 struct rpc_clnt *clnt = server->client;
7274 u32 bitmask[2] = {
7275 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7276 };
7277 struct nfs4_fs_locations_arg args = {
7278 .clientid = server->nfs_client->cl_clientid,
7279 .fh = NFS_FH(inode),
7280 .page = page,
7281 .bitmask = bitmask,
7282 .migration = 1, /* skip LOOKUP */
7283 .renew = 1, /* append RENEW */
7284 };
7285 struct nfs4_fs_locations_res res = {
7286 .fs_locations = locations,
7287 .migration = 1,
7288 .renew = 1,
7289 };
7290 struct rpc_message msg = {
7291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7292 .rpc_argp = &args,
7293 .rpc_resp = &res,
7294 .rpc_cred = cred,
7295 };
7296 unsigned long now = jiffies;
7297 int status;
7298
7299 nfs_fattr_init(&locations->fattr);
7300 locations->server = server;
7301 locations->nlocations = 0;
7302
Anna Schumakerfba83f32018-05-04 16:22:50 -04007303 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007304 status = nfs4_call_sync_sequence(clnt, server, &msg,
7305 &args.seq_args, &res.seq_res);
7306 if (status)
7307 return status;
7308
7309 renew_lease(server, now);
7310 return 0;
7311}
7312
7313#ifdef CONFIG_NFS_V4_1
7314
7315/*
7316 * This operation also signals the server that this client is
7317 * performing migration recovery. The server can stop asserting
7318 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7319 * performing this operation is identified in the SEQUENCE
7320 * operation in this compound.
7321 *
7322 * When the client supports GETATTR(fs_locations_info), it can
7323 * be plumbed in here.
7324 */
7325static int _nfs41_proc_get_locations(struct inode *inode,
7326 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007327 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007328{
7329 struct nfs_server *server = NFS_SERVER(inode);
7330 struct rpc_clnt *clnt = server->client;
7331 u32 bitmask[2] = {
7332 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7333 };
7334 struct nfs4_fs_locations_arg args = {
7335 .fh = NFS_FH(inode),
7336 .page = page,
7337 .bitmask = bitmask,
7338 .migration = 1, /* skip LOOKUP */
7339 };
7340 struct nfs4_fs_locations_res res = {
7341 .fs_locations = locations,
7342 .migration = 1,
7343 };
7344 struct rpc_message msg = {
7345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7346 .rpc_argp = &args,
7347 .rpc_resp = &res,
7348 .rpc_cred = cred,
7349 };
7350 int status;
7351
7352 nfs_fattr_init(&locations->fattr);
7353 locations->server = server;
7354 locations->nlocations = 0;
7355
Anna Schumakerfba83f32018-05-04 16:22:50 -04007356 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007357 status = nfs4_call_sync_sequence(clnt, server, &msg,
7358 &args.seq_args, &res.seq_res);
7359 if (status == NFS4_OK &&
7360 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7361 status = -NFS4ERR_LEASE_MOVED;
7362 return status;
7363}
7364
7365#endif /* CONFIG_NFS_V4_1 */
7366
7367/**
7368 * nfs4_proc_get_locations - discover locations for a migrated FSID
7369 * @inode: inode on FSID that is migrating
7370 * @locations: result of query
7371 * @page: buffer
7372 * @cred: credential to use for this operation
7373 *
7374 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7375 * operation failed, or a negative errno if a local error occurred.
7376 *
7377 * On success, "locations" is filled in, but if the server has
7378 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7379 * asserted.
7380 *
7381 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7382 * from this client that require migration recovery.
7383 */
7384int nfs4_proc_get_locations(struct inode *inode,
7385 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007386 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007387{
7388 struct nfs_server *server = NFS_SERVER(inode);
7389 struct nfs_client *clp = server->nfs_client;
7390 const struct nfs4_mig_recovery_ops *ops =
7391 clp->cl_mvops->mig_recovery_ops;
7392 struct nfs4_exception exception = { };
7393 int status;
7394
7395 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7396 (unsigned long long)server->fsid.major,
7397 (unsigned long long)server->fsid.minor,
7398 clp->cl_hostname);
7399 nfs_display_fhandle(NFS_FH(inode), __func__);
7400
7401 do {
7402 status = ops->get_locations(inode, locations, page, cred);
7403 if (status != -NFS4ERR_DELAY)
7404 break;
7405 nfs4_handle_exception(server, status, &exception);
7406 } while (exception.retry);
7407 return status;
7408}
7409
Chuck Lever44c99932013-10-17 14:13:30 -04007410/*
7411 * This operation also signals the server that this client is
7412 * performing "lease moved" recovery. The server can stop
7413 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7414 * is appended to this compound to identify the client ID which is
7415 * performing recovery.
7416 */
NeilBrowna52458b2018-12-03 11:30:31 +11007417static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007418{
7419 struct nfs_server *server = NFS_SERVER(inode);
7420 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7421 struct rpc_clnt *clnt = server->client;
7422 struct nfs4_fsid_present_arg args = {
7423 .fh = NFS_FH(inode),
7424 .clientid = clp->cl_clientid,
7425 .renew = 1, /* append RENEW */
7426 };
7427 struct nfs4_fsid_present_res res = {
7428 .renew = 1,
7429 };
7430 struct rpc_message msg = {
7431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7432 .rpc_argp = &args,
7433 .rpc_resp = &res,
7434 .rpc_cred = cred,
7435 };
7436 unsigned long now = jiffies;
7437 int status;
7438
7439 res.fh = nfs_alloc_fhandle();
7440 if (res.fh == NULL)
7441 return -ENOMEM;
7442
Anna Schumakerfba83f32018-05-04 16:22:50 -04007443 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007444 status = nfs4_call_sync_sequence(clnt, server, &msg,
7445 &args.seq_args, &res.seq_res);
7446 nfs_free_fhandle(res.fh);
7447 if (status)
7448 return status;
7449
7450 do_renew_lease(clp, now);
7451 return 0;
7452}
7453
7454#ifdef CONFIG_NFS_V4_1
7455
7456/*
7457 * This operation also signals the server that this client is
7458 * performing "lease moved" recovery. The server can stop asserting
7459 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7460 * this operation is identified in the SEQUENCE operation in this
7461 * compound.
7462 */
NeilBrowna52458b2018-12-03 11:30:31 +11007463static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007464{
7465 struct nfs_server *server = NFS_SERVER(inode);
7466 struct rpc_clnt *clnt = server->client;
7467 struct nfs4_fsid_present_arg args = {
7468 .fh = NFS_FH(inode),
7469 };
7470 struct nfs4_fsid_present_res res = {
7471 };
7472 struct rpc_message msg = {
7473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7474 .rpc_argp = &args,
7475 .rpc_resp = &res,
7476 .rpc_cred = cred,
7477 };
7478 int status;
7479
7480 res.fh = nfs_alloc_fhandle();
7481 if (res.fh == NULL)
7482 return -ENOMEM;
7483
Anna Schumakerfba83f32018-05-04 16:22:50 -04007484 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007485 status = nfs4_call_sync_sequence(clnt, server, &msg,
7486 &args.seq_args, &res.seq_res);
7487 nfs_free_fhandle(res.fh);
7488 if (status == NFS4_OK &&
7489 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7490 status = -NFS4ERR_LEASE_MOVED;
7491 return status;
7492}
7493
7494#endif /* CONFIG_NFS_V4_1 */
7495
7496/**
7497 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7498 * @inode: inode on FSID to check
7499 * @cred: credential to use for this operation
7500 *
7501 * Server indicates whether the FSID is present, moved, or not
7502 * recognized. This operation is necessary to clear a LEASE_MOVED
7503 * condition for this client ID.
7504 *
7505 * Returns NFS4_OK if the FSID is present on this server,
7506 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7507 * NFS4ERR code if some error occurred on the server, or a
7508 * negative errno if a local failure occurred.
7509 */
NeilBrowna52458b2018-12-03 11:30:31 +11007510int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007511{
7512 struct nfs_server *server = NFS_SERVER(inode);
7513 struct nfs_client *clp = server->nfs_client;
7514 const struct nfs4_mig_recovery_ops *ops =
7515 clp->cl_mvops->mig_recovery_ops;
7516 struct nfs4_exception exception = { };
7517 int status;
7518
7519 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7520 (unsigned long long)server->fsid.major,
7521 (unsigned long long)server->fsid.minor,
7522 clp->cl_hostname);
7523 nfs_display_fhandle(NFS_FH(inode), __func__);
7524
7525 do {
7526 status = ops->fsid_present(inode, cred);
7527 if (status != -NFS4ERR_DELAY)
7528 break;
7529 nfs4_handle_exception(server, status, &exception);
7530 } while (exception.retry);
7531 return status;
7532}
7533
Trond Myklebust302fad72019-02-18 13:32:38 -05007534/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007535 * If 'use_integrity' is true and the state managment nfs_client
7536 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7537 * and the machine credential as per RFC3530bis and RFC5661 Security
7538 * Considerations sections. Otherwise, just use the user cred with the
7539 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007540 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007541static 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 +00007542{
7543 int status;
7544 struct nfs4_secinfo_arg args = {
7545 .dir_fh = NFS_FH(dir),
7546 .name = name,
7547 };
7548 struct nfs4_secinfo_res res = {
7549 .flavors = flavors,
7550 };
7551 struct rpc_message msg = {
7552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7553 .rpc_argp = &args,
7554 .rpc_resp = &res,
7555 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007556 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
NeilBrowna52458b2018-12-03 11:30:31 +11007557 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007558
7559 if (use_integrity) {
7560 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007561 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7562 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007563 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007564
7565 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007566
7567 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7568 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7569
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007570 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7571 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007572 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007573
NeilBrowna52458b2018-12-03 11:30:31 +11007574 put_cred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007575
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007576 return status;
7577}
7578
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007579int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7580 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007581{
7582 struct nfs4_exception exception = { };
7583 int err;
7584 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007585 err = -NFS4ERR_WRONGSEC;
7586
7587 /* try to use integrity protection with machine cred */
7588 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7589 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7590
7591 /*
7592 * if unable to use integrity protection, or SECINFO with
7593 * integrity protection returns NFS4ERR_WRONGSEC (which is
7594 * disallowed by spec, but exists in deployed servers) use
7595 * the current filesystem's rpc_client and the user cred.
7596 */
7597 if (err == -NFS4ERR_WRONGSEC)
7598 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7599
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007600 trace_nfs4_secinfo(dir, name, err);
7601 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007602 &exception);
7603 } while (exception.retry);
7604 return err;
7605}
7606
Andy Adamson557134a2009-04-01 09:21:53 -04007607#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007608/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007609 * Check the exchange flags returned by the server for invalid flags, having
7610 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7611 * DS flags set.
7612 */
7613static int nfs4_check_cl_exchange_flags(u32 flags)
7614{
7615 if (flags & ~EXCHGID4_FLAG_MASK_R)
7616 goto out_inval;
7617 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7618 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7619 goto out_inval;
7620 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7621 goto out_inval;
7622 return NFS_OK;
7623out_inval:
7624 return -NFS4ERR_INVAL;
7625}
7626
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007627static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007628nfs41_same_server_scope(struct nfs41_server_scope *a,
7629 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007630{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007631 if (a->server_scope_sz != b->server_scope_sz)
7632 return false;
7633 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007634}
7635
Andy Adamson02a95de2016-02-05 16:08:37 -05007636static void
7637nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7638{
7639}
7640
7641static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7642 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7643};
7644
Andy Adamson357f54d2010-12-14 10:11:57 -05007645/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007646 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007647 *
7648 * The 4.1 client currently uses the same TCP connection for the
7649 * fore and backchannel.
7650 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007651static
7652int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7653 struct rpc_xprt *xprt,
7654 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007655 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007656{
7657 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007658 struct nfs41_bind_conn_to_session_args args = {
7659 .client = clp,
7660 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7661 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007662 struct nfs41_bind_conn_to_session_res res;
7663 struct rpc_message msg = {
7664 .rpc_proc =
7665 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007666 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007667 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007668 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007669 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007670 struct rpc_task_setup task_setup_data = {
7671 .rpc_client = clnt,
7672 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007673 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007674 .rpc_message = &msg,
7675 .flags = RPC_TASK_TIMEOUT,
7676 };
7677 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007678
Trond Myklebust71a097c2015-02-18 09:27:18 -08007679 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7680 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7681 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007682
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007683 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7684 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7685 args.dir = NFS4_CDFC4_FORE;
7686
7687 task = rpc_run_task(&task_setup_data);
7688 if (!IS_ERR(task)) {
7689 status = task->tk_status;
7690 rpc_put_task(task);
7691 } else
7692 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007693 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007694 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007695 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007696 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7697 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007698 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007699 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007700 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007701 dprintk("NFS: %s: Unexpected direction from server\n",
7702 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007703 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007704 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007705 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007706 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7707 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007708 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007709 }
7710 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007711
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007712 return status;
7713}
7714
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007715struct rpc_bind_conn_calldata {
7716 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007717 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007718};
7719
7720static int
7721nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7722 struct rpc_xprt *xprt,
7723 void *calldata)
7724{
7725 struct rpc_bind_conn_calldata *p = calldata;
7726
7727 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7728}
7729
NeilBrowna52458b2018-12-03 11:30:31 +11007730int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007731{
7732 struct rpc_bind_conn_calldata data = {
7733 .clp = clp,
7734 .cred = cred,
7735 };
7736 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7737 nfs4_proc_bind_conn_to_session_callback, &data);
7738}
7739
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007740/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007741 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7742 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007743 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007744static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7745 .how = SP4_MACH_CRED,
7746 .enforce.u.words = {
7747 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7748 1 << (OP_EXCHANGE_ID - 32) |
7749 1 << (OP_CREATE_SESSION - 32) |
7750 1 << (OP_DESTROY_SESSION - 32) |
7751 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007752 },
7753 .allow.u.words = {
7754 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007755 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007756 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007757 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007758 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007759 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007760 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007761 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007762 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007763 1 << (OP_FREE_STATEID - 32) |
7764 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007765 }
7766};
7767
7768/*
7769 * Select the state protection mode for client `clp' given the server results
7770 * from exchange_id in `sp'.
7771 *
7772 * Returns 0 on success, negative errno otherwise.
7773 */
7774static int nfs4_sp4_select_mode(struct nfs_client *clp,
7775 struct nfs41_state_protection *sp)
7776{
7777 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7778 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7779 1 << (OP_EXCHANGE_ID - 32) |
7780 1 << (OP_CREATE_SESSION - 32) |
7781 1 << (OP_DESTROY_SESSION - 32) |
7782 1 << (OP_DESTROY_CLIENTID - 32)
7783 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007784 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007785 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007786 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007787
7788 if (sp->how == SP4_MACH_CRED) {
7789 /* Print state protect result */
7790 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7791 for (i = 0; i <= LAST_NFS4_OP; i++) {
7792 if (test_bit(i, sp->enforce.u.longs))
7793 dfprintk(MOUNT, " enforce op %d\n", i);
7794 if (test_bit(i, sp->allow.u.longs))
7795 dfprintk(MOUNT, " allow op %d\n", i);
7796 }
7797
7798 /* make sure nothing is on enforce list that isn't supported */
7799 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7800 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7801 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007802 ret = -EINVAL;
7803 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007804 }
7805 }
7806
7807 /*
7808 * Minimal mode - state operations are allowed to use machine
7809 * credential. Note this already happens by default, so the
7810 * client doesn't have to do anything more than the negotiation.
7811 *
7812 * NOTE: we don't care if EXCHANGE_ID is in the list -
7813 * we're already using the machine cred for exchange_id
7814 * and will never use a different cred.
7815 */
7816 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7817 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7818 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7819 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7820 dfprintk(MOUNT, "sp4_mach_cred:\n");
7821 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007822 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007823 } else {
7824 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007825 ret = -EINVAL;
7826 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007827 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007828
7829 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007830 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7831 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007832 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7833 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007834 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007835 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007836
Andrew Elble99ade3c2015-12-02 09:39:51 -05007837 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7838 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007839 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007840 }
7841
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007842 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7843 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7844 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007845 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007846 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007847
7848 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7849 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7850 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007851 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007852 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007853
7854 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7855 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007856 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007857 }
7858
7859 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7860 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007861 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007862 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007863 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007864out:
7865 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00007866 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007867}
7868
Andy Adamson8d89bd72016-09-09 09:22:18 -04007869struct nfs41_exchange_id_data {
7870 struct nfs41_exchange_id_res res;
7871 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007872};
7873
Andy Adamson8d89bd72016-09-09 09:22:18 -04007874static void nfs4_exchange_id_release(void *data)
7875{
7876 struct nfs41_exchange_id_data *cdata =
7877 (struct nfs41_exchange_id_data *)data;
7878
Olga Kornievskaia63513232017-03-13 10:36:19 -04007879 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007880 kfree(cdata->res.impl_id);
7881 kfree(cdata->res.server_scope);
7882 kfree(cdata->res.server_owner);
7883 kfree(cdata);
7884}
7885
7886static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007887 .rpc_release = nfs4_exchange_id_release,
7888};
7889
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007890/*
7891 * _nfs4_proc_exchange_id()
7892 *
7893 * Wrapper for EXCHANGE_ID operation.
7894 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007895static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11007896nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007897 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007898{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007899 struct rpc_message msg = {
7900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007901 .rpc_cred = cred,
7902 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007903 struct rpc_task_setup task_setup_data = {
7904 .rpc_client = clp->cl_rpcclient,
7905 .callback_ops = &nfs4_exchange_id_call_ops,
7906 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007907 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007908 };
7909 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007910 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007911
Elena Reshetova212bf412017-10-20 12:53:38 +03007912 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007913 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007914
Trond Myklebust9c760d12017-07-31 18:38:50 -04007915 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007916 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007917 if (!calldata)
7918 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007919
Trond Myklebustfd405592017-08-01 16:02:47 -04007920 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007921
7922 status = nfs4_init_uniform_client_string(clp);
7923 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007924 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007925
Andy Adamson8d89bd72016-09-09 09:22:18 -04007926 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7927 GFP_NOFS);
7928 status = -ENOMEM;
7929 if (unlikely(calldata->res.server_owner == NULL))
7930 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007931
Andy Adamson8d89bd72016-09-09 09:22:18 -04007932 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007933 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007934 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007935 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007936
Andy Adamson8d89bd72016-09-09 09:22:18 -04007937 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7938 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007939 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007940
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007941 switch (sp4_how) {
7942 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007943 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007944 break;
7945
7946 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007947 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007948 break;
7949
7950 default:
7951 /* unsupported! */
7952 WARN_ON_ONCE(1);
7953 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007954 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007955 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007956 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007957 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007958 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007959 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7960 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007961 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007962 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007963 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7964 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007965#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007966 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007967#endif
7968 msg.rpc_argp = &calldata->args;
7969 msg.rpc_resp = &calldata->res;
7970 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007971
Trond Myklebust9c760d12017-07-31 18:38:50 -04007972 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007973
7974out_impl_id:
7975 kfree(calldata->res.impl_id);
7976out_server_scope:
7977 kfree(calldata->res.server_scope);
7978out_server_owner:
7979 kfree(calldata->res.server_owner);
7980out_calldata:
7981 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007982out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007983 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007984 return ERR_PTR(status);
7985}
7986
7987/*
7988 * _nfs4_proc_exchange_id()
7989 *
7990 * Wrapper for EXCHANGE_ID operation.
7991 */
NeilBrowna52458b2018-12-03 11:30:31 +11007992static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04007993 u32 sp4_how)
7994{
7995 struct rpc_task *task;
7996 struct nfs41_exchange_id_args *argp;
7997 struct nfs41_exchange_id_res *resp;
7998 int status;
7999
8000 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8001 if (IS_ERR(task))
8002 return PTR_ERR(task);
8003
8004 argp = task->tk_msg.rpc_argp;
8005 resp = task->tk_msg.rpc_resp;
8006 status = task->tk_status;
8007 if (status != 0)
8008 goto out;
8009
8010 status = nfs4_check_cl_exchange_flags(resp->flags);
8011 if (status != 0)
8012 goto out;
8013
8014 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8015 if (status != 0)
8016 goto out;
8017
8018 clp->cl_clientid = resp->clientid;
8019 clp->cl_exchange_flags = resp->flags;
8020 clp->cl_seqid = resp->seqid;
8021 /* Client ID is not confirmed */
8022 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8023 clear_bit(NFS4_SESSION_ESTABLISHED,
8024 &clp->cl_session->session_state);
8025
8026 if (clp->cl_serverscope != NULL &&
8027 !nfs41_same_server_scope(clp->cl_serverscope,
8028 resp->server_scope)) {
8029 dprintk("%s: server_scope mismatch detected\n",
8030 __func__);
8031 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8032 }
8033
8034 swap(clp->cl_serverowner, resp->server_owner);
8035 swap(clp->cl_serverscope, resp->server_scope);
8036 swap(clp->cl_implid, resp->impl_id);
8037
8038 /* Save the EXCHANGE_ID verifier session trunk tests */
8039 memcpy(clp->cl_confirm.data, argp->verifier.data,
8040 sizeof(clp->cl_confirm.data));
8041out:
8042 trace_nfs4_exchange_id(clp, status);
8043 rpc_put_task(task);
8044 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008045}
8046
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008047/*
8048 * nfs4_proc_exchange_id()
8049 *
8050 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8051 *
8052 * Since the clientid has expired, all compounds using sessions
8053 * associated with the stale clientid will be returning
8054 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8055 * be in some phase of session reset.
8056 *
8057 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8058 */
NeilBrowna52458b2018-12-03 11:30:31 +11008059int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008060{
8061 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8062 int status;
8063
8064 /* try SP4_MACH_CRED if krb5i/p */
8065 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8066 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008067 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008068 if (!status)
8069 return 0;
8070 }
8071
8072 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008073 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008074}
8075
Andy Adamson04fa2c62016-09-09 09:22:29 -04008076/**
8077 * nfs4_test_session_trunk
8078 *
8079 * This is an add_xprt_test() test function called from
8080 * rpc_clnt_setup_test_and_add_xprt.
8081 *
8082 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8083 * and is dereferrenced in nfs4_exchange_id_release
8084 *
8085 * Upon success, add the new transport to the rpc_clnt
8086 *
8087 * @clnt: struct rpc_clnt to get new transport
8088 * @xprt: the rpc_xprt to test
8089 * @data: call data for _nfs4_proc_exchange_id.
8090 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308091void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008092 void *data)
8093{
8094 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008095 struct rpc_task *task;
8096 int status;
8097
Andy Adamson04fa2c62016-09-09 09:22:29 -04008098 u32 sp4_how;
8099
8100 dprintk("--> %s try %s\n", __func__,
8101 xprt->address_strings[RPC_DISPLAY_ADDR]);
8102
8103 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8104
8105 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008106 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8107 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308108 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008109
8110 status = task->tk_status;
8111 if (status == 0)
8112 status = nfs4_detect_session_trunking(adata->clp,
8113 task->tk_msg.rpc_resp, xprt);
8114
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308115 if (status == 0)
8116 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8117
Trond Myklebust9c760d12017-07-31 18:38:50 -04008118 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008119}
8120EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8121
Trond Myklebust66245532012-05-25 17:18:09 -04008122static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008123 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008124{
8125 struct rpc_message msg = {
8126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8127 .rpc_argp = clp,
8128 .rpc_cred = cred,
8129 };
8130 int status;
8131
8132 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008133 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008134 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008135 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008136 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8137 return status;
8138}
8139
8140static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008141 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008142{
8143 unsigned int loop;
8144 int ret;
8145
8146 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8147 ret = _nfs4_proc_destroy_clientid(clp, cred);
8148 switch (ret) {
8149 case -NFS4ERR_DELAY:
8150 case -NFS4ERR_CLIENTID_BUSY:
8151 ssleep(1);
8152 break;
8153 default:
8154 return ret;
8155 }
8156 }
8157 return 0;
8158}
8159
8160int nfs4_destroy_clientid(struct nfs_client *clp)
8161{
NeilBrowna52458b2018-12-03 11:30:31 +11008162 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008163 int ret = 0;
8164
8165 if (clp->cl_mvops->minor_version < 1)
8166 goto out;
8167 if (clp->cl_exchange_flags == 0)
8168 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008169 if (clp->cl_preserve_clid)
8170 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008171 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008172 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008173 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008174 switch (ret) {
8175 case 0:
8176 case -NFS4ERR_STALE_CLIENTID:
8177 clp->cl_exchange_flags = 0;
8178 }
8179out:
8180 return ret;
8181}
8182
Andy Adamson2050f0c2009-04-01 09:22:30 -04008183struct nfs4_get_lease_time_data {
8184 struct nfs4_get_lease_time_args *args;
8185 struct nfs4_get_lease_time_res *res;
8186 struct nfs_client *clp;
8187};
8188
8189static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8190 void *calldata)
8191{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008192 struct nfs4_get_lease_time_data *data =
8193 (struct nfs4_get_lease_time_data *)calldata;
8194
8195 dprintk("--> %s\n", __func__);
8196 /* just setup sequence, do not trigger session recovery
8197 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008198 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008199 &data->args->la_seq_args,
8200 &data->res->lr_seq_res,
8201 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008202 dprintk("<-- %s\n", __func__);
8203}
8204
8205/*
8206 * Called from nfs4_state_manager thread for session setup, so don't recover
8207 * from sequence operation or clientid errors.
8208 */
8209static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8210{
8211 struct nfs4_get_lease_time_data *data =
8212 (struct nfs4_get_lease_time_data *)calldata;
8213
8214 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008215 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8216 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008217 switch (task->tk_status) {
8218 case -NFS4ERR_DELAY:
8219 case -NFS4ERR_GRACE:
8220 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8221 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8222 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008223 /* fall through */
8224 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008225 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008226 return;
8227 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008228 dprintk("<-- %s\n", __func__);
8229}
8230
Trond Myklebust17280172012-03-11 13:11:00 -04008231static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008232 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8233 .rpc_call_done = nfs4_get_lease_time_done,
8234};
8235
8236int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8237{
8238 struct rpc_task *task;
8239 struct nfs4_get_lease_time_args args;
8240 struct nfs4_get_lease_time_res res = {
8241 .lr_fsinfo = fsinfo,
8242 };
8243 struct nfs4_get_lease_time_data data = {
8244 .args = &args,
8245 .res = &res,
8246 .clp = clp,
8247 };
8248 struct rpc_message msg = {
8249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8250 .rpc_argp = &args,
8251 .rpc_resp = &res,
8252 };
8253 struct rpc_task_setup task_setup = {
8254 .rpc_client = clp->cl_rpcclient,
8255 .rpc_message = &msg,
8256 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008257 .callback_data = &data,
8258 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008259 };
8260 int status;
8261
Anna Schumakerfba83f32018-05-04 16:22:50 -04008262 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008263 task = rpc_run_task(&task_setup);
8264
8265 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008266 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008267
Anna Schumakerf6148712017-04-07 14:15:23 -04008268 status = task->tk_status;
8269 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008270 return status;
8271}
8272
Andy Adamsonfc931582009-04-01 09:22:31 -04008273/*
8274 * Initialize the values to be used by the client in CREATE_SESSION
8275 * If nfs4_init_session set the fore channel request and response sizes,
8276 * use them.
8277 *
8278 * Set the back channel max_resp_sz_cached to zero to force the client to
8279 * always set csa_cachethis to FALSE because the current implementation
8280 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8281 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008282static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8283 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008284{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008285 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008286 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008287
Andy Adamson18aad3d2013-06-26 12:21:49 -04008288 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8289 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8290
Andy Adamsonfc931582009-04-01 09:22:31 -04008291 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008292 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8293 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008294 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008295 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008296
8297 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008298 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008299 __func__,
8300 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008301 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008302
8303 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008304 args->bc_attrs.max_rqst_sz = max_bc_payload;
8305 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008306 args->bc_attrs.max_resp_sz_cached = 0;
8307 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008308 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008309
8310 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8311 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8312 __func__,
8313 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8314 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8315 args->bc_attrs.max_reqs);
8316}
8317
Trond Myklebust79969dd2015-02-18 11:30:18 -08008318static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8319 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008320{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008321 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008322 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008323
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008324 if (rcvd->max_resp_sz > sent->max_resp_sz)
8325 return -EINVAL;
8326 /*
8327 * Our requested max_ops is the minimum we need; we're not
8328 * prepared to break up compounds into smaller pieces than that.
8329 * So, no point even trying to continue if the server won't
8330 * cooperate:
8331 */
8332 if (rcvd->max_ops < sent->max_ops)
8333 return -EINVAL;
8334 if (rcvd->max_reqs == 0)
8335 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008336 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8337 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008338 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008339}
8340
Trond Myklebust79969dd2015-02-18 11:30:18 -08008341static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8342 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008343{
8344 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008345 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008346
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008347 if (!(res->flags & SESSION4_BACK_CHAN))
8348 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008349 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8350 return -EINVAL;
8351 if (rcvd->max_resp_sz < sent->max_resp_sz)
8352 return -EINVAL;
8353 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8354 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008355 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008356 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008357 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008358 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008359out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008360 return 0;
8361}
Andy Adamson8d353012009-04-01 09:22:32 -04008362
Andy Adamson8d353012009-04-01 09:22:32 -04008363static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008364 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008365{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008366 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008367
Trond Myklebust79969dd2015-02-18 11:30:18 -08008368 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008369 if (ret)
8370 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008371 return nfs4_verify_back_channel_attrs(args, res);
8372}
8373
8374static void nfs4_update_session(struct nfs4_session *session,
8375 struct nfs41_create_session_res *res)
8376{
8377 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008378 /* Mark client id and session as being confirmed */
8379 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8380 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008381 session->flags = res->flags;
8382 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008383 if (res->flags & SESSION4_BACK_CHAN)
8384 memcpy(&session->bc_attrs, &res->bc_attrs,
8385 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008386}
8387
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008388static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008389 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008390{
8391 struct nfs4_session *session = clp->cl_session;
8392 struct nfs41_create_session_args args = {
8393 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008394 .clientid = clp->cl_clientid,
8395 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008396 .cb_program = NFS4_CALLBACK,
8397 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008398 struct nfs41_create_session_res res;
8399
Andy Adamsonfc931582009-04-01 09:22:31 -04008400 struct rpc_message msg = {
8401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8402 .rpc_argp = &args,
8403 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008404 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008405 };
8406 int status;
8407
Chuck Lever6b26cc82016-05-02 14:40:40 -04008408 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008409 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008410
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008411 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008412 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008413
Trond Myklebustb519d402016-09-11 14:50:01 -04008414 switch (status) {
8415 case -NFS4ERR_STALE_CLIENTID:
8416 case -NFS4ERR_DELAY:
8417 case -ETIMEDOUT:
8418 case -EACCES:
8419 case -EAGAIN:
8420 goto out;
8421 };
8422
8423 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008424 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008425 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008426 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008427 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008428 if (status)
8429 goto out;
8430 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008431 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008432out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008433 return status;
8434}
8435
8436/*
8437 * Issues a CREATE_SESSION operation to the server.
8438 * It is the responsibility of the caller to verify the session is
8439 * expired before calling this routine.
8440 */
NeilBrowna52458b2018-12-03 11:30:31 +11008441int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008442{
8443 int status;
8444 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008445 struct nfs4_session *session = clp->cl_session;
8446
8447 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8448
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008449 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008450 if (status)
8451 goto out;
8452
Andy Adamsonaacd5532011-11-09 13:58:21 -05008453 /* Init or reset the session slot tables */
8454 status = nfs4_setup_session_slot_tables(session);
8455 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008456 if (status)
8457 goto out;
8458
8459 ptr = (unsigned *)&session->sess_id.data[0];
8460 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8461 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008462out:
8463 dprintk("<-- %s\n", __func__);
8464 return status;
8465}
8466
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008467/*
8468 * Issue the over-the-wire RPC DESTROY_SESSION.
8469 * The caller must serialize access to this routine.
8470 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008471int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008472 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008473{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008474 struct rpc_message msg = {
8475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8476 .rpc_argp = session,
8477 .rpc_cred = cred,
8478 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008479 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008480
8481 dprintk("--> nfs4_proc_destroy_session\n");
8482
8483 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008484 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8485 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008486
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008487 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008488 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008489
8490 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008491 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008492 "Session has been destroyed regardless...\n", status);
8493
8494 dprintk("<-- nfs4_proc_destroy_session\n");
8495 return status;
8496}
8497
Trond Myklebust7b38c362012-05-23 13:23:31 -04008498/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008499 * Renew the cl_session lease.
8500 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008501struct nfs4_sequence_data {
8502 struct nfs_client *clp;
8503 struct nfs4_sequence_args args;
8504 struct nfs4_sequence_res res;
8505};
8506
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008507static void nfs41_sequence_release(void *data)
8508{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008509 struct nfs4_sequence_data *calldata = data;
8510 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008511
Elena Reshetova212bf412017-10-20 12:53:38 +03008512 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008513 nfs4_schedule_state_renewal(clp);
8514 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008515 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008516}
8517
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008518static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8519{
8520 switch(task->tk_status) {
8521 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008522 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8523 return -EAGAIN;
8524 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008525 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008526 }
8527 return 0;
8528}
8529
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008530static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008531{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008532 struct nfs4_sequence_data *calldata = data;
8533 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008534
Trond Myklebust14516c32010-07-31 14:29:06 -04008535 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8536 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008537
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008538 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008539 if (task->tk_status < 0) {
8540 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008541 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008542 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008543
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008544 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8545 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008546 return;
8547 }
8548 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008549 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008550out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008551 dprintk("<-- %s\n", __func__);
8552}
8553
8554static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8555{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008556 struct nfs4_sequence_data *calldata = data;
8557 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008558 struct nfs4_sequence_args *args;
8559 struct nfs4_sequence_res *res;
8560
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008561 args = task->tk_msg.rpc_argp;
8562 res = task->tk_msg.rpc_resp;
8563
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008564 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008565}
8566
8567static const struct rpc_call_ops nfs41_sequence_ops = {
8568 .rpc_call_done = nfs41_sequence_call_done,
8569 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008570 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008571};
8572
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008573static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008574 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008575 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008576 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008577{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008578 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008579 struct rpc_message msg = {
8580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8581 .rpc_cred = cred,
8582 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008583 struct rpc_task_setup task_setup_data = {
8584 .rpc_client = clp->cl_rpcclient,
8585 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008586 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008587 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008588 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008589 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008590
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008591 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008592 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008593 goto out_err;
8594
8595 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008596 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008597 if (calldata == NULL)
8598 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008599 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008600 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008601 msg.rpc_argp = &calldata->args;
8602 msg.rpc_resp = &calldata->res;
8603 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008604 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008605
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008606 ret = rpc_run_task(&task_setup_data);
8607 if (IS_ERR(ret))
8608 goto out_err;
8609 return ret;
8610out_put_clp:
8611 nfs_put_client(clp);
8612out_err:
8613 nfs41_release_slot(slot);
8614 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008615}
8616
NeilBrowna52458b2018-12-03 11:30:31 +11008617static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008618{
8619 struct rpc_task *task;
8620 int ret = 0;
8621
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008622 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008623 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008624 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008625 if (IS_ERR(task))
8626 ret = PTR_ERR(task);
8627 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008628 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008629 dprintk("<-- %s status=%d\n", __func__, ret);
8630 return ret;
8631}
8632
NeilBrowna52458b2018-12-03 11:30:31 +11008633static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008634{
8635 struct rpc_task *task;
8636 int ret;
8637
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008638 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008639 if (IS_ERR(task)) {
8640 ret = PTR_ERR(task);
8641 goto out;
8642 }
8643 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008644 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008645 ret = task->tk_status;
8646 rpc_put_task(task);
8647out:
8648 dprintk("<-- %s status=%d\n", __func__, ret);
8649 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008650}
8651
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008652struct nfs4_reclaim_complete_data {
8653 struct nfs_client *clp;
8654 struct nfs41_reclaim_complete_args arg;
8655 struct nfs41_reclaim_complete_res res;
8656};
8657
8658static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8659{
8660 struct nfs4_reclaim_complete_data *calldata = data;
8661
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008662 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008663 &calldata->arg.seq_args,
8664 &calldata->res.seq_res,
8665 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008666}
8667
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008668static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8669{
8670 switch(task->tk_status) {
8671 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008672 wake_up_all(&clp->cl_lock_waitq);
8673 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008674 case -NFS4ERR_COMPLETE_ALREADY:
8675 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8676 break;
8677 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008678 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008679 /* fall through */
8680 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008681 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008682 case -NFS4ERR_BADSESSION:
8683 case -NFS4ERR_DEADSESSION:
8684 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8685 nfs4_schedule_session_recovery(clp->cl_session,
8686 task->tk_status);
8687 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008688 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008689 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008690 }
8691 return 0;
8692}
8693
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008694static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8695{
8696 struct nfs4_reclaim_complete_data *calldata = data;
8697 struct nfs_client *clp = calldata->clp;
8698 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8699
8700 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008701 if (!nfs41_sequence_done(task, res))
8702 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008703
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008704 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008705 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8706 rpc_restart_call_prepare(task);
8707 return;
8708 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008709 dprintk("<-- %s\n", __func__);
8710}
8711
8712static void nfs4_free_reclaim_complete_data(void *data)
8713{
8714 struct nfs4_reclaim_complete_data *calldata = data;
8715
8716 kfree(calldata);
8717}
8718
8719static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8720 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8721 .rpc_call_done = nfs4_reclaim_complete_done,
8722 .rpc_release = nfs4_free_reclaim_complete_data,
8723};
8724
8725/*
8726 * Issue a global reclaim complete.
8727 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008728static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008729 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008730{
8731 struct nfs4_reclaim_complete_data *calldata;
8732 struct rpc_task *task;
8733 struct rpc_message msg = {
8734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008735 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008736 };
8737 struct rpc_task_setup task_setup_data = {
8738 .rpc_client = clp->cl_rpcclient,
8739 .rpc_message = &msg,
8740 .callback_ops = &nfs4_reclaim_complete_call_ops,
8741 .flags = RPC_TASK_ASYNC,
8742 };
8743 int status = -ENOMEM;
8744
8745 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008746 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008747 if (calldata == NULL)
8748 goto out;
8749 calldata->clp = clp;
8750 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008751
Anna Schumakerfba83f32018-05-04 16:22:50 -04008752 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008753 msg.rpc_argp = &calldata->arg;
8754 msg.rpc_resp = &calldata->res;
8755 task_setup_data.callback_data = calldata;
8756 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008757 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008758 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008759 goto out;
8760 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008761 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008762 if (status == 0)
8763 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008764 rpc_put_task(task);
8765out:
8766 dprintk("<-- %s status=%d\n", __func__, status);
8767 return status;
8768}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008769
8770static void
8771nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8772{
8773 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008774 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008775
8776 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008777 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008778 &lgp->res.seq_res, task);
8779 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008780}
8781
8782static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8783{
8784 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008785
8786 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008787 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008788 dprintk("<-- %s\n", __func__);
8789}
8790
8791static int
8792nfs4_layoutget_handle_exception(struct rpc_task *task,
8793 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8794{
Trond Myklebustee314c22012-10-01 17:25:48 -07008795 struct inode *inode = lgp->args.inode;
8796 struct nfs_server *server = NFS_SERVER(inode);
8797 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008798 int nfs4err = task->tk_status;
8799 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008800 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008801
Boaz Harroshed7e5422014-01-22 20:34:54 +02008802 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008803
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008804 nfs4_sequence_free_slot(&lgp->res.seq_res);
8805
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008806 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008807 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008808 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008809
8810 /*
8811 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8812 * on the file. set tk_status to -ENODATA to tell upper layer to
8813 * retry go inband.
8814 */
8815 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008816 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008817 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008818 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008819 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8820 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8821 */
8822 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008823 status = -EOVERFLOW;
8824 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008825 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008826 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008827 * (or clients) writing to the same RAID stripe except when
8828 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008829 *
8830 * Treat it like we would RECALLCONFLICT -- we retry for a little
8831 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008832 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008833 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008834 if (lgp->args.minlength == 0) {
8835 status = -EOVERFLOW;
8836 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008837 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008838 status = -EBUSY;
8839 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008840 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008841 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008842 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008843 case -NFS4ERR_DELEG_REVOKED:
8844 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008845 case -NFS4ERR_EXPIRED:
8846 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008847 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008848 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008849 lo = NFS_I(inode)->layout;
8850 /* If the open stateid was bad, then recover it. */
8851 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008852 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008853 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008854 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008855 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008856 break;
8857 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008858
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008859 /*
8860 * Mark the bad layout state as invalid, then retry
8861 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008862 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008863 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008864 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008865 pnfs_free_lseg_list(&head);
8866 status = -EAGAIN;
8867 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008868 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008869
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008870 err = nfs4_handle_exception(server, nfs4err, exception);
8871 if (!status) {
8872 if (exception->retry)
8873 status = -EAGAIN;
8874 else
8875 status = err;
8876 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008877out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008878 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008879 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008880}
8881
Fred Isamandacb4522016-09-19 17:47:09 -04008882size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03008883{
8884 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8885 return nfs_page_array_len(0, max_resp_sz);
8886}
8887
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008888static void nfs4_layoutget_release(void *calldata)
8889{
8890 struct nfs4_layoutget *lgp = calldata;
8891
8892 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008893 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04008894 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008895 dprintk("<-- %s\n", __func__);
8896}
8897
8898static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8899 .rpc_call_prepare = nfs4_layoutget_prepare,
8900 .rpc_call_done = nfs4_layoutget_done,
8901 .rpc_release = nfs4_layoutget_release,
8902};
8903
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008904struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04008905nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008906{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008907 struct inode *inode = lgp->args.inode;
8908 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008909 struct rpc_task *task;
8910 struct rpc_message msg = {
8911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8912 .rpc_argp = &lgp->args,
8913 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008914 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008915 };
8916 struct rpc_task_setup task_setup_data = {
8917 .rpc_client = server->client,
8918 .rpc_message = &msg,
8919 .callback_ops = &nfs4_layoutget_call_ops,
8920 .callback_data = lgp,
8921 .flags = RPC_TASK_ASYNC,
8922 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008923 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008924 struct nfs4_exception exception = {
8925 .inode = inode,
8926 .timeout = *timeout,
8927 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008928 int status = 0;
8929
8930 dprintk("--> %s\n", __func__);
8931
Peng Tao4bd5a982014-11-17 11:05:17 +08008932 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8933 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8934
Anna Schumakerfba83f32018-05-04 16:22:50 -04008935 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008936
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008937 task = rpc_run_task(&task_setup_data);
8938 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008939 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008940 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008941 if (status != 0)
8942 goto out;
8943
8944 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8945 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04008946 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8947 *timeout = exception.timeout;
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008948 } else
8949 lseg = pnfs_layout_process(lgp);
8950out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008951 trace_nfs4_layoutget(lgp->args.ctx,
8952 &lgp->args.range,
8953 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008954 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008955 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008956
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008957 rpc_put_task(task);
8958 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008959 if (status)
8960 return ERR_PTR(status);
8961 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008962}
8963
Benny Halevycbe82602011-05-22 19:52:37 +03008964static void
8965nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8966{
8967 struct nfs4_layoutreturn *lrp = calldata;
8968
8969 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008970 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008971 &lrp->args.seq_args,
8972 &lrp->res.seq_res,
8973 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04008974 if (!pnfs_layout_is_valid(lrp->args.layout))
8975 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008976}
8977
8978static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8979{
8980 struct nfs4_layoutreturn *lrp = calldata;
8981 struct nfs_server *server;
8982
8983 dprintk("--> %s\n", __func__);
8984
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008985 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008986 return;
8987
8988 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008989 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008990 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04008991 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04008992 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05008993 lrp->args.inode))
8994 goto out_restart;
8995 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008996 default:
8997 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008998 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008999 case 0:
9000 break;
9001 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009002 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009003 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009004 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009005 }
Benny Halevycbe82602011-05-22 19:52:37 +03009006 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009007 return;
9008out_restart:
9009 task->tk_status = 0;
9010 nfs4_sequence_free_slot(&lrp->res.seq_res);
9011 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009012}
9013
9014static void nfs4_layoutreturn_release(void *calldata)
9015{
9016 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009017 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009018
9019 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009020 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009021 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009022 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009023 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9024 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009025 pnfs_put_layout_hdr(lrp->args.layout);
9026 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009027 kfree(calldata);
9028 dprintk("<-- %s\n", __func__);
9029}
9030
9031static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9032 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9033 .rpc_call_done = nfs4_layoutreturn_done,
9034 .rpc_release = nfs4_layoutreturn_release,
9035};
9036
Peng Tao6c166052014-11-17 09:30:40 +08009037int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009038{
9039 struct rpc_task *task;
9040 struct rpc_message msg = {
9041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9042 .rpc_argp = &lrp->args,
9043 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009044 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009045 };
9046 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009047 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009048 .rpc_message = &msg,
9049 .callback_ops = &nfs4_layoutreturn_call_ops,
9050 .callback_data = lrp,
9051 };
Peng Tao6c166052014-11-17 09:30:40 +08009052 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009053
Andrew Elble99ade3c2015-12-02 09:39:51 -05009054 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9055 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9056 &task_setup_data.rpc_client, &msg);
9057
Benny Halevycbe82602011-05-22 19:52:37 +03009058 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009059 if (!sync) {
9060 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9061 if (!lrp->inode) {
9062 nfs4_layoutreturn_release(lrp);
9063 return -EAGAIN;
9064 }
9065 task_setup_data.flags |= RPC_TASK_ASYNC;
9066 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009067 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009068 task = rpc_run_task(&task_setup_data);
9069 if (IS_ERR(task))
9070 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009071 if (sync)
9072 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009073 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009074 dprintk("<-- %s status=%d\n", __func__, status);
9075 rpc_put_task(task);
9076 return status;
9077}
9078
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009079static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009080_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9081 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009082 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009083{
9084 struct nfs4_getdeviceinfo_args args = {
9085 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009086 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9087 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009088 };
9089 struct nfs4_getdeviceinfo_res res = {
9090 .pdev = pdev,
9091 };
9092 struct rpc_message msg = {
9093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9094 .rpc_argp = &args,
9095 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009096 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009097 };
9098 int status;
9099
9100 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009101 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009102 if (res.notification & ~args.notify_types)
9103 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009104 if (res.notification != args.notify_types)
9105 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009106
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009107 dprintk("<-- %s status=%d\n", __func__, status);
9108
9109 return status;
9110}
9111
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009112int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9113 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009114 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009115{
9116 struct nfs4_exception exception = { };
9117 int err;
9118
9119 do {
9120 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009121 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009122 &exception);
9123 } while (exception.retry);
9124 return err;
9125}
9126EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9127
Andy Adamson863a3c62011-03-23 13:27:54 +00009128static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9129{
9130 struct nfs4_layoutcommit_data *data = calldata;
9131 struct nfs_server *server = NFS_SERVER(data->args.inode);
9132
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009133 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009134 &data->args.seq_args,
9135 &data->res.seq_res,
9136 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009137}
9138
9139static void
9140nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9141{
9142 struct nfs4_layoutcommit_data *data = calldata;
9143 struct nfs_server *server = NFS_SERVER(data->args.inode);
9144
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009145 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009146 return;
9147
9148 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009149 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9150 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9151 case -NFS4ERR_BADLAYOUT: /* no layout */
9152 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009153 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009154 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009155 break;
9156 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009157 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009158 rpc_restart_call_prepare(task);
9159 return;
9160 }
9161 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009162}
9163
9164static void nfs4_layoutcommit_release(void *calldata)
9165{
9166 struct nfs4_layoutcommit_data *data = calldata;
9167
Andy Adamsondb29c082011-07-30 20:52:38 -04009168 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009169 nfs_post_op_update_inode_force_wcc(data->args.inode,
9170 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009171 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009172 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009173 kfree(data);
9174}
9175
9176static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9177 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9178 .rpc_call_done = nfs4_layoutcommit_done,
9179 .rpc_release = nfs4_layoutcommit_release,
9180};
9181
9182int
Andy Adamsonef311532011-03-12 02:58:10 -05009183nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009184{
9185 struct rpc_message msg = {
9186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9187 .rpc_argp = &data->args,
9188 .rpc_resp = &data->res,
9189 .rpc_cred = data->cred,
9190 };
9191 struct rpc_task_setup task_setup_data = {
9192 .task = &data->task,
9193 .rpc_client = NFS_CLIENT(data->args.inode),
9194 .rpc_message = &msg,
9195 .callback_ops = &nfs4_layoutcommit_ops,
9196 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009197 };
9198 struct rpc_task *task;
9199 int status = 0;
9200
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009201 dprintk("NFS: initiating layoutcommit call. sync %d "
9202 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009203 data->args.lastbytewritten,
9204 data->args.inode->i_ino);
9205
Trond Myklebust472e2592015-02-05 16:50:30 -05009206 if (!sync) {
9207 data->inode = nfs_igrab_and_active(data->args.inode);
9208 if (data->inode == NULL) {
9209 nfs4_layoutcommit_release(data);
9210 return -EAGAIN;
9211 }
9212 task_setup_data.flags = RPC_TASK_ASYNC;
9213 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009214 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009215 task = rpc_run_task(&task_setup_data);
9216 if (IS_ERR(task))
9217 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009218 if (sync)
9219 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009220 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009221 dprintk("%s: status %d\n", __func__, status);
9222 rpc_put_task(task);
9223 return status;
9224}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009225
Trond Myklebust302fad72019-02-18 13:32:38 -05009226/*
Andy Adamson97431202013-08-08 10:57:56 -04009227 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9228 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9229 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009230static int
9231_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009232 struct nfs_fsinfo *info,
9233 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009234{
9235 struct nfs41_secinfo_no_name_args args = {
9236 .style = SECINFO_STYLE_CURRENT_FH,
9237 };
9238 struct nfs4_secinfo_res res = {
9239 .flavors = flavors,
9240 };
9241 struct rpc_message msg = {
9242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9243 .rpc_argp = &args,
9244 .rpc_resp = &res,
9245 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009246 struct rpc_clnt *clnt = server->client;
NeilBrowna52458b2018-12-03 11:30:31 +11009247 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009248 int status;
9249
9250 if (use_integrity) {
9251 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009252 cred = nfs4_get_clid_cred(server->nfs_client);
9253 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009254 }
9255
9256 dprintk("--> %s\n", __func__);
9257 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9258 &res.seq_res, 0);
9259 dprintk("<-- %s status=%d\n", __func__, status);
9260
NeilBrowna52458b2018-12-03 11:30:31 +11009261 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009262
9263 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009264}
9265
9266static int
9267nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9268 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9269{
9270 struct nfs4_exception exception = { };
9271 int err;
9272 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009273 /* first try using integrity protection */
9274 err = -NFS4ERR_WRONGSEC;
9275
9276 /* try to use integrity protection with machine cred */
9277 if (_nfs4_is_integrity_protected(server->nfs_client))
9278 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9279 flavors, true);
9280
9281 /*
9282 * if unable to use integrity protection, or SECINFO with
9283 * integrity protection returns NFS4ERR_WRONGSEC (which is
9284 * disallowed by spec, but exists in deployed servers) use
9285 * the current filesystem's rpc_client and the user cred.
9286 */
9287 if (err == -NFS4ERR_WRONGSEC)
9288 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9289 flavors, false);
9290
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009291 switch (err) {
9292 case 0:
9293 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009294 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009295 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009296 default:
9297 err = nfs4_handle_exception(server, err, &exception);
9298 }
9299 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009300out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009301 return err;
9302}
9303
9304static int
9305nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9306 struct nfs_fsinfo *info)
9307{
9308 int err;
9309 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009310 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009311 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009312 struct nfs4_secinfo4 *secinfo;
9313 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009314
9315 page = alloc_page(GFP_KERNEL);
9316 if (!page) {
9317 err = -ENOMEM;
9318 goto out;
9319 }
9320
9321 flavors = page_address(page);
9322 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9323
9324 /*
9325 * Fall back on "guess and check" method if
9326 * the server doesn't support SECINFO_NO_NAME
9327 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009328 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009329 err = nfs4_find_root_sec(server, fhandle, info);
9330 goto out_freepage;
9331 }
9332 if (err)
9333 goto out_freepage;
9334
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009335 for (i = 0; i < flavors->num_flavors; i++) {
9336 secinfo = &flavors->flavors[i];
9337
9338 switch (secinfo->flavor) {
9339 case RPC_AUTH_NULL:
9340 case RPC_AUTH_UNIX:
9341 case RPC_AUTH_GSS:
9342 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9343 &secinfo->flavor_info);
9344 break;
9345 default:
9346 flavor = RPC_AUTH_MAXFLAVOR;
9347 break;
9348 }
9349
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009350 if (!nfs_auth_info_match(&server->auth_info, flavor))
9351 flavor = RPC_AUTH_MAXFLAVOR;
9352
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009353 if (flavor != RPC_AUTH_MAXFLAVOR) {
9354 err = nfs4_lookup_root_sec(server, fhandle,
9355 info, flavor);
9356 if (!err)
9357 break;
9358 }
9359 }
9360
9361 if (flavor == RPC_AUTH_MAXFLAVOR)
9362 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009363
9364out_freepage:
9365 put_page(page);
9366 if (err == -EACCES)
9367 return -EPERM;
9368out:
9369 return err;
9370}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009371
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009372static int _nfs41_test_stateid(struct nfs_server *server,
9373 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009374 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009375{
9376 int status;
9377 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009378 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009379 };
9380 struct nfs41_test_stateid_res res;
9381 struct rpc_message msg = {
9382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9383 .rpc_argp = &args,
9384 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009385 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009386 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009387 struct rpc_clnt *rpc_client = server->client;
9388
9389 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9390 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009391
Chuck Lever38527b12012-07-11 16:30:23 -04009392 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009393 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009394 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009395 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009396 if (status != NFS_OK) {
9397 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009398 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009399 }
9400 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009401 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009402}
9403
Trond Myklebust43912bb2016-09-22 13:38:56 -04009404static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9405 int err, struct nfs4_exception *exception)
9406{
9407 exception->retry = 0;
9408 switch(err) {
9409 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009410 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009411 nfs4_handle_exception(server, err, exception);
9412 break;
9413 case -NFS4ERR_BADSESSION:
9414 case -NFS4ERR_BADSLOT:
9415 case -NFS4ERR_BAD_HIGH_SLOT:
9416 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9417 case -NFS4ERR_DEADSESSION:
9418 nfs4_do_handle_exception(server, err, exception);
9419 }
9420}
9421
Chuck Lever38527b12012-07-11 16:30:23 -04009422/**
9423 * nfs41_test_stateid - perform a TEST_STATEID operation
9424 *
9425 * @server: server / transport on which to perform the operation
9426 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009427 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009428 *
9429 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9430 * Otherwise a negative NFS4ERR value is returned if the operation
9431 * failed or the state ID is not currently valid.
9432 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009433static int nfs41_test_stateid(struct nfs_server *server,
9434 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009435 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009436{
9437 struct nfs4_exception exception = { };
9438 int err;
9439 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009440 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009441 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009442 } while (exception.retry);
9443 return err;
9444}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009445
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009446struct nfs_free_stateid_data {
9447 struct nfs_server *server;
9448 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009449 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009450};
9451
9452static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9453{
9454 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009455 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009456 &data->args.seq_args,
9457 &data->res.seq_res,
9458 task);
9459}
9460
9461static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9462{
9463 struct nfs_free_stateid_data *data = calldata;
9464
9465 nfs41_sequence_done(task, &data->res.seq_res);
9466
9467 switch (task->tk_status) {
9468 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009469 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009470 rpc_restart_call_prepare(task);
9471 }
9472}
9473
9474static void nfs41_free_stateid_release(void *calldata)
9475{
9476 kfree(calldata);
9477}
9478
Trond Myklebust17f26b12013-08-21 15:48:42 -04009479static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009480 .rpc_call_prepare = nfs41_free_stateid_prepare,
9481 .rpc_call_done = nfs41_free_stateid_done,
9482 .rpc_release = nfs41_free_stateid_release,
9483};
9484
Anna Schumaker2f261022018-05-15 13:03:39 -04009485/**
9486 * nfs41_free_stateid - perform a FREE_STATEID operation
9487 *
9488 * @server: server / transport on which to perform the operation
9489 * @stateid: state ID to release
9490 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009491 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009492 *
9493 * Note: this function is always asynchronous.
9494 */
9495static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009496 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009497 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009498 bool privileged)
9499{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009500 struct rpc_message msg = {
9501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009502 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009503 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009504 struct rpc_task_setup task_setup = {
9505 .rpc_client = server->client,
9506 .rpc_message = &msg,
9507 .callback_ops = &nfs41_free_stateid_ops,
9508 .flags = RPC_TASK_ASYNC,
9509 };
9510 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009511 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009512
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009513 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9514 &task_setup.rpc_client, &msg);
9515
Chuck Lever38527b12012-07-11 16:30:23 -04009516 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009517 data = kmalloc(sizeof(*data), GFP_NOFS);
9518 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009519 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009520 data->server = server;
9521 nfs4_stateid_copy(&data->args.stateid, stateid);
9522
9523 task_setup.callback_data = data;
9524
9525 msg.rpc_argp = &data->args;
9526 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009527 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009528 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009529 if (IS_ERR(task))
9530 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009531 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009532 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009533}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009534
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009535static void
9536nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009537{
NeilBrowna52458b2018-12-03 11:30:31 +11009538 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009539
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009540 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009541 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009542}
9543
Trond Myklebust36281ca2012-03-04 18:13:56 -05009544static bool nfs41_match_stateid(const nfs4_stateid *s1,
9545 const nfs4_stateid *s2)
9546{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009547 if (s1->type != s2->type)
9548 return false;
9549
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009550 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009551 return false;
9552
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009553 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009554 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009555
Anna Schumaker045c5512017-01-11 16:59:48 -05009556 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009557}
9558
Andy Adamson557134a2009-04-01 09:21:53 -04009559#endif /* CONFIG_NFS_V4_1 */
9560
Trond Myklebust36281ca2012-03-04 18:13:56 -05009561static bool nfs4_match_stateid(const nfs4_stateid *s1,
9562 const nfs4_stateid *s2)
9563{
Trond Myklebustf597c532012-03-04 18:13:56 -05009564 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009565}
9566
9567
Trond Myklebust17280172012-03-11 13:11:00 -04009568static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009569 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009570 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009571 .recover_open = nfs4_open_reclaim,
9572 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009573 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009574 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009575};
9576
Andy Adamson591d71c2009-04-01 09:22:47 -04009577#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009578static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009579 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9580 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9581 .recover_open = nfs4_open_reclaim,
9582 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009583 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009584 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009585 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009586};
9587#endif /* CONFIG_NFS_V4_1 */
9588
Trond Myklebust17280172012-03-11 13:11:00 -04009589static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009590 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009591 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009592 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009593 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009594 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009595};
9596
Andy Adamson591d71c2009-04-01 09:22:47 -04009597#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009598static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009599 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9600 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009601 .recover_open = nfs41_open_expired,
9602 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009603 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009604};
9605#endif /* CONFIG_NFS_V4_1 */
9606
Trond Myklebust17280172012-03-11 13:11:00 -04009607static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009608 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009609 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009610 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009611};
9612
9613#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009614static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009615 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009616 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009617 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009618};
9619#endif
9620
Chuck Leverec011fe2013-10-17 14:12:39 -04009621static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009622 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009623 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009624};
9625
9626#if defined(CONFIG_NFS_V4_1)
9627static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009628 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009629 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009630};
9631#endif /* CONFIG_NFS_V4_1 */
9632
Trond Myklebust97dc1352010-06-16 09:52:26 -04009633static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9634 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009635 .init_caps = NFS_CAP_READDIRPLUS
9636 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009637 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009638 .init_client = nfs40_init_client,
9639 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009640 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009641 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009642 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009643 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009644 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009645 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009646 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9647 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9648 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009649 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009650};
9651
9652#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009653static struct nfs_seqid *
9654nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9655{
9656 return NULL;
9657}
9658
Trond Myklebust97dc1352010-06-16 09:52:26 -04009659static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9660 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009661 .init_caps = NFS_CAP_READDIRPLUS
9662 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009663 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009664 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009665 | NFS_CAP_ATOMIC_OPEN_V1
9666 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009667 .init_client = nfs41_init_client,
9668 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009669 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009670 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009671 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009672 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009673 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009674 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009675 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009676 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9677 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9678 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009679 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009680};
9681#endif
9682
Steve Dickson42c2c422013-05-22 12:50:38 -04009683#if defined(CONFIG_NFS_V4_2)
9684static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9685 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009686 .init_caps = NFS_CAP_READDIRPLUS
9687 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009688 | NFS_CAP_POSIX_LOCK
9689 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009690 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009691 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009692 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009693 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009694 | NFS_CAP_OFFLOAD_CANCEL
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009695 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009696 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009697 | NFS_CAP_LAYOUTSTATS
9698 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009699 .init_client = nfs41_init_client,
9700 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009701 .match_stateid = nfs41_match_stateid,
9702 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009703 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009704 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009705 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009706 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009707 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009708 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9709 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9710 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009711 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009712};
9713#endif
9714
Trond Myklebust97dc1352010-06-16 09:52:26 -04009715const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9716 [0] = &nfs_v4_0_minor_ops,
9717#if defined(CONFIG_NFS_V4_1)
9718 [1] = &nfs_v4_1_minor_ops,
9719#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009720#if defined(CONFIG_NFS_V4_2)
9721 [2] = &nfs_v4_2_minor_ops,
9722#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009723};
9724
Trond Myklebust13997822016-07-24 17:10:52 -04009725static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009726{
9727 ssize_t error, error2;
9728
9729 error = generic_listxattr(dentry, list, size);
9730 if (error < 0)
9731 return error;
9732 if (list) {
9733 list += error;
9734 size -= error;
9735 }
9736
9737 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9738 if (error2 < 0)
9739 return error2;
9740 return error + error2;
9741}
9742
Trond Myklebust17f26b12013-08-21 15:48:42 -04009743static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009744 .create = nfs_create,
9745 .lookup = nfs_lookup,
9746 .atomic_open = nfs_atomic_open,
9747 .link = nfs_link,
9748 .unlink = nfs_unlink,
9749 .symlink = nfs_symlink,
9750 .mkdir = nfs_mkdir,
9751 .rmdir = nfs_rmdir,
9752 .mknod = nfs_mknod,
9753 .rename = nfs_rename,
9754 .permission = nfs_permission,
9755 .getattr = nfs_getattr,
9756 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009757 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009758};
9759
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009760static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009761 .permission = nfs_permission,
9762 .getattr = nfs_getattr,
9763 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009764 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009765};
9766
David Howells509de812006-08-22 20:06:11 -04009767const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009768 .version = 4, /* protocol version */
9769 .dentry_ops = &nfs4_dentry_operations,
9770 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009771 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009772 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009774 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009775 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 .getattr = nfs4_proc_getattr,
9777 .setattr = nfs4_proc_setattr,
9778 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009779 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780 .access = nfs4_proc_access,
9781 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782 .create = nfs4_proc_create,
9783 .remove = nfs4_proc_remove,
9784 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009785 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009787 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009788 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009789 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790 .link = nfs4_proc_link,
9791 .symlink = nfs4_proc_symlink,
9792 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009793 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794 .readdir = nfs4_proc_readdir,
9795 .mknod = nfs4_proc_mknod,
9796 .statfs = nfs4_proc_statfs,
9797 .fsinfo = nfs4_proc_fsinfo,
9798 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009799 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009801 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009803 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009804 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009805 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009807 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009808 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009809 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009810 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009811 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009812 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009813 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009814 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009815 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009816 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009817 .create_server = nfs4_create_server,
9818 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009819};
9820
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009821static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009822 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009823 .list = nfs4_xattr_list_nfs4_acl,
9824 .get = nfs4_xattr_get_nfs4_acl,
9825 .set = nfs4_xattr_set_nfs4_acl,
9826};
9827
9828const struct xattr_handler *nfs4_xattr_handlers[] = {
9829 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009830#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9831 &nfs4_xattr_nfs4_label_handler,
9832#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009833 NULL
9834};
9835
Linus Torvalds1da177e2005-04-16 15:20:36 -07009836/*
9837 * Local variables:
9838 * c-basic-offset: 8
9839 * End:
9840 */