blob: 77c6e2d3f3fc0af7f7373263a238fbecfa911d2b [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 */
757 if (!RPC_WAS_SENT(task))
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 Myklebust3be0f80b2017-10-19 15:46:45 -0400948static
949void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
950 struct nfs4_sequence_res *res,
951 struct nfs4_slot *slot)
952{
953 if (!slot)
954 return;
955 slot->privileged = args->sa_privileged ? 1 : 0;
956 args->sa_slot = slot;
957
958 res->sr_slot = slot;
959 res->sr_timestamp = jiffies;
960 res->sr_status_flags = 0;
961 res->sr_status = 1;
962
963}
964
965int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500966 struct nfs4_sequence_args *args,
967 struct nfs4_sequence_res *res,
968 struct rpc_task *task)
969{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500970 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500971 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500972 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500973
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500974 /* slot already allocated? */
975 if (res->sr_slot != NULL)
976 goto out_start;
977
Anna Schumaker76ee0352017-01-10 16:49:31 -0500978 if (session) {
979 tbl = &session->fc_slot_table;
980 task->tk_timeout = 0;
981 }
982
Trond Myklebust3453d572018-06-20 17:53:34 -0400983 spin_lock(&tbl->slot_tbl_lock);
984 /* The state manager will wait until the slot table is empty */
985 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
986 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500987
Trond Myklebust3453d572018-06-20 17:53:34 -0400988 slot = nfs4_alloc_slot(tbl);
989 if (IS_ERR(slot)) {
990 /* Try again in 1/4 second */
991 if (slot == ERR_PTR(-ENOMEM))
992 task->tk_timeout = HZ >> 2;
993 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -0500994 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400995 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500996
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400997 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500998
Anna Schumakerad05cc02017-01-11 13:37:06 -0500999 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001000out_start:
1001 rpc_call_start(task);
1002 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001003
1004out_sleep:
1005 if (args->sa_privileged)
1006 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1007 NULL, RPC_PRIORITY_PRIVILEGED);
1008 else
1009 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1010 spin_unlock(&tbl->slot_tbl_lock);
1011 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001012}
1013EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1014
Chuck Lever9915ea72013-08-09 12:48:27 -04001015static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1016{
1017 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001018 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001019 data->seq_args, data->seq_res, task);
1020}
1021
1022static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1023{
1024 struct nfs4_call_sync_data *data = calldata;
1025 nfs4_sequence_done(task, data->seq_res);
1026}
1027
1028static const struct rpc_call_ops nfs40_call_sync_ops = {
1029 .rpc_call_prepare = nfs40_call_sync_prepare,
1030 .rpc_call_done = nfs40_call_sync_done,
1031};
1032
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001033static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001034 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001036 struct nfs4_sequence_args *args,
1037 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001038{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001039 int ret;
1040 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001041 struct nfs_client *clp = server->nfs_client;
1042 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 .seq_server = server,
1044 .seq_args = args,
1045 .seq_res = res,
1046 };
1047 struct rpc_task_setup task_setup = {
1048 .rpc_client = clnt,
1049 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001050 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001051 .callback_data = &data
1052 };
1053
1054 task = rpc_run_task(&task_setup);
1055 if (IS_ERR(task))
1056 ret = PTR_ERR(task);
1057 else {
1058 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001059 rpc_put_task(task);
1060 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001061 return ret;
1062}
1063
Bryan Schumaker7c513052011-03-24 17:12:24 +00001064int nfs4_call_sync(struct rpc_clnt *clnt,
1065 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001066 struct rpc_message *msg,
1067 struct nfs4_sequence_args *args,
1068 struct nfs4_sequence_res *res,
1069 int cache_reply)
1070{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001071 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001072 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001073}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Trond Myklebust3c591172018-07-31 15:54:10 -04001075static void
1076nfs4_inc_nlink_locked(struct inode *inode)
1077{
1078 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1079 inc_nlink(inode);
1080}
1081
1082static void
1083nfs4_dec_nlink_locked(struct inode *inode)
1084{
1085 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1086 drop_nlink(inode);
1087}
1088
1089static void
1090update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001091 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 struct nfs_inode *nfsi = NFS_I(dir);
1094
Trond Myklebust16e14372018-03-20 16:53:31 -04001095 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1096 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001097 | NFS_INO_INVALID_DATA
1098 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001099 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001100 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1101 nfsi->attrtimeo_timestamp = jiffies;
1102 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001104 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001105 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1106 NFS_INO_INVALID_ACL;
1107 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001108 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001109 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001110 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001111 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001112 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001113}
1114
1115static void
1116update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001117 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001118{
1119 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001120 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 spin_unlock(&dir->i_lock);
1122}
1123
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001124struct nfs4_open_createattrs {
1125 struct nfs4_label *label;
1126 struct iattr *sattr;
1127 const __u32 verf[2];
1128};
1129
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001130static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1131 int err, struct nfs4_exception *exception)
1132{
1133 if (err != -EINVAL)
1134 return false;
1135 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1136 return false;
1137 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1138 exception->retry = 1;
1139 return true;
1140}
1141
Trond Myklebust6ae37332015-01-30 14:21:14 -05001142static u32
1143nfs4_map_atomic_open_share(struct nfs_server *server,
1144 fmode_t fmode, int openflags)
1145{
1146 u32 res = 0;
1147
1148 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1149 case FMODE_READ:
1150 res = NFS4_SHARE_ACCESS_READ;
1151 break;
1152 case FMODE_WRITE:
1153 res = NFS4_SHARE_ACCESS_WRITE;
1154 break;
1155 case FMODE_READ|FMODE_WRITE:
1156 res = NFS4_SHARE_ACCESS_BOTH;
1157 }
1158 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1159 goto out;
1160 /* Want no delegation if we're using O_DIRECT */
1161 if (openflags & O_DIRECT)
1162 res |= NFS4_SHARE_WANT_NO_DELEG;
1163out:
1164 return res;
1165}
1166
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001167static enum open_claim_type4
1168nfs4_map_atomic_open_claim(struct nfs_server *server,
1169 enum open_claim_type4 claim)
1170{
1171 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1172 return claim;
1173 switch (claim) {
1174 default:
1175 return claim;
1176 case NFS4_OPEN_CLAIM_FH:
1177 return NFS4_OPEN_CLAIM_NULL;
1178 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1179 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1180 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1181 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1182 }
1183}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186{
1187 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001188 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 p->o_res.seqid = p->o_arg.seqid;
1190 p->c_res.seqid = p->c_arg.seqid;
1191 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001192 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001194 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195}
1196
Al Viro82a2c1b2011-06-22 18:30:55 -04001197static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001199 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001200 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001201 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202{
Al Viro82a2c1b2011-06-22 18:30:55 -04001203 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001204 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001205 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001206 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001207 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001208 struct nfs4_opendata *p;
1209
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001210 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211 if (p == NULL)
1212 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001213
1214 p->f_label = nfs4_label_alloc(server, gfp_mask);
1215 if (IS_ERR(p->f_label))
1216 goto err_free_p;
1217
Kinglong Meea49c2692015-07-27 15:31:38 +08001218 p->a_label = nfs4_label_alloc(server, gfp_mask);
1219 if (IS_ERR(p->a_label))
1220 goto err_free_f;
1221
Trond Myklebust63f5f792015-01-23 19:19:25 -05001222 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1223 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001224 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001225 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001226 nfs_sb_active(dentry->d_sb);
1227 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001228 p->dir = parent;
1229 p->owner = sp;
1230 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001231 p->o_arg.open_flags = flags;
1232 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001233 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001234 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001235 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1236 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001237 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1238 * will return permission denied for all bits until close */
1239 if (!(flags & O_EXCL)) {
1240 /* ask server to check for all possible rights as results
1241 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001242 switch (p->o_arg.claim) {
1243 default:
1244 break;
1245 case NFS4_OPEN_CLAIM_NULL:
1246 case NFS4_OPEN_CLAIM_FH:
1247 p->o_arg.access = NFS4_ACCESS_READ |
1248 NFS4_ACCESS_MODIFY |
1249 NFS4_ACCESS_EXTEND |
1250 NFS4_ACCESS_EXECUTE;
1251 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001252 }
David Howells7539bba2006-08-22 20:06:09 -04001253 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001254 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1255 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001256 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001257 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001258 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001259 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001260 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001261 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001262 case NFS4_OPEN_CLAIM_NULL:
1263 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1264 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1265 p->o_arg.fh = NFS_FH(dir);
1266 break;
1267 case NFS4_OPEN_CLAIM_PREVIOUS:
1268 case NFS4_OPEN_CLAIM_FH:
1269 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1270 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001271 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001272 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001273 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001274 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001275 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001276
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001277 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001278 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001279 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001280 p->c_arg.fh = &p->o_res.fh;
1281 p->c_arg.stateid = &p->o_res.stateid;
1282 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001283 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001284 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001285 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001286
1287err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001288 nfs4_label_free(p->a_label);
1289err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001290 nfs4_label_free(p->f_label);
1291err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001292 kfree(p);
1293err:
1294 dput(parent);
1295 return NULL;
1296}
1297
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001298static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001299{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001300 struct nfs4_opendata *p = container_of(kref,
1301 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001302 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001303
Fred Isaman30ae2412016-10-18 13:39:51 -04001304 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001305 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001306 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001307 if (p->state != NULL)
1308 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001310
Kinglong Meea49c2692015-07-27 15:31:38 +08001311 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001312 nfs4_label_free(p->f_label);
1313
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001314 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001315 dput(p->dentry);
1316 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001317 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001318 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001319 kfree(p);
1320}
1321
1322static void nfs4_opendata_put(struct nfs4_opendata *p)
1323{
1324 if (p != NULL)
1325 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001326}
1327
Trond Myklebust24311f82015-09-20 10:50:17 -04001328static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1329 fmode_t fmode)
1330{
1331 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1332 case FMODE_READ|FMODE_WRITE:
1333 return state->n_rdwr != 0;
1334 case FMODE_WRITE:
1335 return state->n_wronly != 0;
1336 case FMODE_READ:
1337 return state->n_rdonly != 0;
1338 }
1339 WARN_ON_ONCE(1);
1340 return false;
1341}
1342
Trond Myklebustbe189f72018-09-27 17:12:33 -04001343static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1344 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001345{
1346 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001347
Trond Myklebust536e43d2012-01-17 22:04:26 -05001348 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001349 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001350 switch (claim) {
1351 case NFS4_OPEN_CLAIM_NULL:
1352 case NFS4_OPEN_CLAIM_FH:
1353 goto out;
1354 default:
1355 break;
1356 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001358 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001359 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1360 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361 break;
1362 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001363 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1364 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001365 break;
1366 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001367 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1368 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001369 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001370out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001371 return ret;
1372}
1373
Trond Myklebust2a606182015-08-19 22:30:00 -05001374static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1375 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001376{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001377 if (delegation == NULL)
1378 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001379 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001380 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001381 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1382 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001383 switch (claim) {
1384 case NFS4_OPEN_CLAIM_NULL:
1385 case NFS4_OPEN_CLAIM_FH:
1386 break;
1387 case NFS4_OPEN_CLAIM_PREVIOUS:
1388 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1389 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001390 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001391 default:
1392 return 0;
1393 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001394 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001395 return 1;
1396}
1397
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001398static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001399{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001400 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001401 case FMODE_WRITE:
1402 state->n_wronly++;
1403 break;
1404 case FMODE_READ:
1405 state->n_rdonly++;
1406 break;
1407 case FMODE_READ|FMODE_WRITE:
1408 state->n_rdwr++;
1409 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001410 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001411}
1412
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001413#ifdef CONFIG_NFS_V4_1
1414static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1415{
1416 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1417 return true;
1418 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1419 return true;
1420 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1421 return true;
1422 return false;
1423}
1424#endif /* CONFIG_NFS_V4_1 */
1425
Trond Myklebustc9399f22017-11-06 15:28:01 -05001426static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1427{
1428 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1429 wake_up_all(&state->waitq);
1430}
1431
1432static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1433 const nfs4_stateid *stateid)
1434{
1435 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1436 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1437
1438 if (stateid_seqid == state_seqid + 1U ||
1439 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1440 nfs_state_log_update_open_stateid(state);
1441 else
1442 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1443}
1444
Trond Myklebust4f14c192014-02-12 19:15:06 -05001445static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001446{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001447 struct nfs_client *clp = state->owner->so_server->nfs_client;
1448 bool need_recover = false;
1449
1450 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1451 need_recover = true;
1452 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1453 need_recover = true;
1454 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1455 need_recover = true;
1456 if (need_recover)
1457 nfs4_state_mark_reclaim_nograce(clp, state);
1458}
1459
Trond Myklebustc9399f22017-11-06 15:28:01 -05001460/*
1461 * Check for whether or not the caller may update the open stateid
1462 * to the value passed in by stateid.
1463 *
1464 * Note: This function relies heavily on the server implementing
1465 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1466 * correctly.
1467 * i.e. The stateid seqids have to be initialised to 1, and
1468 * are then incremented on every state transition.
1469 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001470static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001471 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001472{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001473 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1474 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1475 if (stateid->seqid == cpu_to_be32(1))
1476 nfs_state_log_update_open_stateid(state);
1477 else
1478 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001479 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001480 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001481
1482 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1483 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001484 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001485 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001486 return false;
1487}
1488
Trond Myklebustf95549c2015-01-23 18:06:09 -05001489static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1490{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001491 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1492 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001493 if (state->n_wronly)
1494 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1495 if (state->n_rdonly)
1496 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1497 if (state->n_rdwr)
1498 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001499 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001500}
1501
Trond Myklebust226056c2014-02-11 10:41:07 -05001502static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1503 nfs4_stateid *stateid, fmode_t fmode)
1504{
1505 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1506 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1507 case FMODE_WRITE:
1508 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1509 break;
1510 case FMODE_READ:
1511 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1512 break;
1513 case 0:
1514 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1515 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1516 clear_bit(NFS_OPEN_STATE, &state->flags);
1517 }
1518 if (stateid == NULL)
1519 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001520 /* Handle OPEN+OPEN_DOWNGRADE races */
1521 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1522 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001523 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001524 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001525 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001526 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001527 nfs4_stateid_copy(&state->stateid, stateid);
1528 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001529 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001530out:
1531 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001532}
1533
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001534static void nfs_clear_open_stateid(struct nfs4_state *state,
1535 nfs4_stateid *arg_stateid,
1536 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001537{
1538 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001539 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1540 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1541 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001542 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001543 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1544 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001545}
1546
Trond Myklebust1393d962016-09-22 13:39:13 -04001547static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001548 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001549 __must_hold(&state->owner->so_lock)
1550 __must_hold(&state->seqlock)
1551 __must_hold(RCU)
1552
Trond Myklebust003707c2007-07-05 18:07:55 -04001553{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001554 DEFINE_WAIT(wait);
1555 int status = 0;
1556 for (;;) {
1557
1558 if (!nfs_need_update_open_stateid(state, stateid))
1559 return;
1560 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001561 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001562 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001563 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001564 /* Rely on seqids for serialisation with NFSv4.0 */
1565 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1566 break;
1567
1568 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1569 /*
1570 * Ensure we process the state changes in the same order
1571 * in which the server processed them by delaying the
1572 * update of the stateid until we are in sequence.
1573 */
1574 write_sequnlock(&state->seqlock);
1575 spin_unlock(&state->owner->so_lock);
1576 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001577 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001578 if (!signal_pending(current)) {
1579 if (schedule_timeout(5*HZ) == 0)
1580 status = -EAGAIN;
1581 else
1582 status = 0;
1583 } else
1584 status = -EINTR;
1585 finish_wait(&state->waitq, &wait);
1586 rcu_read_lock();
1587 spin_lock(&state->owner->so_lock);
1588 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001589 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001590
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001591 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1592 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001593 nfs4_stateid_copy(freeme, &state->open_stateid);
1594 nfs_test_and_clear_all_open_stateid(state);
1595 }
1596
Trond Myklebuste999e802014-02-10 18:20:47 -05001597 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1598 nfs4_stateid_copy(&state->stateid, stateid);
1599 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001600 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001601 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001602}
1603
Trond Myklebustc9399f22017-11-06 15:28:01 -05001604static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001605 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001606 fmode_t fmode,
1607 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001609 /*
1610 * Protect the call to nfs4_state_set_mode_locked and
1611 * serialise the stateid update
1612 */
1613 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001614 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1615 switch (fmode) {
1616 case FMODE_READ:
1617 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1618 break;
1619 case FMODE_WRITE:
1620 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1621 break;
1622 case FMODE_READ|FMODE_WRITE:
1623 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001624 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001625 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001626 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001627}
1628
1629static void nfs_state_set_delegation(struct nfs4_state *state,
1630 const nfs4_stateid *deleg_stateid,
1631 fmode_t fmode)
1632{
1633 /*
1634 * Protect the call to nfs4_state_set_mode_locked and
1635 * serialise the stateid update
1636 */
1637 write_seqlock(&state->seqlock);
1638 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1639 set_bit(NFS_DELEGATED_STATE, &state->flags);
1640 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001643static void nfs_state_clear_delegation(struct nfs4_state *state)
1644{
1645 write_seqlock(&state->seqlock);
1646 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1647 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1648 write_sequnlock(&state->seqlock);
1649}
1650
Trond Myklebust1393d962016-09-22 13:39:13 -04001651static int update_open_stateid(struct nfs4_state *state,
1652 const nfs4_stateid *open_stateid,
1653 const nfs4_stateid *delegation,
1654 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001655{
Trond Myklebust1393d962016-09-22 13:39:13 -04001656 struct nfs_server *server = NFS_SERVER(state->inode);
1657 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001658 struct nfs_inode *nfsi = NFS_I(state->inode);
1659 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001660 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001661 int ret = 0;
1662
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001663 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001664
1665 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001666 spin_lock(&state->owner->so_lock);
1667 if (open_stateid != NULL) {
1668 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1669 ret = 1;
1670 }
1671
Trond Myklebust34310432008-12-23 15:21:38 -05001672 deleg_cur = rcu_dereference(nfsi->delegation);
1673 if (deleg_cur == NULL)
1674 goto no_delegation;
1675
1676 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001677 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001678 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001679 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001680 goto no_delegation_unlock;
1681
1682 if (delegation == NULL)
1683 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001684 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001685 goto no_delegation_unlock;
1686
Trond Myklebustb7391f42008-12-23 15:21:52 -05001687 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001688 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001689 ret = 1;
1690no_delegation_unlock:
1691 spin_unlock(&deleg_cur->lock);
1692no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001693 if (ret)
1694 update_open_stateflags(state, fmode);
1695 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001696 rcu_read_unlock();
1697
Trond Myklebust4f14c192014-02-12 19:15:06 -05001698 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001699 nfs4_schedule_state_manager(clp);
1700 if (freeme.type != 0)
1701 nfs4_test_and_free_stateid(server, &freeme,
1702 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001703
1704 return ret;
1705}
1706
Trond Myklebust39071e62015-01-24 15:07:56 -05001707static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1708 const nfs4_stateid *stateid)
1709{
1710 struct nfs4_state *state = lsp->ls_state;
1711 bool ret = false;
1712
1713 spin_lock(&state->state_lock);
1714 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1715 goto out_noupdate;
1716 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1717 goto out_noupdate;
1718 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1719 ret = true;
1720out_noupdate:
1721 spin_unlock(&state->state_lock);
1722 return ret;
1723}
Trond Myklebust34310432008-12-23 15:21:38 -05001724
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001725static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001726{
1727 struct nfs_delegation *delegation;
1728
Trond Myklebustf5086242018-03-20 16:43:13 -04001729 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001730 rcu_read_lock();
1731 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001732 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001733 rcu_read_unlock();
1734 return;
1735 }
1736 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001737 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001738}
1739
Trond Myklebust6ee41262007-07-08 14:11:36 -04001740static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001741{
1742 struct nfs4_state *state = opendata->state;
1743 struct nfs_inode *nfsi = NFS_I(state->inode);
1744 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001745 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001746 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001747 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001748 nfs4_stateid stateid;
1749 int ret = -EAGAIN;
1750
Trond Myklebustaac00a82007-07-05 19:02:21 -04001751 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001752 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001753 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001754 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001755 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001756 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001757 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001758 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001759 rcu_read_lock();
1760 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001761 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001762 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001763 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001764 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001765 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001766 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001767 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001768 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001769 if (!opendata->is_recover) {
1770 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1771 if (ret != 0)
1772 goto out;
1773 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001774 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001775
1776 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001777 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001778 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001779 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001780out:
1781 return ERR_PTR(ret);
1782out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001783 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001784 return state;
1785}
1786
Andy Adamsone23008e2012-10-02 21:07:32 -04001787static void
1788nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1789{
1790 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1791 struct nfs_delegation *delegation;
1792 int delegation_flags = 0;
1793
1794 rcu_read_lock();
1795 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1796 if (delegation)
1797 delegation_flags = delegation->flags;
1798 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001799 switch (data->o_arg.claim) {
1800 default:
1801 break;
1802 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1803 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001804 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1805 "returning a delegation for "
1806 "OPEN(CLAIM_DELEGATE_CUR)\n",
1807 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001808 return;
1809 }
1810 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001811 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001812 data->owner->so_cred,
1813 data->o_res.delegation_type,
1814 &data->o_res.delegation,
1815 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001816 else
1817 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001818 data->owner->so_cred,
1819 data->o_res.delegation_type,
1820 &data->o_res.delegation,
1821 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001822
1823 if (data->o_res.do_recall)
1824 nfs_async_inode_return_delegation(state->inode,
1825 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001826}
1827
1828/*
1829 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1830 * and update the nfs4_state.
1831 */
1832static struct nfs4_state *
1833_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1834{
1835 struct inode *inode = data->state->inode;
1836 struct nfs4_state *state = data->state;
1837 int ret;
1838
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001839 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001840 if (data->rpc_status)
1841 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001842 /* cached opens have already been processed */
1843 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001844 }
1845
Andy Adamsone23008e2012-10-02 21:07:32 -04001846 ret = nfs_refresh_inode(inode, &data->f_attr);
1847 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001848 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001849
1850 if (data->o_res.delegation_type != 0)
1851 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001852update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001853 update_open_stateid(state, &data->o_res.stateid, NULL,
1854 data->o_arg.fmode);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001855 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001856
1857 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001858}
1859
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001860static struct inode *
1861nfs4_opendata_get_inode(struct nfs4_opendata *data)
1862{
1863 struct inode *inode;
1864
1865 switch (data->o_arg.claim) {
1866 case NFS4_OPEN_CLAIM_NULL:
1867 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1868 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1869 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1870 return ERR_PTR(-EAGAIN);
1871 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1872 &data->f_attr, data->f_label);
1873 break;
1874 default:
1875 inode = d_inode(data->dentry);
1876 ihold(inode);
1877 nfs_refresh_inode(inode, &data->f_attr);
1878 }
1879 return inode;
1880}
1881
Andy Adamsone23008e2012-10-02 21:07:32 -04001882static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001883nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1884{
1885 struct nfs4_state *state;
1886 struct inode *inode;
1887
1888 inode = nfs4_opendata_get_inode(data);
1889 if (IS_ERR(inode))
1890 return ERR_CAST(inode);
1891 if (data->state != NULL && data->state->inode == inode) {
1892 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001893 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04001894 } else
1895 state = nfs4_get_open_state(inode, data->owner);
1896 iput(inode);
1897 if (state == NULL)
1898 state = ERR_PTR(-ENOMEM);
1899 return state;
1900}
1901
Andy Adamsone23008e2012-10-02 21:07:32 -04001902static struct nfs4_state *
1903_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001904{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001905 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001906
Trond Myklebustaac00a82007-07-05 19:02:21 -04001907 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001908 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001909 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001910 goto out;
1911 }
1912
Trond Myklebust75e8c482017-08-08 10:38:07 -04001913 state = nfs4_opendata_find_nfs4_state(data);
1914 if (IS_ERR(state))
1915 goto out;
1916
Andy Adamsone23008e2012-10-02 21:07:32 -04001917 if (data->o_res.delegation_type != 0)
1918 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001919 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001920 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001921out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001922 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001923 return state;
1924}
1925
Andy Adamsone23008e2012-10-02 21:07:32 -04001926static struct nfs4_state *
1927nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1928{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001929 struct nfs4_state *ret;
1930
Andy Adamsone23008e2012-10-02 21:07:32 -04001931 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001932 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1933 else
1934 ret = _nfs4_opendata_to_nfs4_state(data);
1935 nfs4_sequence_free_slot(&data->o_res.seq_res);
1936 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001937}
1938
Trond Myklebust0de43972018-09-02 15:57:01 -04001939static struct nfs_open_context *
1940nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001941{
1942 struct nfs_inode *nfsi = NFS_I(state->inode);
1943 struct nfs_open_context *ctx;
1944
Trond Myklebust0de43972018-09-02 15:57:01 -04001945 rcu_read_lock();
1946 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01001947 if (ctx->state != state)
1948 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04001949 if ((ctx->mode & mode) != mode)
1950 continue;
1951 if (!get_nfs_open_context(ctx))
1952 continue;
1953 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001954 return ctx;
1955 }
Trond Myklebust0de43972018-09-02 15:57:01 -04001956 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001957 return ERR_PTR(-ENOENT);
1958}
1959
Trond Myklebust0de43972018-09-02 15:57:01 -04001960static struct nfs_open_context *
1961nfs4_state_find_open_context(struct nfs4_state *state)
1962{
1963 struct nfs_open_context *ctx;
1964
1965 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
1966 if (!IS_ERR(ctx))
1967 return ctx;
1968 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
1969 if (!IS_ERR(ctx))
1970 return ctx;
1971 return nfs4_state_find_open_context_mode(state, FMODE_READ);
1972}
1973
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001974static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1975 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001976{
1977 struct nfs4_opendata *opendata;
1978
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001979 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001980 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001981 if (opendata == NULL)
1982 return ERR_PTR(-ENOMEM);
1983 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001984 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001985 return opendata;
1986}
1987
Trond Myklebust24311f82015-09-20 10:50:17 -04001988static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1989 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001990{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001991 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001992 int ret;
1993
Trond Myklebust24311f82015-09-20 10:50:17 -04001994 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001995 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001996 opendata->o_arg.open_flags = 0;
1997 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001998 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1999 NFS_SB(opendata->dentry->d_sb),
2000 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002001 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2002 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2003 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002004 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002005 if (ret != 0)
2006 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002007 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002008 if (IS_ERR(newstate))
2009 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002010 if (newstate != opendata->state)
2011 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002012 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002013 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002014}
2015
2016static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2017{
Trond Myklebust864472e2006-01-03 09:55:15 +01002018 int ret;
2019
Trond Myklebust4f14c192014-02-12 19:15:06 -05002020 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2021 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2022 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2023 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002024 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002025 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002026 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002027 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002028 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2029 if (ret != 0)
2030 return ret;
2031 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2032 if (ret != 0)
2033 return ret;
2034 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2035 if (ret != 0)
2036 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002037 /*
2038 * We may have performed cached opens for all three recoveries.
2039 * Check if we need to update the current stateid.
2040 */
2041 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002042 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002043 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002044 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002045 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002046 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002047 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002048 return 0;
2049}
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051/*
2052 * OPEN_RECLAIM:
2053 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002055static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002057 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002058 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002059 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 int status;
2061
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002062 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2063 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002064 if (IS_ERR(opendata))
2065 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002066 rcu_read_lock();
2067 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002068 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002069 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002070 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002071 opendata->o_arg.u.delegation_type = delegation_type;
2072 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002073 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return status;
2075}
2076
Trond Myklebust539cd032007-06-05 11:46:42 -04002077static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 struct nfs_server *server = NFS_SERVER(state->inode);
2080 struct nfs4_exception exception = { };
2081 int err;
2082 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002083 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002084 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002085 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2086 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002087 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002088 break;
2089 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 } while (exception.retry);
2091 return err;
2092}
2093
Trond Myklebust864472e2006-01-03 09:55:15 +01002094static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2095{
2096 struct nfs_open_context *ctx;
2097 int ret;
2098
2099 ctx = nfs4_state_find_open_context(state);
2100 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002101 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002102 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002103 put_nfs_open_context(ctx);
2104 return ret;
2105}
2106
NeilBrowndce26302017-12-13 09:57:09 +11002107static 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 -07002108{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002109 switch (err) {
2110 default:
2111 printk(KERN_ERR "NFS: %s: unhandled error "
2112 "%d.\n", __func__, err);
2113 case 0:
2114 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002115 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002116 case -ESTALE:
2117 break;
2118 case -NFS4ERR_BADSESSION:
2119 case -NFS4ERR_BADSLOT:
2120 case -NFS4ERR_BAD_HIGH_SLOT:
2121 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2122 case -NFS4ERR_DEADSESSION:
2123 set_bit(NFS_DELEGATED_STATE, &state->flags);
2124 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2125 return -EAGAIN;
2126 case -NFS4ERR_STALE_CLIENTID:
2127 case -NFS4ERR_STALE_STATEID:
2128 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002129 /* Don't recall a delegation if it was lost */
2130 nfs4_schedule_lease_recovery(server->nfs_client);
2131 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002132 case -NFS4ERR_MOVED:
2133 nfs4_schedule_migration_recovery(server);
2134 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002135 case -NFS4ERR_LEASE_MOVED:
2136 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2137 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002138 case -NFS4ERR_DELEG_REVOKED:
2139 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002140 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002141 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002142 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002143 nfs_inode_find_state_and_recover(state->inode,
2144 stateid);
2145 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002146 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002147 case -NFS4ERR_DELAY:
2148 case -NFS4ERR_GRACE:
2149 set_bit(NFS_DELEGATED_STATE, &state->flags);
2150 ssleep(1);
2151 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002152 case -ENOMEM:
2153 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002154 if (fl) {
2155 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2156 if (lsp)
2157 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2158 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002159 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return err;
2162}
2163
Trond Myklebust24311f82015-09-20 10:50:17 -04002164int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2165 struct nfs4_state *state, const nfs4_stateid *stateid,
2166 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002167{
2168 struct nfs_server *server = NFS_SERVER(state->inode);
2169 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002170 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002171
2172 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2173 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2174 if (IS_ERR(opendata))
2175 return PTR_ERR(opendata);
2176 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002177 nfs_state_clear_delegation(state);
Trond Myklebust24311f82015-09-20 10:50:17 -04002178 switch (type & (FMODE_READ|FMODE_WRITE)) {
2179 case FMODE_READ|FMODE_WRITE:
2180 case FMODE_WRITE:
2181 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2182 if (err)
2183 break;
2184 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2185 if (err)
2186 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002187 /* Fall through */
Trond Myklebust24311f82015-09-20 10:50:17 -04002188 case FMODE_READ:
2189 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2190 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002191 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002192 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002193}
2194
Chuck Leverbe05c862013-08-09 12:49:47 -04002195static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2196{
2197 struct nfs4_opendata *data = calldata;
2198
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002199 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2200 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002201}
2202
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002203static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2204{
2205 struct nfs4_opendata *data = calldata;
2206
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002207 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002208
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002209 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002210 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002211 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002212 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002213 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002214 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002215 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002216}
2217
2218static void nfs4_open_confirm_release(void *calldata)
2219{
2220 struct nfs4_opendata *data = calldata;
2221 struct nfs4_state *state = NULL;
2222
2223 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002224 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002225 goto out_free;
2226 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002227 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002228 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002229 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002230 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002231 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002232out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002233 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002234}
2235
2236static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002237 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002238 .rpc_call_done = nfs4_open_confirm_done,
2239 .rpc_release = nfs4_open_confirm_release,
2240};
2241
2242/*
2243 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2244 */
2245static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2246{
David Howells2b0143b2015-03-17 22:25:59 +00002247 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002248 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002249 struct rpc_message msg = {
2250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2251 .rpc_argp = &data->c_arg,
2252 .rpc_resp = &data->c_res,
2253 .rpc_cred = data->owner->so_cred,
2254 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002255 struct rpc_task_setup task_setup_data = {
2256 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002257 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002258 .callback_ops = &nfs4_open_confirm_ops,
2259 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002260 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002261 .flags = RPC_TASK_ASYNC,
2262 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 int status;
2264
Anna Schumakerfba83f32018-05-04 16:22:50 -04002265 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2266 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002267 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002268 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002269 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002270 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002271 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002272 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002273 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002274 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002275 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002276 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002277 smp_wmb();
2278 } else
2279 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002280 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 return status;
2282}
2283
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002284static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002286 struct nfs4_opendata *data = calldata;
2287 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002288 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002289 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002290
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002291 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002292 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002293 /*
2294 * Check if we still need to send an OPEN call, or if we can use
2295 * a delegation instead.
2296 */
2297 if (data->state != NULL) {
2298 struct nfs_delegation *delegation;
2299
Trond Myklebustbe189f72018-09-27 17:12:33 -04002300 if (can_open_cached(data->state, data->o_arg.fmode,
2301 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002302 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002303 rcu_read_lock();
2304 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002305 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002306 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002307 rcu_read_unlock();
2308 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002309 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002310 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002311 switch (claim) {
2312 default:
2313 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002314 case NFS4_OPEN_CLAIM_PREVIOUS:
2315 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2316 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002317 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002318 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002319 case NFS4_OPEN_CLAIM_FH:
2320 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002321 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002322 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002323 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002324 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002325 &data->o_res.seq_res,
2326 task) != 0)
2327 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002328
2329 /* Set the create mode (note dependency on the session type) */
2330 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2331 if (data->o_arg.open_flags & O_EXCL) {
2332 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2333 if (nfs4_has_persistent_session(clp))
2334 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2335 else if (clp->cl_mvops->minor_version > 0)
2336 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2337 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002338 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002339unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002340 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002341 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002342out_no_action:
2343 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002344out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002345 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002346}
2347
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002348static void nfs4_open_done(struct rpc_task *task, void *calldata)
2349{
2350 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002352 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002353
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002354 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002355 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002356
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002357 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002358 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2359 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002360 case S_IFREG:
2361 break;
2362 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002363 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002364 break;
2365 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002366 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002367 break;
2368 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002369 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002370 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002371 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002372 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002373 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2374 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002375 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002376 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002377}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002378
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002379static void nfs4_open_release(void *calldata)
2380{
2381 struct nfs4_opendata *data = calldata;
2382 struct nfs4_state *state = NULL;
2383
2384 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002385 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002386 goto out_free;
2387 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002388 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002389 goto out_free;
2390 /* In case we need an open_confirm, no cleanup! */
2391 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2392 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002393 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002394 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002395 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002396out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002397 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002398}
2399
2400static const struct rpc_call_ops nfs4_open_ops = {
2401 .rpc_call_prepare = nfs4_open_prepare,
2402 .rpc_call_done = nfs4_open_done,
2403 .rpc_release = nfs4_open_release,
2404};
2405
Fred Isaman3b65a302016-09-19 10:06:49 -04002406static int nfs4_run_open_task(struct nfs4_opendata *data,
2407 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002408{
David Howells2b0143b2015-03-17 22:25:59 +00002409 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002410 struct nfs_server *server = NFS_SERVER(dir);
2411 struct nfs_openargs *o_arg = &data->o_arg;
2412 struct nfs_openres *o_res = &data->o_res;
2413 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002414 struct rpc_message msg = {
2415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2416 .rpc_argp = o_arg,
2417 .rpc_resp = o_res,
2418 .rpc_cred = data->owner->so_cred,
2419 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002420 struct rpc_task_setup task_setup_data = {
2421 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002422 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002423 .callback_ops = &nfs4_open_ops,
2424 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002425 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002426 .flags = RPC_TASK_ASYNC,
2427 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002428 int status;
2429
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002430 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002431 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002432 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002433 data->cancelled = false;
2434 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002435 if (!ctx) {
2436 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002437 data->is_recover = true;
Fred Isaman2409a972016-10-06 12:11:21 -04002438 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002439 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002440 pnfs_lgopen_prepare(data, ctx);
2441 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002442 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002443 if (IS_ERR(task))
2444 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002445 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002446 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002447 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002448 smp_wmb();
2449 } else
2450 status = data->rpc_status;
2451 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002452
2453 return status;
2454}
2455
2456static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2457{
David Howells2b0143b2015-03-17 22:25:59 +00002458 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002459 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002460 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002461
Fred Isaman3b65a302016-09-19 10:06:49 -04002462 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002463 if (status != 0 || !data->rpc_done)
2464 return status;
2465
Trond Myklebust6926afd2012-01-07 13:22:46 -05002466 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2467
Anna Schumakerd7e98252017-01-11 16:13:29 -05002468 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002469 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002470
2471 return status;
2472}
2473
Trond Myklebustf3792d62014-07-10 08:54:32 -04002474/*
2475 * Additional permission checks in order to distinguish between an
2476 * open for read, and an open for execute. This works around the
2477 * fact that NFSv4 OPEN treats read and execute permissions as being
2478 * the same.
2479 * Note that in the non-execute case, we want to turn off permission
2480 * checking if we just created a new file (POSIX open() semantics).
2481 */
NeilBrowna52458b2018-12-03 11:30:31 +11002482static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002483 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002484 struct nfs4_state *state, fmode_t fmode,
2485 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002486{
2487 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002488 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002489
2490 /* access call failed or for some reason the server doesn't
2491 * support any access modes -- defer access call until later */
2492 if (opendata->o_res.access_supported == 0)
2493 return 0;
2494
2495 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002496 /*
2497 * Use openflags to check for exec, because fmode won't
2498 * always have FMODE_EXEC set when file open for exec.
2499 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002500 if (openflags & __FMODE_EXEC) {
2501 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002502 if (S_ISDIR(state->inode->i_mode))
2503 mask = NFS4_ACCESS_LOOKUP;
2504 else
2505 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002506 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002507 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002508
2509 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002510 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2511 nfs_access_add_cache(state->inode, &cache);
2512
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002513 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2514 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002515 return 0;
2516
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002517 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002518}
2519
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002520/*
2521 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2522 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002523static int _nfs4_proc_open(struct nfs4_opendata *data,
2524 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002525{
David Howells2b0143b2015-03-17 22:25:59 +00002526 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002527 struct nfs_server *server = NFS_SERVER(dir);
2528 struct nfs_openargs *o_arg = &data->o_arg;
2529 struct nfs_openres *o_res = &data->o_res;
2530 int status;
2531
Fred Isaman3b65a302016-09-19 10:06:49 -04002532 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002533 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002534 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002535 if (status != 0) {
2536 if (status == -NFS4ERR_BADNAME &&
2537 !(o_arg->open_flags & O_CREAT))
2538 return -ENOENT;
2539 return status;
2540 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002541
Trond Myklebust6926afd2012-01-07 13:22:46 -05002542 nfs_fattr_map_and_free_names(server, &data->f_attr);
2543
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002544 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002545 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002546 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002547 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002548 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002549 if (data->file_created ||
2550 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002551 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002552 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002553 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002554 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2555 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002557 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002559 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002561 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2562 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002563 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2564 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002565 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002566 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569/*
2570 * OPEN_EXPIRED:
2571 * reclaim state on the server after a network partition.
2572 * Assumes caller holds the appropriate lock
2573 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002574static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002576 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002577 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002579 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002580 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002581 if (IS_ERR(opendata))
2582 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002583 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002584 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002585 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002586 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002587 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002590static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002591{
Trond Myklebust539cd032007-06-05 11:46:42 -04002592 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002593 struct nfs4_exception exception = { };
2594 int err;
2595
2596 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002597 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002598 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002599 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2600 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002601 switch (err) {
2602 default:
2603 goto out;
2604 case -NFS4ERR_GRACE:
2605 case -NFS4ERR_DELAY:
2606 nfs4_handle_exception(server, err, &exception);
2607 err = 0;
2608 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002609 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002610out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002611 return err;
2612}
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2615{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002617 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Trond Myklebust864472e2006-01-03 09:55:15 +01002619 ctx = nfs4_state_find_open_context(state);
2620 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002621 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002622 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002623 put_nfs_open_context(ctx);
2624 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
2626
Trond Myklebust41020b62016-09-22 13:38:58 -04002627static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2628 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002629{
Trond Myklebust41020b62016-09-22 13:38:58 -04002630 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002631 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002632}
2633
2634static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2635{
2636 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002637 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002638}
2639
2640static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2641{
2642 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2643 nfs40_clear_delegation_stateid(state);
2644 return nfs4_open_expired(sp, state);
2645}
2646
Trond Myklebust45870d62016-09-22 13:38:59 -04002647static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2648 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002649 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002650{
2651 return -NFS4ERR_BAD_STATEID;
2652}
2653
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002654#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002655static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2656 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002657 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002658{
2659 int status;
2660
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002661 switch (stateid->type) {
2662 default:
2663 break;
2664 case NFS4_INVALID_STATEID_TYPE:
2665 case NFS4_SPECIAL_STATEID_TYPE:
2666 return -NFS4ERR_BAD_STATEID;
2667 case NFS4_REVOKED_STATEID_TYPE:
2668 goto out_free;
2669 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002670
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002671 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002672 switch (status) {
2673 case -NFS4ERR_EXPIRED:
2674 case -NFS4ERR_ADMIN_REVOKED:
2675 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002676 break;
2677 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002678 return status;
2679 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002680out_free:
2681 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002682 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002683 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002684}
2685
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002686static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002687{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002688 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002689 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002690 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002691 const struct cred *cred = NULL;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002692 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002693
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002694 /* Get the delegation credential for use by test/free_stateid */
2695 rcu_read_lock();
2696 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002697 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002698 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002699 nfs_state_clear_delegation(state);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002700 return;
2701 }
2702
2703 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002704 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002705 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002706 nfs_state_clear_delegation(state);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002707 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002708 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002709
Trond Myklebust994b15b2018-09-05 14:07:14 -04002710 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2711 &delegation->flags)) {
2712 rcu_read_unlock();
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002713 return;
2714 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002715
NeilBrowna52458b2018-12-03 11:30:31 +11002716 if (delegation->cred)
2717 cred = get_cred(delegation->cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002718 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002719 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002720 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002721 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002722 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002723
NeilBrowna52458b2018-12-03 11:30:31 +11002724 if (delegation->cred)
2725 put_cred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002726}
2727
2728/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002729 * nfs41_check_expired_locks - possibly free a lock stateid
2730 *
2731 * @state: NFSv4 state for an inode
2732 *
2733 * Returns NFS_OK if recovery for this stateid is now finished.
2734 * Otherwise a negative NFS4ERR value is returned.
2735 */
2736static int nfs41_check_expired_locks(struct nfs4_state *state)
2737{
2738 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002739 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002740 struct nfs_server *server = NFS_SERVER(state->inode);
2741
2742 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2743 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002744
2745 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002746 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2747 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002748 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002749
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002750 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002751 spin_unlock(&state->state_lock);
2752
2753 nfs4_put_lock_state(prev);
2754 prev = lsp;
2755
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002756 status = nfs41_test_and_free_expired_stateid(server,
2757 &lsp->ls_stateid,
2758 cred);
2759 trace_nfs4_test_lock_stateid(state, lsp, status);
2760 if (status == -NFS4ERR_EXPIRED ||
2761 status == -NFS4ERR_BAD_STATEID) {
2762 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002763 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002764 if (!recover_lost_locks)
2765 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2766 } else if (status != NFS_OK) {
2767 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002768 nfs4_put_lock_state(prev);
2769 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002770 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002771 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002772 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002773 }
2774 spin_unlock(&state->state_lock);
2775 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002776out:
2777 return ret;
2778}
2779
2780/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002781 * nfs41_check_open_stateid - possibly free an open stateid
2782 *
2783 * @state: NFSv4 state for an inode
2784 *
2785 * Returns NFS_OK if recovery for this stateid is now finished.
2786 * Otherwise a negative NFS4ERR value is returned.
2787 */
2788static int nfs41_check_open_stateid(struct nfs4_state *state)
2789{
2790 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002791 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002792 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002793 int status;
2794
Trond Myklebustb134fc42016-09-22 13:39:16 -04002795 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002796 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2797 if (nfs4_have_delegation(state->inode, state->state))
2798 return NFS_OK;
2799 return -NFS4ERR_OPENMODE;
2800 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002801 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002802 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002803 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002804 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002805 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002806 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2807 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2808 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002809 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002810 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002811 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002812 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002813 if (nfs_open_stateid_recover_openmode(state))
2814 return -NFS4ERR_OPENMODE;
2815 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002816}
2817
2818static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2819{
Chuck Levereb64cf92012-07-11 16:30:05 -04002820 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002821
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002822 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002823 status = nfs41_check_expired_locks(state);
2824 if (status != NFS_OK)
2825 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002826 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002827 if (status != NFS_OK)
2828 status = nfs4_open_expired(sp, state);
2829 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002830}
2831#endif
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002834 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2835 * fields corresponding to attributes that were used to store the verifier.
2836 * Make sure we clobber those fields in the later setattr call
2837 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002838static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002839 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002840{
Trond Myklebust609339c2018-03-28 16:18:17 -04002841 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2842 __u32 attrset[3];
2843 unsigned ret;
2844 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002845
Trond Myklebust609339c2018-03-28 16:18:17 -04002846 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2847 attrset[i] = opendata->o_res.attrset[i];
2848 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2849 attrset[i] &= ~bitmask[i];
2850 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002851
Trond Myklebust609339c2018-03-28 16:18:17 -04002852 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2853 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002854
Trond Myklebust609339c2018-03-28 16:18:17 -04002855 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2856 if (sattr->ia_valid & ATTR_ATIME_SET)
2857 ret |= ATTR_ATIME_SET;
2858 else
2859 ret |= ATTR_ATIME;
2860 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002861
Trond Myklebust609339c2018-03-28 16:18:17 -04002862 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2863 if (sattr->ia_valid & ATTR_MTIME_SET)
2864 ret |= ATTR_MTIME_SET;
2865 else
2866 ret |= ATTR_MTIME;
2867 }
2868
2869 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002870 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002871 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002872}
2873
Trond Myklebustc21443c2013-02-07 14:26:21 -05002874static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2875 fmode_t fmode,
2876 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002877 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002878{
2879 struct nfs4_state_owner *sp = opendata->owner;
2880 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002881 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002882 struct nfs4_state *state;
2883 unsigned int seq;
2884 int ret;
2885
2886 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2887
Fred Isaman3b65a302016-09-19 10:06:49 -04002888 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002889 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002890 goto out;
2891
Trond Myklebustae55e592018-05-22 11:17:16 -04002892 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002893 ret = PTR_ERR(state);
2894 if (IS_ERR(state))
2895 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002896 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002897 if (server->caps & NFS_CAP_POSIX_LOCK)
2898 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002899 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2900 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002901
Trond Myklebust275bb302013-05-29 13:11:28 -04002902 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002903 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002904 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002905 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002906 alias = d_exact_alias(dentry, state->inode);
2907 if (!alias)
2908 alias = d_splice_alias(igrab(state->inode), dentry);
2909 /* d_splice_alias() can't fail here - it's a non-directory */
2910 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002911 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002912 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002913 }
2914 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002915 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002916 }
2917
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04002918 /* Parse layoutget results before we check for access */
2919 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2920
Trond Myklebustc21443c2013-02-07 14:26:21 -05002921 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2922 if (ret != 0)
2923 goto out;
2924
David Howells2b0143b2015-03-17 22:25:59 +00002925 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002926 nfs_inode_attach_open_context(ctx);
2927 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2928 nfs4_schedule_stateid_recovery(server, state);
2929 }
Fred Isaman2409a972016-10-06 12:11:21 -04002930
Trond Myklebustc21443c2013-02-07 14:26:21 -05002931out:
Trond Myklebustae55e592018-05-22 11:17:16 -04002932 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002933 return ret;
2934}
2935
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002936/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002937 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 */
Andy Adamson82be4172012-05-23 05:02:35 -04002939static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002940 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002941 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002942 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002943 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
2945 struct nfs4_state_owner *sp;
2946 struct nfs4_state *state = NULL;
2947 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002948 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002949 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11002950 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04002951 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2952 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002953 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002954 struct iattr *sattr = c->sattr;
2955 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002956 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002957 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
2959 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002961 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2962 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2964 goto out_err;
2965 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002966 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002967 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002968 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002969 if (d_really_is_positive(dentry))
2970 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002971 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002972 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002973 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002974 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2975 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002976 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002977 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
David Quigley14c43f72013-05-22 12:50:43 -04002979 if (label) {
2980 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2981 if (IS_ERR(olabel)) {
2982 status = PTR_ERR(olabel);
2983 goto err_opendata_put;
2984 }
2985 }
2986
Trond Myklebuste911b812014-03-26 13:24:37 -07002987 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2988 if (!opendata->f_attr.mdsthreshold) {
2989 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2990 if (!opendata->f_attr.mdsthreshold)
2991 goto err_free_label;
2992 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002993 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002994 }
David Howells2b0143b2015-03-17 22:25:59 +00002995 if (d_really_is_positive(dentry))
2996 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002997
Trond Myklebust3efb9722013-05-29 13:17:04 -04002998 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002999 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003000 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003001 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003002
NeilBrownefcbc042015-07-30 13:00:56 +10003003 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003004 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003005 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003006 /*
3007 * send create attributes which was not set by open
3008 * with an extra setattr.
3009 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003010 if (attrs || label) {
3011 unsigned ia_old = sattr->ia_valid;
3012
3013 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003014 nfs_fattr_init(opendata->o_res.f_attr);
3015 status = nfs4_do_setattr(state->inode, cred,
3016 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003017 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003018 if (status == 0) {
3019 nfs_setattr_update_inode(state->inode, sattr,
3020 opendata->o_res.f_attr);
3021 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3022 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003023 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003024 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003025 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003026 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003027 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003028
Trond Myklebuste911b812014-03-26 13:24:37 -07003029 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003030 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003031 opendata->f_attr.mdsthreshold = NULL;
3032 }
Andy Adamson82be4172012-05-23 05:02:35 -04003033
David Quigley14c43f72013-05-22 12:50:43 -04003034 nfs4_label_free(olabel);
3035
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003036 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003039err_free_label:
3040 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003041err_opendata_put:
3042 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003043err_put_state_owner:
3044 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return status;
3047}
3048
3049
Andy Adamson82be4172012-05-23 05:02:35 -04003050static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003051 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003052 int flags,
3053 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003054 struct nfs4_label *label,
3055 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003057 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 struct nfs4_exception exception = { };
3059 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003060 struct nfs4_open_createattrs c = {
3061 .label = label,
3062 .sattr = sattr,
3063 .verf = {
3064 [0] = (__u32)jiffies,
3065 [1] = (__u32)current->pid,
3066 },
3067 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 int status;
3069
3070 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003071 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003072 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003073 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 if (status == 0)
3075 break;
3076 /* NOTE: BAD_SEQID means the server and client disagree about the
3077 * book-keeping w.r.t. state-changing operations
3078 * (OPEN/CLOSE/LOCK/LOCKU...)
3079 * It is actually a sign of a bug on the client or on the server.
3080 *
3081 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003082 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 * have unhashed the old state_owner for us, and that we can
3084 * therefore safely retry using a new one. We should still warn
3085 * the user though...
3086 */
3087 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003088 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003089 " returned a bad sequence-id error!\n",
3090 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 exception.retry = 1;
3092 continue;
3093 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003094 /*
3095 * BAD_STATEID on OPEN means that the server cancelled our
3096 * state before it received the OPEN_CONFIRM.
3097 * Recover by retrying the request as per the discussion
3098 * on Page 181 of RFC3530.
3099 */
3100 if (status == -NFS4ERR_BAD_STATEID) {
3101 exception.retry = 1;
3102 continue;
3103 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003104 if (status == -EAGAIN) {
3105 /* We must have found a delegation */
3106 exception.retry = 1;
3107 continue;
3108 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003109 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3110 continue;
3111 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 status, &exception));
3113 } while (exception.retry);
3114 return res;
3115}
3116
Trond Myklebust8487c472016-06-26 08:44:35 -04003117static int _nfs4_do_setattr(struct inode *inode,
3118 struct nfs_setattrargs *arg,
3119 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003120 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003121 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003122{
3123 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003124 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3126 .rpc_argp = arg,
3127 .rpc_resp = res,
3128 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003129 };
NeilBrowna52458b2018-12-03 11:30:31 +11003130 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003131 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003132 bool truncate;
3133 int status;
3134
3135 nfs_fattr_init(res->fattr);
3136
3137 /* Servers should only apply open mode checks for file size changes */
3138 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003139 if (!truncate)
3140 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003141
Trond Myklebust991eedb2018-04-09 11:15:30 -04003142 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003143 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003144 } else if (ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003145 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003146 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003147 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003148 l_ctx = nfs_get_lock_context(ctx);
3149 if (IS_ERR(l_ctx))
3150 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003151 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3152 &arg->stateid, &delegation_cred);
3153 nfs_put_lock_context(l_ctx);
3154 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003155 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003156 } else {
3157zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003158 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003159 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003160 if (delegation_cred)
3161 msg.rpc_cred = delegation_cred;
3162
3163 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3164
NeilBrowna52458b2018-12-03 11:30:31 +11003165 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003166 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003167 renew_lease(server, timestamp);
3168 trace_nfs4_setattr(inode, &arg->stateid, status);
3169 return status;
3170}
3171
NeilBrowna52458b2018-12-03 11:30:31 +11003172static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003173 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003174 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003175 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003177 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003178 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003179 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003180 struct nfs_setattrargs arg = {
3181 .fh = NFS_FH(inode),
3182 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003184 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003185 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003186 };
3187 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003189 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003191 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003192 struct nfs4_exception exception = {
3193 .state = state,
3194 .inode = inode,
3195 .stateid = &arg.stateid,
3196 };
3197 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003200 nfs4_bitmap_copy_adjust_setattr(bitmask,
3201 nfs4_bitmask(server, olabel),
3202 inode);
3203
NeilBrown29b59f92016-10-13 15:26:47 +11003204 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003205 switch (err) {
3206 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003207 if (!(sattr->ia_valid & ATTR_SIZE)) {
3208 pr_warn_once("NFSv4: server %s is incorrectly "
3209 "applying open mode checks to "
3210 "a SETATTR that is not "
3211 "changing file size.\n",
3212 server->nfs_client->cl_hostname);
3213 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003214 if (state && !(state->state & FMODE_WRITE)) {
3215 err = -EBADF;
3216 if (sattr->ia_valid & ATTR_OPEN)
3217 err = -EACCES;
3218 goto out;
3219 }
3220 }
3221 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003223out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return err;
3225}
3226
Peng Tao500d7012015-09-22 11:35:22 +08003227static bool
3228nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3229{
3230 if (inode == NULL || !nfs_have_layout(inode))
3231 return false;
3232
3233 return pnfs_wait_on_layoutreturn(inode, task);
3234}
3235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236struct nfs4_closedata {
3237 struct inode *inode;
3238 struct nfs4_state *state;
3239 struct nfs_closeargs arg;
3240 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003241 struct {
3242 struct nfs4_layoutreturn_args arg;
3243 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003244 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003245 u32 roc_barrier;
3246 bool roc;
3247 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003248 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003249 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250};
3251
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003252static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003253{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003254 struct nfs4_closedata *calldata = data;
3255 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003256 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003257
Trond Myklebustcf805162016-11-15 14:56:07 -05003258 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003259 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3260 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003261 nfs4_put_open_state(calldata->state);
3262 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003263 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003264 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003265 kfree(calldata);
3266}
3267
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003268static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003270 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003273 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003274 struct nfs4_exception exception = {
3275 .state = state,
3276 .inode = calldata->inode,
3277 .stateid = &calldata->arg.stateid,
3278 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Chuck Levera3ca5652012-03-01 17:00:40 -05003280 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003281 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3282 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003283 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003284
3285 /* Handle Layoutreturn errors */
3286 if (calldata->arg.lr_args && task->tk_status != 0) {
3287 switch (calldata->res.lr_ret) {
3288 default:
3289 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3290 break;
3291 case 0:
3292 calldata->arg.lr_args = NULL;
3293 calldata->res.lr_res = NULL;
3294 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003295 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04003296 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04003297 &calldata->arg.lr_args->range,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003298 calldata->inode))
3299 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003300 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003301 case -NFS4ERR_ADMIN_REVOKED:
3302 case -NFS4ERR_DELEG_REVOKED:
3303 case -NFS4ERR_EXPIRED:
3304 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003305 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3306 case -NFS4ERR_WRONG_CRED:
3307 calldata->arg.lr_args = NULL;
3308 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003309 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003310 }
3311 }
3312
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003313 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 * the state_owner. we keep this around to process errors
3315 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 switch (task->tk_status) {
3317 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003318 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003319 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003320 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003321 case -NFS4ERR_ACCESS:
3322 if (calldata->arg.bitmask != NULL) {
3323 calldata->arg.bitmask = NULL;
3324 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003325 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003326
3327 }
3328 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003329 case -NFS4ERR_OLD_STATEID:
3330 /* Did we race with OPEN? */
3331 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003332 state))
3333 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003334 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003335 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003337 case -NFS4ERR_EXPIRED:
3338 nfs4_free_revoked_stateid(server,
3339 &calldata->arg.stateid,
3340 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003341 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003342 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003345 task->tk_status = nfs4_async_handle_exception(task,
3346 server, task->tk_status, &exception);
3347 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003348 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003350 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3351 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003352out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003353 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003354 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003355 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003356 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003357 return;
3358lr_restart:
3359 calldata->res.lr_ret = 0;
3360out_restart:
3361 task->tk_status = 0;
3362 rpc_restart_call_prepare(task);
3363 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364}
3365
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003366static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003368 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003369 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003370 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003371 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003372 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003373 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003374
Chuck Levera3ca5652012-03-01 17:00:40 -05003375 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003376 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003377 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003378
Trond Myklebust88069f72009-12-08 08:33:16 -05003379 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003380 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003381 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3382 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3383 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003384 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003385 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003386 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003387 if (state->n_rdonly == 0)
3388 call_close |= is_rdonly;
3389 else if (is_rdonly)
3390 calldata->arg.fmode |= FMODE_READ;
3391 if (state->n_wronly == 0)
3392 call_close |= is_wronly;
3393 else if (is_wronly)
3394 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003395 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3396 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003397 } else if (is_rdwr)
3398 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3399
Trond Myklebust5cc78612016-11-14 11:19:56 -05003400 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003401 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003402 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003403 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003404
3405 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003406 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003407 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003408 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003409
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003410 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003411 nfs_release_seqid(calldata->arg.seqid);
3412 goto out_wait;
3413 }
3414
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003415 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3416 if (lo && !pnfs_layout_is_valid(lo)) {
3417 calldata->arg.lr_args = NULL;
3418 calldata->res.lr_res = NULL;
3419 }
3420
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003421 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003422 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003423
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003424 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003425 /* Close-to-open cache consistency revalidation */
3426 if (!nfs4_have_delegation(inode, FMODE_READ))
3427 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3428 else
3429 calldata->arg.bitmask = NULL;
3430 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003431
Trond Myklebust6ae37332015-01-30 14:21:14 -05003432 calldata->arg.share_access =
3433 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3434 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003435
Trond Myklebustd8d84982016-12-19 12:14:44 -05003436 if (calldata->res.fattr == NULL)
3437 calldata->arg.bitmask = NULL;
3438 else if (calldata->arg.bitmask == NULL)
3439 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003440 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003441 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003442 &calldata->arg.seq_args,
3443 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003444 task) != 0)
3445 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003446 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003447 return;
3448out_no_action:
3449 task->tk_action = NULL;
3450out_wait:
3451 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003454static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003455 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003456 .rpc_call_done = nfs4_close_done,
3457 .rpc_release = nfs4_free_closedata,
3458};
3459
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460/*
3461 * It is possible for data to be read/written from a mem-mapped file
3462 * after the sys_close call (which hits the vfs layer as a flush).
3463 * This means that we can't safely call nfsv4 close on a file until
3464 * the inode is cleared. This in turn means that we are not good
3465 * NFSv4 citizens - we do not indicate to the server to update the file's
3466 * share state even when we are done with one of the three share
3467 * stateid's in the inode.
3468 *
3469 * NOTE: Caller must be holding the sp->so_owner semaphore!
3470 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003471int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003473 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003474 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003476 struct nfs4_state_owner *sp = state->owner;
3477 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003478 struct rpc_message msg = {
3479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3480 .rpc_cred = state->owner->so_cred,
3481 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003482 struct rpc_task_setup task_setup_data = {
3483 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003484 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003485 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003486 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003487 .flags = RPC_TASK_ASYNC,
3488 };
Trond Myklebust95121352005-10-18 14:20:12 -07003489 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003491 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3492 &task_setup_data.rpc_client, &msg);
3493
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003494 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003496 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003497 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003498 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003500 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003501 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3502 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003504 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3505 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003506 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003507 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003508 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003509 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003510 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003511 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003512 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003513 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003514 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003515 calldata->lr.roc = pnfs_roc(state->inode,
3516 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3517 if (calldata->lr.roc) {
3518 calldata->arg.lr_args = &calldata->lr.arg;
3519 calldata->res.lr_res = &calldata->lr.res;
3520 }
Al Viro643168c2011-06-22 18:20:23 -04003521 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003522
Trond Myklebust1174dd12010-12-21 10:52:24 -05003523 msg.rpc_argp = &calldata->arg;
3524 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003525 task_setup_data.callback_data = calldata;
3526 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003527 if (IS_ERR(task))
3528 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003529 status = 0;
3530 if (wait)
3531 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003532 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003533 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003534out_free_calldata:
3535 kfree(calldata);
3536out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003537 nfs4_put_open_state(state);
3538 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003539 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
3541
Trond Myklebust2b484292010-09-17 10:56:51 -04003542static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003543nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3544 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003547 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3548
3549 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Trond Myklebust565277f2007-10-15 18:17:53 -04003551 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003552 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003553
3554 nfs4_label_release_security(label);
3555
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003556 if (IS_ERR(state))
3557 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003558 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559}
3560
Trond Myklebust1185a552009-12-03 15:54:02 -05003561static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003562{
3563 if (ctx->state == NULL)
3564 return;
3565 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003566 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003567 else
Al Viro643168c2011-06-22 18:20:23 -04003568 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003569}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Trond Myklebustb944dba2013-11-04 15:20:20 -05003571#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3572#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003573#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3576{
Kinglong Mee8c612822015-08-26 21:12:58 +08003577 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003578 struct nfs4_server_caps_arg args = {
3579 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003580 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003581 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 struct nfs4_server_caps_res res = {};
3583 struct rpc_message msg = {
3584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003585 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 .rpc_resp = &res,
3587 };
3588 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003589 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Kinglong Mee8c612822015-08-26 21:12:58 +08003591 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3592 FATTR4_WORD0_FH_EXPIRE_TYPE |
3593 FATTR4_WORD0_LINK_SUPPORT |
3594 FATTR4_WORD0_SYMLINK_SUPPORT |
3595 FATTR4_WORD0_ACLSUPPORT;
3596 if (minorversion)
3597 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3598
Bryan Schumaker7c513052011-03-24 17:12:24 +00003599 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003601 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003602 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003603 case 0:
3604 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3605 res.attr_bitmask[2] = 0;
3606 break;
3607 case 1:
3608 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3609 break;
3610 case 2:
3611 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003614 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3615 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3616 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3617 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003618 NFS_CAP_CTIME|NFS_CAP_MTIME|
3619 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003620 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3621 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 server->caps |= NFS_CAP_ACLS;
3623 if (res.has_links != 0)
3624 server->caps |= NFS_CAP_HARDLINKS;
3625 if (res.has_symlinks != 0)
3626 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003627 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3628 server->caps |= NFS_CAP_FILEID;
3629 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3630 server->caps |= NFS_CAP_MODE;
3631 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3632 server->caps |= NFS_CAP_NLINK;
3633 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3634 server->caps |= NFS_CAP_OWNER;
3635 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3636 server->caps |= NFS_CAP_OWNER_GROUP;
3637 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3638 server->caps |= NFS_CAP_ATIME;
3639 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3640 server->caps |= NFS_CAP_CTIME;
3641 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3642 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003643#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3644 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3645 server->caps |= NFS_CAP_SECURITY_LABEL;
3646#endif
3647 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3648 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003649 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003650
Trond Myklebusta65318b2009-03-11 14:10:28 -04003651 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3652 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3653 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003654 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003655
3656 /* Avoid a regression due to buggy server */
3657 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3658 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003659 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3660 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003663 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 return status;
3667}
3668
Trond Myklebust55a97592006-06-09 09:34:19 -04003669int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670{
3671 struct nfs4_exception exception = { };
3672 int err;
3673 do {
3674 err = nfs4_handle_exception(server,
3675 _nfs4_server_capabilities(server, fhandle),
3676 &exception);
3677 } while (exception.retry);
3678 return err;
3679}
3680
3681static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3682 struct nfs_fsinfo *info)
3683{
David Quigleyaa9c2662013-05-22 12:50:44 -04003684 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003686 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 };
3688 struct nfs4_lookup_res res = {
3689 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003690 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 .fh = fhandle,
3692 };
3693 struct rpc_message msg = {
3694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3695 .rpc_argp = &args,
3696 .rpc_resp = &res,
3697 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003698
David Quigleyaa9c2662013-05-22 12:50:44 -04003699 bitmask[0] = nfs4_fattr_bitmap[0];
3700 bitmask[1] = nfs4_fattr_bitmap[1];
3701 /*
3702 * Process the label in the upcoming getfattr
3703 */
3704 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3705
Trond Myklebust0e574af2005-10-27 22:12:38 -04003706 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003707 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
3709
3710static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3711 struct nfs_fsinfo *info)
3712{
3713 struct nfs4_exception exception = { };
3714 int err;
3715 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003716 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003717 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003718 switch (err) {
3719 case 0:
3720 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003721 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003722 default:
3723 err = nfs4_handle_exception(server, err, &exception);
3724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003726out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 return err;
3728}
3729
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003730static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3731 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3732{
Trond Myklebustc2190662013-08-26 19:23:04 -04003733 struct rpc_auth_create_args auth_args = {
3734 .pseudoflavor = flavor,
3735 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003736 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003737
Trond Myklebustc2190662013-08-26 19:23:04 -04003738 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003739 if (IS_ERR(auth))
3740 return -EACCES;
3741 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003742}
3743
Chuck Lever9a744ba2013-03-16 15:56:02 -04003744/*
3745 * Retry pseudoroot lookup with various security flavors. We do this when:
3746 *
3747 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3748 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3749 *
3750 * Returns zero on success, or a negative NFS4ERR value, or a
3751 * negative errno value.
3752 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003753static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003754 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003756 /* Per 3530bis 15.33.5 */
3757 static const rpc_authflavor_t flav_array[] = {
3758 RPC_AUTH_GSS_KRB5P,
3759 RPC_AUTH_GSS_KRB5I,
3760 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003761 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003762 RPC_AUTH_NULL,
3763 };
3764 int status = -EPERM;
3765 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003767 if (server->auth_info.flavor_len > 0) {
3768 /* try each flavor specified by user */
3769 for (i = 0; i < server->auth_info.flavor_len; i++) {
3770 status = nfs4_lookup_root_sec(server, fhandle, info,
3771 server->auth_info.flavors[i]);
3772 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3773 continue;
3774 break;
3775 }
3776 } else {
3777 /* no flavors specified by user, try default list */
3778 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3779 status = nfs4_lookup_root_sec(server, fhandle, info,
3780 flav_array[i]);
3781 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3782 continue;
3783 break;
3784 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003785 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003786
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003787 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003788 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003789 * to access the mount. It could also mean that we tried to mount
3790 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3791 * existing mount programs don't handle -EACCES very well so it should
3792 * be mapped to -EPERM instead.
3793 */
3794 if (status == -EACCES)
3795 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003796 return status;
3797}
3798
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003799/**
3800 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3801 * @server: initialized nfs_server handle
3802 * @fhandle: we fill in the pseudo-fs root file handle
3803 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003804 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003805 *
3806 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003807 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003808int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003809 struct nfs_fsinfo *info,
3810 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003811{
Andre Przywarac7757072015-04-23 17:17:40 +01003812 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003813
Andre Przywarac7757072015-04-23 17:17:40 +01003814 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003815 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003816
3817 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003818 status = server->nfs_client->cl_mvops->find_root_sec(server,
3819 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 if (status == 0)
3822 status = nfs4_server_capabilities(server, fhandle);
3823 if (status == 0)
3824 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003825
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003826 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827}
3828
Bryan Schumakerbae36242012-05-10 15:07:31 -04003829static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3830 struct nfs_fsinfo *info)
3831{
3832 int error;
3833 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003834 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003835
3836 error = nfs4_server_capabilities(server, mntfh);
3837 if (error < 0) {
3838 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3839 return error;
3840 }
3841
David Quigley14c43f72013-05-22 12:50:43 -04003842 label = nfs4_label_alloc(server, GFP_KERNEL);
3843 if (IS_ERR(label))
3844 return PTR_ERR(label);
3845
Trond Myklebusta841b542018-04-07 13:50:59 -04003846 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003847 if (error < 0) {
3848 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003849 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003850 }
3851
3852 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3853 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3854 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3855
David Quigley14c43f72013-05-22 12:50:43 -04003856err_free_label:
3857 nfs4_label_free(label);
3858
Bryan Schumakerbae36242012-05-10 15:07:31 -04003859 return error;
3860}
3861
Manoj Naik6b97fd32006-06-09 09:34:29 -04003862/*
3863 * Get locations and (maybe) other attributes of a referral.
3864 * Note that we'll actually follow the referral later when
3865 * we detect fsid mismatch in inode revalidation
3866 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003867static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3868 const struct qstr *name, struct nfs_fattr *fattr,
3869 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003870{
3871 int status = -ENOMEM;
3872 struct page *page = NULL;
3873 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003874
3875 page = alloc_page(GFP_KERNEL);
3876 if (page == NULL)
3877 goto out;
3878 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3879 if (locations == NULL)
3880 goto out;
3881
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003882 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003883 if (status != 0)
3884 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003885
3886 /*
3887 * If the fsid didn't change, this is a migration event, not a
3888 * referral. Cause us to drop into the exception handler, which
3889 * will kick off migration recovery.
3890 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003891 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003892 dprintk("%s: server did not return a different fsid for"
3893 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003894 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003895 goto out;
3896 }
Andy Adamson533eb462011-06-13 18:25:56 -04003897 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3898 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003899
Andy Adamson533eb462011-06-13 18:25:56 -04003900 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003901 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003902 memset(fhandle, 0, sizeof(struct nfs_fh));
3903out:
3904 if (page)
3905 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003906 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003907 return status;
3908}
3909
David Quigley1775fd32013-05-22 12:50:42 -04003910static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003911 struct nfs_fattr *fattr, struct nfs4_label *label,
3912 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913{
Trond Myklebust771734f2018-04-07 13:54:23 -04003914 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 struct nfs4_getattr_arg args = {
3916 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04003917 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 };
3919 struct nfs4_getattr_res res = {
3920 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003921 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 .server = server,
3923 };
3924 struct rpc_message msg = {
3925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3926 .rpc_argp = &args,
3927 .rpc_resp = &res,
3928 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003929
Trond Myklebust771734f2018-04-07 13:54:23 -04003930 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04003931
Trond Myklebust0e574af2005-10-27 22:12:38 -04003932 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003933 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934}
3935
David Quigley1775fd32013-05-22 12:50:42 -04003936static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003937 struct nfs_fattr *fattr, struct nfs4_label *label,
3938 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939{
3940 struct nfs4_exception exception = { };
3941 int err;
3942 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04003943 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003944 trace_nfs4_getattr(server, fhandle, fattr, err);
3945 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 &exception);
3947 } while (exception.retry);
3948 return err;
3949}
3950
3951/*
3952 * The file is not closed if it is opened due to the a request to change
3953 * the size of the file. The open call will not be needed once the
3954 * VFS layer lookup-intents are implemented.
3955 *
3956 * Close is called when the inode is destroyed.
3957 * If we haven't opened the file for O_WRONLY, we
3958 * need to in the size_change case to obtain a stateid.
3959 *
3960 * Got race?
3961 * Because OPEN is always done by name in nfsv4, it is
3962 * possible that we opened a different file by the same
3963 * name. We can recognize this race condition, but we
3964 * can't do anything about it besides returning an error.
3965 *
3966 * This will be fixed with VFS changes (lookup-intent).
3967 */
3968static int
3969nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3970 struct iattr *sattr)
3971{
David Howells2b0143b2015-03-17 22:25:59 +00003972 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11003973 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003974 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003975 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 int status;
3977
Peng Tao88ac8152014-09-12 11:04:10 +08003978 if (pnfs_ld_layoutret_on_setattr(inode) &&
3979 sattr->ia_valid & ATTR_SIZE &&
3980 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003981 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003982
Trond Myklebust0e574af2005-10-27 22:12:38 -04003983 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
Andy Adamson26699402012-05-30 16:12:24 -04003985 /* Deal with open(O_TRUNC) */
3986 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003987 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003988
3989 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003990 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003991 return 0;
3992
Trond Myklebustd5308382005-11-04 15:33:38 -05003993 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003994 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003995
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003996 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003997 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003998 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003999 }
4000
David Quigley14c43f72013-05-22 12:50:43 -04004001 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4002 if (IS_ERR(label))
4003 return PTR_ERR(label);
4004
Trond Myklebust199366f2018-03-20 16:43:18 -04004005 /* Return any delegations if we're going to change ACLs */
4006 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004007 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004008
NeilBrown29b59f92016-10-13 15:26:47 +11004009 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004010 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004011 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004012 nfs_setsecurity(inode, fattr, label);
4013 }
David Quigley14c43f72013-05-22 12:50:43 -04004014 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 return status;
4016}
4017
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004018static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4019 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004020 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004021{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004022 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004023 int status;
4024 struct nfs4_lookup_arg args = {
4025 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004026 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04004027 .name = name,
4028 };
4029 struct nfs4_lookup_res res = {
4030 .server = server,
4031 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004032 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004033 .fh = fhandle,
4034 };
4035 struct rpc_message msg = {
4036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4037 .rpc_argp = &args,
4038 .rpc_resp = &res,
4039 };
4040
David Quigleyaa9c2662013-05-22 12:50:44 -04004041 args.bitmask = nfs4_bitmask(server, label);
4042
David Howells2b3de442006-08-22 20:06:09 -04004043 nfs_fattr_init(fattr);
4044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004046 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 dprintk("NFS reply lookup: %d\n", status);
4048 return status;
4049}
4050
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004051static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004052{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004053 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004054 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004055 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4056 fattr->nlink = 2;
4057}
4058
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004059static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004060 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004061 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004062{
4063 struct nfs4_exception exception = { };
4064 struct rpc_clnt *client = *clnt;
4065 int err;
4066 do {
David Quigley1775fd32013-05-22 12:50:42 -04004067 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004068 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004069 switch (err) {
4070 case -NFS4ERR_BADNAME:
4071 err = -ENOENT;
4072 goto out;
4073 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004074 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004075 if (err == -NFS4ERR_MOVED)
4076 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004077 goto out;
4078 case -NFS4ERR_WRONGSEC:
4079 err = -EPERM;
4080 if (client != *clnt)
4081 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004082 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004083 if (IS_ERR(client))
4084 return PTR_ERR(client);
4085
4086 exception.retry = 1;
4087 break;
4088 default:
4089 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4090 }
4091 } while (exception.retry);
4092
4093out:
4094 if (err == 0)
4095 *clnt = client;
4096 else if (client != *clnt)
4097 rpc_shutdown_client(client);
4098
4099 return err;
4100}
4101
Al Virobeffb8f2016-07-20 16:34:42 -04004102static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004103 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4104 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004106 int status;
4107 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004108
David Quigley1775fd32013-05-22 12:50:42 -04004109 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004110 if (client != NFS_CLIENT(dir)) {
4111 rpc_shutdown_client(client);
4112 nfs_fixup_secinfo_attributes(fattr);
4113 }
4114 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115}
4116
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004117struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004118nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004119 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4120{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004121 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004122 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004123
David Quigley1775fd32013-05-22 12:50:42 -04004124 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004125 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004126 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004127 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004128}
4129
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004130static int _nfs4_proc_lookupp(struct inode *inode,
4131 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4132 struct nfs4_label *label)
4133{
4134 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4135 struct nfs_server *server = NFS_SERVER(inode);
4136 int status;
4137 struct nfs4_lookupp_arg args = {
4138 .bitmask = server->attr_bitmask,
4139 .fh = NFS_FH(inode),
4140 };
4141 struct nfs4_lookupp_res res = {
4142 .server = server,
4143 .fattr = fattr,
4144 .label = label,
4145 .fh = fhandle,
4146 };
4147 struct rpc_message msg = {
4148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4149 .rpc_argp = &args,
4150 .rpc_resp = &res,
4151 };
4152
4153 args.bitmask = nfs4_bitmask(server, label);
4154
4155 nfs_fattr_init(fattr);
4156
4157 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4158 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4159 &res.seq_res, 0);
4160 dprintk("NFS reply lookupp: %d\n", status);
4161 return status;
4162}
4163
4164static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4165 struct nfs_fattr *fattr, struct nfs4_label *label)
4166{
4167 struct nfs4_exception exception = { };
4168 int err;
4169 do {
4170 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4171 trace_nfs4_lookupp(inode, err);
4172 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4173 &exception);
4174 } while (exception.retry);
4175 return err;
4176}
4177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4179{
Trond Myklebust76b32992007-08-10 17:45:11 -04004180 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 struct nfs4_accessargs args = {
4182 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004183 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004185 struct nfs4_accessres res = {
4186 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004187 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 struct rpc_message msg = {
4189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4190 .rpc_argp = &args,
4191 .rpc_resp = &res,
4192 .rpc_cred = entry->cred,
4193 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004194 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Trond Myklebust7c672652018-06-04 15:00:53 -04004196 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004197 res.fattr = nfs_alloc_fattr();
4198 if (res.fattr == NULL)
4199 return -ENOMEM;
4200 args.bitmask = server->cache_consistency_bitmask;
4201 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004202 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004204 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004205 if (res.fattr)
4206 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004208 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 return status;
4210}
4211
4212static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4213{
4214 struct nfs4_exception exception = { };
4215 int err;
4216 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004217 err = _nfs4_proc_access(inode, entry);
4218 trace_nfs4_access(inode, err);
4219 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 &exception);
4221 } while (exception.retry);
4222 return err;
4223}
4224
4225/*
4226 * TODO: For the time being, we don't try to get any attributes
4227 * along with any of the zero-copy operations READ, READDIR,
4228 * READLINK, WRITE.
4229 *
4230 * In the case of the first three, we want to put the GETATTR
4231 * after the read-type operation -- this is because it is hard
4232 * to predict the length of a GETATTR response in v4, and thus
4233 * align the READ data correctly. This means that the GETATTR
4234 * may end up partially falling into the page cache, and we should
4235 * shift it into the 'tail' of the xdr_buf before processing.
4236 * To do this efficiently, we need to know the total length
4237 * of data received, which doesn't seem to be available outside
4238 * of the RPC layer.
4239 *
4240 * In the case of WRITE, we also want to put the GETATTR after
4241 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004242 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 *
4244 * Both of these changes to the XDR layer would in fact be quite
4245 * minor, but I decided to leave them for a subsequent patch.
4246 */
4247static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4248 unsigned int pgbase, unsigned int pglen)
4249{
4250 struct nfs4_readlink args = {
4251 .fh = NFS_FH(inode),
4252 .pgbase = pgbase,
4253 .pglen = pglen,
4254 .pages = &page,
4255 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004256 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 struct rpc_message msg = {
4258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4259 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004260 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 };
4262
Bryan Schumaker7c513052011-03-24 17:12:24 +00004263 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 -07004264}
4265
4266static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4267 unsigned int pgbase, unsigned int pglen)
4268{
4269 struct nfs4_exception exception = { };
4270 int err;
4271 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004272 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4273 trace_nfs4_readlink(inode, err);
4274 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 &exception);
4276 } while (exception.retry);
4277 return err;
4278}
4279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004281 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283static int
4284nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004285 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004287 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004288 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004289 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 int status = 0;
4292
NeilBrown532d4de2016-10-13 15:26:47 +11004293 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004294 if (IS_ERR(ctx))
4295 return PTR_ERR(ctx);
4296
David Quigleyaa9c2662013-05-22 12:50:44 -04004297 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4298
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004299 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4300 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004301 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 if (IS_ERR(state)) {
4303 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004304 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004307 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004308 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return status;
4310}
4311
Trond Myklebust3c591172018-07-31 15:54:10 -04004312static int
4313_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314{
Trond Myklebust16e42952005-10-27 22:12:44 -04004315 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004316 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004318 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004320 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004321 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004322 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4325 .rpc_argp = &args,
4326 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004328 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004329 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004330
Bryan Schumaker7c513052011-03-24 17:12:24 +00004331 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004332 if (status == 0) {
4333 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004334 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004335 /* Removing a directory decrements nlink in the parent */
4336 if (ftype == NF4DIR && dir->i_nlink > 2)
4337 nfs4_dec_nlink_locked(dir);
4338 spin_unlock(&dir->i_lock);
4339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 return status;
4341}
4342
Trond Myklebust912678d2018-03-20 16:43:15 -04004343static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4344{
4345 struct nfs4_exception exception = { };
4346 struct inode *inode = d_inode(dentry);
4347 int err;
4348
Trond Myklebustc01d3642018-03-20 16:43:20 -04004349 if (inode) {
4350 if (inode->i_nlink == 1)
4351 nfs4_inode_return_delegation(inode);
4352 else
4353 nfs4_inode_make_writeable(inode);
4354 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004355 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004356 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004357 trace_nfs4_remove(dir, &dentry->d_name, err);
4358 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4359 &exception);
4360 } while (exception.retry);
4361 return err;
4362}
4363
4364static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365{
4366 struct nfs4_exception exception = { };
4367 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004370 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004371 trace_nfs4_remove(dir, name, err);
4372 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 &exception);
4374 } while (exception.retry);
4375 return err;
4376}
4377
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004378static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4379 struct dentry *dentry,
4380 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004382 struct nfs_removeargs *args = msg->rpc_argp;
4383 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Trond Myklebust977fcc22018-03-20 16:43:17 -04004385 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004387 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004388
4389 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004390
4391 if (inode)
4392 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393}
4394
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004395static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4396{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004397 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004398 &data->args.seq_args,
4399 &data->res.seq_res,
4400 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401}
4402
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004403static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004405 struct nfs_unlinkdata *data = task->tk_calldata;
4406 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004407
Trond Myklebust14516c32010-07-31 14:29:06 -04004408 if (!nfs4_sequence_done(task, &res->seq_res))
4409 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004410 if (nfs4_async_handle_error(task, res->server, NULL,
4411 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004412 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004413 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004414 update_changeattr(dir, &res->cinfo,
4415 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004416 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004419static void nfs4_proc_rename_setup(struct rpc_message *msg,
4420 struct dentry *old_dentry,
4421 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004422{
Jeff Laytond3d41522010-09-17 17:31:57 -04004423 struct nfs_renameargs *arg = msg->rpc_argp;
4424 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004425 struct inode *old_inode = d_inode(old_dentry);
4426 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004427
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004428 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004429 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004430 if (new_inode)
4431 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004432 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004433 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004434 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004435}
4436
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004437static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4438{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004439 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004440 &data->args.seq_args,
4441 &data->res.seq_res,
4442 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004443}
4444
4445static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4446 struct inode *new_dir)
4447{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004448 struct nfs_renamedata *data = task->tk_calldata;
4449 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004450
4451 if (!nfs4_sequence_done(task, &res->seq_res))
4452 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004453 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004454 return 0;
4455
Trond Myklebustc733c492017-01-11 12:32:26 -05004456 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004457 if (new_dir != old_dir) {
4458 /* Note: If we moved a directory, nlink will change */
4459 update_changeattr(old_dir, &res->old_cinfo,
4460 res->old_fattr->time_start,
4461 NFS_INO_INVALID_OTHER);
4462 update_changeattr(new_dir, &res->new_cinfo,
4463 res->new_fattr->time_start,
4464 NFS_INO_INVALID_OTHER);
4465 } else
4466 update_changeattr(old_dir, &res->old_cinfo,
4467 res->old_fattr->time_start,
4468 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004469 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004470 return 1;
4471}
4472
Al Virobeffb8f2016-07-20 16:34:42 -04004473static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004475 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004476 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 struct nfs4_link_arg arg = {
4478 .fh = NFS_FH(inode),
4479 .dir_fh = NFS_FH(dir),
4480 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004481 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004483 struct nfs4_link_res res = {
4484 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004485 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004486 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 struct rpc_message msg = {
4488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4489 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004490 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004492 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Trond Myklebust136f2622010-04-16 16:22:49 -04004494 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004495 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004496 goto out;
4497
David Quigley14c43f72013-05-22 12:50:43 -04004498 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4499 if (IS_ERR(res.label)) {
4500 status = PTR_ERR(res.label);
4501 goto out;
4502 }
4503
Trond Myklebustc01d3642018-03-20 16:43:20 -04004504 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004505 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004506
Bryan Schumaker7c513052011-03-24 17:12:24 +00004507 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004508 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004509 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004510 status = nfs_post_op_update_inode(inode, res.fattr);
4511 if (!status)
4512 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004513 }
David Quigley14c43f72013-05-22 12:50:43 -04004514
4515
4516 nfs4_label_free(res.label);
4517
Trond Myklebust136f2622010-04-16 16:22:49 -04004518out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004519 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 return status;
4521}
4522
Al Virobeffb8f2016-07-20 16:34:42 -04004523static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
4525 struct nfs4_exception exception = { };
4526 int err;
4527 do {
4528 err = nfs4_handle_exception(NFS_SERVER(inode),
4529 _nfs4_proc_link(inode, dir, name),
4530 &exception);
4531 } while (exception.retry);
4532 return err;
4533}
4534
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004535struct nfs4_createdata {
4536 struct rpc_message msg;
4537 struct nfs4_create_arg arg;
4538 struct nfs4_create_res res;
4539 struct nfs_fh fh;
4540 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004541 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004542};
4543
4544static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004545 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004546{
4547 struct nfs4_createdata *data;
4548
4549 data = kzalloc(sizeof(*data), GFP_KERNEL);
4550 if (data != NULL) {
4551 struct nfs_server *server = NFS_SERVER(dir);
4552
David Quigley14c43f72013-05-22 12:50:43 -04004553 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4554 if (IS_ERR(data->label))
4555 goto out_free;
4556
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004557 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4558 data->msg.rpc_argp = &data->arg;
4559 data->msg.rpc_resp = &data->res;
4560 data->arg.dir_fh = NFS_FH(dir);
4561 data->arg.server = server;
4562 data->arg.name = name;
4563 data->arg.attrs = sattr;
4564 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004565 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004566 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004567 data->res.server = server;
4568 data->res.fh = &data->fh;
4569 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004570 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004571 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004572 }
4573 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004574out_free:
4575 kfree(data);
4576 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004577}
4578
4579static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4580{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004581 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004582 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004583 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004584 spin_lock(&dir->i_lock);
4585 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004586 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004587 /* Creating a directory bumps nlink in the parent */
4588 if (data->arg.ftype == NF4DIR)
4589 nfs4_inc_nlink_locked(dir);
4590 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004591 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004592 }
4593 return status;
4594}
4595
4596static void nfs4_free_createdata(struct nfs4_createdata *data)
4597{
David Quigley14c43f72013-05-22 12:50:43 -04004598 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004599 kfree(data);
4600}
4601
Chuck Lever4f390c12006-08-22 20:06:22 -04004602static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004603 struct page *page, unsigned int len, struct iattr *sattr,
4604 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004606 struct nfs4_createdata *data;
4607 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Chuck Lever94a6d752006-08-22 20:06:23 -04004609 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004610 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004611
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004612 status = -ENOMEM;
4613 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4614 if (data == NULL)
4615 goto out;
4616
4617 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4618 data->arg.u.symlink.pages = &page;
4619 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004620 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004622 status = nfs4_do_create(dir, dentry, data);
4623
4624 nfs4_free_createdata(data);
4625out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 return status;
4627}
4628
Chuck Lever4f390c12006-08-22 20:06:22 -04004629static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004630 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631{
4632 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004633 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004635
4636 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004639 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4640 trace_nfs4_symlink(dir, &dentry->d_name, err);
4641 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 &exception);
4643 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004644
4645 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 return err;
4647}
4648
4649static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004650 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004652 struct nfs4_createdata *data;
4653 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004655 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4656 if (data == NULL)
4657 goto out;
4658
David Quigley1775fd32013-05-22 12:50:42 -04004659 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004660 status = nfs4_do_create(dir, dentry, data);
4661
4662 nfs4_free_createdata(data);
4663out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 return status;
4665}
4666
4667static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4668 struct iattr *sattr)
4669{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004670 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004672 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004674
David Quigleyaa9c2662013-05-22 12:50:44 -04004675 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4676
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004677 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4678 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004680 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4681 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4682 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 &exception);
4684 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004685 nfs4_label_release_security(label);
4686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 return err;
4688}
4689
NeilBrown684f39b2018-12-03 11:30:30 +11004690static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004691 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692{
David Howells2b0143b2015-03-17 22:25:59 +00004693 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 struct nfs4_readdir_arg args = {
4695 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004696 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 .pgbase = 0,
4698 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004699 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004700 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 };
4702 struct nfs4_readdir_res res;
4703 struct rpc_message msg = {
4704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4705 .rpc_argp = &args,
4706 .rpc_resp = &res,
4707 .rpc_cred = cred,
4708 };
4709 int status;
4710
Al Viro6de14722013-09-16 10:53:17 -04004711 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4712 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004713 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004714 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004716 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 -05004717 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004718 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004719 status += args.pgbase;
4720 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004721
4722 nfs_invalidate_atime(dir);
4723
Harvey Harrison3110ff82008-05-02 13:42:44 -07004724 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 return status;
4726}
4727
NeilBrown684f39b2018-12-03 11:30:30 +11004728static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004729 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730{
4731 struct nfs4_exception exception = { };
4732 int err;
4733 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004734 err = _nfs4_proc_readdir(dentry, cred, cookie,
4735 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004736 trace_nfs4_readdir(d_inode(dentry), err);
4737 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 &exception);
4739 } while (exception.retry);
4740 return err;
4741}
4742
4743static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004744 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004746 struct nfs4_createdata *data;
4747 int mode = sattr->ia_mode;
4748 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004750 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4751 if (data == NULL)
4752 goto out;
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004755 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004757 data->arg.ftype = NF4BLK;
4758 data->arg.u.device.specdata1 = MAJOR(rdev);
4759 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 }
4761 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004762 data->arg.ftype = NF4CHR;
4763 data->arg.u.device.specdata1 = MAJOR(rdev);
4764 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004765 } else if (!S_ISSOCK(mode)) {
4766 status = -EINVAL;
4767 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 }
David Quigley1775fd32013-05-22 12:50:42 -04004769
David Quigleyaa9c2662013-05-22 12:50:44 -04004770 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004771 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004772out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004773 nfs4_free_createdata(data);
4774out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 return status;
4776}
4777
4778static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4779 struct iattr *sattr, dev_t rdev)
4780{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004781 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004783 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004785
David Quigleyaa9c2662013-05-22 12:50:44 -04004786 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4787
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004788 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4789 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004791 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4792 trace_nfs4_mknod(dir, &dentry->d_name, err);
4793 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 &exception);
4795 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004796
4797 nfs4_label_release_security(label);
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 return err;
4800}
4801
4802static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4803 struct nfs_fsstat *fsstat)
4804{
4805 struct nfs4_statfs_arg args = {
4806 .fh = fhandle,
4807 .bitmask = server->attr_bitmask,
4808 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004809 struct nfs4_statfs_res res = {
4810 .fsstat = fsstat,
4811 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 struct rpc_message msg = {
4813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4814 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004815 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 };
4817
Trond Myklebust0e574af2005-10-27 22:12:38 -04004818 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004819 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820}
4821
4822static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4823{
4824 struct nfs4_exception exception = { };
4825 int err;
4826 do {
4827 err = nfs4_handle_exception(server,
4828 _nfs4_proc_statfs(server, fhandle, fsstat),
4829 &exception);
4830 } while (exception.retry);
4831 return err;
4832}
4833
4834static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4835 struct nfs_fsinfo *fsinfo)
4836{
4837 struct nfs4_fsinfo_arg args = {
4838 .fh = fhandle,
4839 .bitmask = server->attr_bitmask,
4840 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004841 struct nfs4_fsinfo_res res = {
4842 .fsinfo = fsinfo,
4843 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 struct rpc_message msg = {
4845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4846 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004847 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 };
4849
Bryan Schumaker7c513052011-03-24 17:12:24 +00004850 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851}
4852
4853static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4854{
4855 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004856 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 int err;
4858
4859 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004860 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004861 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004862 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004863 nfs4_set_lease_period(server->nfs_client,
4864 fsinfo->lease_time * HZ,
4865 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004866 break;
4867 }
4868 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 } while (exception.retry);
4870 return err;
4871}
4872
4873static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4874{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004875 int error;
4876
Trond Myklebust0e574af2005-10-27 22:12:38 -04004877 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004878 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004879 if (error == 0) {
4880 /* block layout checks this! */
4881 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004882 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004883 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004884
4885 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886}
4887
4888static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4889 struct nfs_pathconf *pathconf)
4890{
4891 struct nfs4_pathconf_arg args = {
4892 .fh = fhandle,
4893 .bitmask = server->attr_bitmask,
4894 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004895 struct nfs4_pathconf_res res = {
4896 .pathconf = pathconf,
4897 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 struct rpc_message msg = {
4899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4900 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004901 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 };
4903
4904 /* None of the pathconf attributes are mandatory to implement */
4905 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4906 memset(pathconf, 0, sizeof(*pathconf));
4907 return 0;
4908 }
4909
Trond Myklebust0e574af2005-10-27 22:12:38 -04004910 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004911 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912}
4913
4914static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4915 struct nfs_pathconf *pathconf)
4916{
4917 struct nfs4_exception exception = { };
4918 int err;
4919
4920 do {
4921 err = nfs4_handle_exception(server,
4922 _nfs4_proc_pathconf(server, fhandle, pathconf),
4923 &exception);
4924 } while (exception.retry);
4925 return err;
4926}
4927
Trond Myklebust5521abf2013-03-16 20:54:34 -04004928int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004929 const struct nfs_open_context *ctx,
4930 const struct nfs_lock_context *l_ctx,
4931 fmode_t fmode)
4932{
NeilBrown17393472016-10-13 15:26:47 +11004933 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004934}
4935EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4936
Trond Myklebust5521abf2013-03-16 20:54:34 -04004937static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4938 const struct nfs_open_context *ctx,
4939 const struct nfs_lock_context *l_ctx,
4940 fmode_t fmode)
4941{
4942 nfs4_stateid current_stateid;
4943
Trond Myklebuste1253be2014-03-05 08:44:23 -05004944 /* If the current stateid represents a lost lock, then exit */
4945 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4946 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004947 return nfs4_stateid_match(stateid, &current_stateid);
4948}
4949
4950static bool nfs4_error_stateid_expired(int err)
4951{
4952 switch (err) {
4953 case -NFS4ERR_DELEG_REVOKED:
4954 case -NFS4ERR_ADMIN_REVOKED:
4955 case -NFS4ERR_BAD_STATEID:
4956 case -NFS4ERR_STALE_STATEID:
4957 case -NFS4ERR_OLD_STATEID:
4958 case -NFS4ERR_OPENMODE:
4959 case -NFS4ERR_EXPIRED:
4960 return true;
4961 }
4962 return false;
4963}
4964
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004965static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004967 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004969 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004970 if (task->tk_status < 0) {
4971 struct nfs4_exception exception = {
4972 .inode = hdr->inode,
4973 .state = hdr->args.context->state,
4974 .stateid = &hdr->args.stateid,
4975 };
4976 task->tk_status = nfs4_async_handle_exception(task,
4977 server, task->tk_status, &exception);
4978 if (exception.retry) {
4979 rpc_restart_call_prepare(task);
4980 return -EAGAIN;
4981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004985 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987}
4988
Trond Myklebust5521abf2013-03-16 20:54:34 -04004989static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004990 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004991{
4992
4993 if (!nfs4_error_stateid_expired(task->tk_status) ||
4994 nfs4_stateid_is_current(&args->stateid,
4995 args->context,
4996 args->lock_context,
4997 FMODE_READ))
4998 return false;
4999 rpc_restart_call_prepare(task);
5000 return true;
5001}
5002
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005003static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005004{
5005
5006 dprintk("--> %s\n", __func__);
5007
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005008 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005009 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005010 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005011 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005012 if (task->tk_status > 0)
5013 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005014 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5015 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005016}
5017
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005018static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5019 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005021 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005022 if (!hdr->pgio_done_cb)
5023 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005024 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005025 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026}
5027
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005028static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5029 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005030{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005031 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005032 &hdr->args.seq_args,
5033 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005034 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005035 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005036 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5037 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005038 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005039 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005040 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005041 return -EIO;
5042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043}
5044
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005045static int nfs4_write_done_cb(struct rpc_task *task,
5046 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005048 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005049
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005050 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005051 if (task->tk_status < 0) {
5052 struct nfs4_exception exception = {
5053 .inode = hdr->inode,
5054 .state = hdr->args.context->state,
5055 .stateid = &hdr->args.stateid,
5056 };
5057 task->tk_status = nfs4_async_handle_exception(task,
5058 NFS_SERVER(inode), task->tk_status,
5059 &exception);
5060 if (exception.retry) {
5061 rpc_restart_call_prepare(task);
5062 return -EAGAIN;
5063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005065 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005066 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005067 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005068 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070}
5071
Trond Myklebust5521abf2013-03-16 20:54:34 -04005072static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005073 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005074{
5075
5076 if (!nfs4_error_stateid_expired(task->tk_status) ||
5077 nfs4_stateid_is_current(&args->stateid,
5078 args->context,
5079 args->lock_context,
5080 FMODE_WRITE))
5081 return false;
5082 rpc_restart_call_prepare(task);
5083 return true;
5084}
5085
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005086static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005087{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005088 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005089 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005090 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005091 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005092 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5093 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005094}
5095
Trond Myklebust5a37f852012-04-28 14:55:16 -04005096static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005097bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005098{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005099 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005100 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005101 return false;
5102 /* Otherwise, request attributes if and only if we don't hold
5103 * a delegation
5104 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005105 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005106}
Fred Isamana69aef12011-03-03 15:13:47 +00005107
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005108static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005109 struct rpc_message *msg,
5110 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005112 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005113
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005114 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5115 hdr->args.bitmask = NULL;
5116 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005117 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005118 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005119
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005120 if (!hdr->pgio_done_cb)
5121 hdr->pgio_done_cb = nfs4_write_done_cb;
5122 hdr->res.server = server;
5123 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005125 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005126 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005127 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128}
5129
Fred Isaman0b7c0152012-04-20 14:47:39 -04005130static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5131{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005132 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005133 &data->args.seq_args,
5134 &data->res.seq_res,
5135 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136}
5137
Fred Isaman0b7c0152012-04-20 14:47:39 -04005138static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005141
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005142 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005143 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5144 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005145 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005146 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149}
5150
Fred Isaman0b7c0152012-04-20 14:47:39 -04005151static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005152{
5153 if (!nfs4_sequence_done(task, &data->res.seq_res))
5154 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005155 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005156}
5157
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005158static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5159 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005161 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005162
Fred Isaman0b7c0152012-04-20 14:47:39 -04005163 if (data->commit_done_cb == NULL)
5164 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005165 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005166 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005167 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005168 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169}
5170
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005171static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5172 struct nfs_commitres *res)
5173{
5174 struct inode *dst_inode = file_inode(dst);
5175 struct nfs_server *server = NFS_SERVER(dst_inode);
5176 struct rpc_message msg = {
5177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5178 .rpc_argp = args,
5179 .rpc_resp = res,
5180 };
5181
5182 args->fh = NFS_FH(dst_inode);
5183 return nfs4_call_sync(server->client, server, &msg,
5184 &args->seq_args, &res->seq_res, 1);
5185}
5186
5187int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5188{
5189 struct nfs_commitargs args = {
5190 .offset = offset,
5191 .count = count,
5192 };
5193 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5194 struct nfs4_exception exception = { };
5195 int status;
5196
5197 do {
5198 status = _nfs4_proc_commit(dst, &args, res);
5199 status = nfs4_handle_exception(dst_server, status, &exception);
5200 } while (exception.retry);
5201
5202 return status;
5203}
5204
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005205struct nfs4_renewdata {
5206 struct nfs_client *client;
5207 unsigned long timestamp;
5208};
5209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210/*
5211 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5212 * standalone procedure for queueing an asynchronous RENEW.
5213 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005214static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005215{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005216 struct nfs4_renewdata *data = calldata;
5217 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005218
Elena Reshetova212bf412017-10-20 12:53:38 +03005219 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005220 nfs4_schedule_state_renewal(clp);
5221 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005222 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005223}
5224
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005225static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005227 struct nfs4_renewdata *data = calldata;
5228 struct nfs_client *clp = data->client;
5229 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005231 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005232 switch (task->tk_status) {
5233 case 0:
5234 break;
5235 case -NFS4ERR_LEASE_MOVED:
5236 nfs4_schedule_lease_moved_recovery(clp);
5237 break;
5238 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005239 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005240 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5241 return;
5242 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005243 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005244 return;
5245 }
5246 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005248 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249}
5250
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005251static const struct rpc_call_ops nfs4_renew_ops = {
5252 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005253 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005254};
5255
NeilBrowna52458b2018-12-03 11:30:31 +11005256static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
5258 struct rpc_message msg = {
5259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5260 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005261 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005263 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005265 if (renew_flags == 0)
5266 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005267 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005268 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005269 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005270 if (data == NULL) {
5271 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005272 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005273 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005274 data->client = clp;
5275 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005276 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005277 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278}
5279
NeilBrowna52458b2018-12-03 11:30:31 +11005280static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281{
5282 struct rpc_message msg = {
5283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5284 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005285 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 };
5287 unsigned long now = jiffies;
5288 int status;
5289
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005290 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 if (status < 0)
5292 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005293 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 return 0;
5295}
5296
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005297static inline int nfs4_server_supports_acls(struct nfs_server *server)
5298{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005299 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005300}
5301
Trond Myklebust21f498c2012-08-24 10:59:25 -04005302/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5303 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005304 * the stack.
5305 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005306#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005307
Neil Hormane9e3d722011-03-04 19:26:03 -05005308static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005309 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005310{
5311 struct page *newpage, **spages;
5312 int rc = 0;
5313 size_t len;
5314 spages = pages;
5315
5316 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005317 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005318 newpage = alloc_page(GFP_KERNEL);
5319
5320 if (newpage == NULL)
5321 goto unwind;
5322 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005323 buf += len;
5324 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005325 *pages++ = newpage;
5326 rc++;
5327 } while (buflen != 0);
5328
5329 return rc;
5330
5331unwind:
5332 for(; rc > 0; rc--)
5333 __free_page(spages[rc-1]);
5334 return -ENOMEM;
5335}
5336
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005337struct nfs4_cached_acl {
5338 int cached;
5339 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005340 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005341};
5342
5343static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005344{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005345 struct nfs_inode *nfsi = NFS_I(inode);
5346
5347 spin_lock(&inode->i_lock);
5348 kfree(nfsi->nfs4_acl);
5349 nfsi->nfs4_acl = acl;
5350 spin_unlock(&inode->i_lock);
5351}
5352
5353static void nfs4_zap_acl_attr(struct inode *inode)
5354{
5355 nfs4_set_cached_acl(inode, NULL);
5356}
5357
5358static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5359{
5360 struct nfs_inode *nfsi = NFS_I(inode);
5361 struct nfs4_cached_acl *acl;
5362 int ret = -ENOENT;
5363
5364 spin_lock(&inode->i_lock);
5365 acl = nfsi->nfs4_acl;
5366 if (acl == NULL)
5367 goto out;
5368 if (buf == NULL) /* user is just asking for length */
5369 goto out_len;
5370 if (acl->cached == 0)
5371 goto out;
5372 ret = -ERANGE; /* see getxattr(2) man page */
5373 if (acl->len > buflen)
5374 goto out;
5375 memcpy(buf, acl->data, acl->len);
5376out_len:
5377 ret = acl->len;
5378out:
5379 spin_unlock(&inode->i_lock);
5380 return ret;
5381}
5382
Sachin Prabhu5794d212012-04-17 14:36:40 +01005383static 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 +00005384{
5385 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005386 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005387
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005388 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005389 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005390 if (acl == NULL)
5391 goto out;
5392 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005393 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005394 } else {
5395 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5396 if (acl == NULL)
5397 goto out;
5398 acl->cached = 0;
5399 }
5400 acl->len = acl_len;
5401out:
5402 nfs4_set_cached_acl(inode, acl);
5403}
5404
Andy Adamsonbf118a32011-12-07 11:55:27 -05005405/*
5406 * The getxattr API returns the required buffer length when called with a
5407 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5408 * the required buf. On a NULL buf, we send a page of data to the server
5409 * guessing that the ACL request can be serviced by a page. If so, we cache
5410 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5411 * the cache. If not so, we throw away the page, and cache the required
5412 * length. The next getxattr call will then produce another round trip to
5413 * the server, this time with the input buf of the required size.
5414 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005415static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005416{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005417 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005418 struct nfs_getaclargs args = {
5419 .fh = NFS_FH(inode),
5420 .acl_pages = pages,
5421 .acl_len = buflen,
5422 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005423 struct nfs_getaclres res = {
5424 .acl_len = buflen,
5425 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005426 struct rpc_message msg = {
5427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5428 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005429 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005430 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005431 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005432 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005433
Trond Myklebust21f498c2012-08-24 10:59:25 -04005434 if (npages > ARRAY_SIZE(pages))
5435 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005436
Andy Adamsonbf118a32011-12-07 11:55:27 -05005437 for (i = 0; i < npages; i++) {
5438 pages[i] = alloc_page(GFP_KERNEL);
5439 if (!pages[i])
5440 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005441 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005442
5443 /* for decoding across pages */
5444 res.acl_scratch = alloc_page(GFP_KERNEL);
5445 if (!res.acl_scratch)
5446 goto out_free;
5447
Andy Adamsonbf118a32011-12-07 11:55:27 -05005448 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005449
Peng Taode040be2012-01-10 22:42:47 +08005450 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005451 __func__, buf, buflen, npages, args.acl_len);
5452 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5453 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005454 if (ret)
5455 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005456
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005457 /* Handle the case where the passed-in buffer is too short */
5458 if (res.acl_flags & NFS4_ACL_TRUNC) {
5459 /* Did the user only issue a request for the acl length? */
5460 if (buf == NULL)
5461 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005462 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005463 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005464 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005465 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005466 if (buf) {
5467 if (res.acl_len > buflen) {
5468 ret = -ERANGE;
5469 goto out_free;
5470 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005471 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005472 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005473out_ok:
5474 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005475out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005476 for (i = 0; i < npages; i++)
5477 if (pages[i])
5478 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005479 if (res.acl_scratch)
5480 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005481 return ret;
5482}
5483
Trond Myklebust16b42892006-08-24 12:27:15 -04005484static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5485{
5486 struct nfs4_exception exception = { };
5487 ssize_t ret;
5488 do {
5489 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005490 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005491 if (ret >= 0)
5492 break;
5493 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5494 } while (exception.retry);
5495 return ret;
5496}
5497
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005498static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5499{
5500 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005501 int ret;
5502
5503 if (!nfs4_server_supports_acls(server))
5504 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005505 ret = nfs_revalidate_inode(server, inode);
5506 if (ret < 0)
5507 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005508 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5509 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005510 ret = nfs4_read_cached_acl(inode, buf, buflen);
5511 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005512 /* -ENOENT is returned if there is no ACL or if there is an ACL
5513 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005514 return ret;
5515 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005516}
5517
Trond Myklebust16b42892006-08-24 12:27:15 -04005518static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005519{
5520 struct nfs_server *server = NFS_SERVER(inode);
5521 struct page *pages[NFS4ACL_MAXPAGES];
5522 struct nfs_setaclargs arg = {
5523 .fh = NFS_FH(inode),
5524 .acl_pages = pages,
5525 .acl_len = buflen,
5526 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005527 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005528 struct rpc_message msg = {
5529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5530 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005531 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005532 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005533 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005534 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005535
5536 if (!nfs4_server_supports_acls(server))
5537 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005538 if (npages > ARRAY_SIZE(pages))
5539 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005540 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005541 if (i < 0)
5542 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005543 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005544 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005545
5546 /*
5547 * Free each page after tx, so the only ref left is
5548 * held by the network stack
5549 */
5550 for (; i > 0; i--)
5551 put_page(pages[i-1]);
5552
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005553 /*
5554 * Acl update can result in inode attribute update.
5555 * so mark the attribute cache invalid.
5556 */
5557 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005558 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005559 | NFS_INO_INVALID_CTIME
5560 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005561 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005562 nfs_access_zap_cache(inode);
5563 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005564 return ret;
5565}
5566
Trond Myklebust16b42892006-08-24 12:27:15 -04005567static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5568{
5569 struct nfs4_exception exception = { };
5570 int err;
5571 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005572 err = __nfs4_proc_set_acl(inode, buf, buflen);
5573 trace_nfs4_set_acl(inode, err);
5574 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005575 &exception);
5576 } while (exception.retry);
5577 return err;
5578}
5579
David Quigleyaa9c2662013-05-22 12:50:44 -04005580#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5581static int _nfs4_get_security_label(struct inode *inode, void *buf,
5582 size_t buflen)
5583{
5584 struct nfs_server *server = NFS_SERVER(inode);
5585 struct nfs_fattr fattr;
5586 struct nfs4_label label = {0, 0, buflen, buf};
5587
5588 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005589 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005590 .fh = NFS_FH(inode),
5591 .bitmask = bitmask,
5592 };
5593 struct nfs4_getattr_res res = {
5594 .fattr = &fattr,
5595 .label = &label,
5596 .server = server,
5597 };
5598 struct rpc_message msg = {
5599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005600 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005601 .rpc_resp = &res,
5602 };
5603 int ret;
5604
5605 nfs_fattr_init(&fattr);
5606
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005607 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005608 if (ret)
5609 return ret;
5610 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5611 return -ENOENT;
5612 if (buflen < label.len)
5613 return -ERANGE;
5614 return 0;
5615}
5616
5617static int nfs4_get_security_label(struct inode *inode, void *buf,
5618 size_t buflen)
5619{
5620 struct nfs4_exception exception = { };
5621 int err;
5622
5623 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5624 return -EOPNOTSUPP;
5625
5626 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005627 err = _nfs4_get_security_label(inode, buf, buflen);
5628 trace_nfs4_get_security_label(inode, err);
5629 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005630 &exception);
5631 } while (exception.retry);
5632 return err;
5633}
5634
5635static int _nfs4_do_set_security_label(struct inode *inode,
5636 struct nfs4_label *ilabel,
5637 struct nfs_fattr *fattr,
5638 struct nfs4_label *olabel)
5639{
5640
5641 struct iattr sattr = {0};
5642 struct nfs_server *server = NFS_SERVER(inode);
5643 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005644 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005645 .fh = NFS_FH(inode),
5646 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005647 .server = server,
5648 .bitmask = bitmask,
5649 .label = ilabel,
5650 };
5651 struct nfs_setattrres res = {
5652 .fattr = fattr,
5653 .label = olabel,
5654 .server = server,
5655 };
5656 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5658 .rpc_argp = &arg,
5659 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005660 };
5661 int status;
5662
Jeff Layton12207f62013-11-01 10:49:32 -04005663 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005664
Jeff Layton12207f62013-11-01 10:49:32 -04005665 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005666 if (status)
5667 dprintk("%s failed: %d\n", __func__, status);
5668
5669 return status;
5670}
5671
5672static int nfs4_do_set_security_label(struct inode *inode,
5673 struct nfs4_label *ilabel,
5674 struct nfs_fattr *fattr,
5675 struct nfs4_label *olabel)
5676{
5677 struct nfs4_exception exception = { };
5678 int err;
5679
5680 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005681 err = _nfs4_do_set_security_label(inode, ilabel,
5682 fattr, olabel);
5683 trace_nfs4_set_security_label(inode, err);
5684 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005685 &exception);
5686 } while (exception.retry);
5687 return err;
5688}
5689
5690static int
Al Viro59301222016-05-27 10:19:30 -04005691nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005692{
5693 struct nfs4_label ilabel, *olabel = NULL;
5694 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005695 int status;
5696
5697 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5698 return -EOPNOTSUPP;
5699
5700 nfs_fattr_init(&fattr);
5701
5702 ilabel.pi = 0;
5703 ilabel.lfs = 0;
5704 ilabel.label = (char *)buf;
5705 ilabel.len = buflen;
5706
David Quigleyaa9c2662013-05-22 12:50:44 -04005707 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5708 if (IS_ERR(olabel)) {
5709 status = -PTR_ERR(olabel);
5710 goto out;
5711 }
5712
5713 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5714 if (status == 0)
5715 nfs_setsecurity(inode, &fattr, olabel);
5716
5717 nfs4_label_free(olabel);
5718out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005719 return status;
5720}
5721#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5722
5723
Chuck Leverf0920752012-05-21 22:45:41 -04005724static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5725 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005726{
5727 __be32 verf[2];
5728
Chuck Lever2c820d92012-05-21 22:45:33 -04005729 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5730 /* An impossible timestamp guarantees this value
5731 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005732 verf[0] = cpu_to_be32(U32_MAX);
5733 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005734 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005735 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005736 u64 ns = ktime_to_ns(nn->boot_time);
5737
5738 verf[0] = cpu_to_be32(ns >> 32);
5739 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005740 }
Chuck Levercd937102012-03-02 17:14:31 -05005741 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5742}
5743
Jeff Laytona3192682015-06-09 19:43:59 -04005744static int
5745nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005746{
Jeff Laytona3192682015-06-09 19:43:59 -04005747 size_t len;
5748 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005749
Trond Myklebustceb3a162015-01-03 15:16:04 -05005750 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005751 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005752
Jeff Laytona3192682015-06-09 19:43:59 -04005753 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005754 len = 14 +
5755 strlen(clp->cl_rpcclient->cl_nodename) +
5756 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005757 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005758 1;
5759 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005760 if (nfs4_client_id_uniquifier[0] != '\0')
5761 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005762 if (len > NFS4_OPAQUE_LIMIT + 1)
5763 return -EINVAL;
5764
5765 /*
5766 * Since this string is allocated at mount time, and held until the
5767 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5768 * about a memory-reclaim deadlock.
5769 */
5770 str = kmalloc(len, GFP_KERNEL);
5771 if (!str)
5772 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005773
Chuck Levere984a552012-09-14 17:24:21 -04005774 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005775 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005776 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005777 clp->cl_rpcclient->cl_nodename,
5778 nfs4_client_id_uniquifier,
5779 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005780 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005781 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005782 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005783 clp->cl_rpcclient->cl_nodename,
5784 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005785 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005786 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005787
Jeff Laytona3192682015-06-09 19:43:59 -04005788 clp->cl_owner_id = str;
5789 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005790}
5791
Jeff Layton873e3852015-06-09 19:44:00 -04005792static int
5793nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005794{
Jeff Layton873e3852015-06-09 19:44:00 -04005795 size_t len;
5796 char *str;
5797
5798 len = 10 + 10 + 1 + 10 + 1 +
5799 strlen(nfs4_client_id_uniquifier) + 1 +
5800 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5801
5802 if (len > NFS4_OPAQUE_LIMIT + 1)
5803 return -EINVAL;
5804
5805 /*
5806 * Since this string is allocated at mount time, and held until the
5807 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5808 * about a memory-reclaim deadlock.
5809 */
5810 str = kmalloc(len, GFP_KERNEL);
5811 if (!str)
5812 return -ENOMEM;
5813
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005814 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005815 clp->rpc_ops->version, clp->cl_minorversion,
5816 nfs4_client_id_uniquifier,
5817 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005818 clp->cl_owner_id = str;
5819 return 0;
5820}
5821
5822static int
5823nfs4_init_uniform_client_string(struct nfs_client *clp)
5824{
Jeff Layton873e3852015-06-09 19:44:00 -04005825 size_t len;
5826 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005827
5828 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005829 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005830
5831 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005832 return nfs4_init_uniquifier_client_string(clp);
5833
5834 len = 10 + 10 + 1 + 10 + 1 +
5835 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5836
5837 if (len > NFS4_OPAQUE_LIMIT + 1)
5838 return -EINVAL;
5839
5840 /*
5841 * Since this string is allocated at mount time, and held until the
5842 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5843 * about a memory-reclaim deadlock.
5844 */
5845 str = kmalloc(len, GFP_KERNEL);
5846 if (!str)
5847 return -ENOMEM;
5848
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005849 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005850 clp->rpc_ops->version, clp->cl_minorversion,
5851 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005852 clp->cl_owner_id = str;
5853 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005854}
5855
Chuck Lever706cb8d2014-03-12 12:51:47 -04005856/*
5857 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5858 * services. Advertise one based on the address family of the
5859 * clientaddr.
5860 */
5861static unsigned int
5862nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5863{
5864 if (strchr(clp->cl_ipaddr, ':') != NULL)
5865 return scnprintf(buf, len, "tcp6");
5866 else
5867 return scnprintf(buf, len, "tcp");
5868}
5869
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005870static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5871{
5872 struct nfs4_setclientid *sc = calldata;
5873
5874 if (task->tk_status == 0)
5875 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5876}
5877
5878static const struct rpc_call_ops nfs4_setclientid_ops = {
5879 .rpc_call_done = nfs4_setclientid_done,
5880};
5881
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005882/**
5883 * nfs4_proc_setclientid - Negotiate client ID
5884 * @clp: state data structure
5885 * @program: RPC program for NFSv4 callback service
5886 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11005887 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005888 * @res: where to place the result
5889 *
5890 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5891 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005892int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11005893 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005894 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895{
5896 nfs4_verifier sc_verifier;
5897 struct nfs4_setclientid setclientid = {
5898 .sc_verifier = &sc_verifier,
5899 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005900 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 };
5902 struct rpc_message msg = {
5903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5904 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005905 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005906 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005908 struct rpc_task *task;
5909 struct rpc_task_setup task_setup_data = {
5910 .rpc_client = clp->cl_rpcclient,
5911 .rpc_message = &msg,
5912 .callback_ops = &nfs4_setclientid_ops,
5913 .callback_data = &setclientid,
5914 .flags = RPC_TASK_TIMEOUT,
5915 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005916 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
Chuck Leverde734832012-07-11 16:30:50 -04005918 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005919 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005920
5921 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5922 status = nfs4_init_uniform_client_string(clp);
5923 else
Jeff Laytona3192682015-06-09 19:43:59 -04005924 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005925
5926 if (status)
5927 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005928
Chuck Leverde734832012-07-11 16:30:50 -04005929 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005930 setclientid.sc_netid_len =
5931 nfs4_init_callback_netid(clp,
5932 setclientid.sc_netid,
5933 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005934 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005935 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 clp->cl_ipaddr, port >> 8, port & 255);
5937
Jeff Layton3a6bb732015-06-09 19:43:57 -04005938 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005939 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005940 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005941 task = rpc_run_task(&task_setup_data);
5942 if (IS_ERR(task)) {
5943 status = PTR_ERR(task);
5944 goto out;
5945 }
5946 status = task->tk_status;
5947 if (setclientid.sc_cred) {
5948 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5949 put_rpccred(setclientid.sc_cred);
5950 }
5951 rpc_put_task(task);
5952out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005953 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005954 dprintk("NFS reply setclientid: %d\n", status);
5955 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956}
5957
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005958/**
5959 * nfs4_proc_setclientid_confirm - Confirm client ID
5960 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05005961 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11005962 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005963 *
5964 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5965 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005966int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005967 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11005968 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 struct rpc_message msg = {
5971 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005972 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005973 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 int status;
5976
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005977 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5978 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5979 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005980 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005981 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005982 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 return status;
5984}
5985
Trond Myklebustfe650402006-01-03 09:55:18 +01005986struct nfs4_delegreturndata {
5987 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005988 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005989 struct nfs_fh fh;
5990 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005991 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005992 struct {
5993 struct nfs4_layoutreturn_args arg;
5994 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005995 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005996 u32 roc_barrier;
5997 bool roc;
5998 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005999 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006000 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006001 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006002};
6003
Trond Myklebustfe650402006-01-03 09:55:18 +01006004static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6005{
6006 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006007 struct nfs4_exception exception = {
6008 .inode = data->inode,
6009 .stateid = &data->stateid,
6010 };
Andy Adamson938e1012009-04-01 09:22:28 -04006011
Trond Myklebust14516c32010-07-31 14:29:06 -04006012 if (!nfs4_sequence_done(task, &data->res.seq_res))
6013 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006014
Trond Myklebustca8acf82013-08-13 10:36:56 -04006015 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006016
6017 /* Handle Layoutreturn errors */
6018 if (data->args.lr_args && task->tk_status != 0) {
6019 switch(data->res.lr_ret) {
6020 default:
6021 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6022 break;
6023 case 0:
6024 data->args.lr_args = NULL;
6025 data->res.lr_res = NULL;
6026 break;
Trond Myklebust73800202017-11-06 15:28:07 -05006027 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04006028 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04006029 &data->args.lr_args->range,
Trond Myklebust140087fd2017-11-06 15:28:10 -05006030 data->inode))
6031 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05006032 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05006033 case -NFS4ERR_ADMIN_REVOKED:
6034 case -NFS4ERR_DELEG_REVOKED:
6035 case -NFS4ERR_EXPIRED:
6036 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05006037 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6038 case -NFS4ERR_WRONG_CRED:
6039 data->args.lr_args = NULL;
6040 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006041 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006042 }
6043 }
6044
Ricardo Labiaga79708862009-12-07 09:23:21 -05006045 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006046 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006047 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006048 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006049 case -NFS4ERR_ADMIN_REVOKED:
6050 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006051 case -NFS4ERR_EXPIRED:
6052 nfs4_free_revoked_stateid(data->res.server,
6053 data->args.stateid,
6054 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006055 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006056 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006057 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006058 task->tk_status = 0;
6059 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006060 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05006061 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6062 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006063 task->tk_status = 0;
6064 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006065 case -NFS4ERR_ACCESS:
6066 if (data->args.bitmask) {
6067 data->args.bitmask = NULL;
6068 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006069 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006070 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006071 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006072 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006073 task->tk_status = nfs4_async_handle_exception(task,
6074 data->res.server, task->tk_status,
6075 &exception);
6076 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006077 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006078 }
6079 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006080 return;
6081lr_restart:
6082 data->res.lr_ret = 0;
6083out_restart:
6084 task->tk_status = 0;
6085 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006086}
6087
6088static void nfs4_delegreturn_release(void *calldata)
6089{
Peng Tao039b7562014-07-03 13:05:02 +08006090 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006091 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006092
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006093 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006094 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006095 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6096 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006097 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006098 nfs_iput_and_deactive(inode);
6099 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006100 kfree(calldata);
6101}
6102
Andy Adamson938e1012009-04-01 09:22:28 -04006103static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6104{
6105 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006106 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006107
6108 d_data = (struct nfs4_delegreturndata *)data;
6109
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006110 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08006111 return;
6112
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006113 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6114 if (lo && !pnfs_layout_is_valid(lo)) {
6115 d_data->args.lr_args = NULL;
6116 d_data->res.lr_res = NULL;
6117 }
6118
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006119 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006120 &d_data->args.seq_args,
6121 &d_data->res.seq_res,
6122 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006123}
Andy Adamson938e1012009-04-01 09:22:28 -04006124
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006125static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006126 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006127 .rpc_call_done = nfs4_delegreturn_done,
6128 .rpc_release = nfs4_delegreturn_release,
6129};
6130
NeilBrowna52458b2018-12-03 11:30:31 +11006131static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006132{
6133 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006134 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006135 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006136 struct rpc_message msg = {
6137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6138 .rpc_cred = cred,
6139 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006140 struct rpc_task_setup task_setup_data = {
6141 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006142 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006143 .callback_ops = &nfs4_delegreturn_ops,
6144 .flags = RPC_TASK_ASYNC,
6145 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006146 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006147
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006148 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006149 if (data == NULL)
6150 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006151 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006152
6153 nfs4_state_protect(server->nfs_client,
6154 NFS_SP4_MACH_CRED_CLEANUP,
6155 &task_setup_data.rpc_client, &msg);
6156
Trond Myklebustfe650402006-01-03 09:55:18 +01006157 data->args.fhandle = &data->fh;
6158 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006159 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006160 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006161 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006162 data->res.fattr = &data->fattr;
6163 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006164 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006165 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006166 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006167 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006168 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006169 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006170 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006171 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006172 if (data->lr.roc) {
6173 data->args.lr_args = &data->lr.arg;
6174 data->res.lr_res = &data->lr.res;
6175 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006176 } else if (data->lr.roc) {
6177 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6178 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006179 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006180
Trond Myklebustc970aa82007-07-14 15:39:59 -04006181 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006182 msg.rpc_argp = &data->args;
6183 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006184 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006185 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006186 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006187 if (!issync)
6188 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006189 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006190 if (status != 0)
6191 goto out;
6192 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006193out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006194 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006195 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196}
6197
NeilBrowna52458b2018-12-03 11:30:31 +11006198int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199{
6200 struct nfs_server *server = NFS_SERVER(inode);
6201 struct nfs4_exception exception = { };
6202 int err;
6203 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006204 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006205 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 switch (err) {
6207 case -NFS4ERR_STALE_STATEID:
6208 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 case 0:
6210 return 0;
6211 }
6212 err = nfs4_handle_exception(server, err, &exception);
6213 } while (exception.retry);
6214 return err;
6215}
6216
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6218{
6219 struct inode *inode = state->inode;
6220 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006221 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006222 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006224 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006226 struct nfs_lockt_res res = {
6227 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 };
6229 struct rpc_message msg = {
6230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006231 .rpc_argp = &arg,
6232 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 .rpc_cred = state->owner->so_cred,
6234 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 struct nfs4_lock_state *lsp;
6236 int status;
6237
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006238 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006239 status = nfs4_set_lock_state(state, request);
6240 if (status != 0)
6241 goto out;
6242 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006243 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006244 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006245 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006246 switch (status) {
6247 case 0:
6248 request->fl_type = F_UNLCK;
6249 break;
6250 case -NFS4ERR_DENIED:
6251 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006253 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006254 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006255out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 return status;
6257}
6258
6259static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6260{
6261 struct nfs4_exception exception = { };
6262 int err;
6263
6264 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006265 err = _nfs4_proc_getlk(state, cmd, request);
6266 trace_nfs4_get_lock(request, state, cmd, err);
6267 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 &exception);
6269 } while (exception.retry);
6270 return err;
6271}
6272
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006273struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006274 struct nfs_locku_args arg;
6275 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006276 struct nfs4_lock_state *lsp;
6277 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006278 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006279 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006280 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006281 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006282};
6283
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006284static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6285 struct nfs_open_context *ctx,
6286 struct nfs4_lock_state *lsp,
6287 struct nfs_seqid *seqid)
6288{
6289 struct nfs4_unlockdata *p;
6290 struct inode *inode = lsp->ls_state->inode;
6291
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006292 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006293 if (p == NULL)
6294 return NULL;
6295 p->arg.fh = NFS_FH(inode);
6296 p->arg.fl = &p->fl;
6297 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006298 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006299 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006300 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006301 /* Ensure we don't close file until we're done freeing locks! */
6302 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006303 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006304 locks_init_lock(&p->fl);
6305 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006306 p->server = NFS_SERVER(inode);
6307 return p;
6308}
6309
Trond Myklebust06f814a2006-01-03 09:55:07 +01006310static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006311{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006312 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006313 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006314 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006315 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006316 put_nfs_open_context(calldata->ctx);
6317 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006318}
6319
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006320static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006321{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006322 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006323 struct nfs4_exception exception = {
6324 .inode = calldata->lsp->ls_state->inode,
6325 .stateid = &calldata->arg.stateid,
6326 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006327
Trond Myklebust14516c32010-07-31 14:29:06 -04006328 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6329 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006330 switch (task->tk_status) {
6331 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006332 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006333 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006334 if (nfs4_update_lock_stateid(calldata->lsp,
6335 &calldata->res.stateid))
6336 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006337 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006338 case -NFS4ERR_ADMIN_REVOKED:
6339 case -NFS4ERR_EXPIRED:
6340 nfs4_free_revoked_stateid(calldata->server,
6341 &calldata->arg.stateid,
6342 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006343 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006344 case -NFS4ERR_BAD_STATEID:
6345 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006346 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006347 if (!nfs4_stateid_match(&calldata->arg.stateid,
6348 &calldata->lsp->ls_stateid))
6349 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006350 break;
6351 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006352 task->tk_status = nfs4_async_handle_exception(task,
6353 calldata->server, task->tk_status,
6354 &exception);
6355 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006356 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006357 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006358 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006359}
6360
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006361static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006362{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006363 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006365 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6366 nfs_async_iocounter_wait(task, calldata->l_ctx))
6367 return;
6368
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006369 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006370 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006371 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006372 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006373 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006374 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006375 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006376 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006377 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006378 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006379 &calldata->res.seq_res,
6380 task) != 0)
6381 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006382 return;
6383out_no_action:
6384 task->tk_action = NULL;
6385out_wait:
6386 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387}
6388
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006389static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006390 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006391 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006392 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006393};
6394
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006395static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6396 struct nfs_open_context *ctx,
6397 struct nfs4_lock_state *lsp,
6398 struct nfs_seqid *seqid)
6399{
6400 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006401 struct rpc_message msg = {
6402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6403 .rpc_cred = ctx->cred,
6404 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006405 struct rpc_task_setup task_setup_data = {
6406 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006407 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006408 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006409 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006410 .flags = RPC_TASK_ASYNC,
6411 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006412
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006413 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6414 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6415
Frank Filz137d6ac2007-07-09 15:32:29 -07006416 /* Ensure this is an unlock - when canceling a lock, the
6417 * canceled lock is passed in, and it won't be an unlock.
6418 */
6419 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006420 if (fl->fl_flags & FL_CLOSE)
6421 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006422
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006423 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6424 if (data == NULL) {
6425 nfs_free_seqid(seqid);
6426 return ERR_PTR(-ENOMEM);
6427 }
6428
Anna Schumakerfba83f32018-05-04 16:22:50 -04006429 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006430 msg.rpc_argp = &data->arg;
6431 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006432 task_setup_data.callback_data = data;
6433 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006434}
6435
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6437{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006438 struct inode *inode = state->inode;
6439 struct nfs4_state_owner *sp = state->owner;
6440 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006441 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006442 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006443 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006444 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006445 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006446 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
Trond Myklebust9b073572006-06-29 16:38:34 -04006448 status = nfs4_set_lock_state(state, request);
6449 /* Unlock _before_ we do the RPC call */
6450 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006451 /* Exclude nfs_delegation_claim_locks() */
6452 mutex_lock(&sp->so_delegreturn_mutex);
6453 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006454 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006455 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006456 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006457 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006458 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006459 }
6460 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 if (status != 0)
6463 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006464 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006465 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006466 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6467 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006468 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6469 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006470 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006471 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006472 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006473 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006474 status = PTR_ERR(task);
6475 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006476 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006477 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006478 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006479out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006480 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006481 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006482 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483}
6484
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006485struct nfs4_lockdata {
6486 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006487 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006488 struct nfs4_lock_state *lsp;
6489 struct nfs_open_context *ctx;
6490 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006491 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006492 int rpc_status;
6493 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006494 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006495};
6496
6497static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006498 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6499 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006500{
6501 struct nfs4_lockdata *p;
6502 struct inode *inode = lsp->ls_state->inode;
6503 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006504 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006505
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006506 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006507 if (p == NULL)
6508 return NULL;
6509
6510 p->arg.fh = NFS_FH(inode);
6511 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006512 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006513 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006514 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006515 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6516 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006517 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006518 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006519 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006520 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006521 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006522 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006523 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006524 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006525 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006526 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006527 locks_init_lock(&p->fl);
6528 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006529 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006530out_free_seqid:
6531 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006532out_free:
6533 kfree(p);
6534 return NULL;
6535}
6536
6537static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6538{
6539 struct nfs4_lockdata *data = calldata;
6540 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541
Harvey Harrison3110ff82008-05-02 13:42:44 -07006542 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006543 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006544 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006545 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006546 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006547 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006548 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006549 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006550 nfs4_stateid_copy(&data->arg.open_stateid,
6551 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006552 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006553 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006554 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006555 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006556 nfs4_stateid_copy(&data->arg.lock_stateid,
6557 &data->lsp->ls_stateid);
6558 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006559 if (!nfs4_valid_open_stateid(state)) {
6560 data->rpc_status = -EBADF;
6561 task->tk_action = NULL;
6562 goto out_release_open_seqid;
6563 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006564 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006565 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006566 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006567 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006568 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006569 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006570out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006571 nfs_release_seqid(data->arg.open_seqid);
6572out_release_lock_seqid:
6573 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006574out_wait:
6575 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006576 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006577}
6578
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006579static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6580{
6581 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006582 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006583
Harvey Harrison3110ff82008-05-02 13:42:44 -07006584 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006585
Trond Myklebust14516c32010-07-31 14:29:06 -04006586 if (!nfs4_sequence_done(task, &data->res.seq_res))
6587 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006588
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006589 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006590 switch (task->tk_status) {
6591 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006592 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006593 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006594 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006595 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006596 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006597 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006598 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006599 if (data->arg.new_lock_owner != 0) {
6600 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6601 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6602 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006603 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6604 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006605 break;
6606 case -NFS4ERR_BAD_STATEID:
6607 case -NFS4ERR_OLD_STATEID:
6608 case -NFS4ERR_STALE_STATEID:
6609 case -NFS4ERR_EXPIRED:
6610 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006611 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006612 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006613 goto out_restart;
6614 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006615 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006616 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006617 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006618out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006619 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006620 return;
6621out_restart:
6622 if (!data->cancelled)
6623 rpc_restart_call_prepare(task);
6624 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006625}
6626
6627static void nfs4_lock_release(void *calldata)
6628{
6629 struct nfs4_lockdata *data = calldata;
6630
Harvey Harrison3110ff82008-05-02 13:42:44 -07006631 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006632 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006633 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006634 struct rpc_task *task;
6635 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6636 data->arg.lock_seqid);
6637 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006638 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006639 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006640 } else
6641 nfs_free_seqid(data->arg.lock_seqid);
6642 nfs4_put_lock_state(data->lsp);
6643 put_nfs_open_context(data->ctx);
6644 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006645 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006646}
6647
6648static const struct rpc_call_ops nfs4_lock_ops = {
6649 .rpc_call_prepare = nfs4_lock_prepare,
6650 .rpc_call_done = nfs4_lock_done,
6651 .rpc_release = nfs4_lock_release,
6652};
6653
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006654static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6655{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006656 switch (error) {
6657 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006658 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006659 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006660 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006661 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006662 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006663 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006664 break;
6665 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006666 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006667 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006668 };
6669}
6670
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006671static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006672{
6673 struct nfs4_lockdata *data;
6674 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006675 struct rpc_message msg = {
6676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6677 .rpc_cred = state->owner->so_cred,
6678 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006679 struct rpc_task_setup task_setup_data = {
6680 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006681 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006682 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006683 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006684 .flags = RPC_TASK_ASYNC,
6685 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006686 int ret;
6687
Harvey Harrison3110ff82008-05-02 13:42:44 -07006688 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006689 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006690 fl->fl_u.nfs4_fl.owner,
6691 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006692 if (data == NULL)
6693 return -ENOMEM;
6694 if (IS_SETLKW(cmd))
6695 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006696 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6697 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006698 msg.rpc_argp = &data->arg;
6699 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006700 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006701 if (recovery_type > NFS_LOCK_NEW) {
6702 if (recovery_type == NFS_LOCK_RECLAIM)
6703 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006704 } else
6705 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006706 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006707 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006708 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006709 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006710 if (ret == 0) {
6711 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006712 if (ret)
6713 nfs4_handle_setlk_error(data->server, data->lsp,
6714 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006715 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006716 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006717 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006718 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006719 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006720 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721}
6722
6723static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6724{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006725 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006726 struct nfs4_exception exception = {
6727 .inode = state->inode,
6728 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006729 int err;
6730
6731 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006732 /* Cache the lock if possible... */
6733 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6734 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006735 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006736 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006737 break;
6738 nfs4_handle_exception(server, err, &exception);
6739 } while (exception.retry);
6740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741}
6742
6743static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6744{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006745 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006746 struct nfs4_exception exception = {
6747 .inode = state->inode,
6748 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006749 int err;
6750
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006751 err = nfs4_set_lock_state(state, request);
6752 if (err != 0)
6753 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006754 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006755 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6756 return 0;
6757 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006758 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006759 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6760 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006761 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006762 switch (err) {
6763 default:
6764 goto out;
6765 case -NFS4ERR_GRACE:
6766 case -NFS4ERR_DELAY:
6767 nfs4_handle_exception(server, err, &exception);
6768 err = 0;
6769 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006770 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006771out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006772 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773}
6774
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006775#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006776static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6777{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006778 struct nfs4_lock_state *lsp;
6779 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006780
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006781 status = nfs4_set_lock_state(state, request);
6782 if (status != 0)
6783 return status;
6784 lsp = request->fl_u.nfs4_fl.owner;
6785 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6786 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6787 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006788 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006789}
6790#endif
6791
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6793{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006794 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006795 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006796 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006797 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
Trond Myklebust01c3b862006-06-29 16:38:39 -04006799 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006800 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006801 if (status < 0)
6802 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006803 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006804 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006805 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006806 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006807 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006808 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006809 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006810 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006811 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006812 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006813 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006814 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006815 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006816 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006817out:
6818 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 return status;
6820}
6821
6822static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6823{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006824 struct nfs4_exception exception = {
6825 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006826 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006827 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 int err;
6829
6830 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006831 err = _nfs4_proc_setlk(state, cmd, request);
6832 if (err == -NFS4ERR_DENIED)
6833 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006835 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 } while (exception.retry);
6837 return err;
6838}
6839
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006840#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6841#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6842
6843static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006844nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6845 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006846{
6847 int status = -ERESTARTSYS;
6848 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6849
6850 while(!signalled()) {
6851 status = nfs4_proc_setlk(state, cmd, request);
6852 if ((status != -EAGAIN) || IS_SETLK(cmd))
6853 break;
6854 freezable_schedule_timeout_interruptible(timeout);
6855 timeout *= 2;
6856 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6857 status = -ERESTARTSYS;
6858 }
6859 return status;
6860}
6861
Jeff Laytona1d617d82016-09-17 18:17:39 -04006862#ifdef CONFIG_NFS_V4_1
6863struct nfs4_lock_waiter {
6864 struct task_struct *task;
6865 struct inode *inode;
6866 struct nfs_lowner *owner;
6867 bool notified;
6868};
6869
6870static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006871nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006872{
6873 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006874 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006875
Jeff Layton57174592018-03-18 08:37:03 -04006876 /* NULL key means to wake up everyone */
6877 if (key) {
6878 struct cb_notify_lock_args *cbnl = key;
6879 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6880 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006881
Jeff Layton57174592018-03-18 08:37:03 -04006882 /* Only wake if the callback was for the same owner. */
6883 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6884 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006885
Jeff Layton57174592018-03-18 08:37:03 -04006886 /* Make sure it's for the right inode */
6887 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6888 return 0;
6889
6890 waiter->notified = true;
6891 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006892
6893 /* override "private" so we can use default_wake_function */
6894 wait->private = waiter->task;
6895 ret = autoremove_wake_function(wait, mode, flags, key);
6896 wait->private = waiter;
6897 return ret;
6898}
6899
6900static int
6901nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6902{
6903 int status = -ERESTARTSYS;
6904 unsigned long flags;
6905 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6906 struct nfs_server *server = NFS_SERVER(state->inode);
6907 struct nfs_client *clp = server->nfs_client;
6908 wait_queue_head_t *q = &clp->cl_lock_waitq;
6909 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6910 .id = lsp->ls_seqid.owner_id,
6911 .s_dev = server->s_dev };
6912 struct nfs4_lock_waiter waiter = { .task = current,
6913 .inode = state->inode,
6914 .owner = &owner,
6915 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006916 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006917
6918 /* Don't bother with waitqueue if we don't expect a callback */
6919 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6920 return nfs4_retry_setlk_simple(state, cmd, request);
6921
6922 init_wait(&wait);
6923 wait.private = &waiter;
6924 wait.func = nfs4_wake_lock_waiter;
6925 add_wait_queue(q, &wait);
6926
6927 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006928 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006929 status = nfs4_proc_setlk(state, cmd, request);
6930 if ((status != -EAGAIN) || IS_SETLK(cmd))
6931 break;
6932
6933 status = -ERESTARTSYS;
6934 spin_lock_irqsave(&q->lock, flags);
6935 if (waiter.notified) {
6936 spin_unlock_irqrestore(&q->lock, flags);
6937 continue;
6938 }
6939 set_current_state(TASK_INTERRUPTIBLE);
6940 spin_unlock_irqrestore(&q->lock, flags);
6941
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006942 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006943 }
6944
6945 finish_wait(q, &wait);
6946 return status;
6947}
6948#else /* !CONFIG_NFS_V4_1 */
6949static inline int
6950nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6951{
6952 return nfs4_retry_setlk_simple(state, cmd, request);
6953}
6954#endif
6955
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956static int
6957nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6958{
6959 struct nfs_open_context *ctx;
6960 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 int status;
6962
6963 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006964 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 state = ctx->state;
6966
Trond Myklebustd9531262009-07-21 19:22:38 -04006967 if (IS_GETLK(cmd)) {
6968 if (state != NULL)
6969 return nfs4_proc_getlk(state, F_GETLK, request);
6970 return 0;
6971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
6973 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6974 return -EINVAL;
6975
Trond Myklebustd9531262009-07-21 19:22:38 -04006976 if (request->fl_type == F_UNLCK) {
6977 if (state != NULL)
6978 return nfs4_proc_unlck(state, cmd, request);
6979 return 0;
6980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
Trond Myklebustd9531262009-07-21 19:22:38 -04006982 if (state == NULL)
6983 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006984
6985 if ((request->fl_flags & FL_POSIX) &&
6986 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6987 return -ENOLCK;
6988
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006989 /*
6990 * Don't rely on the VFS having checked the file open mode,
6991 * since it won't do this for flock() locks.
6992 */
6993 switch (request->fl_type) {
6994 case F_RDLCK:
6995 if (!(filp->f_mode & FMODE_READ))
6996 return -EBADF;
6997 break;
6998 case F_WRLCK:
6999 if (!(filp->f_mode & FMODE_WRITE))
7000 return -EBADF;
7001 }
7002
Jeff Layton1ea67db2016-09-17 18:17:37 -04007003 status = nfs4_set_lock_state(state, request);
7004 if (status != 0)
7005 return status;
7006
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007007 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008}
7009
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007010int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007011{
7012 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007013 int err;
7014
7015 err = nfs4_set_lock_state(state, fl);
7016 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007017 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007018 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007019 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007020}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007021
Trond Myklebustcf470c32012-03-07 13:49:12 -05007022struct nfs_release_lockowner_data {
7023 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007024 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007025 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007026 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007027 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007028};
7029
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007030static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7031{
7032 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007033 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007034 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7035 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007036 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007037 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007038}
7039
7040static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7041{
7042 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007043 struct nfs_server *server = data->server;
7044
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007045 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007046
7047 switch (task->tk_status) {
7048 case 0:
7049 renew_lease(server, data->timestamp);
7050 break;
7051 case -NFS4ERR_STALE_CLIENTID:
7052 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007053 nfs4_schedule_lease_recovery(server->nfs_client);
7054 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007055 case -NFS4ERR_LEASE_MOVED:
7056 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007057 if (nfs4_async_handle_error(task, server,
7058 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007059 rpc_restart_call_prepare(task);
7060 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007061}
7062
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007063static void nfs4_release_lockowner_release(void *calldata)
7064{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007065 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007066 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007067 kfree(calldata);
7068}
7069
Trond Myklebust17280172012-03-11 13:11:00 -04007070static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007071 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7072 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007073 .rpc_release = nfs4_release_lockowner_release,
7074};
7075
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007076static void
7077nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007078{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007079 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007080 struct rpc_message msg = {
7081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7082 };
7083
7084 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007085 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007086
Trond Myklebustcf470c32012-03-07 13:49:12 -05007087 data = kmalloc(sizeof(*data), GFP_NOFS);
7088 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007089 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007090 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007091 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007092 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7093 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7094 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007095
Trond Myklebustcf470c32012-03-07 13:49:12 -05007096 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007097 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007098 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007099 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007100}
7101
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007102#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7103
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007104static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007105 struct dentry *unused, struct inode *inode,
7106 const char *key, const void *buf,
7107 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007108{
Al Viro59301222016-05-27 10:19:30 -04007109 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007110}
7111
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007112static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007113 struct dentry *unused, struct inode *inode,
7114 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007115{
Al Virob2968212016-04-10 20:48:24 -04007116 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007117}
7118
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007119static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007120{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007121 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007122}
7123
David Quigleyc9bccef2013-05-22 12:50:45 -04007124#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007125
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007126static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007127 struct dentry *unused, struct inode *inode,
7128 const char *key, const void *buf,
7129 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007130{
7131 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007132 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007133
7134 return -EOPNOTSUPP;
7135}
7136
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007137static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007138 struct dentry *unused, struct inode *inode,
7139 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007140{
7141 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007142 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007143 return -EOPNOTSUPP;
7144}
7145
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007146static ssize_t
7147nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007148{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007149 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007150
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007151 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7152 len = security_inode_listsecurity(inode, list, list_len);
7153 if (list_len && len > list_len)
7154 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007155 }
7156 return len;
7157}
7158
7159static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7160 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007161 .get = nfs4_xattr_get_nfs4_label,
7162 .set = nfs4_xattr_set_nfs4_label,
7163};
David Quigleyc9bccef2013-05-22 12:50:45 -04007164
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007165#else
7166
7167static ssize_t
7168nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7169{
7170 return 0;
7171}
7172
7173#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007174
Andy Adamson533eb462011-06-13 18:25:56 -04007175/*
7176 * nfs_fhget will use either the mounted_on_fileid or the fileid
7177 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007178static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7179{
Andy Adamson533eb462011-06-13 18:25:56 -04007180 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7181 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7182 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007183 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007184 return;
7185
7186 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007187 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007188 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7189 fattr->nlink = 2;
7190}
7191
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007192static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7193 const struct qstr *name,
7194 struct nfs4_fs_locations *fs_locations,
7195 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007196{
7197 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007198 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007199 struct nfs4_fs_locations_arg args = {
7200 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007201 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007202 .page = page,
7203 .bitmask = bitmask,
7204 };
Benny Halevy22958462009-04-01 09:22:02 -04007205 struct nfs4_fs_locations_res res = {
7206 .fs_locations = fs_locations,
7207 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007208 struct rpc_message msg = {
7209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7210 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007211 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007212 };
7213 int status;
7214
Harvey Harrison3110ff82008-05-02 13:42:44 -07007215 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007216
Chuck Leverc05cefc2017-11-05 15:45:22 -05007217 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7218 bitmask[1] = nfs4_fattr_bitmap[1];
7219
Andy Adamson533eb462011-06-13 18:25:56 -04007220 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7221 * is not supported */
7222 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007223 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007224 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007225 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007226
Trond Myklebustc228fd32007-01-13 02:28:11 -05007227 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007228 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007229 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007230 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007231 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007232 return status;
7233}
7234
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007235int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7236 const struct qstr *name,
7237 struct nfs4_fs_locations *fs_locations,
7238 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007239{
7240 struct nfs4_exception exception = { };
7241 int err;
7242 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007243 err = _nfs4_proc_fs_locations(client, dir, name,
7244 fs_locations, page);
7245 trace_nfs4_get_fs_locations(dir, name, err);
7246 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007247 &exception);
7248 } while (exception.retry);
7249 return err;
7250}
7251
Chuck Leverb03d7352013-10-17 14:12:50 -04007252/*
7253 * This operation also signals the server that this client is
7254 * performing migration recovery. The server can stop returning
7255 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7256 * appended to this compound to identify the client ID which is
7257 * performing recovery.
7258 */
7259static int _nfs40_proc_get_locations(struct inode *inode,
7260 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007261 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007262{
7263 struct nfs_server *server = NFS_SERVER(inode);
7264 struct rpc_clnt *clnt = server->client;
7265 u32 bitmask[2] = {
7266 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7267 };
7268 struct nfs4_fs_locations_arg args = {
7269 .clientid = server->nfs_client->cl_clientid,
7270 .fh = NFS_FH(inode),
7271 .page = page,
7272 .bitmask = bitmask,
7273 .migration = 1, /* skip LOOKUP */
7274 .renew = 1, /* append RENEW */
7275 };
7276 struct nfs4_fs_locations_res res = {
7277 .fs_locations = locations,
7278 .migration = 1,
7279 .renew = 1,
7280 };
7281 struct rpc_message msg = {
7282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7283 .rpc_argp = &args,
7284 .rpc_resp = &res,
7285 .rpc_cred = cred,
7286 };
7287 unsigned long now = jiffies;
7288 int status;
7289
7290 nfs_fattr_init(&locations->fattr);
7291 locations->server = server;
7292 locations->nlocations = 0;
7293
Anna Schumakerfba83f32018-05-04 16:22:50 -04007294 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007295 status = nfs4_call_sync_sequence(clnt, server, &msg,
7296 &args.seq_args, &res.seq_res);
7297 if (status)
7298 return status;
7299
7300 renew_lease(server, now);
7301 return 0;
7302}
7303
7304#ifdef CONFIG_NFS_V4_1
7305
7306/*
7307 * This operation also signals the server that this client is
7308 * performing migration recovery. The server can stop asserting
7309 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7310 * performing this operation is identified in the SEQUENCE
7311 * operation in this compound.
7312 *
7313 * When the client supports GETATTR(fs_locations_info), it can
7314 * be plumbed in here.
7315 */
7316static int _nfs41_proc_get_locations(struct inode *inode,
7317 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007318 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007319{
7320 struct nfs_server *server = NFS_SERVER(inode);
7321 struct rpc_clnt *clnt = server->client;
7322 u32 bitmask[2] = {
7323 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7324 };
7325 struct nfs4_fs_locations_arg args = {
7326 .fh = NFS_FH(inode),
7327 .page = page,
7328 .bitmask = bitmask,
7329 .migration = 1, /* skip LOOKUP */
7330 };
7331 struct nfs4_fs_locations_res res = {
7332 .fs_locations = locations,
7333 .migration = 1,
7334 };
7335 struct rpc_message msg = {
7336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7337 .rpc_argp = &args,
7338 .rpc_resp = &res,
7339 .rpc_cred = cred,
7340 };
7341 int status;
7342
7343 nfs_fattr_init(&locations->fattr);
7344 locations->server = server;
7345 locations->nlocations = 0;
7346
Anna Schumakerfba83f32018-05-04 16:22:50 -04007347 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007348 status = nfs4_call_sync_sequence(clnt, server, &msg,
7349 &args.seq_args, &res.seq_res);
7350 if (status == NFS4_OK &&
7351 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7352 status = -NFS4ERR_LEASE_MOVED;
7353 return status;
7354}
7355
7356#endif /* CONFIG_NFS_V4_1 */
7357
7358/**
7359 * nfs4_proc_get_locations - discover locations for a migrated FSID
7360 * @inode: inode on FSID that is migrating
7361 * @locations: result of query
7362 * @page: buffer
7363 * @cred: credential to use for this operation
7364 *
7365 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7366 * operation failed, or a negative errno if a local error occurred.
7367 *
7368 * On success, "locations" is filled in, but if the server has
7369 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7370 * asserted.
7371 *
7372 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7373 * from this client that require migration recovery.
7374 */
7375int nfs4_proc_get_locations(struct inode *inode,
7376 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007377 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007378{
7379 struct nfs_server *server = NFS_SERVER(inode);
7380 struct nfs_client *clp = server->nfs_client;
7381 const struct nfs4_mig_recovery_ops *ops =
7382 clp->cl_mvops->mig_recovery_ops;
7383 struct nfs4_exception exception = { };
7384 int status;
7385
7386 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7387 (unsigned long long)server->fsid.major,
7388 (unsigned long long)server->fsid.minor,
7389 clp->cl_hostname);
7390 nfs_display_fhandle(NFS_FH(inode), __func__);
7391
7392 do {
7393 status = ops->get_locations(inode, locations, page, cred);
7394 if (status != -NFS4ERR_DELAY)
7395 break;
7396 nfs4_handle_exception(server, status, &exception);
7397 } while (exception.retry);
7398 return status;
7399}
7400
Chuck Lever44c99932013-10-17 14:13:30 -04007401/*
7402 * This operation also signals the server that this client is
7403 * performing "lease moved" recovery. The server can stop
7404 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7405 * is appended to this compound to identify the client ID which is
7406 * performing recovery.
7407 */
NeilBrowna52458b2018-12-03 11:30:31 +11007408static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007409{
7410 struct nfs_server *server = NFS_SERVER(inode);
7411 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7412 struct rpc_clnt *clnt = server->client;
7413 struct nfs4_fsid_present_arg args = {
7414 .fh = NFS_FH(inode),
7415 .clientid = clp->cl_clientid,
7416 .renew = 1, /* append RENEW */
7417 };
7418 struct nfs4_fsid_present_res res = {
7419 .renew = 1,
7420 };
7421 struct rpc_message msg = {
7422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7423 .rpc_argp = &args,
7424 .rpc_resp = &res,
7425 .rpc_cred = cred,
7426 };
7427 unsigned long now = jiffies;
7428 int status;
7429
7430 res.fh = nfs_alloc_fhandle();
7431 if (res.fh == NULL)
7432 return -ENOMEM;
7433
Anna Schumakerfba83f32018-05-04 16:22:50 -04007434 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007435 status = nfs4_call_sync_sequence(clnt, server, &msg,
7436 &args.seq_args, &res.seq_res);
7437 nfs_free_fhandle(res.fh);
7438 if (status)
7439 return status;
7440
7441 do_renew_lease(clp, now);
7442 return 0;
7443}
7444
7445#ifdef CONFIG_NFS_V4_1
7446
7447/*
7448 * This operation also signals the server that this client is
7449 * performing "lease moved" recovery. The server can stop asserting
7450 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7451 * this operation is identified in the SEQUENCE operation in this
7452 * compound.
7453 */
NeilBrowna52458b2018-12-03 11:30:31 +11007454static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007455{
7456 struct nfs_server *server = NFS_SERVER(inode);
7457 struct rpc_clnt *clnt = server->client;
7458 struct nfs4_fsid_present_arg args = {
7459 .fh = NFS_FH(inode),
7460 };
7461 struct nfs4_fsid_present_res res = {
7462 };
7463 struct rpc_message msg = {
7464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7465 .rpc_argp = &args,
7466 .rpc_resp = &res,
7467 .rpc_cred = cred,
7468 };
7469 int status;
7470
7471 res.fh = nfs_alloc_fhandle();
7472 if (res.fh == NULL)
7473 return -ENOMEM;
7474
Anna Schumakerfba83f32018-05-04 16:22:50 -04007475 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007476 status = nfs4_call_sync_sequence(clnt, server, &msg,
7477 &args.seq_args, &res.seq_res);
7478 nfs_free_fhandle(res.fh);
7479 if (status == NFS4_OK &&
7480 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7481 status = -NFS4ERR_LEASE_MOVED;
7482 return status;
7483}
7484
7485#endif /* CONFIG_NFS_V4_1 */
7486
7487/**
7488 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7489 * @inode: inode on FSID to check
7490 * @cred: credential to use for this operation
7491 *
7492 * Server indicates whether the FSID is present, moved, or not
7493 * recognized. This operation is necessary to clear a LEASE_MOVED
7494 * condition for this client ID.
7495 *
7496 * Returns NFS4_OK if the FSID is present on this server,
7497 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7498 * NFS4ERR code if some error occurred on the server, or a
7499 * negative errno if a local failure occurred.
7500 */
NeilBrowna52458b2018-12-03 11:30:31 +11007501int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007502{
7503 struct nfs_server *server = NFS_SERVER(inode);
7504 struct nfs_client *clp = server->nfs_client;
7505 const struct nfs4_mig_recovery_ops *ops =
7506 clp->cl_mvops->mig_recovery_ops;
7507 struct nfs4_exception exception = { };
7508 int status;
7509
7510 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7511 (unsigned long long)server->fsid.major,
7512 (unsigned long long)server->fsid.minor,
7513 clp->cl_hostname);
7514 nfs_display_fhandle(NFS_FH(inode), __func__);
7515
7516 do {
7517 status = ops->fsid_present(inode, cred);
7518 if (status != -NFS4ERR_DELAY)
7519 break;
7520 nfs4_handle_exception(server, status, &exception);
7521 } while (exception.retry);
7522 return status;
7523}
7524
Trond Myklebust302fad72019-02-18 13:32:38 -05007525/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007526 * If 'use_integrity' is true and the state managment nfs_client
7527 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7528 * and the machine credential as per RFC3530bis and RFC5661 Security
7529 * Considerations sections. Otherwise, just use the user cred with the
7530 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007531 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007532static 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 +00007533{
7534 int status;
7535 struct nfs4_secinfo_arg args = {
7536 .dir_fh = NFS_FH(dir),
7537 .name = name,
7538 };
7539 struct nfs4_secinfo_res res = {
7540 .flavors = flavors,
7541 };
7542 struct rpc_message msg = {
7543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7544 .rpc_argp = &args,
7545 .rpc_resp = &res,
7546 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007547 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
NeilBrowna52458b2018-12-03 11:30:31 +11007548 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007549
7550 if (use_integrity) {
7551 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007552 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7553 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007554 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007555
7556 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007557
7558 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7559 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7560
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007561 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7562 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007563 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007564
NeilBrowna52458b2018-12-03 11:30:31 +11007565 put_cred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007566
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007567 return status;
7568}
7569
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007570int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7571 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007572{
7573 struct nfs4_exception exception = { };
7574 int err;
7575 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007576 err = -NFS4ERR_WRONGSEC;
7577
7578 /* try to use integrity protection with machine cred */
7579 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7580 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7581
7582 /*
7583 * if unable to use integrity protection, or SECINFO with
7584 * integrity protection returns NFS4ERR_WRONGSEC (which is
7585 * disallowed by spec, but exists in deployed servers) use
7586 * the current filesystem's rpc_client and the user cred.
7587 */
7588 if (err == -NFS4ERR_WRONGSEC)
7589 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7590
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007591 trace_nfs4_secinfo(dir, name, err);
7592 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007593 &exception);
7594 } while (exception.retry);
7595 return err;
7596}
7597
Andy Adamson557134a2009-04-01 09:21:53 -04007598#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007599/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007600 * Check the exchange flags returned by the server for invalid flags, having
7601 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7602 * DS flags set.
7603 */
7604static int nfs4_check_cl_exchange_flags(u32 flags)
7605{
7606 if (flags & ~EXCHGID4_FLAG_MASK_R)
7607 goto out_inval;
7608 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7609 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7610 goto out_inval;
7611 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7612 goto out_inval;
7613 return NFS_OK;
7614out_inval:
7615 return -NFS4ERR_INVAL;
7616}
7617
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007618static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007619nfs41_same_server_scope(struct nfs41_server_scope *a,
7620 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007621{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007622 if (a->server_scope_sz != b->server_scope_sz)
7623 return false;
7624 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007625}
7626
Andy Adamson02a95de2016-02-05 16:08:37 -05007627static void
7628nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7629{
7630}
7631
7632static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7633 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7634};
7635
Andy Adamson357f54d2010-12-14 10:11:57 -05007636/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007637 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007638 *
7639 * The 4.1 client currently uses the same TCP connection for the
7640 * fore and backchannel.
7641 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007642static
7643int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7644 struct rpc_xprt *xprt,
7645 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007646 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007647{
7648 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007649 struct nfs41_bind_conn_to_session_args args = {
7650 .client = clp,
7651 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7652 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007653 struct nfs41_bind_conn_to_session_res res;
7654 struct rpc_message msg = {
7655 .rpc_proc =
7656 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007657 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007658 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007659 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007660 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007661 struct rpc_task_setup task_setup_data = {
7662 .rpc_client = clnt,
7663 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007664 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007665 .rpc_message = &msg,
7666 .flags = RPC_TASK_TIMEOUT,
7667 };
7668 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007669
Trond Myklebust71a097c2015-02-18 09:27:18 -08007670 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7671 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7672 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007673
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007674 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7675 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7676 args.dir = NFS4_CDFC4_FORE;
7677
7678 task = rpc_run_task(&task_setup_data);
7679 if (!IS_ERR(task)) {
7680 status = task->tk_status;
7681 rpc_put_task(task);
7682 } else
7683 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007684 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007685 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007686 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007687 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7688 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007689 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007690 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007691 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007692 dprintk("NFS: %s: Unexpected direction from server\n",
7693 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007694 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007695 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007696 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007697 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7698 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007699 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007700 }
7701 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007702
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007703 return status;
7704}
7705
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007706struct rpc_bind_conn_calldata {
7707 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007708 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007709};
7710
7711static int
7712nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7713 struct rpc_xprt *xprt,
7714 void *calldata)
7715{
7716 struct rpc_bind_conn_calldata *p = calldata;
7717
7718 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7719}
7720
NeilBrowna52458b2018-12-03 11:30:31 +11007721int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007722{
7723 struct rpc_bind_conn_calldata data = {
7724 .clp = clp,
7725 .cred = cred,
7726 };
7727 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7728 nfs4_proc_bind_conn_to_session_callback, &data);
7729}
7730
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007731/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007732 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7733 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007734 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007735static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7736 .how = SP4_MACH_CRED,
7737 .enforce.u.words = {
7738 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7739 1 << (OP_EXCHANGE_ID - 32) |
7740 1 << (OP_CREATE_SESSION - 32) |
7741 1 << (OP_DESTROY_SESSION - 32) |
7742 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007743 },
7744 .allow.u.words = {
7745 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007746 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007747 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007748 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007749 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007750 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007751 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007752 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007753 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007754 1 << (OP_FREE_STATEID - 32) |
7755 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007756 }
7757};
7758
7759/*
7760 * Select the state protection mode for client `clp' given the server results
7761 * from exchange_id in `sp'.
7762 *
7763 * Returns 0 on success, negative errno otherwise.
7764 */
7765static int nfs4_sp4_select_mode(struct nfs_client *clp,
7766 struct nfs41_state_protection *sp)
7767{
7768 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7769 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7770 1 << (OP_EXCHANGE_ID - 32) |
7771 1 << (OP_CREATE_SESSION - 32) |
7772 1 << (OP_DESTROY_SESSION - 32) |
7773 1 << (OP_DESTROY_CLIENTID - 32)
7774 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007775 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007776 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007777 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007778
7779 if (sp->how == SP4_MACH_CRED) {
7780 /* Print state protect result */
7781 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7782 for (i = 0; i <= LAST_NFS4_OP; i++) {
7783 if (test_bit(i, sp->enforce.u.longs))
7784 dfprintk(MOUNT, " enforce op %d\n", i);
7785 if (test_bit(i, sp->allow.u.longs))
7786 dfprintk(MOUNT, " allow op %d\n", i);
7787 }
7788
7789 /* make sure nothing is on enforce list that isn't supported */
7790 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7791 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7792 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007793 ret = -EINVAL;
7794 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007795 }
7796 }
7797
7798 /*
7799 * Minimal mode - state operations are allowed to use machine
7800 * credential. Note this already happens by default, so the
7801 * client doesn't have to do anything more than the negotiation.
7802 *
7803 * NOTE: we don't care if EXCHANGE_ID is in the list -
7804 * we're already using the machine cred for exchange_id
7805 * and will never use a different cred.
7806 */
7807 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7808 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7809 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7810 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7811 dfprintk(MOUNT, "sp4_mach_cred:\n");
7812 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007813 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007814 } else {
7815 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007816 ret = -EINVAL;
7817 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007818 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007819
7820 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007821 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7822 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007823 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7824 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007825 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007826 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007827
Andrew Elble99ade3c2015-12-02 09:39:51 -05007828 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7829 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007830 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007831 }
7832
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007833 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7834 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7835 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007836 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007837 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007838
7839 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7840 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7841 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007842 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007843 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007844
7845 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7846 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007847 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007848 }
7849
7850 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7851 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007852 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007853 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007854 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007855out:
7856 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00007857 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007858}
7859
Andy Adamson8d89bd72016-09-09 09:22:18 -04007860struct nfs41_exchange_id_data {
7861 struct nfs41_exchange_id_res res;
7862 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007863};
7864
Andy Adamson8d89bd72016-09-09 09:22:18 -04007865static void nfs4_exchange_id_release(void *data)
7866{
7867 struct nfs41_exchange_id_data *cdata =
7868 (struct nfs41_exchange_id_data *)data;
7869
Olga Kornievskaia63513232017-03-13 10:36:19 -04007870 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007871 kfree(cdata->res.impl_id);
7872 kfree(cdata->res.server_scope);
7873 kfree(cdata->res.server_owner);
7874 kfree(cdata);
7875}
7876
7877static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007878 .rpc_release = nfs4_exchange_id_release,
7879};
7880
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007881/*
7882 * _nfs4_proc_exchange_id()
7883 *
7884 * Wrapper for EXCHANGE_ID operation.
7885 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007886static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11007887nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007888 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007889{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007890 struct rpc_message msg = {
7891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007892 .rpc_cred = cred,
7893 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007894 struct rpc_task_setup task_setup_data = {
7895 .rpc_client = clp->cl_rpcclient,
7896 .callback_ops = &nfs4_exchange_id_call_ops,
7897 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007898 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007899 };
7900 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007901 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007902
Elena Reshetova212bf412017-10-20 12:53:38 +03007903 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007904 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007905
Trond Myklebust9c760d12017-07-31 18:38:50 -04007906 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007907 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007908 if (!calldata)
7909 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007910
Trond Myklebustfd405592017-08-01 16:02:47 -04007911 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007912
7913 status = nfs4_init_uniform_client_string(clp);
7914 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007915 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007916
Andy Adamson8d89bd72016-09-09 09:22:18 -04007917 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7918 GFP_NOFS);
7919 status = -ENOMEM;
7920 if (unlikely(calldata->res.server_owner == NULL))
7921 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007922
Andy Adamson8d89bd72016-09-09 09:22:18 -04007923 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007924 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007925 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007926 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007927
Andy Adamson8d89bd72016-09-09 09:22:18 -04007928 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7929 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007930 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007931
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007932 switch (sp4_how) {
7933 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007934 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007935 break;
7936
7937 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007938 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007939 break;
7940
7941 default:
7942 /* unsupported! */
7943 WARN_ON_ONCE(1);
7944 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007945 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007946 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007947 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007948 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007949 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007950 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7951 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007952 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007953 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007954 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7955 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007956#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007957 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007958#endif
7959 msg.rpc_argp = &calldata->args;
7960 msg.rpc_resp = &calldata->res;
7961 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007962
Trond Myklebust9c760d12017-07-31 18:38:50 -04007963 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007964
7965out_impl_id:
7966 kfree(calldata->res.impl_id);
7967out_server_scope:
7968 kfree(calldata->res.server_scope);
7969out_server_owner:
7970 kfree(calldata->res.server_owner);
7971out_calldata:
7972 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007973out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007974 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007975 return ERR_PTR(status);
7976}
7977
7978/*
7979 * _nfs4_proc_exchange_id()
7980 *
7981 * Wrapper for EXCHANGE_ID operation.
7982 */
NeilBrowna52458b2018-12-03 11:30:31 +11007983static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04007984 u32 sp4_how)
7985{
7986 struct rpc_task *task;
7987 struct nfs41_exchange_id_args *argp;
7988 struct nfs41_exchange_id_res *resp;
7989 int status;
7990
7991 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7992 if (IS_ERR(task))
7993 return PTR_ERR(task);
7994
7995 argp = task->tk_msg.rpc_argp;
7996 resp = task->tk_msg.rpc_resp;
7997 status = task->tk_status;
7998 if (status != 0)
7999 goto out;
8000
8001 status = nfs4_check_cl_exchange_flags(resp->flags);
8002 if (status != 0)
8003 goto out;
8004
8005 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8006 if (status != 0)
8007 goto out;
8008
8009 clp->cl_clientid = resp->clientid;
8010 clp->cl_exchange_flags = resp->flags;
8011 clp->cl_seqid = resp->seqid;
8012 /* Client ID is not confirmed */
8013 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8014 clear_bit(NFS4_SESSION_ESTABLISHED,
8015 &clp->cl_session->session_state);
8016
8017 if (clp->cl_serverscope != NULL &&
8018 !nfs41_same_server_scope(clp->cl_serverscope,
8019 resp->server_scope)) {
8020 dprintk("%s: server_scope mismatch detected\n",
8021 __func__);
8022 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8023 }
8024
8025 swap(clp->cl_serverowner, resp->server_owner);
8026 swap(clp->cl_serverscope, resp->server_scope);
8027 swap(clp->cl_implid, resp->impl_id);
8028
8029 /* Save the EXCHANGE_ID verifier session trunk tests */
8030 memcpy(clp->cl_confirm.data, argp->verifier.data,
8031 sizeof(clp->cl_confirm.data));
8032out:
8033 trace_nfs4_exchange_id(clp, status);
8034 rpc_put_task(task);
8035 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008036}
8037
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008038/*
8039 * nfs4_proc_exchange_id()
8040 *
8041 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8042 *
8043 * Since the clientid has expired, all compounds using sessions
8044 * associated with the stale clientid will be returning
8045 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8046 * be in some phase of session reset.
8047 *
8048 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8049 */
NeilBrowna52458b2018-12-03 11:30:31 +11008050int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008051{
8052 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8053 int status;
8054
8055 /* try SP4_MACH_CRED if krb5i/p */
8056 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8057 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008058 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008059 if (!status)
8060 return 0;
8061 }
8062
8063 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008064 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008065}
8066
Andy Adamson04fa2c62016-09-09 09:22:29 -04008067/**
8068 * nfs4_test_session_trunk
8069 *
8070 * This is an add_xprt_test() test function called from
8071 * rpc_clnt_setup_test_and_add_xprt.
8072 *
8073 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8074 * and is dereferrenced in nfs4_exchange_id_release
8075 *
8076 * Upon success, add the new transport to the rpc_clnt
8077 *
8078 * @clnt: struct rpc_clnt to get new transport
8079 * @xprt: the rpc_xprt to test
8080 * @data: call data for _nfs4_proc_exchange_id.
8081 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308082void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008083 void *data)
8084{
8085 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008086 struct rpc_task *task;
8087 int status;
8088
Andy Adamson04fa2c62016-09-09 09:22:29 -04008089 u32 sp4_how;
8090
8091 dprintk("--> %s try %s\n", __func__,
8092 xprt->address_strings[RPC_DISPLAY_ADDR]);
8093
8094 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8095
8096 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008097 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8098 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308099 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008100
8101 status = task->tk_status;
8102 if (status == 0)
8103 status = nfs4_detect_session_trunking(adata->clp,
8104 task->tk_msg.rpc_resp, xprt);
8105
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308106 if (status == 0)
8107 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8108
Trond Myklebust9c760d12017-07-31 18:38:50 -04008109 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008110}
8111EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8112
Trond Myklebust66245532012-05-25 17:18:09 -04008113static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008114 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008115{
8116 struct rpc_message msg = {
8117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8118 .rpc_argp = clp,
8119 .rpc_cred = cred,
8120 };
8121 int status;
8122
8123 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008124 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008125 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008126 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008127 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8128 return status;
8129}
8130
8131static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008132 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008133{
8134 unsigned int loop;
8135 int ret;
8136
8137 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8138 ret = _nfs4_proc_destroy_clientid(clp, cred);
8139 switch (ret) {
8140 case -NFS4ERR_DELAY:
8141 case -NFS4ERR_CLIENTID_BUSY:
8142 ssleep(1);
8143 break;
8144 default:
8145 return ret;
8146 }
8147 }
8148 return 0;
8149}
8150
8151int nfs4_destroy_clientid(struct nfs_client *clp)
8152{
NeilBrowna52458b2018-12-03 11:30:31 +11008153 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008154 int ret = 0;
8155
8156 if (clp->cl_mvops->minor_version < 1)
8157 goto out;
8158 if (clp->cl_exchange_flags == 0)
8159 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008160 if (clp->cl_preserve_clid)
8161 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008162 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008163 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008164 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008165 switch (ret) {
8166 case 0:
8167 case -NFS4ERR_STALE_CLIENTID:
8168 clp->cl_exchange_flags = 0;
8169 }
8170out:
8171 return ret;
8172}
8173
Andy Adamson2050f0c2009-04-01 09:22:30 -04008174struct nfs4_get_lease_time_data {
8175 struct nfs4_get_lease_time_args *args;
8176 struct nfs4_get_lease_time_res *res;
8177 struct nfs_client *clp;
8178};
8179
8180static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8181 void *calldata)
8182{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008183 struct nfs4_get_lease_time_data *data =
8184 (struct nfs4_get_lease_time_data *)calldata;
8185
8186 dprintk("--> %s\n", __func__);
8187 /* just setup sequence, do not trigger session recovery
8188 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008189 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008190 &data->args->la_seq_args,
8191 &data->res->lr_seq_res,
8192 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008193 dprintk("<-- %s\n", __func__);
8194}
8195
8196/*
8197 * Called from nfs4_state_manager thread for session setup, so don't recover
8198 * from sequence operation or clientid errors.
8199 */
8200static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8201{
8202 struct nfs4_get_lease_time_data *data =
8203 (struct nfs4_get_lease_time_data *)calldata;
8204
8205 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008206 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8207 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008208 switch (task->tk_status) {
8209 case -NFS4ERR_DELAY:
8210 case -NFS4ERR_GRACE:
8211 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8212 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8213 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008214 /* fall through */
8215 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008216 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008217 return;
8218 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008219 dprintk("<-- %s\n", __func__);
8220}
8221
Trond Myklebust17280172012-03-11 13:11:00 -04008222static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008223 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8224 .rpc_call_done = nfs4_get_lease_time_done,
8225};
8226
8227int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8228{
8229 struct rpc_task *task;
8230 struct nfs4_get_lease_time_args args;
8231 struct nfs4_get_lease_time_res res = {
8232 .lr_fsinfo = fsinfo,
8233 };
8234 struct nfs4_get_lease_time_data data = {
8235 .args = &args,
8236 .res = &res,
8237 .clp = clp,
8238 };
8239 struct rpc_message msg = {
8240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8241 .rpc_argp = &args,
8242 .rpc_resp = &res,
8243 };
8244 struct rpc_task_setup task_setup = {
8245 .rpc_client = clp->cl_rpcclient,
8246 .rpc_message = &msg,
8247 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008248 .callback_data = &data,
8249 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008250 };
8251 int status;
8252
Anna Schumakerfba83f32018-05-04 16:22:50 -04008253 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008254 task = rpc_run_task(&task_setup);
8255
8256 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008257 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008258
Anna Schumakerf6148712017-04-07 14:15:23 -04008259 status = task->tk_status;
8260 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008261 return status;
8262}
8263
Andy Adamsonfc931582009-04-01 09:22:31 -04008264/*
8265 * Initialize the values to be used by the client in CREATE_SESSION
8266 * If nfs4_init_session set the fore channel request and response sizes,
8267 * use them.
8268 *
8269 * Set the back channel max_resp_sz_cached to zero to force the client to
8270 * always set csa_cachethis to FALSE because the current implementation
8271 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8272 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008273static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8274 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008275{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008276 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008277 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008278
Andy Adamson18aad3d2013-06-26 12:21:49 -04008279 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8280 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8281
Andy Adamsonfc931582009-04-01 09:22:31 -04008282 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008283 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8284 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008285 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008286 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008287
8288 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008289 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008290 __func__,
8291 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008292 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008293
8294 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008295 args->bc_attrs.max_rqst_sz = max_bc_payload;
8296 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008297 args->bc_attrs.max_resp_sz_cached = 0;
8298 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008299 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008300
8301 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8302 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8303 __func__,
8304 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8305 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8306 args->bc_attrs.max_reqs);
8307}
8308
Trond Myklebust79969dd2015-02-18 11:30:18 -08008309static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8310 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008311{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008312 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008313 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008314
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008315 if (rcvd->max_resp_sz > sent->max_resp_sz)
8316 return -EINVAL;
8317 /*
8318 * Our requested max_ops is the minimum we need; we're not
8319 * prepared to break up compounds into smaller pieces than that.
8320 * So, no point even trying to continue if the server won't
8321 * cooperate:
8322 */
8323 if (rcvd->max_ops < sent->max_ops)
8324 return -EINVAL;
8325 if (rcvd->max_reqs == 0)
8326 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008327 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8328 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008329 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008330}
8331
Trond Myklebust79969dd2015-02-18 11:30:18 -08008332static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8333 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008334{
8335 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008336 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008337
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008338 if (!(res->flags & SESSION4_BACK_CHAN))
8339 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008340 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8341 return -EINVAL;
8342 if (rcvd->max_resp_sz < sent->max_resp_sz)
8343 return -EINVAL;
8344 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8345 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008346 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008347 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008348 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008349 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008350out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008351 return 0;
8352}
Andy Adamson8d353012009-04-01 09:22:32 -04008353
Andy Adamson8d353012009-04-01 09:22:32 -04008354static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008355 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008356{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008357 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008358
Trond Myklebust79969dd2015-02-18 11:30:18 -08008359 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008360 if (ret)
8361 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008362 return nfs4_verify_back_channel_attrs(args, res);
8363}
8364
8365static void nfs4_update_session(struct nfs4_session *session,
8366 struct nfs41_create_session_res *res)
8367{
8368 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008369 /* Mark client id and session as being confirmed */
8370 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8371 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008372 session->flags = res->flags;
8373 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008374 if (res->flags & SESSION4_BACK_CHAN)
8375 memcpy(&session->bc_attrs, &res->bc_attrs,
8376 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008377}
8378
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008379static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008380 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008381{
8382 struct nfs4_session *session = clp->cl_session;
8383 struct nfs41_create_session_args args = {
8384 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008385 .clientid = clp->cl_clientid,
8386 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008387 .cb_program = NFS4_CALLBACK,
8388 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008389 struct nfs41_create_session_res res;
8390
Andy Adamsonfc931582009-04-01 09:22:31 -04008391 struct rpc_message msg = {
8392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8393 .rpc_argp = &args,
8394 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008395 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008396 };
8397 int status;
8398
Chuck Lever6b26cc82016-05-02 14:40:40 -04008399 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008400 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008401
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008402 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008403 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008404
Trond Myklebustb519d402016-09-11 14:50:01 -04008405 switch (status) {
8406 case -NFS4ERR_STALE_CLIENTID:
8407 case -NFS4ERR_DELAY:
8408 case -ETIMEDOUT:
8409 case -EACCES:
8410 case -EAGAIN:
8411 goto out;
8412 };
8413
8414 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008415 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008416 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008417 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008418 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008419 if (status)
8420 goto out;
8421 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008422 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008423out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008424 return status;
8425}
8426
8427/*
8428 * Issues a CREATE_SESSION operation to the server.
8429 * It is the responsibility of the caller to verify the session is
8430 * expired before calling this routine.
8431 */
NeilBrowna52458b2018-12-03 11:30:31 +11008432int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008433{
8434 int status;
8435 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008436 struct nfs4_session *session = clp->cl_session;
8437
8438 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8439
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008440 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008441 if (status)
8442 goto out;
8443
Andy Adamsonaacd5532011-11-09 13:58:21 -05008444 /* Init or reset the session slot tables */
8445 status = nfs4_setup_session_slot_tables(session);
8446 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008447 if (status)
8448 goto out;
8449
8450 ptr = (unsigned *)&session->sess_id.data[0];
8451 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8452 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008453out:
8454 dprintk("<-- %s\n", __func__);
8455 return status;
8456}
8457
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008458/*
8459 * Issue the over-the-wire RPC DESTROY_SESSION.
8460 * The caller must serialize access to this routine.
8461 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008462int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008463 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008464{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008465 struct rpc_message msg = {
8466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8467 .rpc_argp = session,
8468 .rpc_cred = cred,
8469 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008470 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008471
8472 dprintk("--> nfs4_proc_destroy_session\n");
8473
8474 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008475 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8476 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008477
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008478 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008479 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008480
8481 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008482 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008483 "Session has been destroyed regardless...\n", status);
8484
8485 dprintk("<-- nfs4_proc_destroy_session\n");
8486 return status;
8487}
8488
Trond Myklebust7b38c362012-05-23 13:23:31 -04008489/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008490 * Renew the cl_session lease.
8491 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008492struct nfs4_sequence_data {
8493 struct nfs_client *clp;
8494 struct nfs4_sequence_args args;
8495 struct nfs4_sequence_res res;
8496};
8497
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008498static void nfs41_sequence_release(void *data)
8499{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008500 struct nfs4_sequence_data *calldata = data;
8501 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008502
Elena Reshetova212bf412017-10-20 12:53:38 +03008503 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008504 nfs4_schedule_state_renewal(clp);
8505 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008506 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008507}
8508
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008509static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8510{
8511 switch(task->tk_status) {
8512 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008513 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8514 return -EAGAIN;
8515 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008516 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008517 }
8518 return 0;
8519}
8520
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008521static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008522{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008523 struct nfs4_sequence_data *calldata = data;
8524 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008525
Trond Myklebust14516c32010-07-31 14:29:06 -04008526 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8527 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008528
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008529 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008530 if (task->tk_status < 0) {
8531 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008532 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008533 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008534
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008535 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8536 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008537 return;
8538 }
8539 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008540 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008541out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008542 dprintk("<-- %s\n", __func__);
8543}
8544
8545static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8546{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008547 struct nfs4_sequence_data *calldata = data;
8548 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008549 struct nfs4_sequence_args *args;
8550 struct nfs4_sequence_res *res;
8551
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008552 args = task->tk_msg.rpc_argp;
8553 res = task->tk_msg.rpc_resp;
8554
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008555 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008556}
8557
8558static const struct rpc_call_ops nfs41_sequence_ops = {
8559 .rpc_call_done = nfs41_sequence_call_done,
8560 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008561 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008562};
8563
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008564static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008565 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008566 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008567 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008568{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008569 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008570 struct rpc_message msg = {
8571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8572 .rpc_cred = cred,
8573 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008574 struct rpc_task_setup task_setup_data = {
8575 .rpc_client = clp->cl_rpcclient,
8576 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008577 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008578 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008579 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008580 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008581
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008582 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008583 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008584 goto out_err;
8585
8586 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008587 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008588 if (calldata == NULL)
8589 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008590 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008591 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008592 msg.rpc_argp = &calldata->args;
8593 msg.rpc_resp = &calldata->res;
8594 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008595 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008596
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008597 ret = rpc_run_task(&task_setup_data);
8598 if (IS_ERR(ret))
8599 goto out_err;
8600 return ret;
8601out_put_clp:
8602 nfs_put_client(clp);
8603out_err:
8604 nfs41_release_slot(slot);
8605 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008606}
8607
NeilBrowna52458b2018-12-03 11:30:31 +11008608static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008609{
8610 struct rpc_task *task;
8611 int ret = 0;
8612
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008613 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008614 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008615 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008616 if (IS_ERR(task))
8617 ret = PTR_ERR(task);
8618 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008619 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008620 dprintk("<-- %s status=%d\n", __func__, ret);
8621 return ret;
8622}
8623
NeilBrowna52458b2018-12-03 11:30:31 +11008624static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008625{
8626 struct rpc_task *task;
8627 int ret;
8628
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008629 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008630 if (IS_ERR(task)) {
8631 ret = PTR_ERR(task);
8632 goto out;
8633 }
8634 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008635 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008636 ret = task->tk_status;
8637 rpc_put_task(task);
8638out:
8639 dprintk("<-- %s status=%d\n", __func__, ret);
8640 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008641}
8642
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008643struct nfs4_reclaim_complete_data {
8644 struct nfs_client *clp;
8645 struct nfs41_reclaim_complete_args arg;
8646 struct nfs41_reclaim_complete_res res;
8647};
8648
8649static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8650{
8651 struct nfs4_reclaim_complete_data *calldata = data;
8652
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008653 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008654 &calldata->arg.seq_args,
8655 &calldata->res.seq_res,
8656 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008657}
8658
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008659static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8660{
8661 switch(task->tk_status) {
8662 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008663 wake_up_all(&clp->cl_lock_waitq);
8664 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008665 case -NFS4ERR_COMPLETE_ALREADY:
8666 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8667 break;
8668 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008669 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008670 /* fall through */
8671 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008672 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008673 case -NFS4ERR_BADSESSION:
8674 case -NFS4ERR_DEADSESSION:
8675 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8676 nfs4_schedule_session_recovery(clp->cl_session,
8677 task->tk_status);
8678 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008679 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008680 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008681 }
8682 return 0;
8683}
8684
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008685static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8686{
8687 struct nfs4_reclaim_complete_data *calldata = data;
8688 struct nfs_client *clp = calldata->clp;
8689 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8690
8691 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008692 if (!nfs41_sequence_done(task, res))
8693 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008694
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008695 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008696 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8697 rpc_restart_call_prepare(task);
8698 return;
8699 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008700 dprintk("<-- %s\n", __func__);
8701}
8702
8703static void nfs4_free_reclaim_complete_data(void *data)
8704{
8705 struct nfs4_reclaim_complete_data *calldata = data;
8706
8707 kfree(calldata);
8708}
8709
8710static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8711 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8712 .rpc_call_done = nfs4_reclaim_complete_done,
8713 .rpc_release = nfs4_free_reclaim_complete_data,
8714};
8715
8716/*
8717 * Issue a global reclaim complete.
8718 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008719static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008720 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008721{
8722 struct nfs4_reclaim_complete_data *calldata;
8723 struct rpc_task *task;
8724 struct rpc_message msg = {
8725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008726 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008727 };
8728 struct rpc_task_setup task_setup_data = {
8729 .rpc_client = clp->cl_rpcclient,
8730 .rpc_message = &msg,
8731 .callback_ops = &nfs4_reclaim_complete_call_ops,
8732 .flags = RPC_TASK_ASYNC,
8733 };
8734 int status = -ENOMEM;
8735
8736 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008737 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008738 if (calldata == NULL)
8739 goto out;
8740 calldata->clp = clp;
8741 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008742
Anna Schumakerfba83f32018-05-04 16:22:50 -04008743 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008744 msg.rpc_argp = &calldata->arg;
8745 msg.rpc_resp = &calldata->res;
8746 task_setup_data.callback_data = calldata;
8747 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008748 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008749 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008750 goto out;
8751 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008752 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008753 if (status == 0)
8754 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008755 rpc_put_task(task);
8756out:
8757 dprintk("<-- %s status=%d\n", __func__, status);
8758 return status;
8759}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008760
8761static void
8762nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8763{
8764 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008765 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008766
8767 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008768 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008769 &lgp->res.seq_res, task);
8770 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008771}
8772
8773static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8774{
8775 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008776
8777 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008778 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008779 dprintk("<-- %s\n", __func__);
8780}
8781
8782static int
8783nfs4_layoutget_handle_exception(struct rpc_task *task,
8784 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8785{
Trond Myklebustee314c22012-10-01 17:25:48 -07008786 struct inode *inode = lgp->args.inode;
8787 struct nfs_server *server = NFS_SERVER(inode);
8788 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008789 int nfs4err = task->tk_status;
8790 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008791 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008792
Boaz Harroshed7e5422014-01-22 20:34:54 +02008793 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008794
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008795 nfs4_sequence_free_slot(&lgp->res.seq_res);
8796
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008797 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008798 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008799 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008800
8801 /*
8802 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8803 * on the file. set tk_status to -ENODATA to tell upper layer to
8804 * retry go inband.
8805 */
8806 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008807 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008808 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008809 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008810 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8811 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8812 */
8813 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008814 status = -EOVERFLOW;
8815 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008816 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008817 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008818 * (or clients) writing to the same RAID stripe except when
8819 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008820 *
8821 * Treat it like we would RECALLCONFLICT -- we retry for a little
8822 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008823 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008824 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008825 if (lgp->args.minlength == 0) {
8826 status = -EOVERFLOW;
8827 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008828 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008829 status = -EBUSY;
8830 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008831 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008832 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008833 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008834 case -NFS4ERR_DELEG_REVOKED:
8835 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008836 case -NFS4ERR_EXPIRED:
8837 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008838 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008839 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008840 lo = NFS_I(inode)->layout;
8841 /* If the open stateid was bad, then recover it. */
8842 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008843 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008844 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008845 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008846 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008847 break;
8848 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008849
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008850 /*
8851 * Mark the bad layout state as invalid, then retry
8852 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008853 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008854 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008855 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008856 pnfs_free_lseg_list(&head);
8857 status = -EAGAIN;
8858 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008859 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008860
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008861 err = nfs4_handle_exception(server, nfs4err, exception);
8862 if (!status) {
8863 if (exception->retry)
8864 status = -EAGAIN;
8865 else
8866 status = err;
8867 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008868out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008869 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008870 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008871}
8872
Fred Isamandacb4522016-09-19 17:47:09 -04008873size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03008874{
8875 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8876 return nfs_page_array_len(0, max_resp_sz);
8877}
8878
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008879static void nfs4_layoutget_release(void *calldata)
8880{
8881 struct nfs4_layoutget *lgp = calldata;
8882
8883 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008884 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04008885 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008886 dprintk("<-- %s\n", __func__);
8887}
8888
8889static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8890 .rpc_call_prepare = nfs4_layoutget_prepare,
8891 .rpc_call_done = nfs4_layoutget_done,
8892 .rpc_release = nfs4_layoutget_release,
8893};
8894
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008895struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04008896nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008897{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008898 struct inode *inode = lgp->args.inode;
8899 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008900 struct rpc_task *task;
8901 struct rpc_message msg = {
8902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8903 .rpc_argp = &lgp->args,
8904 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008905 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008906 };
8907 struct rpc_task_setup task_setup_data = {
8908 .rpc_client = server->client,
8909 .rpc_message = &msg,
8910 .callback_ops = &nfs4_layoutget_call_ops,
8911 .callback_data = lgp,
8912 .flags = RPC_TASK_ASYNC,
8913 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008914 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008915 struct nfs4_exception exception = {
8916 .inode = inode,
8917 .timeout = *timeout,
8918 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008919 int status = 0;
8920
8921 dprintk("--> %s\n", __func__);
8922
Peng Tao4bd5a982014-11-17 11:05:17 +08008923 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8924 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8925
Anna Schumakerfba83f32018-05-04 16:22:50 -04008926 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008927
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008928 task = rpc_run_task(&task_setup_data);
8929 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008930 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008931 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008932 if (status != 0)
8933 goto out;
8934
8935 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8936 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04008937 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8938 *timeout = exception.timeout;
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008939 } else
8940 lseg = pnfs_layout_process(lgp);
8941out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008942 trace_nfs4_layoutget(lgp->args.ctx,
8943 &lgp->args.range,
8944 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008945 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008946 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008947
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008948 rpc_put_task(task);
8949 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008950 if (status)
8951 return ERR_PTR(status);
8952 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008953}
8954
Benny Halevycbe82602011-05-22 19:52:37 +03008955static void
8956nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8957{
8958 struct nfs4_layoutreturn *lrp = calldata;
8959
8960 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008961 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008962 &lrp->args.seq_args,
8963 &lrp->res.seq_res,
8964 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04008965 if (!pnfs_layout_is_valid(lrp->args.layout))
8966 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008967}
8968
8969static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8970{
8971 struct nfs4_layoutreturn *lrp = calldata;
8972 struct nfs_server *server;
8973
8974 dprintk("--> %s\n", __func__);
8975
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008976 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008977 return;
8978
8979 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008980 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008981 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04008982 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04008983 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05008984 lrp->args.inode))
8985 goto out_restart;
8986 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008987 default:
8988 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008989 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008990 case 0:
8991 break;
8992 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008993 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008994 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008995 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008996 }
Benny Halevycbe82602011-05-22 19:52:37 +03008997 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008998 return;
8999out_restart:
9000 task->tk_status = 0;
9001 nfs4_sequence_free_slot(&lrp->res.seq_res);
9002 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009003}
9004
9005static void nfs4_layoutreturn_release(void *calldata)
9006{
9007 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009008 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009009
9010 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009011 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009012 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009013 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009014 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9015 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009016 pnfs_put_layout_hdr(lrp->args.layout);
9017 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009018 kfree(calldata);
9019 dprintk("<-- %s\n", __func__);
9020}
9021
9022static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9023 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9024 .rpc_call_done = nfs4_layoutreturn_done,
9025 .rpc_release = nfs4_layoutreturn_release,
9026};
9027
Peng Tao6c166052014-11-17 09:30:40 +08009028int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009029{
9030 struct rpc_task *task;
9031 struct rpc_message msg = {
9032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9033 .rpc_argp = &lrp->args,
9034 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009035 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009036 };
9037 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009038 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009039 .rpc_message = &msg,
9040 .callback_ops = &nfs4_layoutreturn_call_ops,
9041 .callback_data = lrp,
9042 };
Peng Tao6c166052014-11-17 09:30:40 +08009043 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009044
Andrew Elble99ade3c2015-12-02 09:39:51 -05009045 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9046 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9047 &task_setup_data.rpc_client, &msg);
9048
Benny Halevycbe82602011-05-22 19:52:37 +03009049 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009050 if (!sync) {
9051 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9052 if (!lrp->inode) {
9053 nfs4_layoutreturn_release(lrp);
9054 return -EAGAIN;
9055 }
9056 task_setup_data.flags |= RPC_TASK_ASYNC;
9057 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009058 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009059 task = rpc_run_task(&task_setup_data);
9060 if (IS_ERR(task))
9061 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009062 if (sync)
9063 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009064 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009065 dprintk("<-- %s status=%d\n", __func__, status);
9066 rpc_put_task(task);
9067 return status;
9068}
9069
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009070static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009071_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9072 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009073 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009074{
9075 struct nfs4_getdeviceinfo_args args = {
9076 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009077 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9078 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009079 };
9080 struct nfs4_getdeviceinfo_res res = {
9081 .pdev = pdev,
9082 };
9083 struct rpc_message msg = {
9084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9085 .rpc_argp = &args,
9086 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009087 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009088 };
9089 int status;
9090
9091 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009092 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009093 if (res.notification & ~args.notify_types)
9094 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009095 if (res.notification != args.notify_types)
9096 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009097
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009098 dprintk("<-- %s status=%d\n", __func__, status);
9099
9100 return status;
9101}
9102
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009103int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9104 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009105 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009106{
9107 struct nfs4_exception exception = { };
9108 int err;
9109
9110 do {
9111 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009112 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009113 &exception);
9114 } while (exception.retry);
9115 return err;
9116}
9117EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9118
Andy Adamson863a3c62011-03-23 13:27:54 +00009119static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9120{
9121 struct nfs4_layoutcommit_data *data = calldata;
9122 struct nfs_server *server = NFS_SERVER(data->args.inode);
9123
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009124 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009125 &data->args.seq_args,
9126 &data->res.seq_res,
9127 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009128}
9129
9130static void
9131nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9132{
9133 struct nfs4_layoutcommit_data *data = calldata;
9134 struct nfs_server *server = NFS_SERVER(data->args.inode);
9135
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009136 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009137 return;
9138
9139 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009140 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9141 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9142 case -NFS4ERR_BADLAYOUT: /* no layout */
9143 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009144 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009145 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009146 break;
9147 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009148 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009149 rpc_restart_call_prepare(task);
9150 return;
9151 }
9152 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009153}
9154
9155static void nfs4_layoutcommit_release(void *calldata)
9156{
9157 struct nfs4_layoutcommit_data *data = calldata;
9158
Andy Adamsondb29c082011-07-30 20:52:38 -04009159 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009160 nfs_post_op_update_inode_force_wcc(data->args.inode,
9161 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009162 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009163 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009164 kfree(data);
9165}
9166
9167static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9168 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9169 .rpc_call_done = nfs4_layoutcommit_done,
9170 .rpc_release = nfs4_layoutcommit_release,
9171};
9172
9173int
Andy Adamsonef311532011-03-12 02:58:10 -05009174nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009175{
9176 struct rpc_message msg = {
9177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9178 .rpc_argp = &data->args,
9179 .rpc_resp = &data->res,
9180 .rpc_cred = data->cred,
9181 };
9182 struct rpc_task_setup task_setup_data = {
9183 .task = &data->task,
9184 .rpc_client = NFS_CLIENT(data->args.inode),
9185 .rpc_message = &msg,
9186 .callback_ops = &nfs4_layoutcommit_ops,
9187 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009188 };
9189 struct rpc_task *task;
9190 int status = 0;
9191
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009192 dprintk("NFS: initiating layoutcommit call. sync %d "
9193 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009194 data->args.lastbytewritten,
9195 data->args.inode->i_ino);
9196
Trond Myklebust472e2592015-02-05 16:50:30 -05009197 if (!sync) {
9198 data->inode = nfs_igrab_and_active(data->args.inode);
9199 if (data->inode == NULL) {
9200 nfs4_layoutcommit_release(data);
9201 return -EAGAIN;
9202 }
9203 task_setup_data.flags = RPC_TASK_ASYNC;
9204 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009205 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009206 task = rpc_run_task(&task_setup_data);
9207 if (IS_ERR(task))
9208 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009209 if (sync)
9210 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009211 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009212 dprintk("%s: status %d\n", __func__, status);
9213 rpc_put_task(task);
9214 return status;
9215}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009216
Trond Myklebust302fad72019-02-18 13:32:38 -05009217/*
Andy Adamson97431202013-08-08 10:57:56 -04009218 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9219 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9220 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009221static int
9222_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009223 struct nfs_fsinfo *info,
9224 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009225{
9226 struct nfs41_secinfo_no_name_args args = {
9227 .style = SECINFO_STYLE_CURRENT_FH,
9228 };
9229 struct nfs4_secinfo_res res = {
9230 .flavors = flavors,
9231 };
9232 struct rpc_message msg = {
9233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9234 .rpc_argp = &args,
9235 .rpc_resp = &res,
9236 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009237 struct rpc_clnt *clnt = server->client;
NeilBrowna52458b2018-12-03 11:30:31 +11009238 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009239 int status;
9240
9241 if (use_integrity) {
9242 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009243 cred = nfs4_get_clid_cred(server->nfs_client);
9244 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009245 }
9246
9247 dprintk("--> %s\n", __func__);
9248 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9249 &res.seq_res, 0);
9250 dprintk("<-- %s status=%d\n", __func__, status);
9251
NeilBrowna52458b2018-12-03 11:30:31 +11009252 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009253
9254 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009255}
9256
9257static int
9258nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9259 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9260{
9261 struct nfs4_exception exception = { };
9262 int err;
9263 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009264 /* first try using integrity protection */
9265 err = -NFS4ERR_WRONGSEC;
9266
9267 /* try to use integrity protection with machine cred */
9268 if (_nfs4_is_integrity_protected(server->nfs_client))
9269 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9270 flavors, true);
9271
9272 /*
9273 * if unable to use integrity protection, or SECINFO with
9274 * integrity protection returns NFS4ERR_WRONGSEC (which is
9275 * disallowed by spec, but exists in deployed servers) use
9276 * the current filesystem's rpc_client and the user cred.
9277 */
9278 if (err == -NFS4ERR_WRONGSEC)
9279 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9280 flavors, false);
9281
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009282 switch (err) {
9283 case 0:
9284 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009285 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009286 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009287 default:
9288 err = nfs4_handle_exception(server, err, &exception);
9289 }
9290 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009291out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009292 return err;
9293}
9294
9295static int
9296nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9297 struct nfs_fsinfo *info)
9298{
9299 int err;
9300 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009301 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009302 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009303 struct nfs4_secinfo4 *secinfo;
9304 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009305
9306 page = alloc_page(GFP_KERNEL);
9307 if (!page) {
9308 err = -ENOMEM;
9309 goto out;
9310 }
9311
9312 flavors = page_address(page);
9313 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9314
9315 /*
9316 * Fall back on "guess and check" method if
9317 * the server doesn't support SECINFO_NO_NAME
9318 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009319 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009320 err = nfs4_find_root_sec(server, fhandle, info);
9321 goto out_freepage;
9322 }
9323 if (err)
9324 goto out_freepage;
9325
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009326 for (i = 0; i < flavors->num_flavors; i++) {
9327 secinfo = &flavors->flavors[i];
9328
9329 switch (secinfo->flavor) {
9330 case RPC_AUTH_NULL:
9331 case RPC_AUTH_UNIX:
9332 case RPC_AUTH_GSS:
9333 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9334 &secinfo->flavor_info);
9335 break;
9336 default:
9337 flavor = RPC_AUTH_MAXFLAVOR;
9338 break;
9339 }
9340
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009341 if (!nfs_auth_info_match(&server->auth_info, flavor))
9342 flavor = RPC_AUTH_MAXFLAVOR;
9343
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009344 if (flavor != RPC_AUTH_MAXFLAVOR) {
9345 err = nfs4_lookup_root_sec(server, fhandle,
9346 info, flavor);
9347 if (!err)
9348 break;
9349 }
9350 }
9351
9352 if (flavor == RPC_AUTH_MAXFLAVOR)
9353 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009354
9355out_freepage:
9356 put_page(page);
9357 if (err == -EACCES)
9358 return -EPERM;
9359out:
9360 return err;
9361}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009362
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009363static int _nfs41_test_stateid(struct nfs_server *server,
9364 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009365 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009366{
9367 int status;
9368 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009369 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009370 };
9371 struct nfs41_test_stateid_res res;
9372 struct rpc_message msg = {
9373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9374 .rpc_argp = &args,
9375 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009376 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009377 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009378 struct rpc_clnt *rpc_client = server->client;
9379
9380 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9381 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009382
Chuck Lever38527b12012-07-11 16:30:23 -04009383 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009384 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009385 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009386 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009387 if (status != NFS_OK) {
9388 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009389 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009390 }
9391 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009392 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009393}
9394
Trond Myklebust43912bb2016-09-22 13:38:56 -04009395static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9396 int err, struct nfs4_exception *exception)
9397{
9398 exception->retry = 0;
9399 switch(err) {
9400 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009401 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009402 nfs4_handle_exception(server, err, exception);
9403 break;
9404 case -NFS4ERR_BADSESSION:
9405 case -NFS4ERR_BADSLOT:
9406 case -NFS4ERR_BAD_HIGH_SLOT:
9407 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9408 case -NFS4ERR_DEADSESSION:
9409 nfs4_do_handle_exception(server, err, exception);
9410 }
9411}
9412
Chuck Lever38527b12012-07-11 16:30:23 -04009413/**
9414 * nfs41_test_stateid - perform a TEST_STATEID operation
9415 *
9416 * @server: server / transport on which to perform the operation
9417 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009418 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009419 *
9420 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9421 * Otherwise a negative NFS4ERR value is returned if the operation
9422 * failed or the state ID is not currently valid.
9423 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009424static int nfs41_test_stateid(struct nfs_server *server,
9425 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009426 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009427{
9428 struct nfs4_exception exception = { };
9429 int err;
9430 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009431 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009432 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009433 } while (exception.retry);
9434 return err;
9435}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009436
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009437struct nfs_free_stateid_data {
9438 struct nfs_server *server;
9439 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009440 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009441};
9442
9443static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9444{
9445 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009446 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009447 &data->args.seq_args,
9448 &data->res.seq_res,
9449 task);
9450}
9451
9452static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9453{
9454 struct nfs_free_stateid_data *data = calldata;
9455
9456 nfs41_sequence_done(task, &data->res.seq_res);
9457
9458 switch (task->tk_status) {
9459 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009460 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009461 rpc_restart_call_prepare(task);
9462 }
9463}
9464
9465static void nfs41_free_stateid_release(void *calldata)
9466{
9467 kfree(calldata);
9468}
9469
Trond Myklebust17f26b12013-08-21 15:48:42 -04009470static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009471 .rpc_call_prepare = nfs41_free_stateid_prepare,
9472 .rpc_call_done = nfs41_free_stateid_done,
9473 .rpc_release = nfs41_free_stateid_release,
9474};
9475
Anna Schumaker2f261022018-05-15 13:03:39 -04009476/**
9477 * nfs41_free_stateid - perform a FREE_STATEID operation
9478 *
9479 * @server: server / transport on which to perform the operation
9480 * @stateid: state ID to release
9481 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009482 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009483 *
9484 * Note: this function is always asynchronous.
9485 */
9486static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009487 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009488 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009489 bool privileged)
9490{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009491 struct rpc_message msg = {
9492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009493 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009494 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009495 struct rpc_task_setup task_setup = {
9496 .rpc_client = server->client,
9497 .rpc_message = &msg,
9498 .callback_ops = &nfs41_free_stateid_ops,
9499 .flags = RPC_TASK_ASYNC,
9500 };
9501 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009502 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009503
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009504 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9505 &task_setup.rpc_client, &msg);
9506
Chuck Lever38527b12012-07-11 16:30:23 -04009507 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009508 data = kmalloc(sizeof(*data), GFP_NOFS);
9509 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009510 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009511 data->server = server;
9512 nfs4_stateid_copy(&data->args.stateid, stateid);
9513
9514 task_setup.callback_data = data;
9515
9516 msg.rpc_argp = &data->args;
9517 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009518 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009519 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009520 if (IS_ERR(task))
9521 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009522 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009523 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009524}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009525
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009526static void
9527nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009528{
NeilBrowna52458b2018-12-03 11:30:31 +11009529 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009530
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009531 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009532 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009533}
9534
Trond Myklebust36281ca2012-03-04 18:13:56 -05009535static bool nfs41_match_stateid(const nfs4_stateid *s1,
9536 const nfs4_stateid *s2)
9537{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009538 if (s1->type != s2->type)
9539 return false;
9540
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009541 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009542 return false;
9543
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009544 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009545 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009546
Anna Schumaker045c5512017-01-11 16:59:48 -05009547 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009548}
9549
Andy Adamson557134a2009-04-01 09:21:53 -04009550#endif /* CONFIG_NFS_V4_1 */
9551
Trond Myklebust36281ca2012-03-04 18:13:56 -05009552static bool nfs4_match_stateid(const nfs4_stateid *s1,
9553 const nfs4_stateid *s2)
9554{
Trond Myklebustf597c532012-03-04 18:13:56 -05009555 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009556}
9557
9558
Trond Myklebust17280172012-03-11 13:11:00 -04009559static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009560 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009561 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562 .recover_open = nfs4_open_reclaim,
9563 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009564 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009565 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009566};
9567
Andy Adamson591d71c2009-04-01 09:22:47 -04009568#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009569static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009570 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9571 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9572 .recover_open = nfs4_open_reclaim,
9573 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009574 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009575 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009576 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009577};
9578#endif /* CONFIG_NFS_V4_1 */
9579
Trond Myklebust17280172012-03-11 13:11:00 -04009580static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009581 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009582 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009583 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009584 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009585 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009586};
9587
Andy Adamson591d71c2009-04-01 09:22:47 -04009588#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009589static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009590 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9591 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009592 .recover_open = nfs41_open_expired,
9593 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009594 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009595};
9596#endif /* CONFIG_NFS_V4_1 */
9597
Trond Myklebust17280172012-03-11 13:11:00 -04009598static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009599 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009600 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009601 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009602};
9603
9604#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009605static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009606 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009607 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009608 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009609};
9610#endif
9611
Chuck Leverec011fe2013-10-17 14:12:39 -04009612static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009613 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009614 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009615};
9616
9617#if defined(CONFIG_NFS_V4_1)
9618static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009619 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009620 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009621};
9622#endif /* CONFIG_NFS_V4_1 */
9623
Trond Myklebust97dc1352010-06-16 09:52:26 -04009624static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9625 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009626 .init_caps = NFS_CAP_READDIRPLUS
9627 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009628 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009629 .init_client = nfs40_init_client,
9630 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009631 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009632 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009633 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009634 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009635 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009636 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009637 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9638 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9639 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009640 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009641};
9642
9643#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009644static struct nfs_seqid *
9645nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9646{
9647 return NULL;
9648}
9649
Trond Myklebust97dc1352010-06-16 09:52:26 -04009650static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9651 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009652 .init_caps = NFS_CAP_READDIRPLUS
9653 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009654 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009655 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009656 | NFS_CAP_ATOMIC_OPEN_V1
9657 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009658 .init_client = nfs41_init_client,
9659 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009660 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009661 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009662 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009663 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009664 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009665 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009666 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009667 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9668 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9669 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009670 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009671};
9672#endif
9673
Steve Dickson42c2c422013-05-22 12:50:38 -04009674#if defined(CONFIG_NFS_V4_2)
9675static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9676 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009677 .init_caps = NFS_CAP_READDIRPLUS
9678 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009679 | NFS_CAP_POSIX_LOCK
9680 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009681 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009682 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009683 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009684 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009685 | NFS_CAP_OFFLOAD_CANCEL
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009686 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009687 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009688 | NFS_CAP_LAYOUTSTATS
9689 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009690 .init_client = nfs41_init_client,
9691 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009692 .match_stateid = nfs41_match_stateid,
9693 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009694 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009695 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009696 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009697 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009698 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009699 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9700 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9701 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009702 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009703};
9704#endif
9705
Trond Myklebust97dc1352010-06-16 09:52:26 -04009706const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9707 [0] = &nfs_v4_0_minor_ops,
9708#if defined(CONFIG_NFS_V4_1)
9709 [1] = &nfs_v4_1_minor_ops,
9710#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009711#if defined(CONFIG_NFS_V4_2)
9712 [2] = &nfs_v4_2_minor_ops,
9713#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009714};
9715
Trond Myklebust13997822016-07-24 17:10:52 -04009716static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009717{
9718 ssize_t error, error2;
9719
9720 error = generic_listxattr(dentry, list, size);
9721 if (error < 0)
9722 return error;
9723 if (list) {
9724 list += error;
9725 size -= error;
9726 }
9727
9728 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9729 if (error2 < 0)
9730 return error2;
9731 return error + error2;
9732}
9733
Trond Myklebust17f26b12013-08-21 15:48:42 -04009734static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009735 .create = nfs_create,
9736 .lookup = nfs_lookup,
9737 .atomic_open = nfs_atomic_open,
9738 .link = nfs_link,
9739 .unlink = nfs_unlink,
9740 .symlink = nfs_symlink,
9741 .mkdir = nfs_mkdir,
9742 .rmdir = nfs_rmdir,
9743 .mknod = nfs_mknod,
9744 .rename = nfs_rename,
9745 .permission = nfs_permission,
9746 .getattr = nfs_getattr,
9747 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009748 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009749};
9750
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009751static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009752 .permission = nfs_permission,
9753 .getattr = nfs_getattr,
9754 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009755 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009756};
9757
David Howells509de812006-08-22 20:06:11 -04009758const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009759 .version = 4, /* protocol version */
9760 .dentry_ops = &nfs4_dentry_operations,
9761 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009762 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009763 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009765 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009766 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767 .getattr = nfs4_proc_getattr,
9768 .setattr = nfs4_proc_setattr,
9769 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009770 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009771 .access = nfs4_proc_access,
9772 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773 .create = nfs4_proc_create,
9774 .remove = nfs4_proc_remove,
9775 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009776 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009778 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009779 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009780 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009781 .link = nfs4_proc_link,
9782 .symlink = nfs4_proc_symlink,
9783 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009784 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 .readdir = nfs4_proc_readdir,
9786 .mknod = nfs4_proc_mknod,
9787 .statfs = nfs4_proc_statfs,
9788 .fsinfo = nfs4_proc_fsinfo,
9789 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009790 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009792 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009794 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009795 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009796 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009797 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009798 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009799 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009801 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009802 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009803 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009804 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009805 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009806 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009807 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009808 .create_server = nfs4_create_server,
9809 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810};
9811
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009812static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009813 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009814 .list = nfs4_xattr_list_nfs4_acl,
9815 .get = nfs4_xattr_get_nfs4_acl,
9816 .set = nfs4_xattr_set_nfs4_acl,
9817};
9818
9819const struct xattr_handler *nfs4_xattr_handlers[] = {
9820 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009821#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9822 &nfs4_xattr_nfs4_label_handler,
9823#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009824 NULL
9825};
9826
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827/*
9828 * Local variables:
9829 * c-basic-offset: 8
9830 * End:
9831 */