blob: 73889ea7d196a43fb4e4a4f6643eae9041edc63b [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 Myklebust3453d572018-06-20 17:53:34 -0400733static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
734 u32 seqnr)
735{
736 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
737 slot->seq_nr_highest_sent = seqnr;
738}
739static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
740 u32 seqnr)
741{
742 slot->seq_nr_highest_sent = seqnr;
743 slot->seq_nr_last_acked = seqnr;
744}
745
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400746static int nfs41_sequence_process(struct rpc_task *task,
747 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400748{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500749 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500750 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400751 struct nfs_client *clp;
Trond Myklebust85563072012-12-11 10:31:12 -0500752 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400753
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500754 if (slot == NULL)
755 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400756 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500757 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400758 goto out;
759
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500760 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500761
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400762 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500763 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400764 switch (res->sr_status) {
765 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400766 /* Mark this sequence number as having been acked */
767 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400768 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400769 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500770 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500771 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500772 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400773 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
774 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500775 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500777 case 1:
778 /*
779 * sr_status remains 1 if an RPC level error occurred.
780 * The server may or may not have processed the sequence
781 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500782 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400783 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
784 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500785 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400786 case -NFS4ERR_DELAY:
787 /* The server detected a resend of the RPC call and
788 * returned NFS4ERR_DELAY as per Section 2.10.6.2
789 * of RFC5661.
790 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500791 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400792 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500793 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500794 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400795 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400796 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400797 case -NFS4ERR_RETRY_UNCACHED_REP:
798 case -NFS4ERR_SEQ_FALSE_RETRY:
799 /*
800 * The server thinks we tried to replay a request.
801 * Retry the call after bumping the sequence ID.
802 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400803 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400804 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500805 case -NFS4ERR_BADSLOT:
806 /*
807 * The slot id we used was probably retired. Try again
808 * using a different slot id.
809 */
Trond Myklebust99589102018-06-09 12:50:50 -0400810 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400811 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500812 goto retry_nowait;
813 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400814 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500815 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400816 * Were one or more calls using this slot interrupted?
817 * If the server never received the request, then our
818 * transmitted slot sequence number may be too high.
Trond Myklebustac20d162012-12-15 15:36:07 -0500819 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400820 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
821 slot->seq_nr--;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500822 goto retry_nowait;
823 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400824 /*
825 * RFC5661:
826 * A retry might be sent while the original request is
827 * still in progress on the replier. The replier SHOULD
828 * deal with the issue by returning NFS4ERR_DELAY as the
829 * reply to SEQUENCE or CB_SEQUENCE operation, but
830 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
831 *
832 * Restart the search after a delay.
833 */
834 slot->seq_nr = slot->seq_nr_highest_sent;
835 goto out_retry;
Trond Myklebust14516c32010-07-31 14:29:06 -0400836 default:
837 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400838 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400839 }
840out:
841 /* The session may be reset by one of the error handlers. */
842 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500843out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500844 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400845session_recover:
846 nfs4_schedule_session_recovery(session, res->sr_status);
847 goto retry_nowait;
848retry_new_seq:
849 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500850retry_nowait:
851 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400852 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500853 task->tk_status = 0;
854 ret = 0;
855 }
856 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400857out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400858 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400859 goto out;
860 rpc_delay(task, NFS4_POLL_RETRY_MAX);
861 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400862}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400863
864int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
865{
866 if (!nfs41_sequence_process(task, res))
867 return 0;
868 if (res->sr_slot != NULL)
869 nfs41_sequence_free_slot(res);
870 return 1;
871
872}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500873EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400874
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400875static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
876{
877 if (res->sr_slot == NULL)
878 return 1;
879 if (res->sr_slot->table->session != NULL)
880 return nfs41_sequence_process(task, res);
881 return nfs40_sequence_done(task, res);
882}
883
884static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
885{
886 if (res->sr_slot != NULL) {
887 if (res->sr_slot->table->session != NULL)
888 nfs41_sequence_free_slot(res);
889 else
890 nfs40_sequence_free_slot(res);
891 }
892}
893
Peng Tao2c4b1312014-06-11 05:24:16 +0800894int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400895{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500896 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400897 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400898 if (!res->sr_slot->table->session)
899 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400900 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400901}
Peng Tao2c4b1312014-06-11 05:24:16 +0800902EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400903
Andy Adamsonce5039c2009-04-01 09:22:13 -0400904static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
905{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400906 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400907
Trond Myklebust035168ab2010-06-16 09:52:26 -0400908 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
909
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500910 nfs4_setup_sequence(data->seq_server->nfs_client,
911 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400912}
913
Andy Adamson69ab40c2009-04-01 09:22:19 -0400914static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
915{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400916 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400917
Trond Myklebust14516c32010-07-31 14:29:06 -0400918 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400919}
920
Trond Myklebust17280172012-03-11 13:11:00 -0400921static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400922 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400923 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400924};
925
Chuck Lever3bd23842013-08-09 12:49:19 -0400926#else /* !CONFIG_NFS_V4_1 */
927
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400928static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
929{
930 return nfs40_sequence_done(task, res);
931}
932
933static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
934{
935 if (res->sr_slot != NULL)
936 nfs40_sequence_free_slot(res);
937}
938
Peng Tao2c4b1312014-06-11 05:24:16 +0800939int nfs4_sequence_done(struct rpc_task *task,
940 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400941{
Chuck Lever3bd23842013-08-09 12:49:19 -0400942 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400943}
Peng Tao2c4b1312014-06-11 05:24:16 +0800944EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400945
946#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400947
Trond Myklebustc1dffe02019-03-01 12:13:34 -0500948static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
949{
950 res->sr_timestamp = jiffies;
951 res->sr_status_flags = 0;
952 res->sr_status = 1;
953}
954
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400955static
956void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
957 struct nfs4_sequence_res *res,
958 struct nfs4_slot *slot)
959{
960 if (!slot)
961 return;
962 slot->privileged = args->sa_privileged ? 1 : 0;
963 args->sa_slot = slot;
964
965 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400966}
967
968int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500969 struct nfs4_sequence_args *args,
970 struct nfs4_sequence_res *res,
971 struct rpc_task *task)
972{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500973 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500974 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500975 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500976
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500977 /* slot already allocated? */
978 if (res->sr_slot != NULL)
979 goto out_start;
980
Anna Schumaker76ee0352017-01-10 16:49:31 -0500981 if (session) {
982 tbl = &session->fc_slot_table;
983 task->tk_timeout = 0;
984 }
985
Trond Myklebust3453d572018-06-20 17:53:34 -0400986 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 Myklebust3453d572018-06-20 17:53:34 -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;
Anna Schumaker3d358082017-01-11 10:54:04 -0500997 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400998 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500999
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001000 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001001
Anna Schumakerad05cc02017-01-11 13:37:06 -05001002 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001003out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001004 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001005 rpc_call_start(task);
1006 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001007
1008out_sleep:
1009 if (args->sa_privileged)
1010 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1011 NULL, RPC_PRIORITY_PRIVILEGED);
1012 else
1013 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1014 spin_unlock(&tbl->slot_tbl_lock);
1015 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001016}
1017EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1018
Chuck Lever9915ea72013-08-09 12:48:27 -04001019static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1020{
1021 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001022 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001023 data->seq_args, data->seq_res, task);
1024}
1025
1026static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1027{
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_sequence_done(task, data->seq_res);
1030}
1031
1032static const struct rpc_call_ops nfs40_call_sync_ops = {
1033 .rpc_call_prepare = nfs40_call_sync_prepare,
1034 .rpc_call_done = nfs40_call_sync_done,
1035};
1036
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001037static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001038 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001040 struct nfs4_sequence_args *args,
1041 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001042{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 int ret;
1044 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001045 struct nfs_client *clp = server->nfs_client;
1046 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 .seq_server = server,
1048 .seq_args = args,
1049 .seq_res = res,
1050 };
1051 struct rpc_task_setup task_setup = {
1052 .rpc_client = clnt,
1053 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001054 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001055 .callback_data = &data
1056 };
1057
1058 task = rpc_run_task(&task_setup);
1059 if (IS_ERR(task))
1060 ret = PTR_ERR(task);
1061 else {
1062 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001063 rpc_put_task(task);
1064 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001065 return ret;
1066}
1067
Bryan Schumaker7c513052011-03-24 17:12:24 +00001068int nfs4_call_sync(struct rpc_clnt *clnt,
1069 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001070 struct rpc_message *msg,
1071 struct nfs4_sequence_args *args,
1072 struct nfs4_sequence_res *res,
1073 int cache_reply)
1074{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001075 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001076 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001077}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Trond Myklebust3c591172018-07-31 15:54:10 -04001079static void
1080nfs4_inc_nlink_locked(struct inode *inode)
1081{
1082 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1083 inc_nlink(inode);
1084}
1085
1086static void
1087nfs4_dec_nlink_locked(struct inode *inode)
1088{
1089 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1090 drop_nlink(inode);
1091}
1092
1093static void
1094update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001095 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 struct nfs_inode *nfsi = NFS_I(dir);
1098
Trond Myklebust16e14372018-03-20 16:53:31 -04001099 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1100 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001101 | NFS_INO_INVALID_DATA
1102 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001103 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001104 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1105 nfsi->attrtimeo_timestamp = jiffies;
1106 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001108 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001109 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1110 NFS_INO_INVALID_ACL;
1111 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001112 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001113 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001114 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001115 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001116 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001117}
1118
1119static void
1120update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001121 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001122{
1123 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001124 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 spin_unlock(&dir->i_lock);
1126}
1127
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001128struct nfs4_open_createattrs {
1129 struct nfs4_label *label;
1130 struct iattr *sattr;
1131 const __u32 verf[2];
1132};
1133
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001134static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1135 int err, struct nfs4_exception *exception)
1136{
1137 if (err != -EINVAL)
1138 return false;
1139 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1140 return false;
1141 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1142 exception->retry = 1;
1143 return true;
1144}
1145
Trond Myklebust6ae37332015-01-30 14:21:14 -05001146static u32
1147nfs4_map_atomic_open_share(struct nfs_server *server,
1148 fmode_t fmode, int openflags)
1149{
1150 u32 res = 0;
1151
1152 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1153 case FMODE_READ:
1154 res = NFS4_SHARE_ACCESS_READ;
1155 break;
1156 case FMODE_WRITE:
1157 res = NFS4_SHARE_ACCESS_WRITE;
1158 break;
1159 case FMODE_READ|FMODE_WRITE:
1160 res = NFS4_SHARE_ACCESS_BOTH;
1161 }
1162 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1163 goto out;
1164 /* Want no delegation if we're using O_DIRECT */
1165 if (openflags & O_DIRECT)
1166 res |= NFS4_SHARE_WANT_NO_DELEG;
1167out:
1168 return res;
1169}
1170
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001171static enum open_claim_type4
1172nfs4_map_atomic_open_claim(struct nfs_server *server,
1173 enum open_claim_type4 claim)
1174{
1175 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1176 return claim;
1177 switch (claim) {
1178 default:
1179 return claim;
1180 case NFS4_OPEN_CLAIM_FH:
1181 return NFS4_OPEN_CLAIM_NULL;
1182 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1183 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1184 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1185 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1186 }
1187}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001190{
1191 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001192 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 p->o_res.seqid = p->o_arg.seqid;
1194 p->c_res.seqid = p->c_arg.seqid;
1195 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001196 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001198 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001199}
1200
Al Viro82a2c1b2011-06-22 18:30:55 -04001201static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001203 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001204 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001205 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001206{
Al Viro82a2c1b2011-06-22 18:30:55 -04001207 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001208 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001209 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001210 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001211 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001212 struct nfs4_opendata *p;
1213
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001214 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001215 if (p == NULL)
1216 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001217
1218 p->f_label = nfs4_label_alloc(server, gfp_mask);
1219 if (IS_ERR(p->f_label))
1220 goto err_free_p;
1221
Kinglong Meea49c2692015-07-27 15:31:38 +08001222 p->a_label = nfs4_label_alloc(server, gfp_mask);
1223 if (IS_ERR(p->a_label))
1224 goto err_free_f;
1225
Trond Myklebust63f5f792015-01-23 19:19:25 -05001226 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1227 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001228 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001229 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001230 nfs_sb_active(dentry->d_sb);
1231 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001232 p->dir = parent;
1233 p->owner = sp;
1234 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001235 p->o_arg.open_flags = flags;
1236 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001237 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001238 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001239 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1240 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001241 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1242 * will return permission denied for all bits until close */
1243 if (!(flags & O_EXCL)) {
1244 /* ask server to check for all possible rights as results
1245 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001246 switch (p->o_arg.claim) {
1247 default:
1248 break;
1249 case NFS4_OPEN_CLAIM_NULL:
1250 case NFS4_OPEN_CLAIM_FH:
1251 p->o_arg.access = NFS4_ACCESS_READ |
1252 NFS4_ACCESS_MODIFY |
1253 NFS4_ACCESS_EXTEND |
1254 NFS4_ACCESS_EXECUTE;
1255 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001256 }
David Howells7539bba2006-08-22 20:06:09 -04001257 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001258 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1259 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001260 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001261 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001262 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001263 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001264 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001265 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001266 case NFS4_OPEN_CLAIM_NULL:
1267 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1268 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1269 p->o_arg.fh = NFS_FH(dir);
1270 break;
1271 case NFS4_OPEN_CLAIM_PREVIOUS:
1272 case NFS4_OPEN_CLAIM_FH:
1273 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1274 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001275 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001276 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001277 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001279 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001280
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001281 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001282 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001284 p->c_arg.fh = &p->o_res.fh;
1285 p->c_arg.stateid = &p->o_res.stateid;
1286 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001287 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001288 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001289 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001290
1291err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001292 nfs4_label_free(p->a_label);
1293err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001294 nfs4_label_free(p->f_label);
1295err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001296 kfree(p);
1297err:
1298 dput(parent);
1299 return NULL;
1300}
1301
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001302static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001303{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 struct nfs4_opendata *p = container_of(kref,
1305 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001306 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001307
Fred Isaman30ae2412016-10-18 13:39:51 -04001308 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001310 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001311 if (p->state != NULL)
1312 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001313 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001314
Kinglong Meea49c2692015-07-27 15:31:38 +08001315 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001316 nfs4_label_free(p->f_label);
1317
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001318 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001319 dput(p->dentry);
1320 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001321 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001322 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001323 kfree(p);
1324}
1325
1326static void nfs4_opendata_put(struct nfs4_opendata *p)
1327{
1328 if (p != NULL)
1329 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001330}
1331
Trond Myklebust24311f82015-09-20 10:50:17 -04001332static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1333 fmode_t fmode)
1334{
1335 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1336 case FMODE_READ|FMODE_WRITE:
1337 return state->n_rdwr != 0;
1338 case FMODE_WRITE:
1339 return state->n_wronly != 0;
1340 case FMODE_READ:
1341 return state->n_rdonly != 0;
1342 }
1343 WARN_ON_ONCE(1);
1344 return false;
1345}
1346
Trond Myklebustbe189f72018-09-27 17:12:33 -04001347static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1348 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001349{
1350 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001351
Trond Myklebust536e43d2012-01-17 22:04:26 -05001352 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001354 switch (claim) {
1355 case NFS4_OPEN_CLAIM_NULL:
1356 case NFS4_OPEN_CLAIM_FH:
1357 goto out;
1358 default:
1359 break;
1360 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001361 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001362 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001363 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1364 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001365 break;
1366 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001367 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1368 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001369 break;
1370 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001371 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1372 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001373 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001374out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001375 return ret;
1376}
1377
Trond Myklebust2a606182015-08-19 22:30:00 -05001378static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1379 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001380{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001381 if (delegation == NULL)
1382 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001383 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001384 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001385 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1386 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001387 switch (claim) {
1388 case NFS4_OPEN_CLAIM_NULL:
1389 case NFS4_OPEN_CLAIM_FH:
1390 break;
1391 case NFS4_OPEN_CLAIM_PREVIOUS:
1392 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1393 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001394 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001395 default:
1396 return 0;
1397 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001398 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001399 return 1;
1400}
1401
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001402static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001403{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001404 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001405 case FMODE_WRITE:
1406 state->n_wronly++;
1407 break;
1408 case FMODE_READ:
1409 state->n_rdonly++;
1410 break;
1411 case FMODE_READ|FMODE_WRITE:
1412 state->n_rdwr++;
1413 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001414 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001415}
1416
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001417#ifdef CONFIG_NFS_V4_1
1418static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1419{
1420 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1421 return true;
1422 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1423 return true;
1424 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1425 return true;
1426 return false;
1427}
1428#endif /* CONFIG_NFS_V4_1 */
1429
Trond Myklebustc9399f22017-11-06 15:28:01 -05001430static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1431{
1432 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1433 wake_up_all(&state->waitq);
1434}
1435
1436static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1437 const nfs4_stateid *stateid)
1438{
1439 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1440 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1441
1442 if (stateid_seqid == state_seqid + 1U ||
1443 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1444 nfs_state_log_update_open_stateid(state);
1445 else
1446 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1447}
1448
Trond Myklebust4f14c192014-02-12 19:15:06 -05001449static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001450{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001451 struct nfs_client *clp = state->owner->so_server->nfs_client;
1452 bool need_recover = false;
1453
1454 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1455 need_recover = true;
1456 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1457 need_recover = true;
1458 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1459 need_recover = true;
1460 if (need_recover)
1461 nfs4_state_mark_reclaim_nograce(clp, state);
1462}
1463
Trond Myklebustc9399f22017-11-06 15:28:01 -05001464/*
1465 * Check for whether or not the caller may update the open stateid
1466 * to the value passed in by stateid.
1467 *
1468 * Note: This function relies heavily on the server implementing
1469 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1470 * correctly.
1471 * i.e. The stateid seqids have to be initialised to 1, and
1472 * are then incremented on every state transition.
1473 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001474static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001475 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001476{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001477 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1478 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1479 if (stateid->seqid == cpu_to_be32(1))
1480 nfs_state_log_update_open_stateid(state);
1481 else
1482 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001483 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001484 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001485
1486 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1487 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001488 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001489 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001490 return false;
1491}
1492
Trond Myklebustf95549c2015-01-23 18:06:09 -05001493static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1494{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001495 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1496 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001497 if (state->n_wronly)
1498 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1499 if (state->n_rdonly)
1500 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1501 if (state->n_rdwr)
1502 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001503 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001504}
1505
Trond Myklebust226056c2014-02-11 10:41:07 -05001506static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1507 nfs4_stateid *stateid, fmode_t fmode)
1508{
1509 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1510 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1511 case FMODE_WRITE:
1512 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1513 break;
1514 case FMODE_READ:
1515 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1516 break;
1517 case 0:
1518 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1519 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1520 clear_bit(NFS_OPEN_STATE, &state->flags);
1521 }
1522 if (stateid == NULL)
1523 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001524 /* Handle OPEN+OPEN_DOWNGRADE races */
1525 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1526 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001527 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001528 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001529 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001530 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001531 nfs4_stateid_copy(&state->stateid, stateid);
1532 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001533 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001534out:
1535 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001536}
1537
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001538static void nfs_clear_open_stateid(struct nfs4_state *state,
1539 nfs4_stateid *arg_stateid,
1540 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001541{
1542 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001543 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1544 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1545 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001546 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001547 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1548 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001549}
1550
Trond Myklebust1393d962016-09-22 13:39:13 -04001551static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001552 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001553 __must_hold(&state->owner->so_lock)
1554 __must_hold(&state->seqlock)
1555 __must_hold(RCU)
1556
Trond Myklebust003707c2007-07-05 18:07:55 -04001557{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001558 DEFINE_WAIT(wait);
1559 int status = 0;
1560 for (;;) {
1561
1562 if (!nfs_need_update_open_stateid(state, stateid))
1563 return;
1564 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001565 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001566 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001567 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001568 /* Rely on seqids for serialisation with NFSv4.0 */
1569 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1570 break;
1571
1572 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1573 /*
1574 * Ensure we process the state changes in the same order
1575 * in which the server processed them by delaying the
1576 * update of the stateid until we are in sequence.
1577 */
1578 write_sequnlock(&state->seqlock);
1579 spin_unlock(&state->owner->so_lock);
1580 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001581 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001582 if (!signal_pending(current)) {
1583 if (schedule_timeout(5*HZ) == 0)
1584 status = -EAGAIN;
1585 else
1586 status = 0;
1587 } else
1588 status = -EINTR;
1589 finish_wait(&state->waitq, &wait);
1590 rcu_read_lock();
1591 spin_lock(&state->owner->so_lock);
1592 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001593 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001594
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001595 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1596 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001597 nfs4_stateid_copy(freeme, &state->open_stateid);
1598 nfs_test_and_clear_all_open_stateid(state);
1599 }
1600
Trond Myklebuste999e802014-02-10 18:20:47 -05001601 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1602 nfs4_stateid_copy(&state->stateid, stateid);
1603 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001604 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001605 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001606}
1607
Trond Myklebustc9399f22017-11-06 15:28:01 -05001608static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001609 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001610 fmode_t fmode,
1611 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001613 /*
1614 * Protect the call to nfs4_state_set_mode_locked and
1615 * serialise the stateid update
1616 */
1617 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001618 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1619 switch (fmode) {
1620 case FMODE_READ:
1621 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1622 break;
1623 case FMODE_WRITE:
1624 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1625 break;
1626 case FMODE_READ|FMODE_WRITE:
1627 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001628 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001629 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001630 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001631}
1632
1633static void nfs_state_set_delegation(struct nfs4_state *state,
1634 const nfs4_stateid *deleg_stateid,
1635 fmode_t fmode)
1636{
1637 /*
1638 * Protect the call to nfs4_state_set_mode_locked and
1639 * serialise the stateid update
1640 */
1641 write_seqlock(&state->seqlock);
1642 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1643 set_bit(NFS_DELEGATED_STATE, &state->flags);
1644 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001647static void nfs_state_clear_delegation(struct nfs4_state *state)
1648{
1649 write_seqlock(&state->seqlock);
1650 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1651 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1652 write_sequnlock(&state->seqlock);
1653}
1654
Trond Myklebust1393d962016-09-22 13:39:13 -04001655static int update_open_stateid(struct nfs4_state *state,
1656 const nfs4_stateid *open_stateid,
1657 const nfs4_stateid *delegation,
1658 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001659{
Trond Myklebust1393d962016-09-22 13:39:13 -04001660 struct nfs_server *server = NFS_SERVER(state->inode);
1661 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001662 struct nfs_inode *nfsi = NFS_I(state->inode);
1663 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001664 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001665 int ret = 0;
1666
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001667 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001668
1669 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001670 spin_lock(&state->owner->so_lock);
1671 if (open_stateid != NULL) {
1672 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1673 ret = 1;
1674 }
1675
Trond Myklebust34310432008-12-23 15:21:38 -05001676 deleg_cur = rcu_dereference(nfsi->delegation);
1677 if (deleg_cur == NULL)
1678 goto no_delegation;
1679
1680 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001681 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001682 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001683 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001684 goto no_delegation_unlock;
1685
1686 if (delegation == NULL)
1687 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001688 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001689 goto no_delegation_unlock;
1690
Trond Myklebustb7391f42008-12-23 15:21:52 -05001691 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001692 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001693 ret = 1;
1694no_delegation_unlock:
1695 spin_unlock(&deleg_cur->lock);
1696no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001697 if (ret)
1698 update_open_stateflags(state, fmode);
1699 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001700 rcu_read_unlock();
1701
Trond Myklebust4f14c192014-02-12 19:15:06 -05001702 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001703 nfs4_schedule_state_manager(clp);
1704 if (freeme.type != 0)
1705 nfs4_test_and_free_stateid(server, &freeme,
1706 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001707
1708 return ret;
1709}
1710
Trond Myklebust39071e62015-01-24 15:07:56 -05001711static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1712 const nfs4_stateid *stateid)
1713{
1714 struct nfs4_state *state = lsp->ls_state;
1715 bool ret = false;
1716
1717 spin_lock(&state->state_lock);
1718 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1719 goto out_noupdate;
1720 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1721 goto out_noupdate;
1722 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1723 ret = true;
1724out_noupdate:
1725 spin_unlock(&state->state_lock);
1726 return ret;
1727}
Trond Myklebust34310432008-12-23 15:21:38 -05001728
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001729static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001730{
1731 struct nfs_delegation *delegation;
1732
Trond Myklebustf5086242018-03-20 16:43:13 -04001733 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001734 rcu_read_lock();
1735 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001736 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001737 rcu_read_unlock();
1738 return;
1739 }
1740 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001741 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001742}
1743
Trond Myklebust6ee41262007-07-08 14:11:36 -04001744static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001745{
1746 struct nfs4_state *state = opendata->state;
1747 struct nfs_inode *nfsi = NFS_I(state->inode);
1748 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001749 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001750 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001751 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001752 nfs4_stateid stateid;
1753 int ret = -EAGAIN;
1754
Trond Myklebustaac00a82007-07-05 19:02:21 -04001755 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001756 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001757 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001758 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001759 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001760 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001761 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001762 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001763 rcu_read_lock();
1764 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001765 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001766 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001767 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001768 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001769 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001770 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001771 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001772 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001773 if (!opendata->is_recover) {
1774 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1775 if (ret != 0)
1776 goto out;
1777 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001778 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001779
1780 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001781 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001782 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001783 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001784out:
1785 return ERR_PTR(ret);
1786out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001787 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001788 return state;
1789}
1790
Andy Adamsone23008e2012-10-02 21:07:32 -04001791static void
1792nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1793{
1794 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1795 struct nfs_delegation *delegation;
1796 int delegation_flags = 0;
1797
1798 rcu_read_lock();
1799 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1800 if (delegation)
1801 delegation_flags = delegation->flags;
1802 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001803 switch (data->o_arg.claim) {
1804 default:
1805 break;
1806 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1807 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001808 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1809 "returning a delegation for "
1810 "OPEN(CLAIM_DELEGATE_CUR)\n",
1811 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001812 return;
1813 }
1814 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001815 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001816 data->owner->so_cred,
1817 data->o_res.delegation_type,
1818 &data->o_res.delegation,
1819 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001820 else
1821 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001822 data->owner->so_cred,
1823 data->o_res.delegation_type,
1824 &data->o_res.delegation,
1825 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001826
1827 if (data->o_res.do_recall)
1828 nfs_async_inode_return_delegation(state->inode,
1829 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001830}
1831
1832/*
1833 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1834 * and update the nfs4_state.
1835 */
1836static struct nfs4_state *
1837_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1838{
1839 struct inode *inode = data->state->inode;
1840 struct nfs4_state *state = data->state;
1841 int ret;
1842
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001843 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001844 if (data->rpc_status)
1845 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001846 /* cached opens have already been processed */
1847 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001848 }
1849
Andy Adamsone23008e2012-10-02 21:07:32 -04001850 ret = nfs_refresh_inode(inode, &data->f_attr);
1851 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001852 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001853
1854 if (data->o_res.delegation_type != 0)
1855 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001856update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001857 update_open_stateid(state, &data->o_res.stateid, NULL,
1858 data->o_arg.fmode);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001859 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001860
1861 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001862}
1863
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001864static struct inode *
1865nfs4_opendata_get_inode(struct nfs4_opendata *data)
1866{
1867 struct inode *inode;
1868
1869 switch (data->o_arg.claim) {
1870 case NFS4_OPEN_CLAIM_NULL:
1871 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1872 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1873 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1874 return ERR_PTR(-EAGAIN);
1875 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1876 &data->f_attr, data->f_label);
1877 break;
1878 default:
1879 inode = d_inode(data->dentry);
1880 ihold(inode);
1881 nfs_refresh_inode(inode, &data->f_attr);
1882 }
1883 return inode;
1884}
1885
Andy Adamsone23008e2012-10-02 21:07:32 -04001886static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001887nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1888{
1889 struct nfs4_state *state;
1890 struct inode *inode;
1891
1892 inode = nfs4_opendata_get_inode(data);
1893 if (IS_ERR(inode))
1894 return ERR_CAST(inode);
1895 if (data->state != NULL && data->state->inode == inode) {
1896 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001897 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04001898 } else
1899 state = nfs4_get_open_state(inode, data->owner);
1900 iput(inode);
1901 if (state == NULL)
1902 state = ERR_PTR(-ENOMEM);
1903 return state;
1904}
1905
Andy Adamsone23008e2012-10-02 21:07:32 -04001906static struct nfs4_state *
1907_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001908{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001909 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001910
Trond Myklebustaac00a82007-07-05 19:02:21 -04001911 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001912 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001913 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001914 goto out;
1915 }
1916
Trond Myklebust75e8c482017-08-08 10:38:07 -04001917 state = nfs4_opendata_find_nfs4_state(data);
1918 if (IS_ERR(state))
1919 goto out;
1920
Andy Adamsone23008e2012-10-02 21:07:32 -04001921 if (data->o_res.delegation_type != 0)
1922 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001923 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001924 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001925out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001926 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001927 return state;
1928}
1929
Andy Adamsone23008e2012-10-02 21:07:32 -04001930static struct nfs4_state *
1931nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1932{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001933 struct nfs4_state *ret;
1934
Andy Adamsone23008e2012-10-02 21:07:32 -04001935 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001936 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1937 else
1938 ret = _nfs4_opendata_to_nfs4_state(data);
1939 nfs4_sequence_free_slot(&data->o_res.seq_res);
1940 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001941}
1942
Trond Myklebust0de43972018-09-02 15:57:01 -04001943static struct nfs_open_context *
1944nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001945{
1946 struct nfs_inode *nfsi = NFS_I(state->inode);
1947 struct nfs_open_context *ctx;
1948
Trond Myklebust0de43972018-09-02 15:57:01 -04001949 rcu_read_lock();
1950 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01001951 if (ctx->state != state)
1952 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04001953 if ((ctx->mode & mode) != mode)
1954 continue;
1955 if (!get_nfs_open_context(ctx))
1956 continue;
1957 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001958 return ctx;
1959 }
Trond Myklebust0de43972018-09-02 15:57:01 -04001960 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001961 return ERR_PTR(-ENOENT);
1962}
1963
Trond Myklebust0de43972018-09-02 15:57:01 -04001964static struct nfs_open_context *
1965nfs4_state_find_open_context(struct nfs4_state *state)
1966{
1967 struct nfs_open_context *ctx;
1968
1969 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
1970 if (!IS_ERR(ctx))
1971 return ctx;
1972 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
1973 if (!IS_ERR(ctx))
1974 return ctx;
1975 return nfs4_state_find_open_context_mode(state, FMODE_READ);
1976}
1977
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001978static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1979 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001980{
1981 struct nfs4_opendata *opendata;
1982
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001983 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001984 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001985 if (opendata == NULL)
1986 return ERR_PTR(-ENOMEM);
1987 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001988 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001989 return opendata;
1990}
1991
Trond Myklebust24311f82015-09-20 10:50:17 -04001992static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1993 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001994{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001995 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001996 int ret;
1997
Trond Myklebust24311f82015-09-20 10:50:17 -04001998 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001999 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002000 opendata->o_arg.open_flags = 0;
2001 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002002 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2003 NFS_SB(opendata->dentry->d_sb),
2004 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002005 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2006 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2007 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002008 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002009 if (ret != 0)
2010 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002011 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002012 if (IS_ERR(newstate))
2013 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002014 if (newstate != opendata->state)
2015 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002016 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002017 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002018}
2019
2020static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2021{
Trond Myklebust864472e2006-01-03 09:55:15 +01002022 int ret;
2023
Trond Myklebust4f14c192014-02-12 19:15:06 -05002024 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2025 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2026 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2027 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002028 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002029 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002030 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002031 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002032 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2033 if (ret != 0)
2034 return ret;
2035 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2036 if (ret != 0)
2037 return ret;
2038 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2039 if (ret != 0)
2040 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002041 /*
2042 * We may have performed cached opens for all three recoveries.
2043 * Check if we need to update the current stateid.
2044 */
2045 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002046 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002047 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002048 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002049 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002050 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002051 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002052 return 0;
2053}
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055/*
2056 * OPEN_RECLAIM:
2057 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002059static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002061 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002062 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002063 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 int status;
2065
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002066 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2067 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002068 if (IS_ERR(opendata))
2069 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002070 rcu_read_lock();
2071 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002072 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002073 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002074 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002075 opendata->o_arg.u.delegation_type = delegation_type;
2076 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002077 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return status;
2079}
2080
Trond Myklebust539cd032007-06-05 11:46:42 -04002081static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 struct nfs_server *server = NFS_SERVER(state->inode);
2084 struct nfs4_exception exception = { };
2085 int err;
2086 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002087 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002088 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002089 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2090 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002091 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002092 break;
2093 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 } while (exception.retry);
2095 return err;
2096}
2097
Trond Myklebust864472e2006-01-03 09:55:15 +01002098static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2099{
2100 struct nfs_open_context *ctx;
2101 int ret;
2102
2103 ctx = nfs4_state_find_open_context(state);
2104 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002105 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002106 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002107 put_nfs_open_context(ctx);
2108 return ret;
2109}
2110
NeilBrowndce26302017-12-13 09:57:09 +11002111static 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 -07002112{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002113 switch (err) {
2114 default:
2115 printk(KERN_ERR "NFS: %s: unhandled error "
2116 "%d.\n", __func__, err);
2117 case 0:
2118 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002119 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002120 case -ESTALE:
2121 break;
2122 case -NFS4ERR_BADSESSION:
2123 case -NFS4ERR_BADSLOT:
2124 case -NFS4ERR_BAD_HIGH_SLOT:
2125 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2126 case -NFS4ERR_DEADSESSION:
2127 set_bit(NFS_DELEGATED_STATE, &state->flags);
2128 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2129 return -EAGAIN;
2130 case -NFS4ERR_STALE_CLIENTID:
2131 case -NFS4ERR_STALE_STATEID:
2132 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002133 /* Don't recall a delegation if it was lost */
2134 nfs4_schedule_lease_recovery(server->nfs_client);
2135 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002136 case -NFS4ERR_MOVED:
2137 nfs4_schedule_migration_recovery(server);
2138 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002139 case -NFS4ERR_LEASE_MOVED:
2140 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2141 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002142 case -NFS4ERR_DELEG_REVOKED:
2143 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002144 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002145 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002146 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002147 nfs_inode_find_state_and_recover(state->inode,
2148 stateid);
2149 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002150 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002151 case -NFS4ERR_DELAY:
2152 case -NFS4ERR_GRACE:
2153 set_bit(NFS_DELEGATED_STATE, &state->flags);
2154 ssleep(1);
2155 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002156 case -ENOMEM:
2157 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002158 if (fl) {
2159 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2160 if (lsp)
2161 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2162 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002163 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return err;
2166}
2167
Trond Myklebust24311f82015-09-20 10:50:17 -04002168int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2169 struct nfs4_state *state, const nfs4_stateid *stateid,
2170 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002171{
2172 struct nfs_server *server = NFS_SERVER(state->inode);
2173 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002174 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002175
2176 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2177 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2178 if (IS_ERR(opendata))
2179 return PTR_ERR(opendata);
2180 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002181 nfs_state_clear_delegation(state);
Trond Myklebust24311f82015-09-20 10:50:17 -04002182 switch (type & (FMODE_READ|FMODE_WRITE)) {
2183 case FMODE_READ|FMODE_WRITE:
2184 case FMODE_WRITE:
2185 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2186 if (err)
2187 break;
2188 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2189 if (err)
2190 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002191 /* Fall through */
Trond Myklebust24311f82015-09-20 10:50:17 -04002192 case FMODE_READ:
2193 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2194 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002195 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002196 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002197}
2198
Chuck Leverbe05c862013-08-09 12:49:47 -04002199static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2200{
2201 struct nfs4_opendata *data = calldata;
2202
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002203 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2204 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002205}
2206
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002207static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2208{
2209 struct nfs4_opendata *data = calldata;
2210
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002211 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002212
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002213 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002214 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002215 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002216 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002217 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002218 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002219 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002220}
2221
2222static void nfs4_open_confirm_release(void *calldata)
2223{
2224 struct nfs4_opendata *data = calldata;
2225 struct nfs4_state *state = NULL;
2226
2227 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002228 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002229 goto out_free;
2230 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002231 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002232 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002233 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002234 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002235 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002236out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002237 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002238}
2239
2240static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002241 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002242 .rpc_call_done = nfs4_open_confirm_done,
2243 .rpc_release = nfs4_open_confirm_release,
2244};
2245
2246/*
2247 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2248 */
2249static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2250{
David Howells2b0143b2015-03-17 22:25:59 +00002251 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002252 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002253 struct rpc_message msg = {
2254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2255 .rpc_argp = &data->c_arg,
2256 .rpc_resp = &data->c_res,
2257 .rpc_cred = data->owner->so_cred,
2258 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002259 struct rpc_task_setup task_setup_data = {
2260 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002261 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002262 .callback_ops = &nfs4_open_confirm_ops,
2263 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002264 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002265 .flags = RPC_TASK_ASYNC,
2266 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 int status;
2268
Anna Schumakerfba83f32018-05-04 16:22:50 -04002269 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2270 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002271 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002272 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002273 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002274 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002275 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002276 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002277 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002278 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002279 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002280 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002281 smp_wmb();
2282 } else
2283 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002284 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return status;
2286}
2287
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002288static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002290 struct nfs4_opendata *data = calldata;
2291 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002292 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002293 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002294
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002295 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002296 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002297 /*
2298 * Check if we still need to send an OPEN call, or if we can use
2299 * a delegation instead.
2300 */
2301 if (data->state != NULL) {
2302 struct nfs_delegation *delegation;
2303
Trond Myklebustbe189f72018-09-27 17:12:33 -04002304 if (can_open_cached(data->state, data->o_arg.fmode,
2305 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002306 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002307 rcu_read_lock();
2308 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002309 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002310 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002311 rcu_read_unlock();
2312 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002313 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002314 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002315 switch (claim) {
2316 default:
2317 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002318 case NFS4_OPEN_CLAIM_PREVIOUS:
2319 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2320 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002321 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002322 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002323 case NFS4_OPEN_CLAIM_FH:
2324 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002325 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002326 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002327 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002328 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002329 &data->o_res.seq_res,
2330 task) != 0)
2331 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002332
2333 /* Set the create mode (note dependency on the session type) */
2334 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2335 if (data->o_arg.open_flags & O_EXCL) {
2336 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2337 if (nfs4_has_persistent_session(clp))
2338 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2339 else if (clp->cl_mvops->minor_version > 0)
2340 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2341 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002342 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002343unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002344 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002345 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002346out_no_action:
2347 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002348out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002349 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002350}
2351
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002352static void nfs4_open_done(struct rpc_task *task, void *calldata)
2353{
2354 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002356 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002357
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002358 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002359 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002360
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002361 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002362 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2363 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002364 case S_IFREG:
2365 break;
2366 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002367 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002368 break;
2369 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002370 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002371 break;
2372 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002373 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002374 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002375 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002376 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002377 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2378 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002379 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002380 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002381}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002382
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002383static void nfs4_open_release(void *calldata)
2384{
2385 struct nfs4_opendata *data = calldata;
2386 struct nfs4_state *state = NULL;
2387
2388 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002389 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002390 goto out_free;
2391 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002392 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002393 goto out_free;
2394 /* In case we need an open_confirm, no cleanup! */
2395 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2396 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002397 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002398 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002399 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002400out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002401 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002402}
2403
2404static const struct rpc_call_ops nfs4_open_ops = {
2405 .rpc_call_prepare = nfs4_open_prepare,
2406 .rpc_call_done = nfs4_open_done,
2407 .rpc_release = nfs4_open_release,
2408};
2409
Fred Isaman3b65a302016-09-19 10:06:49 -04002410static int nfs4_run_open_task(struct nfs4_opendata *data,
2411 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002412{
David Howells2b0143b2015-03-17 22:25:59 +00002413 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002414 struct nfs_server *server = NFS_SERVER(dir);
2415 struct nfs_openargs *o_arg = &data->o_arg;
2416 struct nfs_openres *o_res = &data->o_res;
2417 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002418 struct rpc_message msg = {
2419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2420 .rpc_argp = o_arg,
2421 .rpc_resp = o_res,
2422 .rpc_cred = data->owner->so_cred,
2423 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002424 struct rpc_task_setup task_setup_data = {
2425 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002426 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002427 .callback_ops = &nfs4_open_ops,
2428 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002429 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002430 .flags = RPC_TASK_ASYNC,
2431 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002432 int status;
2433
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002434 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002435 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002436 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002437 data->cancelled = false;
2438 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002439 if (!ctx) {
2440 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002441 data->is_recover = true;
Fred Isaman2409a972016-10-06 12:11:21 -04002442 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002443 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002444 pnfs_lgopen_prepare(data, ctx);
2445 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002446 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002447 if (IS_ERR(task))
2448 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002449 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002450 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002451 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002452 smp_wmb();
2453 } else
2454 status = data->rpc_status;
2455 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002456
2457 return status;
2458}
2459
2460static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2461{
David Howells2b0143b2015-03-17 22:25:59 +00002462 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002463 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002464 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002465
Fred Isaman3b65a302016-09-19 10:06:49 -04002466 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002467 if (status != 0 || !data->rpc_done)
2468 return status;
2469
Trond Myklebust6926afd12012-01-07 13:22:46 -05002470 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2471
Anna Schumakerd7e98252017-01-11 16:13:29 -05002472 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002473 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002474
2475 return status;
2476}
2477
Trond Myklebustf3792d62014-07-10 08:54:32 -04002478/*
2479 * Additional permission checks in order to distinguish between an
2480 * open for read, and an open for execute. This works around the
2481 * fact that NFSv4 OPEN treats read and execute permissions as being
2482 * the same.
2483 * Note that in the non-execute case, we want to turn off permission
2484 * checking if we just created a new file (POSIX open() semantics).
2485 */
NeilBrowna52458b2018-12-03 11:30:31 +11002486static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002487 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002488 struct nfs4_state *state, fmode_t fmode,
2489 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002490{
2491 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002492 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002493
2494 /* access call failed or for some reason the server doesn't
2495 * support any access modes -- defer access call until later */
2496 if (opendata->o_res.access_supported == 0)
2497 return 0;
2498
2499 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002500 /*
2501 * Use openflags to check for exec, because fmode won't
2502 * always have FMODE_EXEC set when file open for exec.
2503 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002504 if (openflags & __FMODE_EXEC) {
2505 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002506 if (S_ISDIR(state->inode->i_mode))
2507 mask = NFS4_ACCESS_LOOKUP;
2508 else
2509 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002510 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002511 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002512
2513 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002514 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2515 nfs_access_add_cache(state->inode, &cache);
2516
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002517 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2518 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002519 return 0;
2520
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002521 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002522}
2523
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002524/*
2525 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2526 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002527static int _nfs4_proc_open(struct nfs4_opendata *data,
2528 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002529{
David Howells2b0143b2015-03-17 22:25:59 +00002530 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002531 struct nfs_server *server = NFS_SERVER(dir);
2532 struct nfs_openargs *o_arg = &data->o_arg;
2533 struct nfs_openres *o_res = &data->o_res;
2534 int status;
2535
Fred Isaman3b65a302016-09-19 10:06:49 -04002536 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002537 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002538 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002539 if (status != 0) {
2540 if (status == -NFS4ERR_BADNAME &&
2541 !(o_arg->open_flags & O_CREAT))
2542 return -ENOENT;
2543 return status;
2544 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002545
Trond Myklebust6926afd12012-01-07 13:22:46 -05002546 nfs_fattr_map_and_free_names(server, &data->f_attr);
2547
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002548 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002549 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002550 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002551 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002552 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002553 if (data->file_created ||
2554 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002555 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002556 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002557 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002558 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2559 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002561 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002563 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002565 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2566 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002567 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2568 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002569 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002570 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573/*
2574 * OPEN_EXPIRED:
2575 * reclaim state on the server after a network partition.
2576 * Assumes caller holds the appropriate lock
2577 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002578static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002580 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002581 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002583 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002584 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002585 if (IS_ERR(opendata))
2586 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002587 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002588 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002589 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002590 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002591 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002594static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002595{
Trond Myklebust539cd032007-06-05 11:46:42 -04002596 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002597 struct nfs4_exception exception = { };
2598 int err;
2599
2600 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002601 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002602 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002603 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2604 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002605 switch (err) {
2606 default:
2607 goto out;
2608 case -NFS4ERR_GRACE:
2609 case -NFS4ERR_DELAY:
2610 nfs4_handle_exception(server, err, &exception);
2611 err = 0;
2612 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002613 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002614out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002615 return err;
2616}
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2619{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002621 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Trond Myklebust864472e2006-01-03 09:55:15 +01002623 ctx = nfs4_state_find_open_context(state);
2624 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002625 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002626 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002627 put_nfs_open_context(ctx);
2628 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
Trond Myklebust41020b62016-09-22 13:38:58 -04002631static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2632 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002633{
Trond Myklebust41020b62016-09-22 13:38:58 -04002634 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002635 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002636}
2637
2638static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2639{
2640 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002641 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002642}
2643
2644static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2645{
2646 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2647 nfs40_clear_delegation_stateid(state);
2648 return nfs4_open_expired(sp, state);
2649}
2650
Trond Myklebust45870d62016-09-22 13:38:59 -04002651static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2652 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002653 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002654{
2655 return -NFS4ERR_BAD_STATEID;
2656}
2657
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002658#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002659static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2660 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002661 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002662{
2663 int status;
2664
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002665 switch (stateid->type) {
2666 default:
2667 break;
2668 case NFS4_INVALID_STATEID_TYPE:
2669 case NFS4_SPECIAL_STATEID_TYPE:
2670 return -NFS4ERR_BAD_STATEID;
2671 case NFS4_REVOKED_STATEID_TYPE:
2672 goto out_free;
2673 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002674
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002675 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002676 switch (status) {
2677 case -NFS4ERR_EXPIRED:
2678 case -NFS4ERR_ADMIN_REVOKED:
2679 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002680 break;
2681 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002682 return status;
2683 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002684out_free:
2685 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002686 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002687 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002688}
2689
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002690static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002691{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002692 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002693 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002694 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002695 const struct cred *cred = NULL;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002696 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002697
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002698 /* Get the delegation credential for use by test/free_stateid */
2699 rcu_read_lock();
2700 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002701 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002702 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002703 nfs_state_clear_delegation(state);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002704 return;
2705 }
2706
2707 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002708 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002709 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002710 nfs_state_clear_delegation(state);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002711 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002712 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002713
Trond Myklebust994b15b2018-09-05 14:07:14 -04002714 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2715 &delegation->flags)) {
2716 rcu_read_unlock();
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002717 return;
2718 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002719
NeilBrowna52458b2018-12-03 11:30:31 +11002720 if (delegation->cred)
2721 cred = get_cred(delegation->cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002722 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002723 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002724 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002725 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002726 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002727
NeilBrowna52458b2018-12-03 11:30:31 +11002728 if (delegation->cred)
2729 put_cred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002730}
2731
2732/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002733 * nfs41_check_expired_locks - possibly free a lock stateid
2734 *
2735 * @state: NFSv4 state for an inode
2736 *
2737 * Returns NFS_OK if recovery for this stateid is now finished.
2738 * Otherwise a negative NFS4ERR value is returned.
2739 */
2740static int nfs41_check_expired_locks(struct nfs4_state *state)
2741{
2742 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002743 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002744 struct nfs_server *server = NFS_SERVER(state->inode);
2745
2746 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2747 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002748
2749 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002750 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2751 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002752 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002753
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002754 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002755 spin_unlock(&state->state_lock);
2756
2757 nfs4_put_lock_state(prev);
2758 prev = lsp;
2759
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002760 status = nfs41_test_and_free_expired_stateid(server,
2761 &lsp->ls_stateid,
2762 cred);
2763 trace_nfs4_test_lock_stateid(state, lsp, status);
2764 if (status == -NFS4ERR_EXPIRED ||
2765 status == -NFS4ERR_BAD_STATEID) {
2766 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002767 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002768 if (!recover_lost_locks)
2769 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2770 } else if (status != NFS_OK) {
2771 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002772 nfs4_put_lock_state(prev);
2773 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002774 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002775 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002776 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002777 }
2778 spin_unlock(&state->state_lock);
2779 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002780out:
2781 return ret;
2782}
2783
2784/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002785 * nfs41_check_open_stateid - possibly free an open stateid
2786 *
2787 * @state: NFSv4 state for an inode
2788 *
2789 * Returns NFS_OK if recovery for this stateid is now finished.
2790 * Otherwise a negative NFS4ERR value is returned.
2791 */
2792static int nfs41_check_open_stateid(struct nfs4_state *state)
2793{
2794 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002795 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002796 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002797 int status;
2798
Trond Myklebustb134fc42016-09-22 13:39:16 -04002799 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002800 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2801 if (nfs4_have_delegation(state->inode, state->state))
2802 return NFS_OK;
2803 return -NFS4ERR_OPENMODE;
2804 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002805 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002806 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002807 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002808 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002809 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002810 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2811 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2812 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002813 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002814 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002815 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002816 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002817 if (nfs_open_stateid_recover_openmode(state))
2818 return -NFS4ERR_OPENMODE;
2819 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002820}
2821
2822static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2823{
Chuck Levereb64cf92012-07-11 16:30:05 -04002824 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002825
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002826 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002827 status = nfs41_check_expired_locks(state);
2828 if (status != NFS_OK)
2829 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002830 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002831 if (status != NFS_OK)
2832 status = nfs4_open_expired(sp, state);
2833 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002834}
2835#endif
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002838 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2839 * fields corresponding to attributes that were used to store the verifier.
2840 * Make sure we clobber those fields in the later setattr call
2841 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002842static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002843 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002844{
Trond Myklebust609339c2018-03-28 16:18:17 -04002845 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2846 __u32 attrset[3];
2847 unsigned ret;
2848 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002849
Trond Myklebust609339c2018-03-28 16:18:17 -04002850 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2851 attrset[i] = opendata->o_res.attrset[i];
2852 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2853 attrset[i] &= ~bitmask[i];
2854 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002855
Trond Myklebust609339c2018-03-28 16:18:17 -04002856 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2857 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002858
Trond Myklebust609339c2018-03-28 16:18:17 -04002859 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2860 if (sattr->ia_valid & ATTR_ATIME_SET)
2861 ret |= ATTR_ATIME_SET;
2862 else
2863 ret |= ATTR_ATIME;
2864 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002865
Trond Myklebust609339c2018-03-28 16:18:17 -04002866 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2867 if (sattr->ia_valid & ATTR_MTIME_SET)
2868 ret |= ATTR_MTIME_SET;
2869 else
2870 ret |= ATTR_MTIME;
2871 }
2872
2873 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002874 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002875 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002876}
2877
Trond Myklebustc21443c2013-02-07 14:26:21 -05002878static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2879 fmode_t fmode,
2880 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002881 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002882{
2883 struct nfs4_state_owner *sp = opendata->owner;
2884 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002885 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002886 struct nfs4_state *state;
2887 unsigned int seq;
2888 int ret;
2889
2890 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2891
Fred Isaman3b65a302016-09-19 10:06:49 -04002892 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002893 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002894 goto out;
2895
Trond Myklebustae55e592018-05-22 11:17:16 -04002896 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002897 ret = PTR_ERR(state);
2898 if (IS_ERR(state))
2899 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002900 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002901 if (server->caps & NFS_CAP_POSIX_LOCK)
2902 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002903 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2904 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002905
Trond Myklebust275bb302013-05-29 13:11:28 -04002906 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002907 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002908 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002909 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002910 alias = d_exact_alias(dentry, state->inode);
2911 if (!alias)
2912 alias = d_splice_alias(igrab(state->inode), dentry);
2913 /* d_splice_alias() can't fail here - it's a non-directory */
2914 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002915 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002916 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002917 }
2918 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002919 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002920 }
2921
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04002922 /* Parse layoutget results before we check for access */
2923 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2924
Trond Myklebustc21443c2013-02-07 14:26:21 -05002925 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2926 if (ret != 0)
2927 goto out;
2928
David Howells2b0143b2015-03-17 22:25:59 +00002929 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002930 nfs_inode_attach_open_context(ctx);
2931 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2932 nfs4_schedule_stateid_recovery(server, state);
2933 }
Fred Isaman2409a972016-10-06 12:11:21 -04002934
Trond Myklebustc21443c2013-02-07 14:26:21 -05002935out:
Trond Myklebustae55e592018-05-22 11:17:16 -04002936 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002937 return ret;
2938}
2939
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002940/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002941 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 */
Andy Adamson82be4172012-05-23 05:02:35 -04002943static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002944 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002945 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002946 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002947 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
2949 struct nfs4_state_owner *sp;
2950 struct nfs4_state *state = NULL;
2951 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002952 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002953 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11002954 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04002955 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2956 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002957 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002958 struct iattr *sattr = c->sattr;
2959 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002960 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002961 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002965 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2966 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2968 goto out_err;
2969 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002970 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002971 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002972 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002973 if (d_really_is_positive(dentry))
2974 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002975 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002976 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002977 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002978 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2979 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002980 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002981 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
David Quigley14c43f72013-05-22 12:50:43 -04002983 if (label) {
2984 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2985 if (IS_ERR(olabel)) {
2986 status = PTR_ERR(olabel);
2987 goto err_opendata_put;
2988 }
2989 }
2990
Trond Myklebuste911b812014-03-26 13:24:37 -07002991 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2992 if (!opendata->f_attr.mdsthreshold) {
2993 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2994 if (!opendata->f_attr.mdsthreshold)
2995 goto err_free_label;
2996 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002997 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002998 }
David Howells2b0143b2015-03-17 22:25:59 +00002999 if (d_really_is_positive(dentry))
3000 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003001
Trond Myklebust3efb9722013-05-29 13:17:04 -04003002 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003003 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003004 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003005 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003006
NeilBrownefcbc042015-07-30 13:00:56 +10003007 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003008 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003009 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003010 /*
3011 * send create attributes which was not set by open
3012 * with an extra setattr.
3013 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003014 if (attrs || label) {
3015 unsigned ia_old = sattr->ia_valid;
3016
3017 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003018 nfs_fattr_init(opendata->o_res.f_attr);
3019 status = nfs4_do_setattr(state->inode, cred,
3020 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003021 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003022 if (status == 0) {
3023 nfs_setattr_update_inode(state->inode, sattr,
3024 opendata->o_res.f_attr);
3025 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3026 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003027 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003028 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003029 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003030 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003031 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003032
Trond Myklebuste911b812014-03-26 13:24:37 -07003033 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003034 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003035 opendata->f_attr.mdsthreshold = NULL;
3036 }
Andy Adamson82be4172012-05-23 05:02:35 -04003037
David Quigley14c43f72013-05-22 12:50:43 -04003038 nfs4_label_free(olabel);
3039
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003040 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003043err_free_label:
3044 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003045err_opendata_put:
3046 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003047err_put_state_owner:
3048 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 return status;
3051}
3052
3053
Andy Adamson82be4172012-05-23 05:02:35 -04003054static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003055 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003056 int flags,
3057 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003058 struct nfs4_label *label,
3059 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003061 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct nfs4_exception exception = { };
3063 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003064 struct nfs4_open_createattrs c = {
3065 .label = label,
3066 .sattr = sattr,
3067 .verf = {
3068 [0] = (__u32)jiffies,
3069 [1] = (__u32)current->pid,
3070 },
3071 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 int status;
3073
3074 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003075 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003076 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003077 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 if (status == 0)
3079 break;
3080 /* NOTE: BAD_SEQID means the server and client disagree about the
3081 * book-keeping w.r.t. state-changing operations
3082 * (OPEN/CLOSE/LOCK/LOCKU...)
3083 * It is actually a sign of a bug on the client or on the server.
3084 *
3085 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003086 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 * have unhashed the old state_owner for us, and that we can
3088 * therefore safely retry using a new one. We should still warn
3089 * the user though...
3090 */
3091 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003092 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003093 " returned a bad sequence-id error!\n",
3094 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 exception.retry = 1;
3096 continue;
3097 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003098 /*
3099 * BAD_STATEID on OPEN means that the server cancelled our
3100 * state before it received the OPEN_CONFIRM.
3101 * Recover by retrying the request as per the discussion
3102 * on Page 181 of RFC3530.
3103 */
3104 if (status == -NFS4ERR_BAD_STATEID) {
3105 exception.retry = 1;
3106 continue;
3107 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003108 if (status == -EAGAIN) {
3109 /* We must have found a delegation */
3110 exception.retry = 1;
3111 continue;
3112 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003113 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3114 continue;
3115 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 status, &exception));
3117 } while (exception.retry);
3118 return res;
3119}
3120
Trond Myklebust8487c472016-06-26 08:44:35 -04003121static int _nfs4_do_setattr(struct inode *inode,
3122 struct nfs_setattrargs *arg,
3123 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003124 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003125 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003126{
3127 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003128 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3130 .rpc_argp = arg,
3131 .rpc_resp = res,
3132 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003133 };
NeilBrowna52458b2018-12-03 11:30:31 +11003134 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003135 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003136 bool truncate;
3137 int status;
3138
3139 nfs_fattr_init(res->fattr);
3140
3141 /* Servers should only apply open mode checks for file size changes */
3142 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003143 if (!truncate)
3144 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003145
Trond Myklebust991eedb2018-04-09 11:15:30 -04003146 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003147 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003148 } else if (ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003149 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003150 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003151 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003152 l_ctx = nfs_get_lock_context(ctx);
3153 if (IS_ERR(l_ctx))
3154 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003155 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3156 &arg->stateid, &delegation_cred);
3157 nfs_put_lock_context(l_ctx);
3158 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003159 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003160 } else {
3161zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003162 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003163 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003164 if (delegation_cred)
3165 msg.rpc_cred = delegation_cred;
3166
3167 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3168
NeilBrowna52458b2018-12-03 11:30:31 +11003169 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003170 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003171 renew_lease(server, timestamp);
3172 trace_nfs4_setattr(inode, &arg->stateid, status);
3173 return status;
3174}
3175
NeilBrowna52458b2018-12-03 11:30:31 +11003176static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003177 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003178 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003179 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003181 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003182 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003183 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003184 struct nfs_setattrargs arg = {
3185 .fh = NFS_FH(inode),
3186 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003188 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003189 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003190 };
3191 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003193 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003195 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003196 struct nfs4_exception exception = {
3197 .state = state,
3198 .inode = inode,
3199 .stateid = &arg.stateid,
3200 };
3201 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003204 nfs4_bitmap_copy_adjust_setattr(bitmask,
3205 nfs4_bitmask(server, olabel),
3206 inode);
3207
NeilBrown29b59f92016-10-13 15:26:47 +11003208 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003209 switch (err) {
3210 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003211 if (!(sattr->ia_valid & ATTR_SIZE)) {
3212 pr_warn_once("NFSv4: server %s is incorrectly "
3213 "applying open mode checks to "
3214 "a SETATTR that is not "
3215 "changing file size.\n",
3216 server->nfs_client->cl_hostname);
3217 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003218 if (state && !(state->state & FMODE_WRITE)) {
3219 err = -EBADF;
3220 if (sattr->ia_valid & ATTR_OPEN)
3221 err = -EACCES;
3222 goto out;
3223 }
3224 }
3225 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003227out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return err;
3229}
3230
Peng Tao500d7012015-09-22 11:35:22 +08003231static bool
3232nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3233{
3234 if (inode == NULL || !nfs_have_layout(inode))
3235 return false;
3236
3237 return pnfs_wait_on_layoutreturn(inode, task);
3238}
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240struct nfs4_closedata {
3241 struct inode *inode;
3242 struct nfs4_state *state;
3243 struct nfs_closeargs arg;
3244 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003245 struct {
3246 struct nfs4_layoutreturn_args arg;
3247 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003248 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003249 u32 roc_barrier;
3250 bool roc;
3251 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003252 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003253 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254};
3255
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003256static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003257{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003258 struct nfs4_closedata *calldata = data;
3259 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003260 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003261
Trond Myklebustcf805162016-11-15 14:56:07 -05003262 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003263 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3264 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003265 nfs4_put_open_state(calldata->state);
3266 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003267 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003268 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003269 kfree(calldata);
3270}
3271
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003272static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003274 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003277 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003278 struct nfs4_exception exception = {
3279 .state = state,
3280 .inode = calldata->inode,
3281 .stateid = &calldata->arg.stateid,
3282 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Chuck Levera3ca5652012-03-01 17:00:40 -05003284 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003285 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3286 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003287 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003288
3289 /* Handle Layoutreturn errors */
3290 if (calldata->arg.lr_args && task->tk_status != 0) {
3291 switch (calldata->res.lr_ret) {
3292 default:
3293 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3294 break;
3295 case 0:
3296 calldata->arg.lr_args = NULL;
3297 calldata->res.lr_res = NULL;
3298 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003299 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04003300 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04003301 &calldata->arg.lr_args->range,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003302 calldata->inode))
3303 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003304 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003305 case -NFS4ERR_ADMIN_REVOKED:
3306 case -NFS4ERR_DELEG_REVOKED:
3307 case -NFS4ERR_EXPIRED:
3308 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003309 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3310 case -NFS4ERR_WRONG_CRED:
3311 calldata->arg.lr_args = NULL;
3312 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003313 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003314 }
3315 }
3316
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003317 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 * the state_owner. we keep this around to process errors
3319 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 switch (task->tk_status) {
3321 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003322 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003323 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003324 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003325 case -NFS4ERR_ACCESS:
3326 if (calldata->arg.bitmask != NULL) {
3327 calldata->arg.bitmask = NULL;
3328 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003329 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003330
3331 }
3332 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003333 case -NFS4ERR_OLD_STATEID:
3334 /* Did we race with OPEN? */
3335 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003336 state))
3337 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003338 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003339 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003341 case -NFS4ERR_EXPIRED:
3342 nfs4_free_revoked_stateid(server,
3343 &calldata->arg.stateid,
3344 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003345 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003346 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003347 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003349 task->tk_status = nfs4_async_handle_exception(task,
3350 server, task->tk_status, &exception);
3351 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003352 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003354 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3355 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003356out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003357 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003358 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003359 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003360 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003361 return;
3362lr_restart:
3363 calldata->res.lr_ret = 0;
3364out_restart:
3365 task->tk_status = 0;
3366 rpc_restart_call_prepare(task);
3367 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003370static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003372 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003373 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003374 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003375 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003376 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003377 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003378
Chuck Levera3ca5652012-03-01 17:00:40 -05003379 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003380 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003381 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003382
Trond Myklebust88069f72009-12-08 08:33:16 -05003383 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003384 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003385 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3386 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3387 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003388 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003389 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003390 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003391 if (state->n_rdonly == 0)
3392 call_close |= is_rdonly;
3393 else if (is_rdonly)
3394 calldata->arg.fmode |= FMODE_READ;
3395 if (state->n_wronly == 0)
3396 call_close |= is_wronly;
3397 else if (is_wronly)
3398 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003399 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3400 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003401 } else if (is_rdwr)
3402 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3403
Trond Myklebust5cc78612016-11-14 11:19:56 -05003404 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003405 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003406 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003407 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003408
3409 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003410 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003411 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003412 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003413
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003414 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003415 nfs_release_seqid(calldata->arg.seqid);
3416 goto out_wait;
3417 }
3418
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003419 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3420 if (lo && !pnfs_layout_is_valid(lo)) {
3421 calldata->arg.lr_args = NULL;
3422 calldata->res.lr_res = NULL;
3423 }
3424
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003425 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003426 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003427
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003428 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003429 /* Close-to-open cache consistency revalidation */
3430 if (!nfs4_have_delegation(inode, FMODE_READ))
3431 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3432 else
3433 calldata->arg.bitmask = NULL;
3434 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003435
Trond Myklebust6ae37332015-01-30 14:21:14 -05003436 calldata->arg.share_access =
3437 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3438 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003439
Trond Myklebustd8d84982016-12-19 12:14:44 -05003440 if (calldata->res.fattr == NULL)
3441 calldata->arg.bitmask = NULL;
3442 else if (calldata->arg.bitmask == NULL)
3443 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003444 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003445 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003446 &calldata->arg.seq_args,
3447 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003448 task) != 0)
3449 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003450 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003451 return;
3452out_no_action:
3453 task->tk_action = NULL;
3454out_wait:
3455 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456}
3457
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003458static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003459 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003460 .rpc_call_done = nfs4_close_done,
3461 .rpc_release = nfs4_free_closedata,
3462};
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464/*
3465 * It is possible for data to be read/written from a mem-mapped file
3466 * after the sys_close call (which hits the vfs layer as a flush).
3467 * This means that we can't safely call nfsv4 close on a file until
3468 * the inode is cleared. This in turn means that we are not good
3469 * NFSv4 citizens - we do not indicate to the server to update the file's
3470 * share state even when we are done with one of the three share
3471 * stateid's in the inode.
3472 *
3473 * NOTE: Caller must be holding the sp->so_owner semaphore!
3474 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003475int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003477 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003478 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003480 struct nfs4_state_owner *sp = state->owner;
3481 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003482 struct rpc_message msg = {
3483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3484 .rpc_cred = state->owner->so_cred,
3485 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003486 struct rpc_task_setup task_setup_data = {
3487 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003488 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003489 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003490 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003491 .flags = RPC_TASK_ASYNC,
3492 };
Trond Myklebust95121352005-10-18 14:20:12 -07003493 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003495 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3496 &task_setup_data.rpc_client, &msg);
3497
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003498 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003500 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003501 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003502 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003504 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003505 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3506 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003508 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3509 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003510 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003511 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003512 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003513 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003514 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003515 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003516 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003517 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003518 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003519 calldata->lr.roc = pnfs_roc(state->inode,
3520 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3521 if (calldata->lr.roc) {
3522 calldata->arg.lr_args = &calldata->lr.arg;
3523 calldata->res.lr_res = &calldata->lr.res;
3524 }
Al Viro643168c2011-06-22 18:20:23 -04003525 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003526
Trond Myklebust1174dd12010-12-21 10:52:24 -05003527 msg.rpc_argp = &calldata->arg;
3528 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003529 task_setup_data.callback_data = calldata;
3530 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003531 if (IS_ERR(task))
3532 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003533 status = 0;
3534 if (wait)
3535 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003536 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003537 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003538out_free_calldata:
3539 kfree(calldata);
3540out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003541 nfs4_put_open_state(state);
3542 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003543 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
Trond Myklebust2b484292010-09-17 10:56:51 -04003546static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003547nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3548 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003551 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3552
3553 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Trond Myklebust565277f2007-10-15 18:17:53 -04003555 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003556 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003557
3558 nfs4_label_release_security(label);
3559
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003560 if (IS_ERR(state))
3561 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003562 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
Trond Myklebust1185a552009-12-03 15:54:02 -05003565static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003566{
3567 if (ctx->state == NULL)
3568 return;
3569 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003570 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003571 else
Al Viro643168c2011-06-22 18:20:23 -04003572 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003573}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Trond Myklebustb944dba2013-11-04 15:20:20 -05003575#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3576#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003577#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3580{
Kinglong Mee8c612822015-08-26 21:12:58 +08003581 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003582 struct nfs4_server_caps_arg args = {
3583 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003584 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003585 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 struct nfs4_server_caps_res res = {};
3587 struct rpc_message msg = {
3588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003589 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 .rpc_resp = &res,
3591 };
3592 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003593 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
Kinglong Mee8c612822015-08-26 21:12:58 +08003595 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3596 FATTR4_WORD0_FH_EXPIRE_TYPE |
3597 FATTR4_WORD0_LINK_SUPPORT |
3598 FATTR4_WORD0_SYMLINK_SUPPORT |
3599 FATTR4_WORD0_ACLSUPPORT;
3600 if (minorversion)
3601 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3602
Bryan Schumaker7c513052011-03-24 17:12:24 +00003603 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003605 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003606 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003607 case 0:
3608 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3609 res.attr_bitmask[2] = 0;
3610 break;
3611 case 1:
3612 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3613 break;
3614 case 2:
3615 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003618 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3619 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3620 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3621 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003622 NFS_CAP_CTIME|NFS_CAP_MTIME|
3623 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003624 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3625 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 server->caps |= NFS_CAP_ACLS;
3627 if (res.has_links != 0)
3628 server->caps |= NFS_CAP_HARDLINKS;
3629 if (res.has_symlinks != 0)
3630 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003631 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3632 server->caps |= NFS_CAP_FILEID;
3633 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3634 server->caps |= NFS_CAP_MODE;
3635 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3636 server->caps |= NFS_CAP_NLINK;
3637 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3638 server->caps |= NFS_CAP_OWNER;
3639 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3640 server->caps |= NFS_CAP_OWNER_GROUP;
3641 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3642 server->caps |= NFS_CAP_ATIME;
3643 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3644 server->caps |= NFS_CAP_CTIME;
3645 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3646 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003647#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3648 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3649 server->caps |= NFS_CAP_SECURITY_LABEL;
3650#endif
3651 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3652 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003653 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003654
Trond Myklebusta65318b2009-03-11 14:10:28 -04003655 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3656 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3657 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003658 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003659
3660 /* Avoid a regression due to buggy server */
3661 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3662 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003663 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3664 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003667 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 return status;
3671}
3672
Trond Myklebust55a97592006-06-09 09:34:19 -04003673int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
3675 struct nfs4_exception exception = { };
3676 int err;
3677 do {
3678 err = nfs4_handle_exception(server,
3679 _nfs4_server_capabilities(server, fhandle),
3680 &exception);
3681 } while (exception.retry);
3682 return err;
3683}
3684
3685static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3686 struct nfs_fsinfo *info)
3687{
David Quigleyaa9c2662013-05-22 12:50:44 -04003688 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003690 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 };
3692 struct nfs4_lookup_res res = {
3693 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003694 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 .fh = fhandle,
3696 };
3697 struct rpc_message msg = {
3698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3699 .rpc_argp = &args,
3700 .rpc_resp = &res,
3701 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003702
David Quigleyaa9c2662013-05-22 12:50:44 -04003703 bitmask[0] = nfs4_fattr_bitmap[0];
3704 bitmask[1] = nfs4_fattr_bitmap[1];
3705 /*
3706 * Process the label in the upcoming getfattr
3707 */
3708 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3709
Trond Myklebust0e574af2005-10-27 22:12:38 -04003710 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003711 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712}
3713
3714static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3715 struct nfs_fsinfo *info)
3716{
3717 struct nfs4_exception exception = { };
3718 int err;
3719 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003720 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003721 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003722 switch (err) {
3723 case 0:
3724 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003725 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003726 default:
3727 err = nfs4_handle_exception(server, err, &exception);
3728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003730out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 return err;
3732}
3733
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003734static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3735 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3736{
Trond Myklebustc2190662013-08-26 19:23:04 -04003737 struct rpc_auth_create_args auth_args = {
3738 .pseudoflavor = flavor,
3739 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003740 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003741
Trond Myklebustc2190662013-08-26 19:23:04 -04003742 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003743 if (IS_ERR(auth))
3744 return -EACCES;
3745 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003746}
3747
Chuck Lever9a744ba2013-03-16 15:56:02 -04003748/*
3749 * Retry pseudoroot lookup with various security flavors. We do this when:
3750 *
3751 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3752 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3753 *
3754 * Returns zero on success, or a negative NFS4ERR value, or a
3755 * negative errno value.
3756 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003757static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003758 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003760 /* Per 3530bis 15.33.5 */
3761 static const rpc_authflavor_t flav_array[] = {
3762 RPC_AUTH_GSS_KRB5P,
3763 RPC_AUTH_GSS_KRB5I,
3764 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003765 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003766 RPC_AUTH_NULL,
3767 };
3768 int status = -EPERM;
3769 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003771 if (server->auth_info.flavor_len > 0) {
3772 /* try each flavor specified by user */
3773 for (i = 0; i < server->auth_info.flavor_len; i++) {
3774 status = nfs4_lookup_root_sec(server, fhandle, info,
3775 server->auth_info.flavors[i]);
3776 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3777 continue;
3778 break;
3779 }
3780 } else {
3781 /* no flavors specified by user, try default list */
3782 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3783 status = nfs4_lookup_root_sec(server, fhandle, info,
3784 flav_array[i]);
3785 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3786 continue;
3787 break;
3788 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003789 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003790
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003791 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003792 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003793 * to access the mount. It could also mean that we tried to mount
3794 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3795 * existing mount programs don't handle -EACCES very well so it should
3796 * be mapped to -EPERM instead.
3797 */
3798 if (status == -EACCES)
3799 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003800 return status;
3801}
3802
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003803/**
3804 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3805 * @server: initialized nfs_server handle
3806 * @fhandle: we fill in the pseudo-fs root file handle
3807 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003808 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003809 *
3810 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003811 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003812int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003813 struct nfs_fsinfo *info,
3814 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003815{
Andre Przywarac7757072015-04-23 17:17:40 +01003816 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003817
Andre Przywarac7757072015-04-23 17:17:40 +01003818 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003819 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003820
3821 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003822 status = server->nfs_client->cl_mvops->find_root_sec(server,
3823 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003824
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 if (status == 0)
3826 status = nfs4_server_capabilities(server, fhandle);
3827 if (status == 0)
3828 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003829
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003830 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831}
3832
Bryan Schumakerbae36242012-05-10 15:07:31 -04003833static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3834 struct nfs_fsinfo *info)
3835{
3836 int error;
3837 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003838 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003839
3840 error = nfs4_server_capabilities(server, mntfh);
3841 if (error < 0) {
3842 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3843 return error;
3844 }
3845
David Quigley14c43f72013-05-22 12:50:43 -04003846 label = nfs4_label_alloc(server, GFP_KERNEL);
3847 if (IS_ERR(label))
3848 return PTR_ERR(label);
3849
Trond Myklebusta841b542018-04-07 13:50:59 -04003850 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003851 if (error < 0) {
3852 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003853 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003854 }
3855
3856 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3857 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3858 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3859
David Quigley14c43f72013-05-22 12:50:43 -04003860err_free_label:
3861 nfs4_label_free(label);
3862
Bryan Schumakerbae36242012-05-10 15:07:31 -04003863 return error;
3864}
3865
Manoj Naik6b97fd32006-06-09 09:34:29 -04003866/*
3867 * Get locations and (maybe) other attributes of a referral.
3868 * Note that we'll actually follow the referral later when
3869 * we detect fsid mismatch in inode revalidation
3870 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003871static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3872 const struct qstr *name, struct nfs_fattr *fattr,
3873 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003874{
3875 int status = -ENOMEM;
3876 struct page *page = NULL;
3877 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003878
3879 page = alloc_page(GFP_KERNEL);
3880 if (page == NULL)
3881 goto out;
3882 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3883 if (locations == NULL)
3884 goto out;
3885
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003886 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003887 if (status != 0)
3888 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003889
3890 /*
3891 * If the fsid didn't change, this is a migration event, not a
3892 * referral. Cause us to drop into the exception handler, which
3893 * will kick off migration recovery.
3894 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003895 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003896 dprintk("%s: server did not return a different fsid for"
3897 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003898 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003899 goto out;
3900 }
Andy Adamson533eb462011-06-13 18:25:56 -04003901 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3902 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003903
Andy Adamson533eb462011-06-13 18:25:56 -04003904 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003905 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003906 memset(fhandle, 0, sizeof(struct nfs_fh));
3907out:
3908 if (page)
3909 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003910 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003911 return status;
3912}
3913
David Quigley1775fd32013-05-22 12:50:42 -04003914static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003915 struct nfs_fattr *fattr, struct nfs4_label *label,
3916 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917{
Trond Myklebust771734f2018-04-07 13:54:23 -04003918 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 struct nfs4_getattr_arg args = {
3920 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04003921 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 };
3923 struct nfs4_getattr_res res = {
3924 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003925 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 .server = server,
3927 };
3928 struct rpc_message msg = {
3929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3930 .rpc_argp = &args,
3931 .rpc_resp = &res,
3932 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003933
Trond Myklebust771734f2018-04-07 13:54:23 -04003934 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04003935
Trond Myklebust0e574af2005-10-27 22:12:38 -04003936 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003937 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938}
3939
David Quigley1775fd32013-05-22 12:50:42 -04003940static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003941 struct nfs_fattr *fattr, struct nfs4_label *label,
3942 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943{
3944 struct nfs4_exception exception = { };
3945 int err;
3946 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04003947 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003948 trace_nfs4_getattr(server, fhandle, fattr, err);
3949 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 &exception);
3951 } while (exception.retry);
3952 return err;
3953}
3954
3955/*
3956 * The file is not closed if it is opened due to the a request to change
3957 * the size of the file. The open call will not be needed once the
3958 * VFS layer lookup-intents are implemented.
3959 *
3960 * Close is called when the inode is destroyed.
3961 * If we haven't opened the file for O_WRONLY, we
3962 * need to in the size_change case to obtain a stateid.
3963 *
3964 * Got race?
3965 * Because OPEN is always done by name in nfsv4, it is
3966 * possible that we opened a different file by the same
3967 * name. We can recognize this race condition, but we
3968 * can't do anything about it besides returning an error.
3969 *
3970 * This will be fixed with VFS changes (lookup-intent).
3971 */
3972static int
3973nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3974 struct iattr *sattr)
3975{
David Howells2b0143b2015-03-17 22:25:59 +00003976 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11003977 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003978 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003979 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 int status;
3981
Peng Tao88ac8152014-09-12 11:04:10 +08003982 if (pnfs_ld_layoutret_on_setattr(inode) &&
3983 sattr->ia_valid & ATTR_SIZE &&
3984 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003985 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003986
Trond Myklebust0e574af2005-10-27 22:12:38 -04003987 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Andy Adamson26699402012-05-30 16:12:24 -04003989 /* Deal with open(O_TRUNC) */
3990 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003991 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003992
3993 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003994 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003995 return 0;
3996
Trond Myklebustd5308382005-11-04 15:33:38 -05003997 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003998 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003999
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004000 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004001 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004002 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004003 }
4004
David Quigley14c43f72013-05-22 12:50:43 -04004005 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4006 if (IS_ERR(label))
4007 return PTR_ERR(label);
4008
Trond Myklebust199366f2018-03-20 16:43:18 -04004009 /* Return any delegations if we're going to change ACLs */
4010 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004011 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004012
NeilBrown29b59f92016-10-13 15:26:47 +11004013 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004014 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004015 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004016 nfs_setsecurity(inode, fattr, label);
4017 }
David Quigley14c43f72013-05-22 12:50:43 -04004018 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 return status;
4020}
4021
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004022static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4023 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004024 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004025{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004026 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004027 int status;
4028 struct nfs4_lookup_arg args = {
4029 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004030 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04004031 .name = name,
4032 };
4033 struct nfs4_lookup_res res = {
4034 .server = server,
4035 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004036 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004037 .fh = fhandle,
4038 };
4039 struct rpc_message msg = {
4040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4041 .rpc_argp = &args,
4042 .rpc_resp = &res,
4043 };
4044
David Quigleyaa9c2662013-05-22 12:50:44 -04004045 args.bitmask = nfs4_bitmask(server, label);
4046
David Howells2b3de442006-08-22 20:06:09 -04004047 nfs_fattr_init(fattr);
4048
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004050 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 dprintk("NFS reply lookup: %d\n", status);
4052 return status;
4053}
4054
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004055static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004056{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004057 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004058 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004059 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4060 fattr->nlink = 2;
4061}
4062
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004063static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004064 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004065 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004066{
4067 struct nfs4_exception exception = { };
4068 struct rpc_clnt *client = *clnt;
4069 int err;
4070 do {
David Quigley1775fd32013-05-22 12:50:42 -04004071 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004072 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004073 switch (err) {
4074 case -NFS4ERR_BADNAME:
4075 err = -ENOENT;
4076 goto out;
4077 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004078 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004079 if (err == -NFS4ERR_MOVED)
4080 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004081 goto out;
4082 case -NFS4ERR_WRONGSEC:
4083 err = -EPERM;
4084 if (client != *clnt)
4085 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004086 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004087 if (IS_ERR(client))
4088 return PTR_ERR(client);
4089
4090 exception.retry = 1;
4091 break;
4092 default:
4093 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4094 }
4095 } while (exception.retry);
4096
4097out:
4098 if (err == 0)
4099 *clnt = client;
4100 else if (client != *clnt)
4101 rpc_shutdown_client(client);
4102
4103 return err;
4104}
4105
Al Virobeffb8f2016-07-20 16:34:42 -04004106static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004107 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4108 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004110 int status;
4111 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004112
David Quigley1775fd32013-05-22 12:50:42 -04004113 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004114 if (client != NFS_CLIENT(dir)) {
4115 rpc_shutdown_client(client);
4116 nfs_fixup_secinfo_attributes(fattr);
4117 }
4118 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119}
4120
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004121struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004122nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004123 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4124{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004125 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004126 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004127
David Quigley1775fd32013-05-22 12:50:42 -04004128 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004129 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004130 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004131 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004132}
4133
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004134static int _nfs4_proc_lookupp(struct inode *inode,
4135 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4136 struct nfs4_label *label)
4137{
4138 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4139 struct nfs_server *server = NFS_SERVER(inode);
4140 int status;
4141 struct nfs4_lookupp_arg args = {
4142 .bitmask = server->attr_bitmask,
4143 .fh = NFS_FH(inode),
4144 };
4145 struct nfs4_lookupp_res res = {
4146 .server = server,
4147 .fattr = fattr,
4148 .label = label,
4149 .fh = fhandle,
4150 };
4151 struct rpc_message msg = {
4152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4153 .rpc_argp = &args,
4154 .rpc_resp = &res,
4155 };
4156
4157 args.bitmask = nfs4_bitmask(server, label);
4158
4159 nfs_fattr_init(fattr);
4160
4161 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4162 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4163 &res.seq_res, 0);
4164 dprintk("NFS reply lookupp: %d\n", status);
4165 return status;
4166}
4167
4168static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4169 struct nfs_fattr *fattr, struct nfs4_label *label)
4170{
4171 struct nfs4_exception exception = { };
4172 int err;
4173 do {
4174 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4175 trace_nfs4_lookupp(inode, err);
4176 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4177 &exception);
4178 } while (exception.retry);
4179 return err;
4180}
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4183{
Trond Myklebust76b32992007-08-10 17:45:11 -04004184 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 struct nfs4_accessargs args = {
4186 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004187 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004189 struct nfs4_accessres res = {
4190 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004191 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 struct rpc_message msg = {
4193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4194 .rpc_argp = &args,
4195 .rpc_resp = &res,
4196 .rpc_cred = entry->cred,
4197 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004198 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
Trond Myklebust7c672652018-06-04 15:00:53 -04004200 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004201 res.fattr = nfs_alloc_fattr();
4202 if (res.fattr == NULL)
4203 return -ENOMEM;
4204 args.bitmask = server->cache_consistency_bitmask;
4205 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004206 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004208 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004209 if (res.fattr)
4210 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004212 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 return status;
4214}
4215
4216static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4217{
4218 struct nfs4_exception exception = { };
4219 int err;
4220 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004221 err = _nfs4_proc_access(inode, entry);
4222 trace_nfs4_access(inode, err);
4223 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 &exception);
4225 } while (exception.retry);
4226 return err;
4227}
4228
4229/*
4230 * TODO: For the time being, we don't try to get any attributes
4231 * along with any of the zero-copy operations READ, READDIR,
4232 * READLINK, WRITE.
4233 *
4234 * In the case of the first three, we want to put the GETATTR
4235 * after the read-type operation -- this is because it is hard
4236 * to predict the length of a GETATTR response in v4, and thus
4237 * align the READ data correctly. This means that the GETATTR
4238 * may end up partially falling into the page cache, and we should
4239 * shift it into the 'tail' of the xdr_buf before processing.
4240 * To do this efficiently, we need to know the total length
4241 * of data received, which doesn't seem to be available outside
4242 * of the RPC layer.
4243 *
4244 * In the case of WRITE, we also want to put the GETATTR after
4245 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004246 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 *
4248 * Both of these changes to the XDR layer would in fact be quite
4249 * minor, but I decided to leave them for a subsequent patch.
4250 */
4251static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4252 unsigned int pgbase, unsigned int pglen)
4253{
4254 struct nfs4_readlink args = {
4255 .fh = NFS_FH(inode),
4256 .pgbase = pgbase,
4257 .pglen = pglen,
4258 .pages = &page,
4259 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004260 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 struct rpc_message msg = {
4262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4263 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004264 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 };
4266
Bryan Schumaker7c513052011-03-24 17:12:24 +00004267 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 -07004268}
4269
4270static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4271 unsigned int pgbase, unsigned int pglen)
4272{
4273 struct nfs4_exception exception = { };
4274 int err;
4275 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004276 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4277 trace_nfs4_readlink(inode, err);
4278 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 &exception);
4280 } while (exception.retry);
4281 return err;
4282}
4283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004285 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287static int
4288nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004289 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004291 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004292 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004293 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 int status = 0;
4296
NeilBrown532d4de2016-10-13 15:26:47 +11004297 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004298 if (IS_ERR(ctx))
4299 return PTR_ERR(ctx);
4300
David Quigleyaa9c2662013-05-22 12:50:44 -04004301 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4302
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004303 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4304 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004305 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 if (IS_ERR(state)) {
4307 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004311 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004312 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 return status;
4314}
4315
Trond Myklebust3c591172018-07-31 15:54:10 -04004316static int
4317_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
Trond Myklebust16e42952005-10-27 22:12:44 -04004319 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004320 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004322 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004324 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004325 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004326 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4329 .rpc_argp = &args,
4330 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004332 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004333 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004334
Bryan Schumaker7c513052011-03-24 17:12:24 +00004335 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004336 if (status == 0) {
4337 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004338 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004339 /* Removing a directory decrements nlink in the parent */
4340 if (ftype == NF4DIR && dir->i_nlink > 2)
4341 nfs4_dec_nlink_locked(dir);
4342 spin_unlock(&dir->i_lock);
4343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 return status;
4345}
4346
Trond Myklebust912678d2018-03-20 16:43:15 -04004347static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4348{
4349 struct nfs4_exception exception = { };
4350 struct inode *inode = d_inode(dentry);
4351 int err;
4352
Trond Myklebustc01d3642018-03-20 16:43:20 -04004353 if (inode) {
4354 if (inode->i_nlink == 1)
4355 nfs4_inode_return_delegation(inode);
4356 else
4357 nfs4_inode_make_writeable(inode);
4358 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004359 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004360 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004361 trace_nfs4_remove(dir, &dentry->d_name, err);
4362 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4363 &exception);
4364 } while (exception.retry);
4365 return err;
4366}
4367
4368static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369{
4370 struct nfs4_exception exception = { };
4371 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004372
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004374 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004375 trace_nfs4_remove(dir, name, err);
4376 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 &exception);
4378 } while (exception.retry);
4379 return err;
4380}
4381
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004382static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4383 struct dentry *dentry,
4384 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004386 struct nfs_removeargs *args = msg->rpc_argp;
4387 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Trond Myklebust977fcc22018-03-20 16:43:17 -04004389 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004391 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004392
4393 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004394
4395 if (inode)
4396 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397}
4398
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004399static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4400{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004401 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004402 &data->args.seq_args,
4403 &data->res.seq_res,
4404 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405}
4406
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004407static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004409 struct nfs_unlinkdata *data = task->tk_calldata;
4410 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004411
Trond Myklebust14516c32010-07-31 14:29:06 -04004412 if (!nfs4_sequence_done(task, &res->seq_res))
4413 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004414 if (nfs4_async_handle_error(task, res->server, NULL,
4415 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004416 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004417 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004418 update_changeattr(dir, &res->cinfo,
4419 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004420 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421}
4422
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004423static void nfs4_proc_rename_setup(struct rpc_message *msg,
4424 struct dentry *old_dentry,
4425 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004426{
Jeff Laytond3d41522010-09-17 17:31:57 -04004427 struct nfs_renameargs *arg = msg->rpc_argp;
4428 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004429 struct inode *old_inode = d_inode(old_dentry);
4430 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004431
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004432 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004433 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004434 if (new_inode)
4435 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004436 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004437 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004438 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004439}
4440
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004441static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4442{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004443 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004444 &data->args.seq_args,
4445 &data->res.seq_res,
4446 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004447}
4448
4449static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4450 struct inode *new_dir)
4451{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004452 struct nfs_renamedata *data = task->tk_calldata;
4453 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004454
4455 if (!nfs4_sequence_done(task, &res->seq_res))
4456 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004457 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004458 return 0;
4459
Trond Myklebustc733c492017-01-11 12:32:26 -05004460 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004461 if (new_dir != old_dir) {
4462 /* Note: If we moved a directory, nlink will change */
4463 update_changeattr(old_dir, &res->old_cinfo,
4464 res->old_fattr->time_start,
4465 NFS_INO_INVALID_OTHER);
4466 update_changeattr(new_dir, &res->new_cinfo,
4467 res->new_fattr->time_start,
4468 NFS_INO_INVALID_OTHER);
4469 } else
4470 update_changeattr(old_dir, &res->old_cinfo,
4471 res->old_fattr->time_start,
4472 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004473 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004474 return 1;
4475}
4476
Al Virobeffb8f2016-07-20 16:34:42 -04004477static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004479 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004480 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 struct nfs4_link_arg arg = {
4482 .fh = NFS_FH(inode),
4483 .dir_fh = NFS_FH(dir),
4484 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004485 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004487 struct nfs4_link_res res = {
4488 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004489 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004490 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 struct rpc_message msg = {
4492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4493 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004494 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004496 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Trond Myklebust136f2622010-04-16 16:22:49 -04004498 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004499 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004500 goto out;
4501
David Quigley14c43f72013-05-22 12:50:43 -04004502 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4503 if (IS_ERR(res.label)) {
4504 status = PTR_ERR(res.label);
4505 goto out;
4506 }
4507
Trond Myklebustc01d3642018-03-20 16:43:20 -04004508 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004509 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004510
Bryan Schumaker7c513052011-03-24 17:12:24 +00004511 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004512 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004513 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004514 status = nfs_post_op_update_inode(inode, res.fattr);
4515 if (!status)
4516 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004517 }
David Quigley14c43f72013-05-22 12:50:43 -04004518
4519
4520 nfs4_label_free(res.label);
4521
Trond Myklebust136f2622010-04-16 16:22:49 -04004522out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004523 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 return status;
4525}
4526
Al Virobeffb8f2016-07-20 16:34:42 -04004527static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528{
4529 struct nfs4_exception exception = { };
4530 int err;
4531 do {
4532 err = nfs4_handle_exception(NFS_SERVER(inode),
4533 _nfs4_proc_link(inode, dir, name),
4534 &exception);
4535 } while (exception.retry);
4536 return err;
4537}
4538
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004539struct nfs4_createdata {
4540 struct rpc_message msg;
4541 struct nfs4_create_arg arg;
4542 struct nfs4_create_res res;
4543 struct nfs_fh fh;
4544 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004545 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004546};
4547
4548static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004549 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004550{
4551 struct nfs4_createdata *data;
4552
4553 data = kzalloc(sizeof(*data), GFP_KERNEL);
4554 if (data != NULL) {
4555 struct nfs_server *server = NFS_SERVER(dir);
4556
David Quigley14c43f72013-05-22 12:50:43 -04004557 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4558 if (IS_ERR(data->label))
4559 goto out_free;
4560
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004561 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4562 data->msg.rpc_argp = &data->arg;
4563 data->msg.rpc_resp = &data->res;
4564 data->arg.dir_fh = NFS_FH(dir);
4565 data->arg.server = server;
4566 data->arg.name = name;
4567 data->arg.attrs = sattr;
4568 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004569 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004570 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004571 data->res.server = server;
4572 data->res.fh = &data->fh;
4573 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004574 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004575 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004576 }
4577 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004578out_free:
4579 kfree(data);
4580 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004581}
4582
4583static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4584{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004585 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004586 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004587 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004588 spin_lock(&dir->i_lock);
4589 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004590 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004591 /* Creating a directory bumps nlink in the parent */
4592 if (data->arg.ftype == NF4DIR)
4593 nfs4_inc_nlink_locked(dir);
4594 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004595 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004596 }
4597 return status;
4598}
4599
4600static void nfs4_free_createdata(struct nfs4_createdata *data)
4601{
David Quigley14c43f72013-05-22 12:50:43 -04004602 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004603 kfree(data);
4604}
4605
Chuck Lever4f390c12006-08-22 20:06:22 -04004606static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004607 struct page *page, unsigned int len, struct iattr *sattr,
4608 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004610 struct nfs4_createdata *data;
4611 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Chuck Lever94a6d752006-08-22 20:06:23 -04004613 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004614 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004615
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004616 status = -ENOMEM;
4617 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4618 if (data == NULL)
4619 goto out;
4620
4621 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4622 data->arg.u.symlink.pages = &page;
4623 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004624 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004626 status = nfs4_do_create(dir, dentry, data);
4627
4628 nfs4_free_createdata(data);
4629out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 return status;
4631}
4632
Chuck Lever4f390c12006-08-22 20:06:22 -04004633static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004634 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
4636 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004637 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004639
4640 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004643 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4644 trace_nfs4_symlink(dir, &dentry->d_name, err);
4645 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 &exception);
4647 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004648
4649 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 return err;
4651}
4652
4653static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004654 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004656 struct nfs4_createdata *data;
4657 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004659 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4660 if (data == NULL)
4661 goto out;
4662
David Quigley1775fd32013-05-22 12:50:42 -04004663 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004664 status = nfs4_do_create(dir, dentry, data);
4665
4666 nfs4_free_createdata(data);
4667out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 return status;
4669}
4670
4671static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4672 struct iattr *sattr)
4673{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004674 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004676 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004678
David Quigleyaa9c2662013-05-22 12:50:44 -04004679 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4680
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004681 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4682 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004684 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4685 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4686 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 &exception);
4688 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004689 nfs4_label_release_security(label);
4690
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 return err;
4692}
4693
NeilBrown684f39b2018-12-03 11:30:30 +11004694static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004695 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696{
David Howells2b0143b2015-03-17 22:25:59 +00004697 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 struct nfs4_readdir_arg args = {
4699 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004700 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 .pgbase = 0,
4702 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004703 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004704 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 };
4706 struct nfs4_readdir_res res;
4707 struct rpc_message msg = {
4708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4709 .rpc_argp = &args,
4710 .rpc_resp = &res,
4711 .rpc_cred = cred,
4712 };
4713 int status;
4714
Al Viro6de14722013-09-16 10:53:17 -04004715 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4716 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004717 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004718 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004720 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 -05004721 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004722 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004723 status += args.pgbase;
4724 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004725
4726 nfs_invalidate_atime(dir);
4727
Harvey Harrison3110ff82008-05-02 13:42:44 -07004728 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 return status;
4730}
4731
NeilBrown684f39b2018-12-03 11:30:30 +11004732static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004733 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
4735 struct nfs4_exception exception = { };
4736 int err;
4737 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004738 err = _nfs4_proc_readdir(dentry, cred, cookie,
4739 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004740 trace_nfs4_readdir(d_inode(dentry), err);
4741 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 &exception);
4743 } while (exception.retry);
4744 return err;
4745}
4746
4747static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004748 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004750 struct nfs4_createdata *data;
4751 int mode = sattr->ia_mode;
4752 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004754 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4755 if (data == NULL)
4756 goto out;
4757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004759 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004761 data->arg.ftype = NF4BLK;
4762 data->arg.u.device.specdata1 = MAJOR(rdev);
4763 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 }
4765 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004766 data->arg.ftype = NF4CHR;
4767 data->arg.u.device.specdata1 = MAJOR(rdev);
4768 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004769 } else if (!S_ISSOCK(mode)) {
4770 status = -EINVAL;
4771 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 }
David Quigley1775fd32013-05-22 12:50:42 -04004773
David Quigleyaa9c2662013-05-22 12:50:44 -04004774 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004775 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004776out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004777 nfs4_free_createdata(data);
4778out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 return status;
4780}
4781
4782static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4783 struct iattr *sattr, dev_t rdev)
4784{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004785 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004787 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004789
David Quigleyaa9c2662013-05-22 12:50:44 -04004790 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4791
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004792 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4793 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004795 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4796 trace_nfs4_mknod(dir, &dentry->d_name, err);
4797 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 &exception);
4799 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004800
4801 nfs4_label_release_security(label);
4802
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 return err;
4804}
4805
4806static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4807 struct nfs_fsstat *fsstat)
4808{
4809 struct nfs4_statfs_arg args = {
4810 .fh = fhandle,
4811 .bitmask = server->attr_bitmask,
4812 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004813 struct nfs4_statfs_res res = {
4814 .fsstat = fsstat,
4815 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 struct rpc_message msg = {
4817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4818 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004819 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 };
4821
Trond Myklebust0e574af2005-10-27 22:12:38 -04004822 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004823 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824}
4825
4826static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4827{
4828 struct nfs4_exception exception = { };
4829 int err;
4830 do {
4831 err = nfs4_handle_exception(server,
4832 _nfs4_proc_statfs(server, fhandle, fsstat),
4833 &exception);
4834 } while (exception.retry);
4835 return err;
4836}
4837
4838static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4839 struct nfs_fsinfo *fsinfo)
4840{
4841 struct nfs4_fsinfo_arg args = {
4842 .fh = fhandle,
4843 .bitmask = server->attr_bitmask,
4844 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004845 struct nfs4_fsinfo_res res = {
4846 .fsinfo = fsinfo,
4847 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 struct rpc_message msg = {
4849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4850 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004851 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 };
4853
Bryan Schumaker7c513052011-03-24 17:12:24 +00004854 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855}
4856
4857static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4858{
4859 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004860 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 int err;
4862
4863 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004864 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004865 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004866 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004867 nfs4_set_lease_period(server->nfs_client,
4868 fsinfo->lease_time * HZ,
4869 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004870 break;
4871 }
4872 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 } while (exception.retry);
4874 return err;
4875}
4876
4877static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4878{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004879 int error;
4880
Trond Myklebust0e574af2005-10-27 22:12:38 -04004881 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004882 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004883 if (error == 0) {
4884 /* block layout checks this! */
4885 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004886 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004887 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004888
4889 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890}
4891
4892static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4893 struct nfs_pathconf *pathconf)
4894{
4895 struct nfs4_pathconf_arg args = {
4896 .fh = fhandle,
4897 .bitmask = server->attr_bitmask,
4898 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004899 struct nfs4_pathconf_res res = {
4900 .pathconf = pathconf,
4901 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 struct rpc_message msg = {
4903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4904 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004905 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 };
4907
4908 /* None of the pathconf attributes are mandatory to implement */
4909 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4910 memset(pathconf, 0, sizeof(*pathconf));
4911 return 0;
4912 }
4913
Trond Myklebust0e574af2005-10-27 22:12:38 -04004914 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004915 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916}
4917
4918static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4919 struct nfs_pathconf *pathconf)
4920{
4921 struct nfs4_exception exception = { };
4922 int err;
4923
4924 do {
4925 err = nfs4_handle_exception(server,
4926 _nfs4_proc_pathconf(server, fhandle, pathconf),
4927 &exception);
4928 } while (exception.retry);
4929 return err;
4930}
4931
Trond Myklebust5521abf2013-03-16 20:54:34 -04004932int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004933 const struct nfs_open_context *ctx,
4934 const struct nfs_lock_context *l_ctx,
4935 fmode_t fmode)
4936{
NeilBrown17393472016-10-13 15:26:47 +11004937 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004938}
4939EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4940
Trond Myklebust5521abf2013-03-16 20:54:34 -04004941static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4942 const struct nfs_open_context *ctx,
4943 const struct nfs_lock_context *l_ctx,
4944 fmode_t fmode)
4945{
4946 nfs4_stateid current_stateid;
4947
Trond Myklebuste1253be2014-03-05 08:44:23 -05004948 /* If the current stateid represents a lost lock, then exit */
4949 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4950 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004951 return nfs4_stateid_match(stateid, &current_stateid);
4952}
4953
4954static bool nfs4_error_stateid_expired(int err)
4955{
4956 switch (err) {
4957 case -NFS4ERR_DELEG_REVOKED:
4958 case -NFS4ERR_ADMIN_REVOKED:
4959 case -NFS4ERR_BAD_STATEID:
4960 case -NFS4ERR_STALE_STATEID:
4961 case -NFS4ERR_OLD_STATEID:
4962 case -NFS4ERR_OPENMODE:
4963 case -NFS4ERR_EXPIRED:
4964 return true;
4965 }
4966 return false;
4967}
4968
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004969static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004971 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004973 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004974 if (task->tk_status < 0) {
4975 struct nfs4_exception exception = {
4976 .inode = hdr->inode,
4977 .state = hdr->args.context->state,
4978 .stateid = &hdr->args.stateid,
4979 };
4980 task->tk_status = nfs4_async_handle_exception(task,
4981 server, task->tk_status, &exception);
4982 if (exception.retry) {
4983 rpc_restart_call_prepare(task);
4984 return -EAGAIN;
4985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004987
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004989 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004990 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991}
4992
Trond Myklebust5521abf2013-03-16 20:54:34 -04004993static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004994 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004995{
4996
4997 if (!nfs4_error_stateid_expired(task->tk_status) ||
4998 nfs4_stateid_is_current(&args->stateid,
4999 args->context,
5000 args->lock_context,
5001 FMODE_READ))
5002 return false;
5003 rpc_restart_call_prepare(task);
5004 return true;
5005}
5006
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005007static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005008{
5009
5010 dprintk("--> %s\n", __func__);
5011
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005012 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005013 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005014 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005015 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005016 if (task->tk_status > 0)
5017 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005018 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5019 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005020}
5021
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005022static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5023 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005025 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005026 if (!hdr->pgio_done_cb)
5027 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005028 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005029 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030}
5031
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005032static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5033 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005034{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005035 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005036 &hdr->args.seq_args,
5037 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005038 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005039 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005040 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5041 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005042 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005043 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005044 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005045 return -EIO;
5046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047}
5048
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005049static int nfs4_write_done_cb(struct rpc_task *task,
5050 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005052 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005053
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005054 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005055 if (task->tk_status < 0) {
5056 struct nfs4_exception exception = {
5057 .inode = hdr->inode,
5058 .state = hdr->args.context->state,
5059 .stateid = &hdr->args.stateid,
5060 };
5061 task->tk_status = nfs4_async_handle_exception(task,
5062 NFS_SERVER(inode), task->tk_status,
5063 &exception);
5064 if (exception.retry) {
5065 rpc_restart_call_prepare(task);
5066 return -EAGAIN;
5067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005069 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005070 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005071 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005072 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074}
5075
Trond Myklebust5521abf2013-03-16 20:54:34 -04005076static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005077 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005078{
5079
5080 if (!nfs4_error_stateid_expired(task->tk_status) ||
5081 nfs4_stateid_is_current(&args->stateid,
5082 args->context,
5083 args->lock_context,
5084 FMODE_WRITE))
5085 return false;
5086 rpc_restart_call_prepare(task);
5087 return true;
5088}
5089
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005090static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005091{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005092 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005093 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005094 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005095 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005096 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5097 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005098}
5099
Trond Myklebust5a37f852012-04-28 14:55:16 -04005100static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005101bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005102{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005103 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005104 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005105 return false;
5106 /* Otherwise, request attributes if and only if we don't hold
5107 * a delegation
5108 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005109 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005110}
Fred Isamana69aef12011-03-03 15:13:47 +00005111
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005112static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005113 struct rpc_message *msg,
5114 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005116 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005117
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005118 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5119 hdr->args.bitmask = NULL;
5120 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005121 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005122 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005123
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005124 if (!hdr->pgio_done_cb)
5125 hdr->pgio_done_cb = nfs4_write_done_cb;
5126 hdr->res.server = server;
5127 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005129 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005130 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005131 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132}
5133
Fred Isaman0b7c0152012-04-20 14:47:39 -04005134static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5135{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005136 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005137 &data->args.seq_args,
5138 &data->res.seq_res,
5139 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140}
5141
Fred Isaman0b7c0152012-04-20 14:47:39 -04005142static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005145
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005146 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005147 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5148 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005149 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005150 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153}
5154
Fred Isaman0b7c0152012-04-20 14:47:39 -04005155static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005156{
5157 if (!nfs4_sequence_done(task, &data->res.seq_res))
5158 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005159 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005160}
5161
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005162static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5163 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005165 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005166
Fred Isaman0b7c0152012-04-20 14:47:39 -04005167 if (data->commit_done_cb == NULL)
5168 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005169 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005170 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005171 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005172 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173}
5174
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005175static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5176 struct nfs_commitres *res)
5177{
5178 struct inode *dst_inode = file_inode(dst);
5179 struct nfs_server *server = NFS_SERVER(dst_inode);
5180 struct rpc_message msg = {
5181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5182 .rpc_argp = args,
5183 .rpc_resp = res,
5184 };
5185
5186 args->fh = NFS_FH(dst_inode);
5187 return nfs4_call_sync(server->client, server, &msg,
5188 &args->seq_args, &res->seq_res, 1);
5189}
5190
5191int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5192{
5193 struct nfs_commitargs args = {
5194 .offset = offset,
5195 .count = count,
5196 };
5197 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5198 struct nfs4_exception exception = { };
5199 int status;
5200
5201 do {
5202 status = _nfs4_proc_commit(dst, &args, res);
5203 status = nfs4_handle_exception(dst_server, status, &exception);
5204 } while (exception.retry);
5205
5206 return status;
5207}
5208
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005209struct nfs4_renewdata {
5210 struct nfs_client *client;
5211 unsigned long timestamp;
5212};
5213
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214/*
5215 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5216 * standalone procedure for queueing an asynchronous RENEW.
5217 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005218static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005219{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005220 struct nfs4_renewdata *data = calldata;
5221 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005222
Elena Reshetova212bf412017-10-20 12:53:38 +03005223 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005224 nfs4_schedule_state_renewal(clp);
5225 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005226 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005227}
5228
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005229static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005231 struct nfs4_renewdata *data = calldata;
5232 struct nfs_client *clp = data->client;
5233 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005235 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005236 switch (task->tk_status) {
5237 case 0:
5238 break;
5239 case -NFS4ERR_LEASE_MOVED:
5240 nfs4_schedule_lease_moved_recovery(clp);
5241 break;
5242 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005243 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005244 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5245 return;
5246 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005247 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005248 return;
5249 }
5250 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005252 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253}
5254
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005255static const struct rpc_call_ops nfs4_renew_ops = {
5256 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005257 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005258};
5259
NeilBrowna52458b2018-12-03 11:30:31 +11005260static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261{
5262 struct rpc_message msg = {
5263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5264 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005265 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005267 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005269 if (renew_flags == 0)
5270 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005271 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005272 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005273 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005274 if (data == NULL) {
5275 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005276 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005277 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005278 data->client = clp;
5279 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005280 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005281 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282}
5283
NeilBrowna52458b2018-12-03 11:30:31 +11005284static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285{
5286 struct rpc_message msg = {
5287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5288 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005289 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 };
5291 unsigned long now = jiffies;
5292 int status;
5293
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005294 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 if (status < 0)
5296 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005297 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 return 0;
5299}
5300
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005301static inline int nfs4_server_supports_acls(struct nfs_server *server)
5302{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005303 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005304}
5305
Trond Myklebust21f498c2012-08-24 10:59:25 -04005306/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5307 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005308 * the stack.
5309 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005310#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005311
Neil Hormane9e3d722011-03-04 19:26:03 -05005312static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005313 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005314{
5315 struct page *newpage, **spages;
5316 int rc = 0;
5317 size_t len;
5318 spages = pages;
5319
5320 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005321 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005322 newpage = alloc_page(GFP_KERNEL);
5323
5324 if (newpage == NULL)
5325 goto unwind;
5326 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005327 buf += len;
5328 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005329 *pages++ = newpage;
5330 rc++;
5331 } while (buflen != 0);
5332
5333 return rc;
5334
5335unwind:
5336 for(; rc > 0; rc--)
5337 __free_page(spages[rc-1]);
5338 return -ENOMEM;
5339}
5340
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005341struct nfs4_cached_acl {
5342 int cached;
5343 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005344 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005345};
5346
5347static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005348{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005349 struct nfs_inode *nfsi = NFS_I(inode);
5350
5351 spin_lock(&inode->i_lock);
5352 kfree(nfsi->nfs4_acl);
5353 nfsi->nfs4_acl = acl;
5354 spin_unlock(&inode->i_lock);
5355}
5356
5357static void nfs4_zap_acl_attr(struct inode *inode)
5358{
5359 nfs4_set_cached_acl(inode, NULL);
5360}
5361
5362static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5363{
5364 struct nfs_inode *nfsi = NFS_I(inode);
5365 struct nfs4_cached_acl *acl;
5366 int ret = -ENOENT;
5367
5368 spin_lock(&inode->i_lock);
5369 acl = nfsi->nfs4_acl;
5370 if (acl == NULL)
5371 goto out;
5372 if (buf == NULL) /* user is just asking for length */
5373 goto out_len;
5374 if (acl->cached == 0)
5375 goto out;
5376 ret = -ERANGE; /* see getxattr(2) man page */
5377 if (acl->len > buflen)
5378 goto out;
5379 memcpy(buf, acl->data, acl->len);
5380out_len:
5381 ret = acl->len;
5382out:
5383 spin_unlock(&inode->i_lock);
5384 return ret;
5385}
5386
Sachin Prabhu5794d212012-04-17 14:36:40 +01005387static 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 +00005388{
5389 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005390 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005391
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005392 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005393 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005394 if (acl == NULL)
5395 goto out;
5396 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005397 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005398 } else {
5399 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5400 if (acl == NULL)
5401 goto out;
5402 acl->cached = 0;
5403 }
5404 acl->len = acl_len;
5405out:
5406 nfs4_set_cached_acl(inode, acl);
5407}
5408
Andy Adamsonbf118a32011-12-07 11:55:27 -05005409/*
5410 * The getxattr API returns the required buffer length when called with a
5411 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5412 * the required buf. On a NULL buf, we send a page of data to the server
5413 * guessing that the ACL request can be serviced by a page. If so, we cache
5414 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5415 * the cache. If not so, we throw away the page, and cache the required
5416 * length. The next getxattr call will then produce another round trip to
5417 * the server, this time with the input buf of the required size.
5418 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005419static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005420{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005421 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005422 struct nfs_getaclargs args = {
5423 .fh = NFS_FH(inode),
5424 .acl_pages = pages,
5425 .acl_len = buflen,
5426 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005427 struct nfs_getaclres res = {
5428 .acl_len = buflen,
5429 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005430 struct rpc_message msg = {
5431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5432 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005433 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005434 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005435 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005436 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005437
Trond Myklebust21f498c2012-08-24 10:59:25 -04005438 if (npages > ARRAY_SIZE(pages))
5439 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005440
Andy Adamsonbf118a32011-12-07 11:55:27 -05005441 for (i = 0; i < npages; i++) {
5442 pages[i] = alloc_page(GFP_KERNEL);
5443 if (!pages[i])
5444 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005445 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005446
5447 /* for decoding across pages */
5448 res.acl_scratch = alloc_page(GFP_KERNEL);
5449 if (!res.acl_scratch)
5450 goto out_free;
5451
Andy Adamsonbf118a32011-12-07 11:55:27 -05005452 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005453
Peng Taode040be2012-01-10 22:42:47 +08005454 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005455 __func__, buf, buflen, npages, args.acl_len);
5456 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5457 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005458 if (ret)
5459 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005460
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005461 /* Handle the case where the passed-in buffer is too short */
5462 if (res.acl_flags & NFS4_ACL_TRUNC) {
5463 /* Did the user only issue a request for the acl length? */
5464 if (buf == NULL)
5465 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005466 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005467 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005468 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005469 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005470 if (buf) {
5471 if (res.acl_len > buflen) {
5472 ret = -ERANGE;
5473 goto out_free;
5474 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005475 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005476 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005477out_ok:
5478 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005479out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005480 for (i = 0; i < npages; i++)
5481 if (pages[i])
5482 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005483 if (res.acl_scratch)
5484 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005485 return ret;
5486}
5487
Trond Myklebust16b42892006-08-24 12:27:15 -04005488static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5489{
5490 struct nfs4_exception exception = { };
5491 ssize_t ret;
5492 do {
5493 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005494 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005495 if (ret >= 0)
5496 break;
5497 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5498 } while (exception.retry);
5499 return ret;
5500}
5501
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005502static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5503{
5504 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005505 int ret;
5506
5507 if (!nfs4_server_supports_acls(server))
5508 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005509 ret = nfs_revalidate_inode(server, inode);
5510 if (ret < 0)
5511 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005512 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5513 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005514 ret = nfs4_read_cached_acl(inode, buf, buflen);
5515 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005516 /* -ENOENT is returned if there is no ACL or if there is an ACL
5517 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005518 return ret;
5519 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005520}
5521
Trond Myklebust16b42892006-08-24 12:27:15 -04005522static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005523{
5524 struct nfs_server *server = NFS_SERVER(inode);
5525 struct page *pages[NFS4ACL_MAXPAGES];
5526 struct nfs_setaclargs arg = {
5527 .fh = NFS_FH(inode),
5528 .acl_pages = pages,
5529 .acl_len = buflen,
5530 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005531 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005532 struct rpc_message msg = {
5533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5534 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005535 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005536 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005537 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005538 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005539
5540 if (!nfs4_server_supports_acls(server))
5541 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005542 if (npages > ARRAY_SIZE(pages))
5543 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005544 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005545 if (i < 0)
5546 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005547 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005548 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005549
5550 /*
5551 * Free each page after tx, so the only ref left is
5552 * held by the network stack
5553 */
5554 for (; i > 0; i--)
5555 put_page(pages[i-1]);
5556
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005557 /*
5558 * Acl update can result in inode attribute update.
5559 * so mark the attribute cache invalid.
5560 */
5561 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005562 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005563 | NFS_INO_INVALID_CTIME
5564 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005565 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005566 nfs_access_zap_cache(inode);
5567 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005568 return ret;
5569}
5570
Trond Myklebust16b42892006-08-24 12:27:15 -04005571static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5572{
5573 struct nfs4_exception exception = { };
5574 int err;
5575 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005576 err = __nfs4_proc_set_acl(inode, buf, buflen);
5577 trace_nfs4_set_acl(inode, err);
5578 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005579 &exception);
5580 } while (exception.retry);
5581 return err;
5582}
5583
David Quigleyaa9c2662013-05-22 12:50:44 -04005584#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5585static int _nfs4_get_security_label(struct inode *inode, void *buf,
5586 size_t buflen)
5587{
5588 struct nfs_server *server = NFS_SERVER(inode);
5589 struct nfs_fattr fattr;
5590 struct nfs4_label label = {0, 0, buflen, buf};
5591
5592 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005593 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005594 .fh = NFS_FH(inode),
5595 .bitmask = bitmask,
5596 };
5597 struct nfs4_getattr_res res = {
5598 .fattr = &fattr,
5599 .label = &label,
5600 .server = server,
5601 };
5602 struct rpc_message msg = {
5603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005604 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005605 .rpc_resp = &res,
5606 };
5607 int ret;
5608
5609 nfs_fattr_init(&fattr);
5610
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005611 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005612 if (ret)
5613 return ret;
5614 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5615 return -ENOENT;
5616 if (buflen < label.len)
5617 return -ERANGE;
5618 return 0;
5619}
5620
5621static int nfs4_get_security_label(struct inode *inode, void *buf,
5622 size_t buflen)
5623{
5624 struct nfs4_exception exception = { };
5625 int err;
5626
5627 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5628 return -EOPNOTSUPP;
5629
5630 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005631 err = _nfs4_get_security_label(inode, buf, buflen);
5632 trace_nfs4_get_security_label(inode, err);
5633 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005634 &exception);
5635 } while (exception.retry);
5636 return err;
5637}
5638
5639static int _nfs4_do_set_security_label(struct inode *inode,
5640 struct nfs4_label *ilabel,
5641 struct nfs_fattr *fattr,
5642 struct nfs4_label *olabel)
5643{
5644
5645 struct iattr sattr = {0};
5646 struct nfs_server *server = NFS_SERVER(inode);
5647 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005648 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005649 .fh = NFS_FH(inode),
5650 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005651 .server = server,
5652 .bitmask = bitmask,
5653 .label = ilabel,
5654 };
5655 struct nfs_setattrres res = {
5656 .fattr = fattr,
5657 .label = olabel,
5658 .server = server,
5659 };
5660 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5662 .rpc_argp = &arg,
5663 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005664 };
5665 int status;
5666
Jeff Layton12207f62013-11-01 10:49:32 -04005667 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005668
Jeff Layton12207f62013-11-01 10:49:32 -04005669 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005670 if (status)
5671 dprintk("%s failed: %d\n", __func__, status);
5672
5673 return status;
5674}
5675
5676static int nfs4_do_set_security_label(struct inode *inode,
5677 struct nfs4_label *ilabel,
5678 struct nfs_fattr *fattr,
5679 struct nfs4_label *olabel)
5680{
5681 struct nfs4_exception exception = { };
5682 int err;
5683
5684 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005685 err = _nfs4_do_set_security_label(inode, ilabel,
5686 fattr, olabel);
5687 trace_nfs4_set_security_label(inode, err);
5688 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005689 &exception);
5690 } while (exception.retry);
5691 return err;
5692}
5693
5694static int
Al Viro59301222016-05-27 10:19:30 -04005695nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005696{
5697 struct nfs4_label ilabel, *olabel = NULL;
5698 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005699 int status;
5700
5701 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5702 return -EOPNOTSUPP;
5703
5704 nfs_fattr_init(&fattr);
5705
5706 ilabel.pi = 0;
5707 ilabel.lfs = 0;
5708 ilabel.label = (char *)buf;
5709 ilabel.len = buflen;
5710
David Quigleyaa9c2662013-05-22 12:50:44 -04005711 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5712 if (IS_ERR(olabel)) {
5713 status = -PTR_ERR(olabel);
5714 goto out;
5715 }
5716
5717 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5718 if (status == 0)
5719 nfs_setsecurity(inode, &fattr, olabel);
5720
5721 nfs4_label_free(olabel);
5722out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005723 return status;
5724}
5725#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5726
5727
Chuck Leverf0920752012-05-21 22:45:41 -04005728static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5729 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005730{
5731 __be32 verf[2];
5732
Chuck Lever2c820d92012-05-21 22:45:33 -04005733 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5734 /* An impossible timestamp guarantees this value
5735 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005736 verf[0] = cpu_to_be32(U32_MAX);
5737 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005738 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005739 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005740 u64 ns = ktime_to_ns(nn->boot_time);
5741
5742 verf[0] = cpu_to_be32(ns >> 32);
5743 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005744 }
Chuck Levercd937102012-03-02 17:14:31 -05005745 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5746}
5747
Jeff Laytona3192682015-06-09 19:43:59 -04005748static int
5749nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005750{
Jeff Laytona3192682015-06-09 19:43:59 -04005751 size_t len;
5752 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005753
Trond Myklebustceb3a162015-01-03 15:16:04 -05005754 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005755 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005756
Jeff Laytona3192682015-06-09 19:43:59 -04005757 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005758 len = 14 +
5759 strlen(clp->cl_rpcclient->cl_nodename) +
5760 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005761 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005762 1;
5763 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005764 if (nfs4_client_id_uniquifier[0] != '\0')
5765 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005766 if (len > NFS4_OPAQUE_LIMIT + 1)
5767 return -EINVAL;
5768
5769 /*
5770 * Since this string is allocated at mount time, and held until the
5771 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5772 * about a memory-reclaim deadlock.
5773 */
5774 str = kmalloc(len, GFP_KERNEL);
5775 if (!str)
5776 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005777
Chuck Levere984a552012-09-14 17:24:21 -04005778 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005779 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005780 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005781 clp->cl_rpcclient->cl_nodename,
5782 nfs4_client_id_uniquifier,
5783 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005784 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005785 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005786 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005787 clp->cl_rpcclient->cl_nodename,
5788 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005789 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005790 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005791
Jeff Laytona3192682015-06-09 19:43:59 -04005792 clp->cl_owner_id = str;
5793 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005794}
5795
Jeff Layton873e3852015-06-09 19:44:00 -04005796static int
5797nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005798{
Jeff Layton873e3852015-06-09 19:44:00 -04005799 size_t len;
5800 char *str;
5801
5802 len = 10 + 10 + 1 + 10 + 1 +
5803 strlen(nfs4_client_id_uniquifier) + 1 +
5804 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5805
5806 if (len > NFS4_OPAQUE_LIMIT + 1)
5807 return -EINVAL;
5808
5809 /*
5810 * Since this string is allocated at mount time, and held until the
5811 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5812 * about a memory-reclaim deadlock.
5813 */
5814 str = kmalloc(len, GFP_KERNEL);
5815 if (!str)
5816 return -ENOMEM;
5817
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005818 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005819 clp->rpc_ops->version, clp->cl_minorversion,
5820 nfs4_client_id_uniquifier,
5821 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005822 clp->cl_owner_id = str;
5823 return 0;
5824}
5825
5826static int
5827nfs4_init_uniform_client_string(struct nfs_client *clp)
5828{
Jeff Layton873e3852015-06-09 19:44:00 -04005829 size_t len;
5830 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005831
5832 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005833 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005834
5835 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005836 return nfs4_init_uniquifier_client_string(clp);
5837
5838 len = 10 + 10 + 1 + 10 + 1 +
5839 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5840
5841 if (len > NFS4_OPAQUE_LIMIT + 1)
5842 return -EINVAL;
5843
5844 /*
5845 * Since this string is allocated at mount time, and held until the
5846 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5847 * about a memory-reclaim deadlock.
5848 */
5849 str = kmalloc(len, GFP_KERNEL);
5850 if (!str)
5851 return -ENOMEM;
5852
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005853 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005854 clp->rpc_ops->version, clp->cl_minorversion,
5855 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005856 clp->cl_owner_id = str;
5857 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005858}
5859
Chuck Lever706cb8d2014-03-12 12:51:47 -04005860/*
5861 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5862 * services. Advertise one based on the address family of the
5863 * clientaddr.
5864 */
5865static unsigned int
5866nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5867{
5868 if (strchr(clp->cl_ipaddr, ':') != NULL)
5869 return scnprintf(buf, len, "tcp6");
5870 else
5871 return scnprintf(buf, len, "tcp");
5872}
5873
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005874static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5875{
5876 struct nfs4_setclientid *sc = calldata;
5877
5878 if (task->tk_status == 0)
5879 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5880}
5881
5882static const struct rpc_call_ops nfs4_setclientid_ops = {
5883 .rpc_call_done = nfs4_setclientid_done,
5884};
5885
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005886/**
5887 * nfs4_proc_setclientid - Negotiate client ID
5888 * @clp: state data structure
5889 * @program: RPC program for NFSv4 callback service
5890 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11005891 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005892 * @res: where to place the result
5893 *
5894 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5895 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005896int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11005897 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005898 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899{
5900 nfs4_verifier sc_verifier;
5901 struct nfs4_setclientid setclientid = {
5902 .sc_verifier = &sc_verifier,
5903 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005904 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 };
5906 struct rpc_message msg = {
5907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5908 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005909 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005910 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005912 struct rpc_task *task;
5913 struct rpc_task_setup task_setup_data = {
5914 .rpc_client = clp->cl_rpcclient,
5915 .rpc_message = &msg,
5916 .callback_ops = &nfs4_setclientid_ops,
5917 .callback_data = &setclientid,
5918 .flags = RPC_TASK_TIMEOUT,
5919 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005920 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
Chuck Leverde734832012-07-11 16:30:50 -04005922 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005923 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005924
5925 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5926 status = nfs4_init_uniform_client_string(clp);
5927 else
Jeff Laytona3192682015-06-09 19:43:59 -04005928 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005929
5930 if (status)
5931 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005932
Chuck Leverde734832012-07-11 16:30:50 -04005933 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005934 setclientid.sc_netid_len =
5935 nfs4_init_callback_netid(clp,
5936 setclientid.sc_netid,
5937 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005938 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005939 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 clp->cl_ipaddr, port >> 8, port & 255);
5941
Jeff Layton3a6bb732015-06-09 19:43:57 -04005942 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005943 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005944 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005945 task = rpc_run_task(&task_setup_data);
5946 if (IS_ERR(task)) {
5947 status = PTR_ERR(task);
5948 goto out;
5949 }
5950 status = task->tk_status;
5951 if (setclientid.sc_cred) {
5952 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5953 put_rpccred(setclientid.sc_cred);
5954 }
5955 rpc_put_task(task);
5956out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005957 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005958 dprintk("NFS reply setclientid: %d\n", status);
5959 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960}
5961
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005962/**
5963 * nfs4_proc_setclientid_confirm - Confirm client ID
5964 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05005965 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11005966 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005967 *
5968 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5969 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005970int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005971 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11005972 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 struct rpc_message msg = {
5975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005976 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005977 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 int status;
5980
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005981 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5982 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5983 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005984 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005985 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005986 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 return status;
5988}
5989
Trond Myklebustfe650402006-01-03 09:55:18 +01005990struct nfs4_delegreturndata {
5991 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005992 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005993 struct nfs_fh fh;
5994 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005995 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005996 struct {
5997 struct nfs4_layoutreturn_args arg;
5998 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005999 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006000 u32 roc_barrier;
6001 bool roc;
6002 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006003 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006004 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006005 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006006};
6007
Trond Myklebustfe650402006-01-03 09:55:18 +01006008static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6009{
6010 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006011 struct nfs4_exception exception = {
6012 .inode = data->inode,
6013 .stateid = &data->stateid,
6014 };
Andy Adamson938e1012009-04-01 09:22:28 -04006015
Trond Myklebust14516c32010-07-31 14:29:06 -04006016 if (!nfs4_sequence_done(task, &data->res.seq_res))
6017 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006018
Trond Myklebustca8acf82013-08-13 10:36:56 -04006019 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006020
6021 /* Handle Layoutreturn errors */
6022 if (data->args.lr_args && task->tk_status != 0) {
6023 switch(data->res.lr_ret) {
6024 default:
6025 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6026 break;
6027 case 0:
6028 data->args.lr_args = NULL;
6029 data->res.lr_res = NULL;
6030 break;
Trond Myklebust73800202017-11-06 15:28:07 -05006031 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04006032 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04006033 &data->args.lr_args->range,
Trond Myklebust140087fd2017-11-06 15:28:10 -05006034 data->inode))
6035 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05006036 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05006037 case -NFS4ERR_ADMIN_REVOKED:
6038 case -NFS4ERR_DELEG_REVOKED:
6039 case -NFS4ERR_EXPIRED:
6040 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05006041 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6042 case -NFS4ERR_WRONG_CRED:
6043 data->args.lr_args = NULL;
6044 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006045 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006046 }
6047 }
6048
Ricardo Labiaga79708862009-12-07 09:23:21 -05006049 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006050 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006051 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006052 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006053 case -NFS4ERR_ADMIN_REVOKED:
6054 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006055 case -NFS4ERR_EXPIRED:
6056 nfs4_free_revoked_stateid(data->res.server,
6057 data->args.stateid,
6058 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006059 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006060 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006061 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006062 task->tk_status = 0;
6063 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006064 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05006065 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6066 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006067 task->tk_status = 0;
6068 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006069 case -NFS4ERR_ACCESS:
6070 if (data->args.bitmask) {
6071 data->args.bitmask = NULL;
6072 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006073 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006074 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006075 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006076 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006077 task->tk_status = nfs4_async_handle_exception(task,
6078 data->res.server, task->tk_status,
6079 &exception);
6080 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006081 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006082 }
6083 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006084 return;
6085lr_restart:
6086 data->res.lr_ret = 0;
6087out_restart:
6088 task->tk_status = 0;
6089 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006090}
6091
6092static void nfs4_delegreturn_release(void *calldata)
6093{
Peng Tao039b7562014-07-03 13:05:02 +08006094 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006095 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006096
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006097 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006098 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006099 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6100 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006101 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006102 nfs_iput_and_deactive(inode);
6103 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006104 kfree(calldata);
6105}
6106
Andy Adamson938e1012009-04-01 09:22:28 -04006107static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6108{
6109 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006110 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006111
6112 d_data = (struct nfs4_delegreturndata *)data;
6113
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006114 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08006115 return;
6116
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006117 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6118 if (lo && !pnfs_layout_is_valid(lo)) {
6119 d_data->args.lr_args = NULL;
6120 d_data->res.lr_res = NULL;
6121 }
6122
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006123 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006124 &d_data->args.seq_args,
6125 &d_data->res.seq_res,
6126 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006127}
Andy Adamson938e1012009-04-01 09:22:28 -04006128
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006129static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006130 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006131 .rpc_call_done = nfs4_delegreturn_done,
6132 .rpc_release = nfs4_delegreturn_release,
6133};
6134
NeilBrowna52458b2018-12-03 11:30:31 +11006135static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006136{
6137 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006138 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006139 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006140 struct rpc_message msg = {
6141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6142 .rpc_cred = cred,
6143 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006144 struct rpc_task_setup task_setup_data = {
6145 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006146 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006147 .callback_ops = &nfs4_delegreturn_ops,
6148 .flags = RPC_TASK_ASYNC,
6149 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006150 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006151
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006152 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006153 if (data == NULL)
6154 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006155 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006156
6157 nfs4_state_protect(server->nfs_client,
6158 NFS_SP4_MACH_CRED_CLEANUP,
6159 &task_setup_data.rpc_client, &msg);
6160
Trond Myklebustfe650402006-01-03 09:55:18 +01006161 data->args.fhandle = &data->fh;
6162 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006163 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006164 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006165 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006166 data->res.fattr = &data->fattr;
6167 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006168 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006169 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006170 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006171 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006172 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006173 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006174 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006175 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006176 if (data->lr.roc) {
6177 data->args.lr_args = &data->lr.arg;
6178 data->res.lr_res = &data->lr.res;
6179 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006180 } else if (data->lr.roc) {
6181 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6182 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006183 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006184
Trond Myklebustc970aa82007-07-14 15:39:59 -04006185 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006186 msg.rpc_argp = &data->args;
6187 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006188 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006189 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006190 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006191 if (!issync)
6192 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006193 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006194 if (status != 0)
6195 goto out;
6196 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006197out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006198 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006199 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200}
6201
NeilBrowna52458b2018-12-03 11:30:31 +11006202int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203{
6204 struct nfs_server *server = NFS_SERVER(inode);
6205 struct nfs4_exception exception = { };
6206 int err;
6207 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006208 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006209 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 switch (err) {
6211 case -NFS4ERR_STALE_STATEID:
6212 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 case 0:
6214 return 0;
6215 }
6216 err = nfs4_handle_exception(server, err, &exception);
6217 } while (exception.retry);
6218 return err;
6219}
6220
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6222{
6223 struct inode *inode = state->inode;
6224 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006225 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006226 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006228 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006230 struct nfs_lockt_res res = {
6231 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 };
6233 struct rpc_message msg = {
6234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006235 .rpc_argp = &arg,
6236 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 .rpc_cred = state->owner->so_cred,
6238 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 struct nfs4_lock_state *lsp;
6240 int status;
6241
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006242 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006243 status = nfs4_set_lock_state(state, request);
6244 if (status != 0)
6245 goto out;
6246 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006247 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006248 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006249 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006250 switch (status) {
6251 case 0:
6252 request->fl_type = F_UNLCK;
6253 break;
6254 case -NFS4ERR_DENIED:
6255 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006257 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006258 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006259out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 return status;
6261}
6262
6263static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6264{
6265 struct nfs4_exception exception = { };
6266 int err;
6267
6268 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006269 err = _nfs4_proc_getlk(state, cmd, request);
6270 trace_nfs4_get_lock(request, state, cmd, err);
6271 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 &exception);
6273 } while (exception.retry);
6274 return err;
6275}
6276
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006277struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006278 struct nfs_locku_args arg;
6279 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006280 struct nfs4_lock_state *lsp;
6281 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006282 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006283 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006284 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006285 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006286};
6287
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006288static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6289 struct nfs_open_context *ctx,
6290 struct nfs4_lock_state *lsp,
6291 struct nfs_seqid *seqid)
6292{
6293 struct nfs4_unlockdata *p;
6294 struct inode *inode = lsp->ls_state->inode;
6295
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006296 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006297 if (p == NULL)
6298 return NULL;
6299 p->arg.fh = NFS_FH(inode);
6300 p->arg.fl = &p->fl;
6301 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006302 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006303 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006304 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006305 /* Ensure we don't close file until we're done freeing locks! */
6306 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006307 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006308 locks_init_lock(&p->fl);
6309 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006310 p->server = NFS_SERVER(inode);
6311 return p;
6312}
6313
Trond Myklebust06f814a2006-01-03 09:55:07 +01006314static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006315{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006316 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006317 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006318 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006319 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006320 put_nfs_open_context(calldata->ctx);
6321 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006322}
6323
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006324static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006325{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006326 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006327 struct nfs4_exception exception = {
6328 .inode = calldata->lsp->ls_state->inode,
6329 .stateid = &calldata->arg.stateid,
6330 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006331
Trond Myklebust14516c32010-07-31 14:29:06 -04006332 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6333 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006334 switch (task->tk_status) {
6335 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006336 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006337 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006338 if (nfs4_update_lock_stateid(calldata->lsp,
6339 &calldata->res.stateid))
6340 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006341 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006342 case -NFS4ERR_ADMIN_REVOKED:
6343 case -NFS4ERR_EXPIRED:
6344 nfs4_free_revoked_stateid(calldata->server,
6345 &calldata->arg.stateid,
6346 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006347 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006348 case -NFS4ERR_BAD_STATEID:
6349 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006350 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006351 if (!nfs4_stateid_match(&calldata->arg.stateid,
6352 &calldata->lsp->ls_stateid))
6353 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006354 break;
6355 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006356 task->tk_status = nfs4_async_handle_exception(task,
6357 calldata->server, task->tk_status,
6358 &exception);
6359 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006360 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006361 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006362 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006363}
6364
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006365static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006366{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006367 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006369 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6370 nfs_async_iocounter_wait(task, calldata->l_ctx))
6371 return;
6372
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006373 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006374 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006375 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006376 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006377 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006378 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006379 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006380 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006381 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006382 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006383 &calldata->res.seq_res,
6384 task) != 0)
6385 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006386 return;
6387out_no_action:
6388 task->tk_action = NULL;
6389out_wait:
6390 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391}
6392
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006393static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006394 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006395 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006396 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006397};
6398
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006399static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6400 struct nfs_open_context *ctx,
6401 struct nfs4_lock_state *lsp,
6402 struct nfs_seqid *seqid)
6403{
6404 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006405 struct rpc_message msg = {
6406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6407 .rpc_cred = ctx->cred,
6408 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006409 struct rpc_task_setup task_setup_data = {
6410 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006411 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006412 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006413 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006414 .flags = RPC_TASK_ASYNC,
6415 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006416
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006417 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6418 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6419
Frank Filz137d6ac2007-07-09 15:32:29 -07006420 /* Ensure this is an unlock - when canceling a lock, the
6421 * canceled lock is passed in, and it won't be an unlock.
6422 */
6423 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006424 if (fl->fl_flags & FL_CLOSE)
6425 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006426
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006427 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6428 if (data == NULL) {
6429 nfs_free_seqid(seqid);
6430 return ERR_PTR(-ENOMEM);
6431 }
6432
Anna Schumakerfba83f32018-05-04 16:22:50 -04006433 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006434 msg.rpc_argp = &data->arg;
6435 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006436 task_setup_data.callback_data = data;
6437 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006438}
6439
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6441{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006442 struct inode *inode = state->inode;
6443 struct nfs4_state_owner *sp = state->owner;
6444 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006445 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006446 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006447 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006448 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006449 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006450 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451
Trond Myklebust9b073572006-06-29 16:38:34 -04006452 status = nfs4_set_lock_state(state, request);
6453 /* Unlock _before_ we do the RPC call */
6454 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006455 /* Exclude nfs_delegation_claim_locks() */
6456 mutex_lock(&sp->so_delegreturn_mutex);
6457 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006458 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006459 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006460 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006461 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006462 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006463 }
6464 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006465 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006466 if (status != 0)
6467 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006468 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006469 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006470 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6471 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006472 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6473 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006474 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006475 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006476 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006477 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006478 status = PTR_ERR(task);
6479 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006480 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006481 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006482 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006483out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006484 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006485 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006486 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487}
6488
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006489struct nfs4_lockdata {
6490 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006491 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006492 struct nfs4_lock_state *lsp;
6493 struct nfs_open_context *ctx;
6494 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006495 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006496 int rpc_status;
6497 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006498 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006499};
6500
6501static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006502 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6503 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006504{
6505 struct nfs4_lockdata *p;
6506 struct inode *inode = lsp->ls_state->inode;
6507 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006508 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006509
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006510 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006511 if (p == NULL)
6512 return NULL;
6513
6514 p->arg.fh = NFS_FH(inode);
6515 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006516 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006517 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006518 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006519 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6520 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006521 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006522 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006523 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006524 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006525 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006526 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006527 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006528 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006529 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006530 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006531 locks_init_lock(&p->fl);
6532 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006533 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006534out_free_seqid:
6535 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006536out_free:
6537 kfree(p);
6538 return NULL;
6539}
6540
6541static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6542{
6543 struct nfs4_lockdata *data = calldata;
6544 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545
Harvey Harrison3110ff82008-05-02 13:42:44 -07006546 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006547 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006548 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006549 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006550 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006551 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006552 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006553 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006554 nfs4_stateid_copy(&data->arg.open_stateid,
6555 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006556 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006557 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006558 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006559 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006560 nfs4_stateid_copy(&data->arg.lock_stateid,
6561 &data->lsp->ls_stateid);
6562 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006563 if (!nfs4_valid_open_stateid(state)) {
6564 data->rpc_status = -EBADF;
6565 task->tk_action = NULL;
6566 goto out_release_open_seqid;
6567 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006568 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006569 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006570 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006571 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006572 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006573 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006574out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006575 nfs_release_seqid(data->arg.open_seqid);
6576out_release_lock_seqid:
6577 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006578out_wait:
6579 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006580 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006581}
6582
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006583static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6584{
6585 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006586 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006587
Harvey Harrison3110ff82008-05-02 13:42:44 -07006588 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006589
Trond Myklebust14516c32010-07-31 14:29:06 -04006590 if (!nfs4_sequence_done(task, &data->res.seq_res))
6591 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006592
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006593 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006594 switch (task->tk_status) {
6595 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006596 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006597 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006598 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006599 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006600 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006601 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006602 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006603 if (data->arg.new_lock_owner != 0) {
6604 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6605 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6606 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006607 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6608 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006609 break;
6610 case -NFS4ERR_BAD_STATEID:
6611 case -NFS4ERR_OLD_STATEID:
6612 case -NFS4ERR_STALE_STATEID:
6613 case -NFS4ERR_EXPIRED:
6614 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006615 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006616 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006617 goto out_restart;
6618 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006619 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006620 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006621 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006622out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006623 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006624 return;
6625out_restart:
6626 if (!data->cancelled)
6627 rpc_restart_call_prepare(task);
6628 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006629}
6630
6631static void nfs4_lock_release(void *calldata)
6632{
6633 struct nfs4_lockdata *data = calldata;
6634
Harvey Harrison3110ff82008-05-02 13:42:44 -07006635 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006636 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006637 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006638 struct rpc_task *task;
6639 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6640 data->arg.lock_seqid);
6641 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006642 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006643 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006644 } else
6645 nfs_free_seqid(data->arg.lock_seqid);
6646 nfs4_put_lock_state(data->lsp);
6647 put_nfs_open_context(data->ctx);
6648 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006649 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006650}
6651
6652static const struct rpc_call_ops nfs4_lock_ops = {
6653 .rpc_call_prepare = nfs4_lock_prepare,
6654 .rpc_call_done = nfs4_lock_done,
6655 .rpc_release = nfs4_lock_release,
6656};
6657
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006658static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6659{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006660 switch (error) {
6661 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006662 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006663 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006664 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006665 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006666 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006667 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006668 break;
6669 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006670 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006671 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006672 };
6673}
6674
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006675static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006676{
6677 struct nfs4_lockdata *data;
6678 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006679 struct rpc_message msg = {
6680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6681 .rpc_cred = state->owner->so_cred,
6682 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006683 struct rpc_task_setup task_setup_data = {
6684 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006685 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006686 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006687 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006688 .flags = RPC_TASK_ASYNC,
6689 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006690 int ret;
6691
Harvey Harrison3110ff82008-05-02 13:42:44 -07006692 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006693 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006694 fl->fl_u.nfs4_fl.owner,
6695 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006696 if (data == NULL)
6697 return -ENOMEM;
6698 if (IS_SETLKW(cmd))
6699 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006700 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6701 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006702 msg.rpc_argp = &data->arg;
6703 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006704 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006705 if (recovery_type > NFS_LOCK_NEW) {
6706 if (recovery_type == NFS_LOCK_RECLAIM)
6707 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006708 } else
6709 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006710 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006711 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006712 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006713 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006714 if (ret == 0) {
6715 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006716 if (ret)
6717 nfs4_handle_setlk_error(data->server, data->lsp,
6718 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006719 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006720 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006721 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006722 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006723 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006724 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725}
6726
6727static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6728{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006729 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006730 struct nfs4_exception exception = {
6731 .inode = state->inode,
6732 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006733 int err;
6734
6735 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006736 /* Cache the lock if possible... */
6737 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6738 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006739 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006740 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006741 break;
6742 nfs4_handle_exception(server, err, &exception);
6743 } while (exception.retry);
6744 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745}
6746
6747static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6748{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006749 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006750 struct nfs4_exception exception = {
6751 .inode = state->inode,
6752 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006753 int err;
6754
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006755 err = nfs4_set_lock_state(state, request);
6756 if (err != 0)
6757 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006758 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006759 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6760 return 0;
6761 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006762 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006763 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6764 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006765 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006766 switch (err) {
6767 default:
6768 goto out;
6769 case -NFS4ERR_GRACE:
6770 case -NFS4ERR_DELAY:
6771 nfs4_handle_exception(server, err, &exception);
6772 err = 0;
6773 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006774 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006775out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006776 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777}
6778
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006779#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006780static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6781{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006782 struct nfs4_lock_state *lsp;
6783 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006784
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006785 status = nfs4_set_lock_state(state, request);
6786 if (status != 0)
6787 return status;
6788 lsp = request->fl_u.nfs4_fl.owner;
6789 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6790 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6791 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006792 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006793}
6794#endif
6795
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6797{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006798 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006799 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006800 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006801 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
Trond Myklebust01c3b862006-06-29 16:38:39 -04006803 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006804 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006805 if (status < 0)
6806 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006807 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006808 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006809 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006810 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006811 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006812 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006813 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006814 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006815 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006816 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006817 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006818 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006819 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006820 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006821out:
6822 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 return status;
6824}
6825
6826static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6827{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006828 struct nfs4_exception exception = {
6829 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006830 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006831 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 int err;
6833
6834 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006835 err = _nfs4_proc_setlk(state, cmd, request);
6836 if (err == -NFS4ERR_DENIED)
6837 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006839 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 } while (exception.retry);
6841 return err;
6842}
6843
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006844#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6845#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6846
6847static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006848nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6849 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006850{
6851 int status = -ERESTARTSYS;
6852 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6853
6854 while(!signalled()) {
6855 status = nfs4_proc_setlk(state, cmd, request);
6856 if ((status != -EAGAIN) || IS_SETLK(cmd))
6857 break;
6858 freezable_schedule_timeout_interruptible(timeout);
6859 timeout *= 2;
6860 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6861 status = -ERESTARTSYS;
6862 }
6863 return status;
6864}
6865
Jeff Laytona1d617d82016-09-17 18:17:39 -04006866#ifdef CONFIG_NFS_V4_1
6867struct nfs4_lock_waiter {
6868 struct task_struct *task;
6869 struct inode *inode;
6870 struct nfs_lowner *owner;
6871 bool notified;
6872};
6873
6874static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006875nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006876{
6877 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006878 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006879
Jeff Layton57174592018-03-18 08:37:03 -04006880 /* NULL key means to wake up everyone */
6881 if (key) {
6882 struct cb_notify_lock_args *cbnl = key;
6883 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6884 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006885
Jeff Layton57174592018-03-18 08:37:03 -04006886 /* Only wake if the callback was for the same owner. */
6887 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6888 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006889
Jeff Layton57174592018-03-18 08:37:03 -04006890 /* Make sure it's for the right inode */
6891 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6892 return 0;
6893
6894 waiter->notified = true;
6895 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006896
6897 /* override "private" so we can use default_wake_function */
6898 wait->private = waiter->task;
6899 ret = autoremove_wake_function(wait, mode, flags, key);
6900 wait->private = waiter;
6901 return ret;
6902}
6903
6904static int
6905nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6906{
6907 int status = -ERESTARTSYS;
6908 unsigned long flags;
6909 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6910 struct nfs_server *server = NFS_SERVER(state->inode);
6911 struct nfs_client *clp = server->nfs_client;
6912 wait_queue_head_t *q = &clp->cl_lock_waitq;
6913 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6914 .id = lsp->ls_seqid.owner_id,
6915 .s_dev = server->s_dev };
6916 struct nfs4_lock_waiter waiter = { .task = current,
6917 .inode = state->inode,
6918 .owner = &owner,
6919 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006920 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006921
6922 /* Don't bother with waitqueue if we don't expect a callback */
6923 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6924 return nfs4_retry_setlk_simple(state, cmd, request);
6925
6926 init_wait(&wait);
6927 wait.private = &waiter;
6928 wait.func = nfs4_wake_lock_waiter;
6929 add_wait_queue(q, &wait);
6930
6931 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006932 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006933 status = nfs4_proc_setlk(state, cmd, request);
6934 if ((status != -EAGAIN) || IS_SETLK(cmd))
6935 break;
6936
6937 status = -ERESTARTSYS;
6938 spin_lock_irqsave(&q->lock, flags);
6939 if (waiter.notified) {
6940 spin_unlock_irqrestore(&q->lock, flags);
6941 continue;
6942 }
6943 set_current_state(TASK_INTERRUPTIBLE);
6944 spin_unlock_irqrestore(&q->lock, flags);
6945
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006946 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006947 }
6948
6949 finish_wait(q, &wait);
6950 return status;
6951}
6952#else /* !CONFIG_NFS_V4_1 */
6953static inline int
6954nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6955{
6956 return nfs4_retry_setlk_simple(state, cmd, request);
6957}
6958#endif
6959
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960static int
6961nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6962{
6963 struct nfs_open_context *ctx;
6964 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 int status;
6966
6967 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006968 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 state = ctx->state;
6970
Trond Myklebustd9531262009-07-21 19:22:38 -04006971 if (IS_GETLK(cmd)) {
6972 if (state != NULL)
6973 return nfs4_proc_getlk(state, F_GETLK, request);
6974 return 0;
6975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976
6977 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6978 return -EINVAL;
6979
Trond Myklebustd9531262009-07-21 19:22:38 -04006980 if (request->fl_type == F_UNLCK) {
6981 if (state != NULL)
6982 return nfs4_proc_unlck(state, cmd, request);
6983 return 0;
6984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
Trond Myklebustd9531262009-07-21 19:22:38 -04006986 if (state == NULL)
6987 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006988
6989 if ((request->fl_flags & FL_POSIX) &&
6990 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6991 return -ENOLCK;
6992
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006993 /*
6994 * Don't rely on the VFS having checked the file open mode,
6995 * since it won't do this for flock() locks.
6996 */
6997 switch (request->fl_type) {
6998 case F_RDLCK:
6999 if (!(filp->f_mode & FMODE_READ))
7000 return -EBADF;
7001 break;
7002 case F_WRLCK:
7003 if (!(filp->f_mode & FMODE_WRITE))
7004 return -EBADF;
7005 }
7006
Jeff Layton1ea67db2016-09-17 18:17:37 -04007007 status = nfs4_set_lock_state(state, request);
7008 if (status != 0)
7009 return status;
7010
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007011 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012}
7013
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007014int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007015{
7016 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007017 int err;
7018
7019 err = nfs4_set_lock_state(state, fl);
7020 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007021 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007022 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007023 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007024}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007025
Trond Myklebustcf470c32012-03-07 13:49:12 -05007026struct nfs_release_lockowner_data {
7027 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007028 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007029 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007030 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007031 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007032};
7033
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007034static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7035{
7036 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007037 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007038 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7039 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007040 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007041 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007042}
7043
7044static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7045{
7046 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007047 struct nfs_server *server = data->server;
7048
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007049 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007050
7051 switch (task->tk_status) {
7052 case 0:
7053 renew_lease(server, data->timestamp);
7054 break;
7055 case -NFS4ERR_STALE_CLIENTID:
7056 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007057 nfs4_schedule_lease_recovery(server->nfs_client);
7058 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007059 case -NFS4ERR_LEASE_MOVED:
7060 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007061 if (nfs4_async_handle_error(task, server,
7062 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007063 rpc_restart_call_prepare(task);
7064 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007065}
7066
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007067static void nfs4_release_lockowner_release(void *calldata)
7068{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007069 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007070 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007071 kfree(calldata);
7072}
7073
Trond Myklebust17280172012-03-11 13:11:00 -04007074static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007075 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7076 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007077 .rpc_release = nfs4_release_lockowner_release,
7078};
7079
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007080static void
7081nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007082{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007083 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007084 struct rpc_message msg = {
7085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7086 };
7087
7088 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007089 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007090
Trond Myklebustcf470c32012-03-07 13:49:12 -05007091 data = kmalloc(sizeof(*data), GFP_NOFS);
7092 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007093 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007094 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007095 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007096 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7097 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7098 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007099
Trond Myklebustcf470c32012-03-07 13:49:12 -05007100 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007101 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007102 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007103 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007104}
7105
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007106#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7107
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007108static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007109 struct dentry *unused, struct inode *inode,
7110 const char *key, const void *buf,
7111 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007112{
Al Viro59301222016-05-27 10:19:30 -04007113 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007114}
7115
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007116static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007117 struct dentry *unused, struct inode *inode,
7118 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007119{
Al Virob2968212016-04-10 20:48:24 -04007120 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007121}
7122
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007123static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007124{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007125 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007126}
7127
David Quigleyc9bccef2013-05-22 12:50:45 -04007128#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007129
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007130static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007131 struct dentry *unused, struct inode *inode,
7132 const char *key, const void *buf,
7133 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007134{
7135 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007136 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007137
7138 return -EOPNOTSUPP;
7139}
7140
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007141static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007142 struct dentry *unused, struct inode *inode,
7143 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007144{
7145 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007146 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007147 return -EOPNOTSUPP;
7148}
7149
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007150static ssize_t
7151nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007152{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007153 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007154
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007155 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7156 len = security_inode_listsecurity(inode, list, list_len);
7157 if (list_len && len > list_len)
7158 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007159 }
7160 return len;
7161}
7162
7163static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7164 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007165 .get = nfs4_xattr_get_nfs4_label,
7166 .set = nfs4_xattr_set_nfs4_label,
7167};
David Quigleyc9bccef2013-05-22 12:50:45 -04007168
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007169#else
7170
7171static ssize_t
7172nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7173{
7174 return 0;
7175}
7176
7177#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007178
Andy Adamson533eb462011-06-13 18:25:56 -04007179/*
7180 * nfs_fhget will use either the mounted_on_fileid or the fileid
7181 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007182static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7183{
Andy Adamson533eb462011-06-13 18:25:56 -04007184 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7185 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7186 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007187 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007188 return;
7189
7190 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007191 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007192 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7193 fattr->nlink = 2;
7194}
7195
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007196static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7197 const struct qstr *name,
7198 struct nfs4_fs_locations *fs_locations,
7199 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007200{
7201 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007202 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007203 struct nfs4_fs_locations_arg args = {
7204 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007205 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007206 .page = page,
7207 .bitmask = bitmask,
7208 };
Benny Halevy22958462009-04-01 09:22:02 -04007209 struct nfs4_fs_locations_res res = {
7210 .fs_locations = fs_locations,
7211 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007212 struct rpc_message msg = {
7213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7214 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007215 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007216 };
7217 int status;
7218
Harvey Harrison3110ff82008-05-02 13:42:44 -07007219 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007220
Chuck Leverc05cefc2017-11-05 15:45:22 -05007221 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7222 bitmask[1] = nfs4_fattr_bitmap[1];
7223
Andy Adamson533eb462011-06-13 18:25:56 -04007224 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7225 * is not supported */
7226 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007227 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007228 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007229 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007230
Trond Myklebustc228fd32007-01-13 02:28:11 -05007231 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007232 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007233 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007234 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007235 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007236 return status;
7237}
7238
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007239int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7240 const struct qstr *name,
7241 struct nfs4_fs_locations *fs_locations,
7242 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007243{
7244 struct nfs4_exception exception = { };
7245 int err;
7246 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007247 err = _nfs4_proc_fs_locations(client, dir, name,
7248 fs_locations, page);
7249 trace_nfs4_get_fs_locations(dir, name, err);
7250 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007251 &exception);
7252 } while (exception.retry);
7253 return err;
7254}
7255
Chuck Leverb03d7352013-10-17 14:12:50 -04007256/*
7257 * This operation also signals the server that this client is
7258 * performing migration recovery. The server can stop returning
7259 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7260 * appended to this compound to identify the client ID which is
7261 * performing recovery.
7262 */
7263static int _nfs40_proc_get_locations(struct inode *inode,
7264 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007265 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007266{
7267 struct nfs_server *server = NFS_SERVER(inode);
7268 struct rpc_clnt *clnt = server->client;
7269 u32 bitmask[2] = {
7270 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7271 };
7272 struct nfs4_fs_locations_arg args = {
7273 .clientid = server->nfs_client->cl_clientid,
7274 .fh = NFS_FH(inode),
7275 .page = page,
7276 .bitmask = bitmask,
7277 .migration = 1, /* skip LOOKUP */
7278 .renew = 1, /* append RENEW */
7279 };
7280 struct nfs4_fs_locations_res res = {
7281 .fs_locations = locations,
7282 .migration = 1,
7283 .renew = 1,
7284 };
7285 struct rpc_message msg = {
7286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7287 .rpc_argp = &args,
7288 .rpc_resp = &res,
7289 .rpc_cred = cred,
7290 };
7291 unsigned long now = jiffies;
7292 int status;
7293
7294 nfs_fattr_init(&locations->fattr);
7295 locations->server = server;
7296 locations->nlocations = 0;
7297
Anna Schumakerfba83f32018-05-04 16:22:50 -04007298 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007299 status = nfs4_call_sync_sequence(clnt, server, &msg,
7300 &args.seq_args, &res.seq_res);
7301 if (status)
7302 return status;
7303
7304 renew_lease(server, now);
7305 return 0;
7306}
7307
7308#ifdef CONFIG_NFS_V4_1
7309
7310/*
7311 * This operation also signals the server that this client is
7312 * performing migration recovery. The server can stop asserting
7313 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7314 * performing this operation is identified in the SEQUENCE
7315 * operation in this compound.
7316 *
7317 * When the client supports GETATTR(fs_locations_info), it can
7318 * be plumbed in here.
7319 */
7320static int _nfs41_proc_get_locations(struct inode *inode,
7321 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007322 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007323{
7324 struct nfs_server *server = NFS_SERVER(inode);
7325 struct rpc_clnt *clnt = server->client;
7326 u32 bitmask[2] = {
7327 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7328 };
7329 struct nfs4_fs_locations_arg args = {
7330 .fh = NFS_FH(inode),
7331 .page = page,
7332 .bitmask = bitmask,
7333 .migration = 1, /* skip LOOKUP */
7334 };
7335 struct nfs4_fs_locations_res res = {
7336 .fs_locations = locations,
7337 .migration = 1,
7338 };
7339 struct rpc_message msg = {
7340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7341 .rpc_argp = &args,
7342 .rpc_resp = &res,
7343 .rpc_cred = cred,
7344 };
7345 int status;
7346
7347 nfs_fattr_init(&locations->fattr);
7348 locations->server = server;
7349 locations->nlocations = 0;
7350
Anna Schumakerfba83f32018-05-04 16:22:50 -04007351 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007352 status = nfs4_call_sync_sequence(clnt, server, &msg,
7353 &args.seq_args, &res.seq_res);
7354 if (status == NFS4_OK &&
7355 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7356 status = -NFS4ERR_LEASE_MOVED;
7357 return status;
7358}
7359
7360#endif /* CONFIG_NFS_V4_1 */
7361
7362/**
7363 * nfs4_proc_get_locations - discover locations for a migrated FSID
7364 * @inode: inode on FSID that is migrating
7365 * @locations: result of query
7366 * @page: buffer
7367 * @cred: credential to use for this operation
7368 *
7369 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7370 * operation failed, or a negative errno if a local error occurred.
7371 *
7372 * On success, "locations" is filled in, but if the server has
7373 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7374 * asserted.
7375 *
7376 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7377 * from this client that require migration recovery.
7378 */
7379int nfs4_proc_get_locations(struct inode *inode,
7380 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007381 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007382{
7383 struct nfs_server *server = NFS_SERVER(inode);
7384 struct nfs_client *clp = server->nfs_client;
7385 const struct nfs4_mig_recovery_ops *ops =
7386 clp->cl_mvops->mig_recovery_ops;
7387 struct nfs4_exception exception = { };
7388 int status;
7389
7390 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7391 (unsigned long long)server->fsid.major,
7392 (unsigned long long)server->fsid.minor,
7393 clp->cl_hostname);
7394 nfs_display_fhandle(NFS_FH(inode), __func__);
7395
7396 do {
7397 status = ops->get_locations(inode, locations, page, cred);
7398 if (status != -NFS4ERR_DELAY)
7399 break;
7400 nfs4_handle_exception(server, status, &exception);
7401 } while (exception.retry);
7402 return status;
7403}
7404
Chuck Lever44c99932013-10-17 14:13:30 -04007405/*
7406 * This operation also signals the server that this client is
7407 * performing "lease moved" recovery. The server can stop
7408 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7409 * is appended to this compound to identify the client ID which is
7410 * performing recovery.
7411 */
NeilBrowna52458b2018-12-03 11:30:31 +11007412static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007413{
7414 struct nfs_server *server = NFS_SERVER(inode);
7415 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7416 struct rpc_clnt *clnt = server->client;
7417 struct nfs4_fsid_present_arg args = {
7418 .fh = NFS_FH(inode),
7419 .clientid = clp->cl_clientid,
7420 .renew = 1, /* append RENEW */
7421 };
7422 struct nfs4_fsid_present_res res = {
7423 .renew = 1,
7424 };
7425 struct rpc_message msg = {
7426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7427 .rpc_argp = &args,
7428 .rpc_resp = &res,
7429 .rpc_cred = cred,
7430 };
7431 unsigned long now = jiffies;
7432 int status;
7433
7434 res.fh = nfs_alloc_fhandle();
7435 if (res.fh == NULL)
7436 return -ENOMEM;
7437
Anna Schumakerfba83f32018-05-04 16:22:50 -04007438 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007439 status = nfs4_call_sync_sequence(clnt, server, &msg,
7440 &args.seq_args, &res.seq_res);
7441 nfs_free_fhandle(res.fh);
7442 if (status)
7443 return status;
7444
7445 do_renew_lease(clp, now);
7446 return 0;
7447}
7448
7449#ifdef CONFIG_NFS_V4_1
7450
7451/*
7452 * This operation also signals the server that this client is
7453 * performing "lease moved" recovery. The server can stop asserting
7454 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7455 * this operation is identified in the SEQUENCE operation in this
7456 * compound.
7457 */
NeilBrowna52458b2018-12-03 11:30:31 +11007458static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007459{
7460 struct nfs_server *server = NFS_SERVER(inode);
7461 struct rpc_clnt *clnt = server->client;
7462 struct nfs4_fsid_present_arg args = {
7463 .fh = NFS_FH(inode),
7464 };
7465 struct nfs4_fsid_present_res res = {
7466 };
7467 struct rpc_message msg = {
7468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7469 .rpc_argp = &args,
7470 .rpc_resp = &res,
7471 .rpc_cred = cred,
7472 };
7473 int status;
7474
7475 res.fh = nfs_alloc_fhandle();
7476 if (res.fh == NULL)
7477 return -ENOMEM;
7478
Anna Schumakerfba83f32018-05-04 16:22:50 -04007479 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007480 status = nfs4_call_sync_sequence(clnt, server, &msg,
7481 &args.seq_args, &res.seq_res);
7482 nfs_free_fhandle(res.fh);
7483 if (status == NFS4_OK &&
7484 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7485 status = -NFS4ERR_LEASE_MOVED;
7486 return status;
7487}
7488
7489#endif /* CONFIG_NFS_V4_1 */
7490
7491/**
7492 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7493 * @inode: inode on FSID to check
7494 * @cred: credential to use for this operation
7495 *
7496 * Server indicates whether the FSID is present, moved, or not
7497 * recognized. This operation is necessary to clear a LEASE_MOVED
7498 * condition for this client ID.
7499 *
7500 * Returns NFS4_OK if the FSID is present on this server,
7501 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7502 * NFS4ERR code if some error occurred on the server, or a
7503 * negative errno if a local failure occurred.
7504 */
NeilBrowna52458b2018-12-03 11:30:31 +11007505int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007506{
7507 struct nfs_server *server = NFS_SERVER(inode);
7508 struct nfs_client *clp = server->nfs_client;
7509 const struct nfs4_mig_recovery_ops *ops =
7510 clp->cl_mvops->mig_recovery_ops;
7511 struct nfs4_exception exception = { };
7512 int status;
7513
7514 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7515 (unsigned long long)server->fsid.major,
7516 (unsigned long long)server->fsid.minor,
7517 clp->cl_hostname);
7518 nfs_display_fhandle(NFS_FH(inode), __func__);
7519
7520 do {
7521 status = ops->fsid_present(inode, cred);
7522 if (status != -NFS4ERR_DELAY)
7523 break;
7524 nfs4_handle_exception(server, status, &exception);
7525 } while (exception.retry);
7526 return status;
7527}
7528
Trond Myklebust302fad72019-02-18 13:32:38 -05007529/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007530 * If 'use_integrity' is true and the state managment nfs_client
7531 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7532 * and the machine credential as per RFC3530bis and RFC5661 Security
7533 * Considerations sections. Otherwise, just use the user cred with the
7534 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007535 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007536static 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 +00007537{
7538 int status;
7539 struct nfs4_secinfo_arg args = {
7540 .dir_fh = NFS_FH(dir),
7541 .name = name,
7542 };
7543 struct nfs4_secinfo_res res = {
7544 .flavors = flavors,
7545 };
7546 struct rpc_message msg = {
7547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7548 .rpc_argp = &args,
7549 .rpc_resp = &res,
7550 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007551 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
NeilBrowna52458b2018-12-03 11:30:31 +11007552 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007553
7554 if (use_integrity) {
7555 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007556 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7557 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007558 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007559
7560 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007561
7562 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7563 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7564
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007565 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7566 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007567 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007568
NeilBrowna52458b2018-12-03 11:30:31 +11007569 put_cred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007570
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007571 return status;
7572}
7573
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007574int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7575 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007576{
7577 struct nfs4_exception exception = { };
7578 int err;
7579 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007580 err = -NFS4ERR_WRONGSEC;
7581
7582 /* try to use integrity protection with machine cred */
7583 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7584 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7585
7586 /*
7587 * if unable to use integrity protection, or SECINFO with
7588 * integrity protection returns NFS4ERR_WRONGSEC (which is
7589 * disallowed by spec, but exists in deployed servers) use
7590 * the current filesystem's rpc_client and the user cred.
7591 */
7592 if (err == -NFS4ERR_WRONGSEC)
7593 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7594
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007595 trace_nfs4_secinfo(dir, name, err);
7596 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007597 &exception);
7598 } while (exception.retry);
7599 return err;
7600}
7601
Andy Adamson557134a2009-04-01 09:21:53 -04007602#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007603/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007604 * Check the exchange flags returned by the server for invalid flags, having
7605 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7606 * DS flags set.
7607 */
7608static int nfs4_check_cl_exchange_flags(u32 flags)
7609{
7610 if (flags & ~EXCHGID4_FLAG_MASK_R)
7611 goto out_inval;
7612 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7613 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7614 goto out_inval;
7615 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7616 goto out_inval;
7617 return NFS_OK;
7618out_inval:
7619 return -NFS4ERR_INVAL;
7620}
7621
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007622static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007623nfs41_same_server_scope(struct nfs41_server_scope *a,
7624 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007625{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007626 if (a->server_scope_sz != b->server_scope_sz)
7627 return false;
7628 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007629}
7630
Andy Adamson02a95de2016-02-05 16:08:37 -05007631static void
7632nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7633{
7634}
7635
7636static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7637 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7638};
7639
Andy Adamson357f54d2010-12-14 10:11:57 -05007640/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007641 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007642 *
7643 * The 4.1 client currently uses the same TCP connection for the
7644 * fore and backchannel.
7645 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007646static
7647int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7648 struct rpc_xprt *xprt,
7649 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007650 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007651{
7652 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007653 struct nfs41_bind_conn_to_session_args args = {
7654 .client = clp,
7655 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7656 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007657 struct nfs41_bind_conn_to_session_res res;
7658 struct rpc_message msg = {
7659 .rpc_proc =
7660 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007661 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007662 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007663 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007664 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007665 struct rpc_task_setup task_setup_data = {
7666 .rpc_client = clnt,
7667 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007668 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007669 .rpc_message = &msg,
7670 .flags = RPC_TASK_TIMEOUT,
7671 };
7672 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007673
Trond Myklebust71a097c2015-02-18 09:27:18 -08007674 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7675 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7676 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007677
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007678 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7679 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7680 args.dir = NFS4_CDFC4_FORE;
7681
7682 task = rpc_run_task(&task_setup_data);
7683 if (!IS_ERR(task)) {
7684 status = task->tk_status;
7685 rpc_put_task(task);
7686 } else
7687 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007688 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007689 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007690 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007691 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7692 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007693 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007694 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007695 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007696 dprintk("NFS: %s: Unexpected direction from server\n",
7697 __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.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007701 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7702 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007703 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007704 }
7705 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007706
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007707 return status;
7708}
7709
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007710struct rpc_bind_conn_calldata {
7711 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007712 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007713};
7714
7715static int
7716nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7717 struct rpc_xprt *xprt,
7718 void *calldata)
7719{
7720 struct rpc_bind_conn_calldata *p = calldata;
7721
7722 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7723}
7724
NeilBrowna52458b2018-12-03 11:30:31 +11007725int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007726{
7727 struct rpc_bind_conn_calldata data = {
7728 .clp = clp,
7729 .cred = cred,
7730 };
7731 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7732 nfs4_proc_bind_conn_to_session_callback, &data);
7733}
7734
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007735/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007736 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7737 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007738 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007739static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7740 .how = SP4_MACH_CRED,
7741 .enforce.u.words = {
7742 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7743 1 << (OP_EXCHANGE_ID - 32) |
7744 1 << (OP_CREATE_SESSION - 32) |
7745 1 << (OP_DESTROY_SESSION - 32) |
7746 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007747 },
7748 .allow.u.words = {
7749 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007750 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007751 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007752 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007753 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007754 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007755 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007756 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007757 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007758 1 << (OP_FREE_STATEID - 32) |
7759 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007760 }
7761};
7762
7763/*
7764 * Select the state protection mode for client `clp' given the server results
7765 * from exchange_id in `sp'.
7766 *
7767 * Returns 0 on success, negative errno otherwise.
7768 */
7769static int nfs4_sp4_select_mode(struct nfs_client *clp,
7770 struct nfs41_state_protection *sp)
7771{
7772 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7773 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7774 1 << (OP_EXCHANGE_ID - 32) |
7775 1 << (OP_CREATE_SESSION - 32) |
7776 1 << (OP_DESTROY_SESSION - 32) |
7777 1 << (OP_DESTROY_CLIENTID - 32)
7778 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007779 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007780 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007781 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007782
7783 if (sp->how == SP4_MACH_CRED) {
7784 /* Print state protect result */
7785 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7786 for (i = 0; i <= LAST_NFS4_OP; i++) {
7787 if (test_bit(i, sp->enforce.u.longs))
7788 dfprintk(MOUNT, " enforce op %d\n", i);
7789 if (test_bit(i, sp->allow.u.longs))
7790 dfprintk(MOUNT, " allow op %d\n", i);
7791 }
7792
7793 /* make sure nothing is on enforce list that isn't supported */
7794 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7795 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7796 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007797 ret = -EINVAL;
7798 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007799 }
7800 }
7801
7802 /*
7803 * Minimal mode - state operations are allowed to use machine
7804 * credential. Note this already happens by default, so the
7805 * client doesn't have to do anything more than the negotiation.
7806 *
7807 * NOTE: we don't care if EXCHANGE_ID is in the list -
7808 * we're already using the machine cred for exchange_id
7809 * and will never use a different cred.
7810 */
7811 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7812 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7813 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7814 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7815 dfprintk(MOUNT, "sp4_mach_cred:\n");
7816 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007817 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007818 } else {
7819 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007820 ret = -EINVAL;
7821 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007822 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007823
7824 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007825 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7826 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007827 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7828 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007829 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007830 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007831
Andrew Elble99ade3c2015-12-02 09:39:51 -05007832 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7833 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007834 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007835 }
7836
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007837 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7838 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7839 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007840 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007841 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007842
7843 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7844 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7845 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007846 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007847 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007848
7849 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7850 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007851 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007852 }
7853
7854 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7855 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007856 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007857 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007858 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007859out:
7860 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00007861 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007862}
7863
Andy Adamson8d89bd72016-09-09 09:22:18 -04007864struct nfs41_exchange_id_data {
7865 struct nfs41_exchange_id_res res;
7866 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007867};
7868
Andy Adamson8d89bd72016-09-09 09:22:18 -04007869static void nfs4_exchange_id_release(void *data)
7870{
7871 struct nfs41_exchange_id_data *cdata =
7872 (struct nfs41_exchange_id_data *)data;
7873
Olga Kornievskaia63513232017-03-13 10:36:19 -04007874 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007875 kfree(cdata->res.impl_id);
7876 kfree(cdata->res.server_scope);
7877 kfree(cdata->res.server_owner);
7878 kfree(cdata);
7879}
7880
7881static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007882 .rpc_release = nfs4_exchange_id_release,
7883};
7884
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007885/*
7886 * _nfs4_proc_exchange_id()
7887 *
7888 * Wrapper for EXCHANGE_ID operation.
7889 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007890static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11007891nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007892 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007893{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007894 struct rpc_message msg = {
7895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007896 .rpc_cred = cred,
7897 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007898 struct rpc_task_setup task_setup_data = {
7899 .rpc_client = clp->cl_rpcclient,
7900 .callback_ops = &nfs4_exchange_id_call_ops,
7901 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007902 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007903 };
7904 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007905 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007906
Elena Reshetova212bf412017-10-20 12:53:38 +03007907 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007908 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007909
Trond Myklebust9c760d12017-07-31 18:38:50 -04007910 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007911 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007912 if (!calldata)
7913 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007914
Trond Myklebustfd405592017-08-01 16:02:47 -04007915 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007916
7917 status = nfs4_init_uniform_client_string(clp);
7918 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007919 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007920
Andy Adamson8d89bd72016-09-09 09:22:18 -04007921 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7922 GFP_NOFS);
7923 status = -ENOMEM;
7924 if (unlikely(calldata->res.server_owner == NULL))
7925 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007926
Andy Adamson8d89bd72016-09-09 09:22:18 -04007927 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007928 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007929 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007930 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007931
Andy Adamson8d89bd72016-09-09 09:22:18 -04007932 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7933 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007934 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007935
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007936 switch (sp4_how) {
7937 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007938 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007939 break;
7940
7941 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007942 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007943 break;
7944
7945 default:
7946 /* unsupported! */
7947 WARN_ON_ONCE(1);
7948 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007949 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007950 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007951 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007952 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007953 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007954 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7955 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007956 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007957 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007958 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7959 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007960#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007961 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007962#endif
7963 msg.rpc_argp = &calldata->args;
7964 msg.rpc_resp = &calldata->res;
7965 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007966
Trond Myklebust9c760d12017-07-31 18:38:50 -04007967 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007968
7969out_impl_id:
7970 kfree(calldata->res.impl_id);
7971out_server_scope:
7972 kfree(calldata->res.server_scope);
7973out_server_owner:
7974 kfree(calldata->res.server_owner);
7975out_calldata:
7976 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007977out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007978 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007979 return ERR_PTR(status);
7980}
7981
7982/*
7983 * _nfs4_proc_exchange_id()
7984 *
7985 * Wrapper for EXCHANGE_ID operation.
7986 */
NeilBrowna52458b2018-12-03 11:30:31 +11007987static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04007988 u32 sp4_how)
7989{
7990 struct rpc_task *task;
7991 struct nfs41_exchange_id_args *argp;
7992 struct nfs41_exchange_id_res *resp;
7993 int status;
7994
7995 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7996 if (IS_ERR(task))
7997 return PTR_ERR(task);
7998
7999 argp = task->tk_msg.rpc_argp;
8000 resp = task->tk_msg.rpc_resp;
8001 status = task->tk_status;
8002 if (status != 0)
8003 goto out;
8004
8005 status = nfs4_check_cl_exchange_flags(resp->flags);
8006 if (status != 0)
8007 goto out;
8008
8009 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8010 if (status != 0)
8011 goto out;
8012
8013 clp->cl_clientid = resp->clientid;
8014 clp->cl_exchange_flags = resp->flags;
8015 clp->cl_seqid = resp->seqid;
8016 /* Client ID is not confirmed */
8017 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8018 clear_bit(NFS4_SESSION_ESTABLISHED,
8019 &clp->cl_session->session_state);
8020
8021 if (clp->cl_serverscope != NULL &&
8022 !nfs41_same_server_scope(clp->cl_serverscope,
8023 resp->server_scope)) {
8024 dprintk("%s: server_scope mismatch detected\n",
8025 __func__);
8026 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8027 }
8028
8029 swap(clp->cl_serverowner, resp->server_owner);
8030 swap(clp->cl_serverscope, resp->server_scope);
8031 swap(clp->cl_implid, resp->impl_id);
8032
8033 /* Save the EXCHANGE_ID verifier session trunk tests */
8034 memcpy(clp->cl_confirm.data, argp->verifier.data,
8035 sizeof(clp->cl_confirm.data));
8036out:
8037 trace_nfs4_exchange_id(clp, status);
8038 rpc_put_task(task);
8039 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008040}
8041
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008042/*
8043 * nfs4_proc_exchange_id()
8044 *
8045 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8046 *
8047 * Since the clientid has expired, all compounds using sessions
8048 * associated with the stale clientid will be returning
8049 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8050 * be in some phase of session reset.
8051 *
8052 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8053 */
NeilBrowna52458b2018-12-03 11:30:31 +11008054int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008055{
8056 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8057 int status;
8058
8059 /* try SP4_MACH_CRED if krb5i/p */
8060 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8061 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008062 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008063 if (!status)
8064 return 0;
8065 }
8066
8067 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008068 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008069}
8070
Andy Adamson04fa2c62016-09-09 09:22:29 -04008071/**
8072 * nfs4_test_session_trunk
8073 *
8074 * This is an add_xprt_test() test function called from
8075 * rpc_clnt_setup_test_and_add_xprt.
8076 *
8077 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8078 * and is dereferrenced in nfs4_exchange_id_release
8079 *
8080 * Upon success, add the new transport to the rpc_clnt
8081 *
8082 * @clnt: struct rpc_clnt to get new transport
8083 * @xprt: the rpc_xprt to test
8084 * @data: call data for _nfs4_proc_exchange_id.
8085 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308086void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008087 void *data)
8088{
8089 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008090 struct rpc_task *task;
8091 int status;
8092
Andy Adamson04fa2c62016-09-09 09:22:29 -04008093 u32 sp4_how;
8094
8095 dprintk("--> %s try %s\n", __func__,
8096 xprt->address_strings[RPC_DISPLAY_ADDR]);
8097
8098 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8099
8100 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008101 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8102 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308103 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008104
8105 status = task->tk_status;
8106 if (status == 0)
8107 status = nfs4_detect_session_trunking(adata->clp,
8108 task->tk_msg.rpc_resp, xprt);
8109
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308110 if (status == 0)
8111 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8112
Trond Myklebust9c760d12017-07-31 18:38:50 -04008113 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008114}
8115EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8116
Trond Myklebust66245532012-05-25 17:18:09 -04008117static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008118 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008119{
8120 struct rpc_message msg = {
8121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8122 .rpc_argp = clp,
8123 .rpc_cred = cred,
8124 };
8125 int status;
8126
8127 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008128 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008129 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008130 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008131 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8132 return status;
8133}
8134
8135static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008136 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008137{
8138 unsigned int loop;
8139 int ret;
8140
8141 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8142 ret = _nfs4_proc_destroy_clientid(clp, cred);
8143 switch (ret) {
8144 case -NFS4ERR_DELAY:
8145 case -NFS4ERR_CLIENTID_BUSY:
8146 ssleep(1);
8147 break;
8148 default:
8149 return ret;
8150 }
8151 }
8152 return 0;
8153}
8154
8155int nfs4_destroy_clientid(struct nfs_client *clp)
8156{
NeilBrowna52458b2018-12-03 11:30:31 +11008157 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008158 int ret = 0;
8159
8160 if (clp->cl_mvops->minor_version < 1)
8161 goto out;
8162 if (clp->cl_exchange_flags == 0)
8163 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008164 if (clp->cl_preserve_clid)
8165 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008166 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008167 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008168 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008169 switch (ret) {
8170 case 0:
8171 case -NFS4ERR_STALE_CLIENTID:
8172 clp->cl_exchange_flags = 0;
8173 }
8174out:
8175 return ret;
8176}
8177
Andy Adamson2050f0c2009-04-01 09:22:30 -04008178struct nfs4_get_lease_time_data {
8179 struct nfs4_get_lease_time_args *args;
8180 struct nfs4_get_lease_time_res *res;
8181 struct nfs_client *clp;
8182};
8183
8184static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8185 void *calldata)
8186{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008187 struct nfs4_get_lease_time_data *data =
8188 (struct nfs4_get_lease_time_data *)calldata;
8189
8190 dprintk("--> %s\n", __func__);
8191 /* just setup sequence, do not trigger session recovery
8192 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008193 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008194 &data->args->la_seq_args,
8195 &data->res->lr_seq_res,
8196 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008197 dprintk("<-- %s\n", __func__);
8198}
8199
8200/*
8201 * Called from nfs4_state_manager thread for session setup, so don't recover
8202 * from sequence operation or clientid errors.
8203 */
8204static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8205{
8206 struct nfs4_get_lease_time_data *data =
8207 (struct nfs4_get_lease_time_data *)calldata;
8208
8209 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008210 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8211 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008212 switch (task->tk_status) {
8213 case -NFS4ERR_DELAY:
8214 case -NFS4ERR_GRACE:
8215 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8216 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8217 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008218 /* fall through */
8219 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008220 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008221 return;
8222 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008223 dprintk("<-- %s\n", __func__);
8224}
8225
Trond Myklebust17280172012-03-11 13:11:00 -04008226static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008227 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8228 .rpc_call_done = nfs4_get_lease_time_done,
8229};
8230
8231int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8232{
8233 struct rpc_task *task;
8234 struct nfs4_get_lease_time_args args;
8235 struct nfs4_get_lease_time_res res = {
8236 .lr_fsinfo = fsinfo,
8237 };
8238 struct nfs4_get_lease_time_data data = {
8239 .args = &args,
8240 .res = &res,
8241 .clp = clp,
8242 };
8243 struct rpc_message msg = {
8244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8245 .rpc_argp = &args,
8246 .rpc_resp = &res,
8247 };
8248 struct rpc_task_setup task_setup = {
8249 .rpc_client = clp->cl_rpcclient,
8250 .rpc_message = &msg,
8251 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008252 .callback_data = &data,
8253 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008254 };
8255 int status;
8256
Anna Schumakerfba83f32018-05-04 16:22:50 -04008257 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008258 task = rpc_run_task(&task_setup);
8259
8260 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008261 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008262
Anna Schumakerf6148712017-04-07 14:15:23 -04008263 status = task->tk_status;
8264 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008265 return status;
8266}
8267
Andy Adamsonfc931582009-04-01 09:22:31 -04008268/*
8269 * Initialize the values to be used by the client in CREATE_SESSION
8270 * If nfs4_init_session set the fore channel request and response sizes,
8271 * use them.
8272 *
8273 * Set the back channel max_resp_sz_cached to zero to force the client to
8274 * always set csa_cachethis to FALSE because the current implementation
8275 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8276 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008277static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8278 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008279{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008280 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008281 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008282
Andy Adamson18aad3d2013-06-26 12:21:49 -04008283 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8284 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8285
Andy Adamsonfc931582009-04-01 09:22:31 -04008286 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008287 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8288 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008289 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008290 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008291
8292 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008293 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008294 __func__,
8295 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008296 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008297
8298 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008299 args->bc_attrs.max_rqst_sz = max_bc_payload;
8300 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008301 args->bc_attrs.max_resp_sz_cached = 0;
8302 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008303 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008304
8305 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8306 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8307 __func__,
8308 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8309 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8310 args->bc_attrs.max_reqs);
8311}
8312
Trond Myklebust79969dd2015-02-18 11:30:18 -08008313static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8314 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008315{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008316 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008317 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008318
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008319 if (rcvd->max_resp_sz > sent->max_resp_sz)
8320 return -EINVAL;
8321 /*
8322 * Our requested max_ops is the minimum we need; we're not
8323 * prepared to break up compounds into smaller pieces than that.
8324 * So, no point even trying to continue if the server won't
8325 * cooperate:
8326 */
8327 if (rcvd->max_ops < sent->max_ops)
8328 return -EINVAL;
8329 if (rcvd->max_reqs == 0)
8330 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008331 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8332 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008333 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008334}
8335
Trond Myklebust79969dd2015-02-18 11:30:18 -08008336static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8337 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008338{
8339 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008340 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008341
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008342 if (!(res->flags & SESSION4_BACK_CHAN))
8343 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008344 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8345 return -EINVAL;
8346 if (rcvd->max_resp_sz < sent->max_resp_sz)
8347 return -EINVAL;
8348 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8349 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008350 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008351 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008352 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008353 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008354out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008355 return 0;
8356}
Andy Adamson8d353012009-04-01 09:22:32 -04008357
Andy Adamson8d353012009-04-01 09:22:32 -04008358static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008359 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008360{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008361 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008362
Trond Myklebust79969dd2015-02-18 11:30:18 -08008363 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008364 if (ret)
8365 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008366 return nfs4_verify_back_channel_attrs(args, res);
8367}
8368
8369static void nfs4_update_session(struct nfs4_session *session,
8370 struct nfs41_create_session_res *res)
8371{
8372 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008373 /* Mark client id and session as being confirmed */
8374 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8375 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008376 session->flags = res->flags;
8377 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008378 if (res->flags & SESSION4_BACK_CHAN)
8379 memcpy(&session->bc_attrs, &res->bc_attrs,
8380 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008381}
8382
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008383static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008384 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008385{
8386 struct nfs4_session *session = clp->cl_session;
8387 struct nfs41_create_session_args args = {
8388 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008389 .clientid = clp->cl_clientid,
8390 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008391 .cb_program = NFS4_CALLBACK,
8392 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008393 struct nfs41_create_session_res res;
8394
Andy Adamsonfc931582009-04-01 09:22:31 -04008395 struct rpc_message msg = {
8396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8397 .rpc_argp = &args,
8398 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008399 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008400 };
8401 int status;
8402
Chuck Lever6b26cc82016-05-02 14:40:40 -04008403 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008404 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008405
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008406 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008407 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008408
Trond Myklebustb519d402016-09-11 14:50:01 -04008409 switch (status) {
8410 case -NFS4ERR_STALE_CLIENTID:
8411 case -NFS4ERR_DELAY:
8412 case -ETIMEDOUT:
8413 case -EACCES:
8414 case -EAGAIN:
8415 goto out;
8416 };
8417
8418 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008419 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008420 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008421 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008422 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008423 if (status)
8424 goto out;
8425 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008426 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008427out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008428 return status;
8429}
8430
8431/*
8432 * Issues a CREATE_SESSION operation to the server.
8433 * It is the responsibility of the caller to verify the session is
8434 * expired before calling this routine.
8435 */
NeilBrowna52458b2018-12-03 11:30:31 +11008436int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008437{
8438 int status;
8439 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008440 struct nfs4_session *session = clp->cl_session;
8441
8442 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8443
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008444 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008445 if (status)
8446 goto out;
8447
Andy Adamsonaacd5532011-11-09 13:58:21 -05008448 /* Init or reset the session slot tables */
8449 status = nfs4_setup_session_slot_tables(session);
8450 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008451 if (status)
8452 goto out;
8453
8454 ptr = (unsigned *)&session->sess_id.data[0];
8455 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8456 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008457out:
8458 dprintk("<-- %s\n", __func__);
8459 return status;
8460}
8461
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008462/*
8463 * Issue the over-the-wire RPC DESTROY_SESSION.
8464 * The caller must serialize access to this routine.
8465 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008466int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008467 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008468{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008469 struct rpc_message msg = {
8470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8471 .rpc_argp = session,
8472 .rpc_cred = cred,
8473 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008474 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008475
8476 dprintk("--> nfs4_proc_destroy_session\n");
8477
8478 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008479 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8480 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008481
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008482 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008483 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008484
8485 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008486 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008487 "Session has been destroyed regardless...\n", status);
8488
8489 dprintk("<-- nfs4_proc_destroy_session\n");
8490 return status;
8491}
8492
Trond Myklebust7b38c362012-05-23 13:23:31 -04008493/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008494 * Renew the cl_session lease.
8495 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008496struct nfs4_sequence_data {
8497 struct nfs_client *clp;
8498 struct nfs4_sequence_args args;
8499 struct nfs4_sequence_res res;
8500};
8501
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008502static void nfs41_sequence_release(void *data)
8503{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008504 struct nfs4_sequence_data *calldata = data;
8505 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008506
Elena Reshetova212bf412017-10-20 12:53:38 +03008507 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008508 nfs4_schedule_state_renewal(clp);
8509 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008510 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008511}
8512
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008513static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8514{
8515 switch(task->tk_status) {
8516 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008517 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8518 return -EAGAIN;
8519 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008520 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008521 }
8522 return 0;
8523}
8524
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008525static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008526{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008527 struct nfs4_sequence_data *calldata = data;
8528 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008529
Trond Myklebust14516c32010-07-31 14:29:06 -04008530 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8531 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008532
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008533 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008534 if (task->tk_status < 0) {
8535 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008536 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008537 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008538
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008539 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8540 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008541 return;
8542 }
8543 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008544 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008545out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008546 dprintk("<-- %s\n", __func__);
8547}
8548
8549static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8550{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008551 struct nfs4_sequence_data *calldata = data;
8552 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008553 struct nfs4_sequence_args *args;
8554 struct nfs4_sequence_res *res;
8555
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008556 args = task->tk_msg.rpc_argp;
8557 res = task->tk_msg.rpc_resp;
8558
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008559 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008560}
8561
8562static const struct rpc_call_ops nfs41_sequence_ops = {
8563 .rpc_call_done = nfs41_sequence_call_done,
8564 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008565 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008566};
8567
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008568static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008569 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008570 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008571 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008572{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008573 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008574 struct rpc_message msg = {
8575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8576 .rpc_cred = cred,
8577 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008578 struct rpc_task_setup task_setup_data = {
8579 .rpc_client = clp->cl_rpcclient,
8580 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008581 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008582 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008583 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008584 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008585
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008586 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008587 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008588 goto out_err;
8589
8590 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008591 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008592 if (calldata == NULL)
8593 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008594 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008595 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008596 msg.rpc_argp = &calldata->args;
8597 msg.rpc_resp = &calldata->res;
8598 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008599 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008600
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008601 ret = rpc_run_task(&task_setup_data);
8602 if (IS_ERR(ret))
8603 goto out_err;
8604 return ret;
8605out_put_clp:
8606 nfs_put_client(clp);
8607out_err:
8608 nfs41_release_slot(slot);
8609 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008610}
8611
NeilBrowna52458b2018-12-03 11:30:31 +11008612static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008613{
8614 struct rpc_task *task;
8615 int ret = 0;
8616
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008617 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008618 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008619 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008620 if (IS_ERR(task))
8621 ret = PTR_ERR(task);
8622 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008623 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008624 dprintk("<-- %s status=%d\n", __func__, ret);
8625 return ret;
8626}
8627
NeilBrowna52458b2018-12-03 11:30:31 +11008628static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008629{
8630 struct rpc_task *task;
8631 int ret;
8632
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008633 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008634 if (IS_ERR(task)) {
8635 ret = PTR_ERR(task);
8636 goto out;
8637 }
8638 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008639 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008640 ret = task->tk_status;
8641 rpc_put_task(task);
8642out:
8643 dprintk("<-- %s status=%d\n", __func__, ret);
8644 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008645}
8646
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008647struct nfs4_reclaim_complete_data {
8648 struct nfs_client *clp;
8649 struct nfs41_reclaim_complete_args arg;
8650 struct nfs41_reclaim_complete_res res;
8651};
8652
8653static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8654{
8655 struct nfs4_reclaim_complete_data *calldata = data;
8656
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008657 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008658 &calldata->arg.seq_args,
8659 &calldata->res.seq_res,
8660 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008661}
8662
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008663static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8664{
8665 switch(task->tk_status) {
8666 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008667 wake_up_all(&clp->cl_lock_waitq);
8668 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008669 case -NFS4ERR_COMPLETE_ALREADY:
8670 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8671 break;
8672 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008673 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008674 /* fall through */
8675 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008676 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008677 case -NFS4ERR_BADSESSION:
8678 case -NFS4ERR_DEADSESSION:
8679 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8680 nfs4_schedule_session_recovery(clp->cl_session,
8681 task->tk_status);
8682 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008683 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008684 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008685 }
8686 return 0;
8687}
8688
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008689static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8690{
8691 struct nfs4_reclaim_complete_data *calldata = data;
8692 struct nfs_client *clp = calldata->clp;
8693 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8694
8695 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008696 if (!nfs41_sequence_done(task, res))
8697 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008698
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008699 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008700 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8701 rpc_restart_call_prepare(task);
8702 return;
8703 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008704 dprintk("<-- %s\n", __func__);
8705}
8706
8707static void nfs4_free_reclaim_complete_data(void *data)
8708{
8709 struct nfs4_reclaim_complete_data *calldata = data;
8710
8711 kfree(calldata);
8712}
8713
8714static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8715 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8716 .rpc_call_done = nfs4_reclaim_complete_done,
8717 .rpc_release = nfs4_free_reclaim_complete_data,
8718};
8719
8720/*
8721 * Issue a global reclaim complete.
8722 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008723static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008724 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008725{
8726 struct nfs4_reclaim_complete_data *calldata;
8727 struct rpc_task *task;
8728 struct rpc_message msg = {
8729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008730 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008731 };
8732 struct rpc_task_setup task_setup_data = {
8733 .rpc_client = clp->cl_rpcclient,
8734 .rpc_message = &msg,
8735 .callback_ops = &nfs4_reclaim_complete_call_ops,
8736 .flags = RPC_TASK_ASYNC,
8737 };
8738 int status = -ENOMEM;
8739
8740 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008741 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008742 if (calldata == NULL)
8743 goto out;
8744 calldata->clp = clp;
8745 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008746
Anna Schumakerfba83f32018-05-04 16:22:50 -04008747 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008748 msg.rpc_argp = &calldata->arg;
8749 msg.rpc_resp = &calldata->res;
8750 task_setup_data.callback_data = calldata;
8751 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008752 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008753 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008754 goto out;
8755 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008756 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008757 if (status == 0)
8758 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008759 rpc_put_task(task);
8760out:
8761 dprintk("<-- %s status=%d\n", __func__, status);
8762 return status;
8763}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008764
8765static void
8766nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8767{
8768 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008769 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008770
8771 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008772 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008773 &lgp->res.seq_res, task);
8774 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008775}
8776
8777static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8778{
8779 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008780
8781 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008782 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008783 dprintk("<-- %s\n", __func__);
8784}
8785
8786static int
8787nfs4_layoutget_handle_exception(struct rpc_task *task,
8788 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8789{
Trond Myklebustee314c22012-10-01 17:25:48 -07008790 struct inode *inode = lgp->args.inode;
8791 struct nfs_server *server = NFS_SERVER(inode);
8792 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008793 int nfs4err = task->tk_status;
8794 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008795 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008796
Boaz Harroshed7e5422014-01-22 20:34:54 +02008797 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008798
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008799 nfs4_sequence_free_slot(&lgp->res.seq_res);
8800
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008801 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008802 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008803 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008804
8805 /*
8806 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8807 * on the file. set tk_status to -ENODATA to tell upper layer to
8808 * retry go inband.
8809 */
8810 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008811 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008812 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008813 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008814 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8815 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8816 */
8817 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008818 status = -EOVERFLOW;
8819 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008820 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008821 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008822 * (or clients) writing to the same RAID stripe except when
8823 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008824 *
8825 * Treat it like we would RECALLCONFLICT -- we retry for a little
8826 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008827 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008828 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008829 if (lgp->args.minlength == 0) {
8830 status = -EOVERFLOW;
8831 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008832 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008833 status = -EBUSY;
8834 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008835 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008836 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008837 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008838 case -NFS4ERR_DELEG_REVOKED:
8839 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008840 case -NFS4ERR_EXPIRED:
8841 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008842 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008843 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008844 lo = NFS_I(inode)->layout;
8845 /* If the open stateid was bad, then recover it. */
8846 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008847 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008848 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008849 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008850 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008851 break;
8852 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008853
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008854 /*
8855 * Mark the bad layout state as invalid, then retry
8856 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008857 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008858 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008859 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008860 pnfs_free_lseg_list(&head);
8861 status = -EAGAIN;
8862 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008863 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008864
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008865 err = nfs4_handle_exception(server, nfs4err, exception);
8866 if (!status) {
8867 if (exception->retry)
8868 status = -EAGAIN;
8869 else
8870 status = err;
8871 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008872out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008873 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008874 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008875}
8876
Fred Isamandacb4522016-09-19 17:47:09 -04008877size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03008878{
8879 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8880 return nfs_page_array_len(0, max_resp_sz);
8881}
8882
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008883static void nfs4_layoutget_release(void *calldata)
8884{
8885 struct nfs4_layoutget *lgp = calldata;
8886
8887 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008888 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04008889 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008890 dprintk("<-- %s\n", __func__);
8891}
8892
8893static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8894 .rpc_call_prepare = nfs4_layoutget_prepare,
8895 .rpc_call_done = nfs4_layoutget_done,
8896 .rpc_release = nfs4_layoutget_release,
8897};
8898
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008899struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04008900nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008901{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008902 struct inode *inode = lgp->args.inode;
8903 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008904 struct rpc_task *task;
8905 struct rpc_message msg = {
8906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8907 .rpc_argp = &lgp->args,
8908 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008909 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008910 };
8911 struct rpc_task_setup task_setup_data = {
8912 .rpc_client = server->client,
8913 .rpc_message = &msg,
8914 .callback_ops = &nfs4_layoutget_call_ops,
8915 .callback_data = lgp,
8916 .flags = RPC_TASK_ASYNC,
8917 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008918 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008919 struct nfs4_exception exception = {
8920 .inode = inode,
8921 .timeout = *timeout,
8922 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008923 int status = 0;
8924
8925 dprintk("--> %s\n", __func__);
8926
Peng Tao4bd5a982014-11-17 11:05:17 +08008927 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8928 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8929
Anna Schumakerfba83f32018-05-04 16:22:50 -04008930 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008931
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008932 task = rpc_run_task(&task_setup_data);
8933 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008934 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008935 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008936 if (status != 0)
8937 goto out;
8938
8939 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8940 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04008941 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8942 *timeout = exception.timeout;
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008943 } else
8944 lseg = pnfs_layout_process(lgp);
8945out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008946 trace_nfs4_layoutget(lgp->args.ctx,
8947 &lgp->args.range,
8948 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008949 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008950 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008951
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008952 rpc_put_task(task);
8953 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008954 if (status)
8955 return ERR_PTR(status);
8956 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008957}
8958
Benny Halevycbe82602011-05-22 19:52:37 +03008959static void
8960nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8961{
8962 struct nfs4_layoutreturn *lrp = calldata;
8963
8964 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008965 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008966 &lrp->args.seq_args,
8967 &lrp->res.seq_res,
8968 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04008969 if (!pnfs_layout_is_valid(lrp->args.layout))
8970 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008971}
8972
8973static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8974{
8975 struct nfs4_layoutreturn *lrp = calldata;
8976 struct nfs_server *server;
8977
8978 dprintk("--> %s\n", __func__);
8979
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008980 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008981 return;
8982
8983 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008984 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008985 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04008986 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04008987 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05008988 lrp->args.inode))
8989 goto out_restart;
8990 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008991 default:
8992 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008993 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008994 case 0:
8995 break;
8996 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008997 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008998 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008999 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009000 }
Benny Halevycbe82602011-05-22 19:52:37 +03009001 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009002 return;
9003out_restart:
9004 task->tk_status = 0;
9005 nfs4_sequence_free_slot(&lrp->res.seq_res);
9006 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009007}
9008
9009static void nfs4_layoutreturn_release(void *calldata)
9010{
9011 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009012 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009013
9014 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009015 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009016 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009017 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009018 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9019 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009020 pnfs_put_layout_hdr(lrp->args.layout);
9021 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009022 kfree(calldata);
9023 dprintk("<-- %s\n", __func__);
9024}
9025
9026static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9027 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9028 .rpc_call_done = nfs4_layoutreturn_done,
9029 .rpc_release = nfs4_layoutreturn_release,
9030};
9031
Peng Tao6c166052014-11-17 09:30:40 +08009032int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009033{
9034 struct rpc_task *task;
9035 struct rpc_message msg = {
9036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9037 .rpc_argp = &lrp->args,
9038 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009039 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009040 };
9041 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009042 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009043 .rpc_message = &msg,
9044 .callback_ops = &nfs4_layoutreturn_call_ops,
9045 .callback_data = lrp,
9046 };
Peng Tao6c166052014-11-17 09:30:40 +08009047 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009048
Andrew Elble99ade3c2015-12-02 09:39:51 -05009049 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9050 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9051 &task_setup_data.rpc_client, &msg);
9052
Benny Halevycbe82602011-05-22 19:52:37 +03009053 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009054 if (!sync) {
9055 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9056 if (!lrp->inode) {
9057 nfs4_layoutreturn_release(lrp);
9058 return -EAGAIN;
9059 }
9060 task_setup_data.flags |= RPC_TASK_ASYNC;
9061 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009062 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009063 task = rpc_run_task(&task_setup_data);
9064 if (IS_ERR(task))
9065 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009066 if (sync)
9067 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009068 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009069 dprintk("<-- %s status=%d\n", __func__, status);
9070 rpc_put_task(task);
9071 return status;
9072}
9073
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009074static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009075_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9076 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009077 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009078{
9079 struct nfs4_getdeviceinfo_args args = {
9080 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009081 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9082 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009083 };
9084 struct nfs4_getdeviceinfo_res res = {
9085 .pdev = pdev,
9086 };
9087 struct rpc_message msg = {
9088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9089 .rpc_argp = &args,
9090 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009091 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009092 };
9093 int status;
9094
9095 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009096 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009097 if (res.notification & ~args.notify_types)
9098 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009099 if (res.notification != args.notify_types)
9100 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009101
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009102 dprintk("<-- %s status=%d\n", __func__, status);
9103
9104 return status;
9105}
9106
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009107int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9108 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009109 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009110{
9111 struct nfs4_exception exception = { };
9112 int err;
9113
9114 do {
9115 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009116 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009117 &exception);
9118 } while (exception.retry);
9119 return err;
9120}
9121EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9122
Andy Adamson863a3c62011-03-23 13:27:54 +00009123static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9124{
9125 struct nfs4_layoutcommit_data *data = calldata;
9126 struct nfs_server *server = NFS_SERVER(data->args.inode);
9127
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009128 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009129 &data->args.seq_args,
9130 &data->res.seq_res,
9131 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009132}
9133
9134static void
9135nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9136{
9137 struct nfs4_layoutcommit_data *data = calldata;
9138 struct nfs_server *server = NFS_SERVER(data->args.inode);
9139
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009140 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009141 return;
9142
9143 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009144 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9145 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9146 case -NFS4ERR_BADLAYOUT: /* no layout */
9147 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009148 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009149 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009150 break;
9151 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009152 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009153 rpc_restart_call_prepare(task);
9154 return;
9155 }
9156 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009157}
9158
9159static void nfs4_layoutcommit_release(void *calldata)
9160{
9161 struct nfs4_layoutcommit_data *data = calldata;
9162
Andy Adamsondb29c082011-07-30 20:52:38 -04009163 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009164 nfs_post_op_update_inode_force_wcc(data->args.inode,
9165 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009166 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009167 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009168 kfree(data);
9169}
9170
9171static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9172 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9173 .rpc_call_done = nfs4_layoutcommit_done,
9174 .rpc_release = nfs4_layoutcommit_release,
9175};
9176
9177int
Andy Adamsonef311532011-03-12 02:58:10 -05009178nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009179{
9180 struct rpc_message msg = {
9181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9182 .rpc_argp = &data->args,
9183 .rpc_resp = &data->res,
9184 .rpc_cred = data->cred,
9185 };
9186 struct rpc_task_setup task_setup_data = {
9187 .task = &data->task,
9188 .rpc_client = NFS_CLIENT(data->args.inode),
9189 .rpc_message = &msg,
9190 .callback_ops = &nfs4_layoutcommit_ops,
9191 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009192 };
9193 struct rpc_task *task;
9194 int status = 0;
9195
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009196 dprintk("NFS: initiating layoutcommit call. sync %d "
9197 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009198 data->args.lastbytewritten,
9199 data->args.inode->i_ino);
9200
Trond Myklebust472e2592015-02-05 16:50:30 -05009201 if (!sync) {
9202 data->inode = nfs_igrab_and_active(data->args.inode);
9203 if (data->inode == NULL) {
9204 nfs4_layoutcommit_release(data);
9205 return -EAGAIN;
9206 }
9207 task_setup_data.flags = RPC_TASK_ASYNC;
9208 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009209 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009210 task = rpc_run_task(&task_setup_data);
9211 if (IS_ERR(task))
9212 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009213 if (sync)
9214 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009215 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009216 dprintk("%s: status %d\n", __func__, status);
9217 rpc_put_task(task);
9218 return status;
9219}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009220
Trond Myklebust302fad72019-02-18 13:32:38 -05009221/*
Andy Adamson97431202013-08-08 10:57:56 -04009222 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9223 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9224 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009225static int
9226_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009227 struct nfs_fsinfo *info,
9228 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009229{
9230 struct nfs41_secinfo_no_name_args args = {
9231 .style = SECINFO_STYLE_CURRENT_FH,
9232 };
9233 struct nfs4_secinfo_res res = {
9234 .flavors = flavors,
9235 };
9236 struct rpc_message msg = {
9237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9238 .rpc_argp = &args,
9239 .rpc_resp = &res,
9240 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009241 struct rpc_clnt *clnt = server->client;
NeilBrowna52458b2018-12-03 11:30:31 +11009242 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009243 int status;
9244
9245 if (use_integrity) {
9246 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009247 cred = nfs4_get_clid_cred(server->nfs_client);
9248 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009249 }
9250
9251 dprintk("--> %s\n", __func__);
9252 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9253 &res.seq_res, 0);
9254 dprintk("<-- %s status=%d\n", __func__, status);
9255
NeilBrowna52458b2018-12-03 11:30:31 +11009256 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009257
9258 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009259}
9260
9261static int
9262nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9263 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9264{
9265 struct nfs4_exception exception = { };
9266 int err;
9267 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009268 /* first try using integrity protection */
9269 err = -NFS4ERR_WRONGSEC;
9270
9271 /* try to use integrity protection with machine cred */
9272 if (_nfs4_is_integrity_protected(server->nfs_client))
9273 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9274 flavors, true);
9275
9276 /*
9277 * if unable to use integrity protection, or SECINFO with
9278 * integrity protection returns NFS4ERR_WRONGSEC (which is
9279 * disallowed by spec, but exists in deployed servers) use
9280 * the current filesystem's rpc_client and the user cred.
9281 */
9282 if (err == -NFS4ERR_WRONGSEC)
9283 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9284 flavors, false);
9285
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009286 switch (err) {
9287 case 0:
9288 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009289 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009290 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009291 default:
9292 err = nfs4_handle_exception(server, err, &exception);
9293 }
9294 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009295out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009296 return err;
9297}
9298
9299static int
9300nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9301 struct nfs_fsinfo *info)
9302{
9303 int err;
9304 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009305 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009306 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009307 struct nfs4_secinfo4 *secinfo;
9308 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009309
9310 page = alloc_page(GFP_KERNEL);
9311 if (!page) {
9312 err = -ENOMEM;
9313 goto out;
9314 }
9315
9316 flavors = page_address(page);
9317 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9318
9319 /*
9320 * Fall back on "guess and check" method if
9321 * the server doesn't support SECINFO_NO_NAME
9322 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009323 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009324 err = nfs4_find_root_sec(server, fhandle, info);
9325 goto out_freepage;
9326 }
9327 if (err)
9328 goto out_freepage;
9329
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009330 for (i = 0; i < flavors->num_flavors; i++) {
9331 secinfo = &flavors->flavors[i];
9332
9333 switch (secinfo->flavor) {
9334 case RPC_AUTH_NULL:
9335 case RPC_AUTH_UNIX:
9336 case RPC_AUTH_GSS:
9337 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9338 &secinfo->flavor_info);
9339 break;
9340 default:
9341 flavor = RPC_AUTH_MAXFLAVOR;
9342 break;
9343 }
9344
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009345 if (!nfs_auth_info_match(&server->auth_info, flavor))
9346 flavor = RPC_AUTH_MAXFLAVOR;
9347
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009348 if (flavor != RPC_AUTH_MAXFLAVOR) {
9349 err = nfs4_lookup_root_sec(server, fhandle,
9350 info, flavor);
9351 if (!err)
9352 break;
9353 }
9354 }
9355
9356 if (flavor == RPC_AUTH_MAXFLAVOR)
9357 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009358
9359out_freepage:
9360 put_page(page);
9361 if (err == -EACCES)
9362 return -EPERM;
9363out:
9364 return err;
9365}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009366
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009367static int _nfs41_test_stateid(struct nfs_server *server,
9368 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009369 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009370{
9371 int status;
9372 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009373 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009374 };
9375 struct nfs41_test_stateid_res res;
9376 struct rpc_message msg = {
9377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9378 .rpc_argp = &args,
9379 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009380 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009381 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009382 struct rpc_clnt *rpc_client = server->client;
9383
9384 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9385 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009386
Chuck Lever38527b12012-07-11 16:30:23 -04009387 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009388 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009389 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009390 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009391 if (status != NFS_OK) {
9392 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009393 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009394 }
9395 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009396 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009397}
9398
Trond Myklebust43912bb2016-09-22 13:38:56 -04009399static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9400 int err, struct nfs4_exception *exception)
9401{
9402 exception->retry = 0;
9403 switch(err) {
9404 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009405 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009406 nfs4_handle_exception(server, err, exception);
9407 break;
9408 case -NFS4ERR_BADSESSION:
9409 case -NFS4ERR_BADSLOT:
9410 case -NFS4ERR_BAD_HIGH_SLOT:
9411 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9412 case -NFS4ERR_DEADSESSION:
9413 nfs4_do_handle_exception(server, err, exception);
9414 }
9415}
9416
Chuck Lever38527b12012-07-11 16:30:23 -04009417/**
9418 * nfs41_test_stateid - perform a TEST_STATEID operation
9419 *
9420 * @server: server / transport on which to perform the operation
9421 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009422 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009423 *
9424 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9425 * Otherwise a negative NFS4ERR value is returned if the operation
9426 * failed or the state ID is not currently valid.
9427 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009428static int nfs41_test_stateid(struct nfs_server *server,
9429 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009430 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009431{
9432 struct nfs4_exception exception = { };
9433 int err;
9434 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009435 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009436 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009437 } while (exception.retry);
9438 return err;
9439}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009440
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009441struct nfs_free_stateid_data {
9442 struct nfs_server *server;
9443 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009444 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009445};
9446
9447static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9448{
9449 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009450 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009451 &data->args.seq_args,
9452 &data->res.seq_res,
9453 task);
9454}
9455
9456static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9457{
9458 struct nfs_free_stateid_data *data = calldata;
9459
9460 nfs41_sequence_done(task, &data->res.seq_res);
9461
9462 switch (task->tk_status) {
9463 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009464 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009465 rpc_restart_call_prepare(task);
9466 }
9467}
9468
9469static void nfs41_free_stateid_release(void *calldata)
9470{
9471 kfree(calldata);
9472}
9473
Trond Myklebust17f26b12013-08-21 15:48:42 -04009474static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009475 .rpc_call_prepare = nfs41_free_stateid_prepare,
9476 .rpc_call_done = nfs41_free_stateid_done,
9477 .rpc_release = nfs41_free_stateid_release,
9478};
9479
Anna Schumaker2f261022018-05-15 13:03:39 -04009480/**
9481 * nfs41_free_stateid - perform a FREE_STATEID operation
9482 *
9483 * @server: server / transport on which to perform the operation
9484 * @stateid: state ID to release
9485 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009486 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009487 *
9488 * Note: this function is always asynchronous.
9489 */
9490static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009491 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009492 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009493 bool privileged)
9494{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009495 struct rpc_message msg = {
9496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009497 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009498 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009499 struct rpc_task_setup task_setup = {
9500 .rpc_client = server->client,
9501 .rpc_message = &msg,
9502 .callback_ops = &nfs41_free_stateid_ops,
9503 .flags = RPC_TASK_ASYNC,
9504 };
9505 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009506 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009507
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009508 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9509 &task_setup.rpc_client, &msg);
9510
Chuck Lever38527b12012-07-11 16:30:23 -04009511 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009512 data = kmalloc(sizeof(*data), GFP_NOFS);
9513 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009514 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009515 data->server = server;
9516 nfs4_stateid_copy(&data->args.stateid, stateid);
9517
9518 task_setup.callback_data = data;
9519
9520 msg.rpc_argp = &data->args;
9521 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009522 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009523 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009524 if (IS_ERR(task))
9525 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009526 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009527 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009528}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009529
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009530static void
9531nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009532{
NeilBrowna52458b2018-12-03 11:30:31 +11009533 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009534
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009535 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009536 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009537}
9538
Trond Myklebust36281ca2012-03-04 18:13:56 -05009539static bool nfs41_match_stateid(const nfs4_stateid *s1,
9540 const nfs4_stateid *s2)
9541{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009542 if (s1->type != s2->type)
9543 return false;
9544
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009545 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009546 return false;
9547
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009548 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009549 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009550
Anna Schumaker045c5512017-01-11 16:59:48 -05009551 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009552}
9553
Andy Adamson557134a2009-04-01 09:21:53 -04009554#endif /* CONFIG_NFS_V4_1 */
9555
Trond Myklebust36281ca2012-03-04 18:13:56 -05009556static bool nfs4_match_stateid(const nfs4_stateid *s1,
9557 const nfs4_stateid *s2)
9558{
Trond Myklebustf597c532012-03-04 18:13:56 -05009559 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009560}
9561
9562
Trond Myklebust17280172012-03-11 13:11:00 -04009563static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009564 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009565 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009566 .recover_open = nfs4_open_reclaim,
9567 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009568 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009569 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570};
9571
Andy Adamson591d71c2009-04-01 09:22:47 -04009572#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009573static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009574 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9575 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9576 .recover_open = nfs4_open_reclaim,
9577 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009578 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009579 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009580 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009581};
9582#endif /* CONFIG_NFS_V4_1 */
9583
Trond Myklebust17280172012-03-11 13:11:00 -04009584static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009585 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009586 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009587 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009588 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009589 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009590};
9591
Andy Adamson591d71c2009-04-01 09:22:47 -04009592#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009593static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009594 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9595 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009596 .recover_open = nfs41_open_expired,
9597 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009598 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009599};
9600#endif /* CONFIG_NFS_V4_1 */
9601
Trond Myklebust17280172012-03-11 13:11:00 -04009602static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009603 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009604 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009605 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009606};
9607
9608#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009609static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009610 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009611 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009612 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009613};
9614#endif
9615
Chuck Leverec011fe2013-10-17 14:12:39 -04009616static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009617 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009618 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009619};
9620
9621#if defined(CONFIG_NFS_V4_1)
9622static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009623 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009624 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009625};
9626#endif /* CONFIG_NFS_V4_1 */
9627
Trond Myklebust97dc1352010-06-16 09:52:26 -04009628static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9629 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009630 .init_caps = NFS_CAP_READDIRPLUS
9631 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009632 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009633 .init_client = nfs40_init_client,
9634 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009635 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009636 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009637 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009638 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009639 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009640 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009641 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9642 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9643 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009644 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009645};
9646
9647#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009648static struct nfs_seqid *
9649nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9650{
9651 return NULL;
9652}
9653
Trond Myklebust97dc1352010-06-16 09:52:26 -04009654static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9655 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009656 .init_caps = NFS_CAP_READDIRPLUS
9657 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009658 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009659 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009660 | NFS_CAP_ATOMIC_OPEN_V1
9661 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009662 .init_client = nfs41_init_client,
9663 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009664 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009665 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009666 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009667 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009668 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009669 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009670 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009671 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9672 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9673 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009674 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009675};
9676#endif
9677
Steve Dickson42c2c422013-05-22 12:50:38 -04009678#if defined(CONFIG_NFS_V4_2)
9679static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9680 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009681 .init_caps = NFS_CAP_READDIRPLUS
9682 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009683 | NFS_CAP_POSIX_LOCK
9684 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009685 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009686 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009687 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009688 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009689 | NFS_CAP_OFFLOAD_CANCEL
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009690 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009691 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009692 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -05009693 | NFS_CAP_CLONE
9694 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009695 .init_client = nfs41_init_client,
9696 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009697 .match_stateid = nfs41_match_stateid,
9698 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009699 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009700 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009701 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009702 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009703 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009704 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9705 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9706 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009707 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009708};
9709#endif
9710
Trond Myklebust97dc1352010-06-16 09:52:26 -04009711const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9712 [0] = &nfs_v4_0_minor_ops,
9713#if defined(CONFIG_NFS_V4_1)
9714 [1] = &nfs_v4_1_minor_ops,
9715#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009716#if defined(CONFIG_NFS_V4_2)
9717 [2] = &nfs_v4_2_minor_ops,
9718#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009719};
9720
Trond Myklebust13997822016-07-24 17:10:52 -04009721static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009722{
9723 ssize_t error, error2;
9724
9725 error = generic_listxattr(dentry, list, size);
9726 if (error < 0)
9727 return error;
9728 if (list) {
9729 list += error;
9730 size -= error;
9731 }
9732
9733 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9734 if (error2 < 0)
9735 return error2;
9736 return error + error2;
9737}
9738
Trond Myklebust17f26b12013-08-21 15:48:42 -04009739static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009740 .create = nfs_create,
9741 .lookup = nfs_lookup,
9742 .atomic_open = nfs_atomic_open,
9743 .link = nfs_link,
9744 .unlink = nfs_unlink,
9745 .symlink = nfs_symlink,
9746 .mkdir = nfs_mkdir,
9747 .rmdir = nfs_rmdir,
9748 .mknod = nfs_mknod,
9749 .rename = nfs_rename,
9750 .permission = nfs_permission,
9751 .getattr = nfs_getattr,
9752 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009753 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009754};
9755
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009756static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009757 .permission = nfs_permission,
9758 .getattr = nfs_getattr,
9759 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009760 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009761};
9762
David Howells509de812006-08-22 20:06:11 -04009763const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764 .version = 4, /* protocol version */
9765 .dentry_ops = &nfs4_dentry_operations,
9766 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009767 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009768 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009769 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009770 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009771 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772 .getattr = nfs4_proc_getattr,
9773 .setattr = nfs4_proc_setattr,
9774 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009775 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 .access = nfs4_proc_access,
9777 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778 .create = nfs4_proc_create,
9779 .remove = nfs4_proc_remove,
9780 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009781 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009783 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009784 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009785 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786 .link = nfs4_proc_link,
9787 .symlink = nfs4_proc_symlink,
9788 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009789 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790 .readdir = nfs4_proc_readdir,
9791 .mknod = nfs4_proc_mknod,
9792 .statfs = nfs4_proc_statfs,
9793 .fsinfo = nfs4_proc_fsinfo,
9794 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009795 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009797 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009799 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009801 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009803 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009804 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009806 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009807 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009808 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009809 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009810 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009811 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009812 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009813 .create_server = nfs4_create_server,
9814 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009815};
9816
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009817static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009818 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009819 .list = nfs4_xattr_list_nfs4_acl,
9820 .get = nfs4_xattr_get_nfs4_acl,
9821 .set = nfs4_xattr_set_nfs4_acl,
9822};
9823
9824const struct xattr_handler *nfs4_xattr_handlers[] = {
9825 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009826#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9827 &nfs4_xattr_nfs4_label_handler,
9828#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009829 NULL
9830};
9831
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832/*
9833 * Local variables:
9834 * c-basic-offset: 8
9835 * End:
9836 */