blob: 72961b5f69933d2df807f6842bc6e88101e05495 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust30846df2018-04-07 13:44:28 -040074#define NFS4_BITMASK_SZ 3
75
Trond Myklebust2066fe82006-09-15 08:30:46 -040076#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define NFS4_POLL_RETRY_MAX (15*HZ)
78
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020079/* file attributes which can be mapped to nfs attributes */
80#define NFS4_VALID_ATTRS (ATTR_MODE \
81 | ATTR_UID \
82 | ATTR_GID \
83 | ATTR_SIZE \
84 | ATTR_ATIME \
85 | ATTR_MTIME \
86 | ATTR_CTIME \
87 | ATTR_ATIME_SET \
88 | ATTR_MTIME_SET)
89
Trond Myklebustcdd4e682006-01-03 09:55:12 +010090struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080091static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050093static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040094static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110096static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040097 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110098 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040099 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400100#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400101static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100102 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400103 struct nfs4_slot *slot,
104 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400105static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100106 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400107static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100108 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400109#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400110
111#ifdef CONFIG_NFS_V4_SECURITY_LABEL
112static inline struct nfs4_label *
113nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
115{
116 int err;
117
118 if (label == NULL)
119 return NULL;
120
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
122 return NULL;
123
David Quigleyaa9c2662013-05-22 12:50:44 -0400124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
126 if (err == 0)
127 return label;
128
129 return NULL;
130}
131static inline void
132nfs4_label_release_security(struct nfs4_label *label)
133{
134 if (label)
135 security_release_secctx(label->label, label->len);
136}
137static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138{
139 if (label)
140 return server->attr_bitmask;
141
142 return server->attr_bitmask_nl;
143}
144#else
145static inline struct nfs4_label *
146nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
148{ return NULL; }
149static inline void
150nfs4_label_release_security(struct nfs4_label *label)
151{ return; }
152static inline u32 *
153nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154{ return server->attr_bitmask; }
155#endif
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500158static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 if (err >= -1000)
161 return err;
162 switch (err) {
163 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400166 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000167 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400168 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000169 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
172 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400173 case -NFS4ERR_SHARE_DENIED:
174 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400177 case -NFS4ERR_FILE_OPEN:
178 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700181 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400184 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/*
188 * This is our standard bitmap for GETATTR requests.
189 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400190const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 FATTR4_WORD0_TYPE
192 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_SIZE
194 | FATTR4_WORD0_FSID
195 | FATTR4_WORD0_FILEID,
196 FATTR4_WORD1_MODE
197 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400206#ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
Trond Myklebust1549210f2012-06-05 09:16:47 -0400211static const u32 nfs4_pnfs_open_bitmap[3] = {
212 FATTR4_WORD0_TYPE
213 | FATTR4_WORD0_CHANGE
214 | FATTR4_WORD0_SIZE
215 | FATTR4_WORD0_FSID
216 | FATTR4_WORD0_FILEID,
217 FATTR4_WORD1_MODE
218 | FATTR4_WORD1_NUMLINKS
219 | FATTR4_WORD1_OWNER
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500227#ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
229#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400230};
231
Andy Adamsone23008e2012-10-02 21:07:32 -0400232static const u32 nfs4_open_noattr_bitmap[3] = {
233 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400234 | FATTR4_WORD0_FILEID,
235};
236
David Quigleya09df2c2013-05-22 12:50:41 -0400237const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 FATTR4_WORD0_MAXLINK
248 | FATTR4_WORD0_MAXNAME,
249 0
250};
251
Fred Isamandae100c2011-07-30 20:52:37 -0400252const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700256 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800259 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
David Quigleya09df2c2013-05-22 12:50:41 -0400262const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500263 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400264 | FATTR4_WORD0_SIZE
265 | FATTR4_WORD0_FSID
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500268 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400276};
277
Trond Myklebust30846df2018-04-07 13:44:28 -0400278static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
279 struct inode *inode)
280{
281 unsigned long cache_validity;
282
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
285 return;
286
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
291
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
294
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
297}
298
299static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
301{
302 nfs4_bitmap_copy_adjust(dst, src, inode);
303}
304
Al Virobc4785c2006-10-19 23:28:51 -0700305static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct nfs4_readdir_arg *readdir)
307{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700309 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000312 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
314 return;
315 }
316
317 readdir->cookie = 0;
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
319 if (cookie == 2)
320 return;
321
322 /*
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
327 * instead of 1 or 2.
328 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800329 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (cookie == 0) {
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
337 p++;
338 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
350 p++;
351 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800359 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Trond Myklebust26d36302016-09-22 13:39:05 -0400362static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100364 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400365{
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
367
368 ops->test_and_free_expired(server, stateid, cred);
369}
370
371static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100373 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400374{
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
377}
378
379static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100381 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400382{
383 nfs4_stateid tmp;
384
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
387}
388
NeilBrown8478eaa2014-09-18 16:09:27 +1000389static long nfs4_update_delay(long *timeout)
390{
391 long ret;
392 if (!timeout)
393 return NFS4_POLL_RETRY_MAX;
394 if (*timeout <= 0)
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
398 ret = *timeout;
399 *timeout <<= 1;
400 return ret;
401}
402
Trond Myklebust65de8722008-12-23 15:21:44 -0500403static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
404{
405 int res = 0;
406
407 might_sleep();
408
NeilBrown8478eaa2014-09-18 16:09:27 +1000409 freezable_schedule_timeout_killable_unsafe(
410 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500411 if (fatal_signal_pending(current))
412 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 return res;
414}
415
416/* This is the error handling routine for processes that are allowed
417 * to sleep.
418 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400419static int nfs4_do_handle_exception(struct nfs_server *server,
420 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500421{
422 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500423 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400424 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500425 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500426 int ret = errorcode;
427
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400428 exception->delay = 0;
429 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500430 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400431
432 if (stateid == NULL && state != NULL)
433 stateid = &state->stateid;
434
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 switch(errorcode) {
436 case 0:
437 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400438 case -NFS4ERR_BADHANDLE:
439 case -ESTALE:
440 if (inode != NULL && S_ISREG(inode->i_mode))
441 pnfs_destroy_layout(NFS_I(inode));
442 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400443 case -NFS4ERR_DELEG_REVOKED:
444 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400445 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400446 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400447 if (inode != NULL && stateid != NULL) {
448 nfs_inode_find_state_and_recover(inode,
449 stateid);
450 goto wait_on_recovery;
451 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500452 /* Fall through */
Trond Myklebust272289a2016-09-22 13:39:15 -0400453 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400454 if (inode) {
455 int err;
456
457 err = nfs_async_inode_return_delegation(inode,
458 stateid);
459 if (err == 0)
460 goto wait_on_recovery;
461 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
462 exception->retry = 1;
463 break;
464 }
465 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500466 if (state == NULL)
467 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400468 ret = nfs4_schedule_stateid_recovery(server, state);
469 if (ret < 0)
470 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500471 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500472 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500473 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500474 nfs4_schedule_lease_recovery(clp);
475 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400476 case -NFS4ERR_MOVED:
477 ret = nfs4_schedule_migration_recovery(server);
478 if (ret < 0)
479 break;
480 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400481 case -NFS4ERR_LEASE_MOVED:
482 nfs4_schedule_lease_moved_recovery(clp);
483 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500484#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400485 case -NFS4ERR_BADSESSION:
486 case -NFS4ERR_BADSLOT:
487 case -NFS4ERR_BAD_HIGH_SLOT:
488 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
489 case -NFS4ERR_DEADSESSION:
490 case -NFS4ERR_SEQ_FALSE_RETRY:
491 case -NFS4ERR_SEQ_MISORDERED:
492 dprintk("%s ERROR: %d Reset session\n", __func__,
493 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400494 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400495 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500496#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500497 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500498 if (exception->timeout > HZ) {
499 /* We have retried a decent amount, time to
500 * fail
501 */
502 ret = -EBUSY;
503 break;
504 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500505 /* Fall through */
Trond Myklebust65de8722008-12-23 15:21:44 -0500506 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400507 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500508 /* Fall through */
Trond Myklebust2598ed32015-09-20 16:10:18 -0400509 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400510 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400511 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400512 exception->delay = 1;
513 return 0;
514
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400515 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500516 case -NFS4ERR_OLD_STATEID:
517 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800518 break;
519 case -NFS4ERR_BADOWNER:
520 /* The following works around a Linux server bug! */
521 case -NFS4ERR_BADNAME:
522 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
523 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
524 exception->retry = 1;
525 printk(KERN_WARNING "NFS: v4 server %s "
526 "does not accept raw "
527 "uid/gids. "
528 "Reenabling the idmapper.\n",
529 server->nfs_client->cl_hostname);
530 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500531 }
532 /* We failed to handle the error */
533 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500534wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400535 exception->recovering = 1;
536 return 0;
537}
538
539/* This is the error handling routine for processes that are allowed
540 * to sleep.
541 */
542int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
543{
544 struct nfs_client *clp = server->nfs_client;
545 int ret;
546
547 ret = nfs4_do_handle_exception(server, errorcode, exception);
548 if (exception->delay) {
549 ret = nfs4_delay(server->client, &exception->timeout);
550 goto out_retry;
551 }
552 if (exception->recovering) {
553 ret = nfs4_wait_clnt_recover(clp);
554 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
555 return -EIO;
556 goto out_retry;
557 }
558 return ret;
559out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500560 if (ret == 0)
561 exception->retry = 1;
562 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500563}
564
Trond Myklebust037fc982015-09-20 15:51:00 -0400565static int
566nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
567 int errorcode, struct nfs4_exception *exception)
568{
569 struct nfs_client *clp = server->nfs_client;
570 int ret;
571
572 ret = nfs4_do_handle_exception(server, errorcode, exception);
573 if (exception->delay) {
574 rpc_delay(task, nfs4_update_delay(&exception->timeout));
575 goto out_retry;
576 }
577 if (exception->recovering) {
578 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
579 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
580 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
581 goto out_retry;
582 }
583 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
584 ret = -EIO;
585 return ret;
586out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500587 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400588 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500589 /*
590 * For NFS4ERR_MOVED, the client transport will need to
591 * be recomputed after migration recovery has completed.
592 */
593 if (errorcode == -NFS4ERR_MOVED)
594 rpc_task_release_transport(task);
595 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400596 return ret;
597}
598
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400599int
Trond Myklebust037fc982015-09-20 15:51:00 -0400600nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
601 struct nfs4_state *state, long *timeout)
602{
603 struct nfs4_exception exception = {
604 .state = state,
605 };
606
607 if (task->tk_status >= 0)
608 return 0;
609 if (timeout)
610 exception.timeout = *timeout;
611 task->tk_status = nfs4_async_handle_exception(task, server,
612 task->tk_status,
613 &exception);
614 if (exception.delay && timeout)
615 *timeout = exception.timeout;
616 if (exception.retry)
617 return -EAGAIN;
618 return 0;
619}
620
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400621/*
622 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
623 * or 'false' otherwise.
624 */
625static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
626{
627 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500628 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400629}
Trond Myklebust65de8722008-12-23 15:21:44 -0500630
Trond Myklebust452e9352010-07-31 14:29:06 -0400631static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 spin_lock(&clp->cl_lock);
634 if (time_before(clp->cl_last_renewal,timestamp))
635 clp->cl_last_renewal = timestamp;
636 spin_unlock(&clp->cl_lock);
637}
638
Trond Myklebust452e9352010-07-31 14:29:06 -0400639static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
640{
Trond Myklebustbe824162015-07-05 14:50:46 -0400641 struct nfs_client *clp = server->nfs_client;
642
643 if (!nfs4_has_session(clp))
644 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400645}
646
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400647struct nfs4_call_sync_data {
648 const struct nfs_server *seq_server;
649 struct nfs4_sequence_args *seq_args;
650 struct nfs4_sequence_res *seq_res;
651};
652
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800653void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400654 struct nfs4_sequence_res *res, int cache_reply,
655 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400656{
657 args->sa_slot = NULL;
658 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400659 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400660
661 res->sr_slot = NULL;
662}
663
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400664static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400665{
666 struct nfs4_slot *slot = res->sr_slot;
667 struct nfs4_slot_table *tbl;
668
Chuck Lever3bd23842013-08-09 12:49:19 -0400669 tbl = slot->table;
670 spin_lock(&tbl->slot_tbl_lock);
671 if (!nfs41_wake_and_assign_slot(tbl, slot))
672 nfs4_free_slot(tbl, slot);
673 spin_unlock(&tbl->slot_tbl_lock);
674
675 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400676}
677
678static int nfs40_sequence_done(struct rpc_task *task,
679 struct nfs4_sequence_res *res)
680{
681 if (res->sr_slot != NULL)
682 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400683 return 1;
684}
685
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400686#if defined(CONFIG_NFS_V4_1)
687
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400688static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400689{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500690 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400691 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500692 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400693
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400694 if (!slot)
695 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500696 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500698
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400699 /* Bump the slot sequence number */
700 if (slot->seq_done)
701 slot->seq_nr++;
702 slot->seq_done = 0;
703
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500704 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500705 /* Be nice to the server: try to ensure that the last transmitted
706 * value for highest_user_slotid <= target_highest_slotid
707 */
708 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
709 send_new_highest_used_slotid = true;
710
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500711 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500712 send_new_highest_used_slotid = false;
713 goto out_unlock;
714 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500716
717 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
718 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500719out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500720 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500721 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400722 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400723 if (waitqueue_active(&tbl->slot_waitq))
724 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400725}
726
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400727static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
728{
729 nfs41_release_slot(res->sr_slot);
730 res->sr_slot = NULL;
731}
732
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400733static int nfs41_sequence_process(struct rpc_task *task,
734 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400735{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500736 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500737 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400738 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500739 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500740 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400741
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500742 if (slot == NULL)
743 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400744 /* don't increment the sequence number if the task wasn't sent */
745 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400746 goto out;
747
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500748 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500749
Trond Myklebustac20d162012-12-15 15:36:07 -0500750 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400751 if (res->sr_status != -NFS4ERR_DELAY)
752 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500753 interrupted = true;
754 }
755
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400756 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500757 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400758 switch (res->sr_status) {
759 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400760 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400761 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500762 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500763 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500764 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400765 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
766 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500767 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400768 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500769 case 1:
770 /*
771 * sr_status remains 1 if an RPC level error occurred.
772 * The server may or may not have processed the sequence
773 * operation..
774 * Mark the slot as having hosted an interrupted RPC call.
775 */
776 slot->interrupted = 1;
777 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400778 case -NFS4ERR_DELAY:
779 /* The server detected a resend of the RPC call and
780 * returned NFS4ERR_DELAY as per Section 2.10.6.2
781 * of RFC5661.
782 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500783 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400784 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500785 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500786 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400787 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400788 case -NFS4ERR_RETRY_UNCACHED_REP:
789 case -NFS4ERR_SEQ_FALSE_RETRY:
790 /*
791 * The server thinks we tried to replay a request.
792 * Retry the call after bumping the sequence ID.
793 */
794 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500795 case -NFS4ERR_BADSLOT:
796 /*
797 * The slot id we used was probably retired. Try again
798 * using a different slot id.
799 */
Trond Myklebust99589102018-06-09 12:50:50 -0400800 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400801 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500802 goto retry_nowait;
803 case -NFS4ERR_SEQ_MISORDERED:
804 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500805 * Was the last operation on this sequence interrupted?
806 * If so, retry after bumping the sequence number.
807 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400808 if (interrupted)
809 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500810 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500811 * Could this slot have been previously retired?
812 * If so, then the server may be expecting seq_nr = 1!
813 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500814 if (slot->seq_nr != 1) {
815 slot->seq_nr = 1;
816 goto retry_nowait;
817 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400818 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400819 default:
820 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400821 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400822 }
823out:
824 /* The session may be reset by one of the error handlers. */
825 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500826out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500827 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400828session_recover:
829 nfs4_schedule_session_recovery(session, res->sr_status);
830 goto retry_nowait;
831retry_new_seq:
832 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500833retry_nowait:
834 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400835 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500836 task->tk_status = 0;
837 ret = 0;
838 }
839 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400840out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400841 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400842 goto out;
843 rpc_delay(task, NFS4_POLL_RETRY_MAX);
844 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400845}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400846
847int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
848{
849 if (!nfs41_sequence_process(task, res))
850 return 0;
851 if (res->sr_slot != NULL)
852 nfs41_sequence_free_slot(res);
853 return 1;
854
855}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500856EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400857
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400858static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
859{
860 if (res->sr_slot == NULL)
861 return 1;
862 if (res->sr_slot->table->session != NULL)
863 return nfs41_sequence_process(task, res);
864 return nfs40_sequence_done(task, res);
865}
866
867static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
868{
869 if (res->sr_slot != NULL) {
870 if (res->sr_slot->table->session != NULL)
871 nfs41_sequence_free_slot(res);
872 else
873 nfs40_sequence_free_slot(res);
874 }
875}
876
Peng Tao2c4b1312014-06-11 05:24:16 +0800877int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400878{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500879 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400880 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400881 if (!res->sr_slot->table->session)
882 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400883 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400884}
Peng Tao2c4b1312014-06-11 05:24:16 +0800885EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400886
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
888{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400889 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400890
Trond Myklebust035168ab2010-06-16 09:52:26 -0400891 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
892
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500893 nfs4_setup_sequence(data->seq_server->nfs_client,
894 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895}
896
Andy Adamson69ab40c2009-04-01 09:22:19 -0400897static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
898{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400899 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400900
Trond Myklebust14516c32010-07-31 14:29:06 -0400901 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400902}
903
Trond Myklebust17280172012-03-11 13:11:00 -0400904static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400905 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400906 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400907};
908
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400909static void
910nfs4_sequence_process_interrupted(struct nfs_client *client,
NeilBrowna52458b2018-12-03 11:30:31 +1100911 struct nfs4_slot *slot, const struct cred *cred)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400912{
913 struct rpc_task *task;
914
915 task = _nfs41_proc_sequence(client, cred, slot, true);
916 if (!IS_ERR(task))
917 rpc_put_task_async(task);
918}
919
Chuck Lever3bd23842013-08-09 12:49:19 -0400920#else /* !CONFIG_NFS_V4_1 */
921
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400922static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
923{
924 return nfs40_sequence_done(task, res);
925}
926
927static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
928{
929 if (res->sr_slot != NULL)
930 nfs40_sequence_free_slot(res);
931}
932
Peng Tao2c4b1312014-06-11 05:24:16 +0800933int nfs4_sequence_done(struct rpc_task *task,
934 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400935{
Chuck Lever3bd23842013-08-09 12:49:19 -0400936 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400937}
Peng Tao2c4b1312014-06-11 05:24:16 +0800938EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400939
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400940static void
941nfs4_sequence_process_interrupted(struct nfs_client *client,
NeilBrowna52458b2018-12-03 11:30:31 +1100942 struct nfs4_slot *slot, const struct cred *cred)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400943{
944 WARN_ON_ONCE(1);
945 slot->interrupted = 0;
946}
947
Chuck Lever3bd23842013-08-09 12:49:19 -0400948#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400949
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400950static
951void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
952 struct nfs4_sequence_res *res,
953 struct nfs4_slot *slot)
954{
955 if (!slot)
956 return;
957 slot->privileged = args->sa_privileged ? 1 : 0;
958 args->sa_slot = slot;
959
960 res->sr_slot = slot;
961 res->sr_timestamp = jiffies;
962 res->sr_status_flags = 0;
963 res->sr_status = 1;
964
965}
966
967int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500968 struct nfs4_sequence_args *args,
969 struct nfs4_sequence_res *res,
970 struct rpc_task *task)
971{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500972 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500973 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500974 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500975
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500976 /* slot already allocated? */
977 if (res->sr_slot != NULL)
978 goto out_start;
979
Anna Schumaker76ee0352017-01-10 16:49:31 -0500980 if (session) {
981 tbl = &session->fc_slot_table;
982 task->tk_timeout = 0;
983 }
984
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400985 for (;;) {
986 spin_lock(&tbl->slot_tbl_lock);
987 /* The state manager will wait until the slot table is empty */
988 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
989 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500990
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400991 slot = nfs4_alloc_slot(tbl);
992 if (IS_ERR(slot)) {
993 /* Try again in 1/4 second */
994 if (slot == ERR_PTR(-ENOMEM))
995 task->tk_timeout = HZ >> 2;
996 goto out_sleep;
997 }
998 spin_unlock(&tbl->slot_tbl_lock);
999
1000 if (likely(!slot->interrupted))
1001 break;
1002 nfs4_sequence_process_interrupted(client,
1003 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -05001004 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001005
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001006 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001007
Anna Schumakerad05cc02017-01-11 13:37:06 -05001008 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001009out_start:
1010 rpc_call_start(task);
1011 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001012
1013out_sleep:
1014 if (args->sa_privileged)
1015 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1016 NULL, RPC_PRIORITY_PRIVILEGED);
1017 else
1018 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1019 spin_unlock(&tbl->slot_tbl_lock);
1020 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001021}
1022EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1023
Chuck Lever9915ea72013-08-09 12:48:27 -04001024static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1025{
1026 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001027 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001028 data->seq_args, data->seq_res, task);
1029}
1030
1031static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1032{
1033 struct nfs4_call_sync_data *data = calldata;
1034 nfs4_sequence_done(task, data->seq_res);
1035}
1036
1037static const struct rpc_call_ops nfs40_call_sync_ops = {
1038 .rpc_call_prepare = nfs40_call_sync_prepare,
1039 .rpc_call_done = nfs40_call_sync_done,
1040};
1041
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001042static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001043 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001045 struct nfs4_sequence_args *args,
1046 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001047{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048 int ret;
1049 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001050 struct nfs_client *clp = server->nfs_client;
1051 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 .seq_server = server,
1053 .seq_args = args,
1054 .seq_res = res,
1055 };
1056 struct rpc_task_setup task_setup = {
1057 .rpc_client = clnt,
1058 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001059 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001060 .callback_data = &data
1061 };
1062
1063 task = rpc_run_task(&task_setup);
1064 if (IS_ERR(task))
1065 ret = PTR_ERR(task);
1066 else {
1067 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001068 rpc_put_task(task);
1069 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001070 return ret;
1071}
1072
Bryan Schumaker7c513052011-03-24 17:12:24 +00001073int nfs4_call_sync(struct rpc_clnt *clnt,
1074 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001075 struct rpc_message *msg,
1076 struct nfs4_sequence_args *args,
1077 struct nfs4_sequence_res *res,
1078 int cache_reply)
1079{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001080 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001081 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Trond Myklebust3c591172018-07-31 15:54:10 -04001084static void
1085nfs4_inc_nlink_locked(struct inode *inode)
1086{
1087 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1088 inc_nlink(inode);
1089}
1090
1091static void
1092nfs4_dec_nlink_locked(struct inode *inode)
1093{
1094 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1095 drop_nlink(inode);
1096}
1097
1098static void
1099update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001100 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct nfs_inode *nfsi = NFS_I(dir);
1103
Trond Myklebust16e14372018-03-20 16:53:31 -04001104 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1105 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001106 | NFS_INO_INVALID_DATA
1107 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001108 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001109 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1110 nfsi->attrtimeo_timestamp = jiffies;
1111 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001113 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001114 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1115 NFS_INO_INVALID_ACL;
1116 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001117 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001118 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001119 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001120 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001121 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001122}
1123
1124static void
1125update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001126 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001127{
1128 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001129 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 spin_unlock(&dir->i_lock);
1131}
1132
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001133struct nfs4_open_createattrs {
1134 struct nfs4_label *label;
1135 struct iattr *sattr;
1136 const __u32 verf[2];
1137};
1138
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001139static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1140 int err, struct nfs4_exception *exception)
1141{
1142 if (err != -EINVAL)
1143 return false;
1144 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1145 return false;
1146 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1147 exception->retry = 1;
1148 return true;
1149}
1150
Trond Myklebust6ae37332015-01-30 14:21:14 -05001151static u32
1152nfs4_map_atomic_open_share(struct nfs_server *server,
1153 fmode_t fmode, int openflags)
1154{
1155 u32 res = 0;
1156
1157 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1158 case FMODE_READ:
1159 res = NFS4_SHARE_ACCESS_READ;
1160 break;
1161 case FMODE_WRITE:
1162 res = NFS4_SHARE_ACCESS_WRITE;
1163 break;
1164 case FMODE_READ|FMODE_WRITE:
1165 res = NFS4_SHARE_ACCESS_BOTH;
1166 }
1167 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1168 goto out;
1169 /* Want no delegation if we're using O_DIRECT */
1170 if (openflags & O_DIRECT)
1171 res |= NFS4_SHARE_WANT_NO_DELEG;
1172out:
1173 return res;
1174}
1175
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001176static enum open_claim_type4
1177nfs4_map_atomic_open_claim(struct nfs_server *server,
1178 enum open_claim_type4 claim)
1179{
1180 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1181 return claim;
1182 switch (claim) {
1183 default:
1184 return claim;
1185 case NFS4_OPEN_CLAIM_FH:
1186 return NFS4_OPEN_CLAIM_NULL;
1187 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1188 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1189 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1190 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1191 }
1192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195{
1196 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001197 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 p->o_res.seqid = p->o_arg.seqid;
1199 p->c_res.seqid = p->c_arg.seqid;
1200 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001201 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001203 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001204}
1205
Al Viro82a2c1b2011-06-22 18:30:55 -04001206static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001207 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001208 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001209 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001210 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211{
Al Viro82a2c1b2011-06-22 18:30:55 -04001212 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001213 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001214 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001215 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001216 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001217 struct nfs4_opendata *p;
1218
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001219 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 if (p == NULL)
1221 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001222
1223 p->f_label = nfs4_label_alloc(server, gfp_mask);
1224 if (IS_ERR(p->f_label))
1225 goto err_free_p;
1226
Kinglong Meea49c2692015-07-27 15:31:38 +08001227 p->a_label = nfs4_label_alloc(server, gfp_mask);
1228 if (IS_ERR(p->a_label))
1229 goto err_free_f;
1230
Trond Myklebust63f5f792015-01-23 19:19:25 -05001231 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1232 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001233 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001234 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001235 nfs_sb_active(dentry->d_sb);
1236 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001237 p->dir = parent;
1238 p->owner = sp;
1239 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001240 p->o_arg.open_flags = flags;
1241 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001242 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001243 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001244 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1245 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001246 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1247 * will return permission denied for all bits until close */
1248 if (!(flags & O_EXCL)) {
1249 /* ask server to check for all possible rights as results
1250 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001251 switch (p->o_arg.claim) {
1252 default:
1253 break;
1254 case NFS4_OPEN_CLAIM_NULL:
1255 case NFS4_OPEN_CLAIM_FH:
1256 p->o_arg.access = NFS4_ACCESS_READ |
1257 NFS4_ACCESS_MODIFY |
1258 NFS4_ACCESS_EXTEND |
1259 NFS4_ACCESS_EXECUTE;
1260 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001261 }
David Howells7539bba2006-08-22 20:06:09 -04001262 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001263 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1264 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001265 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001267 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001268 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001269 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001270 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001271 case NFS4_OPEN_CLAIM_NULL:
1272 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1273 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1274 p->o_arg.fh = NFS_FH(dir);
1275 break;
1276 case NFS4_OPEN_CLAIM_PREVIOUS:
1277 case NFS4_OPEN_CLAIM_FH:
1278 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1279 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001280 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001281 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001282 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001284 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001285
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001286 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001287 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001288 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001289 p->c_arg.fh = &p->o_res.fh;
1290 p->c_arg.stateid = &p->o_res.stateid;
1291 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001292 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001293 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001294 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001295
1296err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001297 nfs4_label_free(p->a_label);
1298err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001299 nfs4_label_free(p->f_label);
1300err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 kfree(p);
1302err:
1303 dput(parent);
1304 return NULL;
1305}
1306
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001307static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001308{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309 struct nfs4_opendata *p = container_of(kref,
1310 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001311 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001312
Fred Isaman30ae2412016-10-18 13:39:51 -04001313 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001314 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001315 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001316 if (p->state != NULL)
1317 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001318 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001319
Kinglong Meea49c2692015-07-27 15:31:38 +08001320 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001321 nfs4_label_free(p->f_label);
1322
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001323 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001324 dput(p->dentry);
1325 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001326 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001327 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001328 kfree(p);
1329}
1330
1331static void nfs4_opendata_put(struct nfs4_opendata *p)
1332{
1333 if (p != NULL)
1334 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001335}
1336
Trond Myklebust24311f82015-09-20 10:50:17 -04001337static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1338 fmode_t fmode)
1339{
1340 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1341 case FMODE_READ|FMODE_WRITE:
1342 return state->n_rdwr != 0;
1343 case FMODE_WRITE:
1344 return state->n_wronly != 0;
1345 case FMODE_READ:
1346 return state->n_rdonly != 0;
1347 }
1348 WARN_ON_ONCE(1);
1349 return false;
1350}
1351
Trond Myklebustbe189f72018-09-27 17:12:33 -04001352static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1353 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001354{
1355 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001356
Trond Myklebust536e43d2012-01-17 22:04:26 -05001357 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001358 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001359 switch (claim) {
1360 case NFS4_OPEN_CLAIM_NULL:
1361 case NFS4_OPEN_CLAIM_FH:
1362 goto out;
1363 default:
1364 break;
1365 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001366 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001367 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001368 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1369 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001370 break;
1371 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001372 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1373 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001374 break;
1375 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001376 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1377 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001378 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001379out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001380 return ret;
1381}
1382
Trond Myklebust2a606182015-08-19 22:30:00 -05001383static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1384 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001385{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001386 if (delegation == NULL)
1387 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001388 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001389 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001390 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1391 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001392 switch (claim) {
1393 case NFS4_OPEN_CLAIM_NULL:
1394 case NFS4_OPEN_CLAIM_FH:
1395 break;
1396 case NFS4_OPEN_CLAIM_PREVIOUS:
1397 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1398 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001399 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001400 default:
1401 return 0;
1402 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001403 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001404 return 1;
1405}
1406
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001407static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001408{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001409 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001410 case FMODE_WRITE:
1411 state->n_wronly++;
1412 break;
1413 case FMODE_READ:
1414 state->n_rdonly++;
1415 break;
1416 case FMODE_READ|FMODE_WRITE:
1417 state->n_rdwr++;
1418 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001419 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001420}
1421
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001422#ifdef CONFIG_NFS_V4_1
1423static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1424{
1425 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1426 return true;
1427 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1428 return true;
1429 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1430 return true;
1431 return false;
1432}
1433#endif /* CONFIG_NFS_V4_1 */
1434
Trond Myklebustc9399f22017-11-06 15:28:01 -05001435static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1436{
1437 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1438 wake_up_all(&state->waitq);
1439}
1440
1441static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1442 const nfs4_stateid *stateid)
1443{
1444 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1445 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1446
1447 if (stateid_seqid == state_seqid + 1U ||
1448 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1449 nfs_state_log_update_open_stateid(state);
1450 else
1451 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1452}
1453
Trond Myklebust4f14c192014-02-12 19:15:06 -05001454static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001455{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001456 struct nfs_client *clp = state->owner->so_server->nfs_client;
1457 bool need_recover = false;
1458
1459 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1460 need_recover = true;
1461 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1462 need_recover = true;
1463 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1464 need_recover = true;
1465 if (need_recover)
1466 nfs4_state_mark_reclaim_nograce(clp, state);
1467}
1468
Trond Myklebustc9399f22017-11-06 15:28:01 -05001469/*
1470 * Check for whether or not the caller may update the open stateid
1471 * to the value passed in by stateid.
1472 *
1473 * Note: This function relies heavily on the server implementing
1474 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1475 * correctly.
1476 * i.e. The stateid seqids have to be initialised to 1, and
1477 * are then incremented on every state transition.
1478 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001479static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001480 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001481{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001482 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1483 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1484 if (stateid->seqid == cpu_to_be32(1))
1485 nfs_state_log_update_open_stateid(state);
1486 else
1487 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001488 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001489 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001490
1491 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1492 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001493 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001494 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001495 return false;
1496}
1497
Trond Myklebustf95549c2015-01-23 18:06:09 -05001498static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1499{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001500 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1501 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001502 if (state->n_wronly)
1503 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1504 if (state->n_rdonly)
1505 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1506 if (state->n_rdwr)
1507 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001508 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001509}
1510
Trond Myklebust226056c2014-02-11 10:41:07 -05001511static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1512 nfs4_stateid *stateid, fmode_t fmode)
1513{
1514 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1515 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1516 case FMODE_WRITE:
1517 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1518 break;
1519 case FMODE_READ:
1520 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1521 break;
1522 case 0:
1523 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1524 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1525 clear_bit(NFS_OPEN_STATE, &state->flags);
1526 }
1527 if (stateid == NULL)
1528 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001529 /* Handle OPEN+OPEN_DOWNGRADE races */
1530 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1531 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001532 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001533 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001534 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001535 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001536 nfs4_stateid_copy(&state->stateid, stateid);
1537 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001538 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001539out:
1540 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001541}
1542
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001543static void nfs_clear_open_stateid(struct nfs4_state *state,
1544 nfs4_stateid *arg_stateid,
1545 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001546{
1547 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001548 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1549 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1550 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001551 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001552 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1553 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001554}
1555
Trond Myklebust1393d962016-09-22 13:39:13 -04001556static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001557 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001558{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001559 DEFINE_WAIT(wait);
1560 int status = 0;
1561 for (;;) {
1562
1563 if (!nfs_need_update_open_stateid(state, stateid))
1564 return;
1565 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001566 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001567 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001568 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001569 /* Rely on seqids for serialisation with NFSv4.0 */
1570 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1571 break;
1572
1573 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1574 /*
1575 * Ensure we process the state changes in the same order
1576 * in which the server processed them by delaying the
1577 * update of the stateid until we are in sequence.
1578 */
1579 write_sequnlock(&state->seqlock);
1580 spin_unlock(&state->owner->so_lock);
1581 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001582 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001583 if (!signal_pending(current)) {
1584 if (schedule_timeout(5*HZ) == 0)
1585 status = -EAGAIN;
1586 else
1587 status = 0;
1588 } else
1589 status = -EINTR;
1590 finish_wait(&state->waitq, &wait);
1591 rcu_read_lock();
1592 spin_lock(&state->owner->so_lock);
1593 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001594 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001595
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001596 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1597 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001598 nfs4_stateid_copy(freeme, &state->open_stateid);
1599 nfs_test_and_clear_all_open_stateid(state);
1600 }
1601
Trond Myklebuste999e802014-02-10 18:20:47 -05001602 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1603 nfs4_stateid_copy(&state->stateid, stateid);
1604 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001605 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001606 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001607}
1608
Trond Myklebustc9399f22017-11-06 15:28:01 -05001609static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001610 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001611 fmode_t fmode,
1612 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001614 /*
1615 * Protect the call to nfs4_state_set_mode_locked and
1616 * serialise the stateid update
1617 */
1618 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001619 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1620 switch (fmode) {
1621 case FMODE_READ:
1622 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1623 break;
1624 case FMODE_WRITE:
1625 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1626 break;
1627 case FMODE_READ|FMODE_WRITE:
1628 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001629 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001630 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001631 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001632}
1633
1634static void nfs_state_set_delegation(struct nfs4_state *state,
1635 const nfs4_stateid *deleg_stateid,
1636 fmode_t fmode)
1637{
1638 /*
1639 * Protect the call to nfs4_state_set_mode_locked and
1640 * serialise the stateid update
1641 */
1642 write_seqlock(&state->seqlock);
1643 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1644 set_bit(NFS_DELEGATED_STATE, &state->flags);
1645 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001648static void nfs_state_clear_delegation(struct nfs4_state *state)
1649{
1650 write_seqlock(&state->seqlock);
1651 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1652 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1653 write_sequnlock(&state->seqlock);
1654}
1655
Trond Myklebust1393d962016-09-22 13:39:13 -04001656static int update_open_stateid(struct nfs4_state *state,
1657 const nfs4_stateid *open_stateid,
1658 const nfs4_stateid *delegation,
1659 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001660{
Trond Myklebust1393d962016-09-22 13:39:13 -04001661 struct nfs_server *server = NFS_SERVER(state->inode);
1662 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001663 struct nfs_inode *nfsi = NFS_I(state->inode);
1664 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001665 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001666 int ret = 0;
1667
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001668 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001669
1670 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001671 spin_lock(&state->owner->so_lock);
1672 if (open_stateid != NULL) {
1673 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1674 ret = 1;
1675 }
1676
Trond Myklebust34310432008-12-23 15:21:38 -05001677 deleg_cur = rcu_dereference(nfsi->delegation);
1678 if (deleg_cur == NULL)
1679 goto no_delegation;
1680
1681 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001682 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001683 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001684 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001685 goto no_delegation_unlock;
1686
1687 if (delegation == NULL)
1688 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001689 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001690 goto no_delegation_unlock;
1691
Trond Myklebustb7391f42008-12-23 15:21:52 -05001692 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001693 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001694 ret = 1;
1695no_delegation_unlock:
1696 spin_unlock(&deleg_cur->lock);
1697no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001698 if (ret)
1699 update_open_stateflags(state, fmode);
1700 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001701 rcu_read_unlock();
1702
Trond Myklebust4f14c192014-02-12 19:15:06 -05001703 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001704 nfs4_schedule_state_manager(clp);
1705 if (freeme.type != 0)
1706 nfs4_test_and_free_stateid(server, &freeme,
1707 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001708
1709 return ret;
1710}
1711
Trond Myklebust39071e62015-01-24 15:07:56 -05001712static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1713 const nfs4_stateid *stateid)
1714{
1715 struct nfs4_state *state = lsp->ls_state;
1716 bool ret = false;
1717
1718 spin_lock(&state->state_lock);
1719 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1720 goto out_noupdate;
1721 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1722 goto out_noupdate;
1723 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1724 ret = true;
1725out_noupdate:
1726 spin_unlock(&state->state_lock);
1727 return ret;
1728}
Trond Myklebust34310432008-12-23 15:21:38 -05001729
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001730static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001731{
1732 struct nfs_delegation *delegation;
1733
Trond Myklebustf5086242018-03-20 16:43:13 -04001734 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001735 rcu_read_lock();
1736 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001737 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001738 rcu_read_unlock();
1739 return;
1740 }
1741 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001742 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001743}
1744
Trond Myklebust6ee41262007-07-08 14:11:36 -04001745static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001746{
1747 struct nfs4_state *state = opendata->state;
1748 struct nfs_inode *nfsi = NFS_I(state->inode);
1749 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001750 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001751 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001752 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001753 nfs4_stateid stateid;
1754 int ret = -EAGAIN;
1755
Trond Myklebustaac00a82007-07-05 19:02:21 -04001756 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001757 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001758 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001759 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001760 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001761 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001762 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001763 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001764 rcu_read_lock();
1765 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001766 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001767 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001768 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001769 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001770 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001771 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001772 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001773 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001774 if (!opendata->is_recover) {
NeilBrowna52458b2018-12-03 11:30:31 +11001775 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001776 if (ret != 0)
1777 goto out;
1778 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001779 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001780
1781 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001782 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001783 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001784 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001785out:
1786 return ERR_PTR(ret);
1787out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001788 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001789 return state;
1790}
1791
Andy Adamsone23008e2012-10-02 21:07:32 -04001792static void
1793nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1794{
1795 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1796 struct nfs_delegation *delegation;
1797 int delegation_flags = 0;
1798
1799 rcu_read_lock();
1800 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1801 if (delegation)
1802 delegation_flags = delegation->flags;
1803 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001804 switch (data->o_arg.claim) {
1805 default:
1806 break;
1807 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1808 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001809 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1810 "returning a delegation for "
1811 "OPEN(CLAIM_DELEGATE_CUR)\n",
1812 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001813 return;
1814 }
1815 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001816 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001817 data->owner->so_cred,
1818 data->o_res.delegation_type,
1819 &data->o_res.delegation,
1820 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001821 else
1822 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001823 data->owner->so_cred,
1824 data->o_res.delegation_type,
1825 &data->o_res.delegation,
1826 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001827
1828 if (data->o_res.do_recall)
1829 nfs_async_inode_return_delegation(state->inode,
1830 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001831}
1832
1833/*
1834 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1835 * and update the nfs4_state.
1836 */
1837static struct nfs4_state *
1838_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1839{
1840 struct inode *inode = data->state->inode;
1841 struct nfs4_state *state = data->state;
1842 int ret;
1843
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001844 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001845 if (data->rpc_status)
1846 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001847 /* cached opens have already been processed */
1848 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001849 }
1850
Andy Adamsone23008e2012-10-02 21:07:32 -04001851 ret = nfs_refresh_inode(inode, &data->f_attr);
1852 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001853 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001854
1855 if (data->o_res.delegation_type != 0)
1856 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001857update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001858 update_open_stateid(state, &data->o_res.stateid, NULL,
1859 data->o_arg.fmode);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001860 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001861
1862 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001863}
1864
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001865static struct inode *
1866nfs4_opendata_get_inode(struct nfs4_opendata *data)
1867{
1868 struct inode *inode;
1869
1870 switch (data->o_arg.claim) {
1871 case NFS4_OPEN_CLAIM_NULL:
1872 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1873 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1874 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1875 return ERR_PTR(-EAGAIN);
1876 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1877 &data->f_attr, data->f_label);
1878 break;
1879 default:
1880 inode = d_inode(data->dentry);
1881 ihold(inode);
1882 nfs_refresh_inode(inode, &data->f_attr);
1883 }
1884 return inode;
1885}
1886
Andy Adamsone23008e2012-10-02 21:07:32 -04001887static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001888nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1889{
1890 struct nfs4_state *state;
1891 struct inode *inode;
1892
1893 inode = nfs4_opendata_get_inode(data);
1894 if (IS_ERR(inode))
1895 return ERR_CAST(inode);
1896 if (data->state != NULL && data->state->inode == inode) {
1897 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001898 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04001899 } else
1900 state = nfs4_get_open_state(inode, data->owner);
1901 iput(inode);
1902 if (state == NULL)
1903 state = ERR_PTR(-ENOMEM);
1904 return state;
1905}
1906
Andy Adamsone23008e2012-10-02 21:07:32 -04001907static struct nfs4_state *
1908_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001909{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001910 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001911
Trond Myklebustaac00a82007-07-05 19:02:21 -04001912 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001913 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001914 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001915 goto out;
1916 }
1917
Trond Myklebust75e8c482017-08-08 10:38:07 -04001918 state = nfs4_opendata_find_nfs4_state(data);
1919 if (IS_ERR(state))
1920 goto out;
1921
Andy Adamsone23008e2012-10-02 21:07:32 -04001922 if (data->o_res.delegation_type != 0)
1923 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001924 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001925 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001926out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001927 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001928 return state;
1929}
1930
Andy Adamsone23008e2012-10-02 21:07:32 -04001931static struct nfs4_state *
1932nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1933{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001934 struct nfs4_state *ret;
1935
Andy Adamsone23008e2012-10-02 21:07:32 -04001936 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001937 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1938 else
1939 ret = _nfs4_opendata_to_nfs4_state(data);
1940 nfs4_sequence_free_slot(&data->o_res.seq_res);
1941 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001942}
1943
Trond Myklebust0de43972018-09-02 15:57:01 -04001944static struct nfs_open_context *
1945nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001946{
1947 struct nfs_inode *nfsi = NFS_I(state->inode);
1948 struct nfs_open_context *ctx;
1949
Trond Myklebust0de43972018-09-02 15:57:01 -04001950 rcu_read_lock();
1951 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01001952 if (ctx->state != state)
1953 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04001954 if ((ctx->mode & mode) != mode)
1955 continue;
1956 if (!get_nfs_open_context(ctx))
1957 continue;
1958 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001959 return ctx;
1960 }
Trond Myklebust0de43972018-09-02 15:57:01 -04001961 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001962 return ERR_PTR(-ENOENT);
1963}
1964
Trond Myklebust0de43972018-09-02 15:57:01 -04001965static struct nfs_open_context *
1966nfs4_state_find_open_context(struct nfs4_state *state)
1967{
1968 struct nfs_open_context *ctx;
1969
1970 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
1971 if (!IS_ERR(ctx))
1972 return ctx;
1973 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
1974 if (!IS_ERR(ctx))
1975 return ctx;
1976 return nfs4_state_find_open_context_mode(state, FMODE_READ);
1977}
1978
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001979static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1980 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001981{
1982 struct nfs4_opendata *opendata;
1983
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001984 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001985 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001986 if (opendata == NULL)
1987 return ERR_PTR(-ENOMEM);
1988 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001989 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001990 return opendata;
1991}
1992
Trond Myklebust24311f82015-09-20 10:50:17 -04001993static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1994 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001995{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001996 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001997 int ret;
1998
Trond Myklebust24311f82015-09-20 10:50:17 -04001999 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002000 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002001 opendata->o_arg.open_flags = 0;
2002 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002003 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2004 NFS_SB(opendata->dentry->d_sb),
2005 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002006 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2007 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2008 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002009 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002010 if (ret != 0)
2011 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002012 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002013 if (IS_ERR(newstate))
2014 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002015 if (newstate != opendata->state)
2016 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002017 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002018 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002019}
2020
2021static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2022{
Trond Myklebust864472e2006-01-03 09:55:15 +01002023 int ret;
2024
Trond Myklebust4f14c192014-02-12 19:15:06 -05002025 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2026 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2027 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2028 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002029 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002030 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002031 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002032 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002033 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2034 if (ret != 0)
2035 return ret;
2036 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2037 if (ret != 0)
2038 return ret;
2039 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2040 if (ret != 0)
2041 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002042 /*
2043 * We may have performed cached opens for all three recoveries.
2044 * Check if we need to update the current stateid.
2045 */
2046 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002047 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002048 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002049 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002050 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002051 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002052 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002053 return 0;
2054}
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056/*
2057 * OPEN_RECLAIM:
2058 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002060static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002062 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002063 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002064 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 int status;
2066
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002067 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2068 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002069 if (IS_ERR(opendata))
2070 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002071 rcu_read_lock();
2072 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002073 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002074 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002075 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002076 opendata->o_arg.u.delegation_type = delegation_type;
2077 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002078 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return status;
2080}
2081
Trond Myklebust539cd032007-06-05 11:46:42 -04002082static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 struct nfs_server *server = NFS_SERVER(state->inode);
2085 struct nfs4_exception exception = { };
2086 int err;
2087 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002088 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002089 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002090 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2091 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002092 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002093 break;
2094 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 } while (exception.retry);
2096 return err;
2097}
2098
Trond Myklebust864472e2006-01-03 09:55:15 +01002099static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2100{
2101 struct nfs_open_context *ctx;
2102 int ret;
2103
2104 ctx = nfs4_state_find_open_context(state);
2105 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002106 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002107 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002108 put_nfs_open_context(ctx);
2109 return ret;
2110}
2111
NeilBrowndce26302017-12-13 09:57:09 +11002112static 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 -07002113{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002114 switch (err) {
2115 default:
2116 printk(KERN_ERR "NFS: %s: unhandled error "
2117 "%d.\n", __func__, err);
2118 case 0:
2119 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002120 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002121 case -ESTALE:
2122 break;
2123 case -NFS4ERR_BADSESSION:
2124 case -NFS4ERR_BADSLOT:
2125 case -NFS4ERR_BAD_HIGH_SLOT:
2126 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2127 case -NFS4ERR_DEADSESSION:
2128 set_bit(NFS_DELEGATED_STATE, &state->flags);
2129 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2130 return -EAGAIN;
2131 case -NFS4ERR_STALE_CLIENTID:
2132 case -NFS4ERR_STALE_STATEID:
2133 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002134 /* Don't recall a delegation if it was lost */
2135 nfs4_schedule_lease_recovery(server->nfs_client);
2136 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002137 case -NFS4ERR_MOVED:
2138 nfs4_schedule_migration_recovery(server);
2139 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002140 case -NFS4ERR_LEASE_MOVED:
2141 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2142 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002143 case -NFS4ERR_DELEG_REVOKED:
2144 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002145 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002146 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002147 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002148 nfs_inode_find_state_and_recover(state->inode,
2149 stateid);
2150 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002151 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002152 case -NFS4ERR_DELAY:
2153 case -NFS4ERR_GRACE:
2154 set_bit(NFS_DELEGATED_STATE, &state->flags);
2155 ssleep(1);
2156 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002157 case -ENOMEM:
2158 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002159 if (fl) {
2160 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2161 if (lsp)
2162 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2163 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002164 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return err;
2167}
2168
Trond Myklebust24311f82015-09-20 10:50:17 -04002169int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2170 struct nfs4_state *state, const nfs4_stateid *stateid,
2171 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002172{
2173 struct nfs_server *server = NFS_SERVER(state->inode);
2174 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002175 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002176
2177 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2178 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2179 if (IS_ERR(opendata))
2180 return PTR_ERR(opendata);
2181 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002182 nfs_state_clear_delegation(state);
Trond Myklebust24311f82015-09-20 10:50:17 -04002183 switch (type & (FMODE_READ|FMODE_WRITE)) {
2184 case FMODE_READ|FMODE_WRITE:
2185 case FMODE_WRITE:
2186 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2187 if (err)
2188 break;
2189 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2190 if (err)
2191 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002192 /* Fall through */
Trond Myklebust24311f82015-09-20 10:50:17 -04002193 case FMODE_READ:
2194 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2195 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002196 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002197 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002198}
2199
Chuck Leverbe05c862013-08-09 12:49:47 -04002200static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2201{
2202 struct nfs4_opendata *data = calldata;
2203
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002204 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2205 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002206}
2207
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002208static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2209{
2210 struct nfs4_opendata *data = calldata;
2211
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002212 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002213
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002214 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002215 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002216 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002217 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002218 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002219 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002220 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002221}
2222
2223static void nfs4_open_confirm_release(void *calldata)
2224{
2225 struct nfs4_opendata *data = calldata;
2226 struct nfs4_state *state = NULL;
2227
2228 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002229 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002230 goto out_free;
2231 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002232 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002233 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002234 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002235 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002236 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002237out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002238 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002239}
2240
2241static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002242 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002243 .rpc_call_done = nfs4_open_confirm_done,
2244 .rpc_release = nfs4_open_confirm_release,
2245};
2246
2247/*
2248 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2249 */
2250static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2251{
David Howells2b0143b2015-03-17 22:25:59 +00002252 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002253 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002254 struct rpc_message msg = {
2255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2256 .rpc_argp = &data->c_arg,
2257 .rpc_resp = &data->c_res,
2258 .rpc_cred = data->owner->so_cred,
2259 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002260 struct rpc_task_setup task_setup_data = {
2261 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002262 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002263 .callback_ops = &nfs4_open_confirm_ops,
2264 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002265 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002266 .flags = RPC_TASK_ASYNC,
2267 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 int status;
2269
Anna Schumakerfba83f32018-05-04 16:22:50 -04002270 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2271 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002272 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002273 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002274 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002275 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002276 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002277 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002278 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002279 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002280 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002281 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002282 smp_wmb();
2283 } else
2284 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002285 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return status;
2287}
2288
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002289static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002291 struct nfs4_opendata *data = calldata;
2292 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002293 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002294 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002295
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002296 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002297 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002298 /*
2299 * Check if we still need to send an OPEN call, or if we can use
2300 * a delegation instead.
2301 */
2302 if (data->state != NULL) {
2303 struct nfs_delegation *delegation;
2304
Trond Myklebustbe189f72018-09-27 17:12:33 -04002305 if (can_open_cached(data->state, data->o_arg.fmode,
2306 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002307 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002308 rcu_read_lock();
2309 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002310 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002311 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002312 rcu_read_unlock();
2313 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002314 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002315 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002316 switch (claim) {
2317 default:
2318 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002319 case NFS4_OPEN_CLAIM_PREVIOUS:
2320 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2321 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002322 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002323 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002324 case NFS4_OPEN_CLAIM_FH:
2325 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002326 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002327 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002328 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002329 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002330 &data->o_res.seq_res,
2331 task) != 0)
2332 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002333
2334 /* Set the create mode (note dependency on the session type) */
2335 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2336 if (data->o_arg.open_flags & O_EXCL) {
2337 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2338 if (nfs4_has_persistent_session(clp))
2339 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2340 else if (clp->cl_mvops->minor_version > 0)
2341 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2342 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002343 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002344unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002345 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002346 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002347out_no_action:
2348 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002349out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002350 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002351}
2352
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002353static void nfs4_open_done(struct rpc_task *task, void *calldata)
2354{
2355 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002357 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002358
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002359 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002360 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002361
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002362 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002363 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2364 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002365 case S_IFREG:
2366 break;
2367 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002368 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002369 break;
2370 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002371 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002372 break;
2373 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002374 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002375 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002376 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002377 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002378 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2379 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002380 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002381 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002382}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002383
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002384static void nfs4_open_release(void *calldata)
2385{
2386 struct nfs4_opendata *data = calldata;
2387 struct nfs4_state *state = NULL;
2388
2389 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002390 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002391 goto out_free;
2392 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002393 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002394 goto out_free;
2395 /* In case we need an open_confirm, no cleanup! */
2396 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2397 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002398 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002399 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002400 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002401out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002402 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002403}
2404
2405static const struct rpc_call_ops nfs4_open_ops = {
2406 .rpc_call_prepare = nfs4_open_prepare,
2407 .rpc_call_done = nfs4_open_done,
2408 .rpc_release = nfs4_open_release,
2409};
2410
Fred Isaman3b65a302016-09-19 10:06:49 -04002411static int nfs4_run_open_task(struct nfs4_opendata *data,
2412 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002413{
David Howells2b0143b2015-03-17 22:25:59 +00002414 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002415 struct nfs_server *server = NFS_SERVER(dir);
2416 struct nfs_openargs *o_arg = &data->o_arg;
2417 struct nfs_openres *o_res = &data->o_res;
2418 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002419 struct rpc_message msg = {
2420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2421 .rpc_argp = o_arg,
2422 .rpc_resp = o_res,
2423 .rpc_cred = data->owner->so_cred,
2424 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002425 struct rpc_task_setup task_setup_data = {
2426 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002427 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002428 .callback_ops = &nfs4_open_ops,
2429 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002430 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002431 .flags = RPC_TASK_ASYNC,
2432 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002433 int status;
2434
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002435 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002436 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002437 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002438 data->cancelled = false;
2439 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002440 if (!ctx) {
2441 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002442 data->is_recover = true;
Fred Isaman2409a972016-10-06 12:11:21 -04002443 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002444 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002445 pnfs_lgopen_prepare(data, ctx);
2446 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002447 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002448 if (IS_ERR(task))
2449 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002450 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002451 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002452 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002453 smp_wmb();
2454 } else
2455 status = data->rpc_status;
2456 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002457
2458 return status;
2459}
2460
2461static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2462{
David Howells2b0143b2015-03-17 22:25:59 +00002463 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002464 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002465 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002466
Fred Isaman3b65a302016-09-19 10:06:49 -04002467 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002468 if (status != 0 || !data->rpc_done)
2469 return status;
2470
Trond Myklebust6926afd12012-01-07 13:22:46 -05002471 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2472
Anna Schumakerd7e98252017-01-11 16:13:29 -05002473 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002474 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002475
2476 return status;
2477}
2478
Trond Myklebustf3792d62014-07-10 08:54:32 -04002479/*
2480 * Additional permission checks in order to distinguish between an
2481 * open for read, and an open for execute. This works around the
2482 * fact that NFSv4 OPEN treats read and execute permissions as being
2483 * the same.
2484 * Note that in the non-execute case, we want to turn off permission
2485 * checking if we just created a new file (POSIX open() semantics).
2486 */
NeilBrowna52458b2018-12-03 11:30:31 +11002487static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002488 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002489 struct nfs4_state *state, fmode_t fmode,
2490 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002491{
2492 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002493 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002494
2495 /* access call failed or for some reason the server doesn't
2496 * support any access modes -- defer access call until later */
2497 if (opendata->o_res.access_supported == 0)
2498 return 0;
2499
2500 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002501 /*
2502 * Use openflags to check for exec, because fmode won't
2503 * always have FMODE_EXEC set when file open for exec.
2504 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002505 if (openflags & __FMODE_EXEC) {
2506 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002507 if (S_ISDIR(state->inode->i_mode))
2508 mask = NFS4_ACCESS_LOOKUP;
2509 else
2510 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002511 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002512 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002513
NeilBrowna52458b2018-12-03 11:30:31 +11002514 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002515 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2516 nfs_access_add_cache(state->inode, &cache);
2517
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002518 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2519 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002520 return 0;
2521
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002522 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002523}
2524
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002525/*
2526 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2527 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002528static int _nfs4_proc_open(struct nfs4_opendata *data,
2529 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002530{
David Howells2b0143b2015-03-17 22:25:59 +00002531 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002532 struct nfs_server *server = NFS_SERVER(dir);
2533 struct nfs_openargs *o_arg = &data->o_arg;
2534 struct nfs_openres *o_res = &data->o_res;
2535 int status;
2536
Fred Isaman3b65a302016-09-19 10:06:49 -04002537 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002538 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002539 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002540 if (status != 0) {
2541 if (status == -NFS4ERR_BADNAME &&
2542 !(o_arg->open_flags & O_CREAT))
2543 return -ENOENT;
2544 return status;
2545 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002546
Trond Myklebust6926afd12012-01-07 13:22:46 -05002547 nfs_fattr_map_and_free_names(server, &data->f_attr);
2548
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002549 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002550 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002551 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002552 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002553 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002554 if (data->file_created ||
2555 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002556 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002557 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002558 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002559 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2560 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002562 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002564 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002566 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2567 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002568 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2569 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002570 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574/*
2575 * OPEN_EXPIRED:
2576 * reclaim state on the server after a network partition.
2577 * Assumes caller holds the appropriate lock
2578 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002579static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002581 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002582 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002584 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002585 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002586 if (IS_ERR(opendata))
2587 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002588 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002589 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002590 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002591 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002592 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002595static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002596{
Trond Myklebust539cd032007-06-05 11:46:42 -04002597 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002598 struct nfs4_exception exception = { };
2599 int err;
2600
2601 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002602 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002603 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002604 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2605 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002606 switch (err) {
2607 default:
2608 goto out;
2609 case -NFS4ERR_GRACE:
2610 case -NFS4ERR_DELAY:
2611 nfs4_handle_exception(server, err, &exception);
2612 err = 0;
2613 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002614 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002615out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002616 return err;
2617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2620{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002622 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Trond Myklebust864472e2006-01-03 09:55:15 +01002624 ctx = nfs4_state_find_open_context(state);
2625 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002626 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002627 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002628 put_nfs_open_context(ctx);
2629 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Trond Myklebust41020b62016-09-22 13:38:58 -04002632static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2633 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002634{
Trond Myklebust41020b62016-09-22 13:38:58 -04002635 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002636 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002637}
2638
2639static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2640{
2641 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002642 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002643}
2644
2645static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2646{
2647 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2648 nfs40_clear_delegation_stateid(state);
2649 return nfs4_open_expired(sp, state);
2650}
2651
Trond Myklebust45870d62016-09-22 13:38:59 -04002652static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2653 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002654 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002655{
2656 return -NFS4ERR_BAD_STATEID;
2657}
2658
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002659#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002660static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2661 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002662 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002663{
2664 int status;
2665
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002666 switch (stateid->type) {
2667 default:
2668 break;
2669 case NFS4_INVALID_STATEID_TYPE:
2670 case NFS4_SPECIAL_STATEID_TYPE:
2671 return -NFS4ERR_BAD_STATEID;
2672 case NFS4_REVOKED_STATEID_TYPE:
2673 goto out_free;
2674 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002675
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002676 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002677 switch (status) {
2678 case -NFS4ERR_EXPIRED:
2679 case -NFS4ERR_ADMIN_REVOKED:
2680 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002681 break;
2682 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002683 return status;
2684 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002685out_free:
2686 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002687 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002688 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002689}
2690
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002691static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002692{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002693 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002694 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002695 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002696 const struct cred *cred = NULL;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002697 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002698
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002699 /* Get the delegation credential for use by test/free_stateid */
2700 rcu_read_lock();
2701 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002702 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002703 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002704 nfs_state_clear_delegation(state);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002705 return;
2706 }
2707
2708 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002709 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002710 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002711 nfs_state_clear_delegation(state);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002712 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002713 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002714
Trond Myklebust994b15b2018-09-05 14:07:14 -04002715 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2716 &delegation->flags)) {
2717 rcu_read_unlock();
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002718 return;
2719 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002720
NeilBrowna52458b2018-12-03 11:30:31 +11002721 if (delegation->cred)
2722 cred = get_cred(delegation->cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002723 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002724 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002725 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002726 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002727 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002728
NeilBrowna52458b2018-12-03 11:30:31 +11002729 if (delegation->cred)
2730 put_cred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002731}
2732
2733/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002734 * nfs41_check_expired_locks - possibly free a lock stateid
2735 *
2736 * @state: NFSv4 state for an inode
2737 *
2738 * Returns NFS_OK if recovery for this stateid is now finished.
2739 * Otherwise a negative NFS4ERR value is returned.
2740 */
2741static int nfs41_check_expired_locks(struct nfs4_state *state)
2742{
2743 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002744 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002745 struct nfs_server *server = NFS_SERVER(state->inode);
2746
2747 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2748 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002749
2750 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002751 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2752 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002753 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002754
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002755 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002756 spin_unlock(&state->state_lock);
2757
2758 nfs4_put_lock_state(prev);
2759 prev = lsp;
2760
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002761 status = nfs41_test_and_free_expired_stateid(server,
2762 &lsp->ls_stateid,
2763 cred);
2764 trace_nfs4_test_lock_stateid(state, lsp, status);
2765 if (status == -NFS4ERR_EXPIRED ||
2766 status == -NFS4ERR_BAD_STATEID) {
2767 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002768 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002769 if (!recover_lost_locks)
2770 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2771 } else if (status != NFS_OK) {
2772 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002773 nfs4_put_lock_state(prev);
2774 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002775 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002776 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002777 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002778 }
2779 spin_unlock(&state->state_lock);
2780 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002781out:
2782 return ret;
2783}
2784
2785/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002786 * nfs41_check_open_stateid - possibly free an open stateid
2787 *
2788 * @state: NFSv4 state for an inode
2789 *
2790 * Returns NFS_OK if recovery for this stateid is now finished.
2791 * Otherwise a negative NFS4ERR value is returned.
2792 */
2793static int nfs41_check_open_stateid(struct nfs4_state *state)
2794{
2795 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002796 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002797 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002798 int status;
2799
Trond Myklebustb134fc42016-09-22 13:39:16 -04002800 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002801 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2802 if (nfs4_have_delegation(state->inode, state->state))
2803 return NFS_OK;
2804 return -NFS4ERR_OPENMODE;
2805 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002806 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002807 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002808 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002809 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002810 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002811 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2812 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2813 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002814 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002815 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002816 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002817 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002818 if (nfs_open_stateid_recover_openmode(state))
2819 return -NFS4ERR_OPENMODE;
2820 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002821}
2822
2823static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2824{
Chuck Levereb64cf92012-07-11 16:30:05 -04002825 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002826
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002827 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002828 status = nfs41_check_expired_locks(state);
2829 if (status != NFS_OK)
2830 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002831 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002832 if (status != NFS_OK)
2833 status = nfs4_open_expired(sp, state);
2834 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002835}
2836#endif
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002839 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2840 * fields corresponding to attributes that were used to store the verifier.
2841 * Make sure we clobber those fields in the later setattr call
2842 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002843static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002844 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002845{
Trond Myklebust609339c2018-03-28 16:18:17 -04002846 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2847 __u32 attrset[3];
2848 unsigned ret;
2849 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002850
Trond Myklebust609339c2018-03-28 16:18:17 -04002851 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2852 attrset[i] = opendata->o_res.attrset[i];
2853 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2854 attrset[i] &= ~bitmask[i];
2855 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002856
Trond Myklebust609339c2018-03-28 16:18:17 -04002857 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2858 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002859
Trond Myklebust609339c2018-03-28 16:18:17 -04002860 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2861 if (sattr->ia_valid & ATTR_ATIME_SET)
2862 ret |= ATTR_ATIME_SET;
2863 else
2864 ret |= ATTR_ATIME;
2865 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002866
Trond Myklebust609339c2018-03-28 16:18:17 -04002867 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2868 if (sattr->ia_valid & ATTR_MTIME_SET)
2869 ret |= ATTR_MTIME_SET;
2870 else
2871 ret |= ATTR_MTIME;
2872 }
2873
2874 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002875 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002876 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002877}
2878
Trond Myklebustc21443c2013-02-07 14:26:21 -05002879static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2880 fmode_t fmode,
2881 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002882 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002883{
2884 struct nfs4_state_owner *sp = opendata->owner;
2885 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002886 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002887 struct nfs4_state *state;
2888 unsigned int seq;
2889 int ret;
2890
2891 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2892
Fred Isaman3b65a302016-09-19 10:06:49 -04002893 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002894 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002895 goto out;
2896
Trond Myklebustae55e592018-05-22 11:17:16 -04002897 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002898 ret = PTR_ERR(state);
2899 if (IS_ERR(state))
2900 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002901 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002902 if (server->caps & NFS_CAP_POSIX_LOCK)
2903 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002904 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2905 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002906
Trond Myklebust275bb302013-05-29 13:11:28 -04002907 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002908 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002909 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002910 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002911 alias = d_exact_alias(dentry, state->inode);
2912 if (!alias)
2913 alias = d_splice_alias(igrab(state->inode), dentry);
2914 /* d_splice_alias() can't fail here - it's a non-directory */
2915 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002916 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002917 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002918 }
2919 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002920 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002921 }
2922
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04002923 /* Parse layoutget results before we check for access */
2924 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2925
Trond Myklebustc21443c2013-02-07 14:26:21 -05002926 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2927 if (ret != 0)
2928 goto out;
2929
David Howells2b0143b2015-03-17 22:25:59 +00002930 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002931 nfs_inode_attach_open_context(ctx);
2932 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2933 nfs4_schedule_stateid_recovery(server, state);
2934 }
Fred Isaman2409a972016-10-06 12:11:21 -04002935
Trond Myklebustc21443c2013-02-07 14:26:21 -05002936out:
Trond Myklebustae55e592018-05-22 11:17:16 -04002937 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002938 return ret;
2939}
2940
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002941/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002942 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 */
Andy Adamson82be4172012-05-23 05:02:35 -04002944static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002945 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002946 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002947 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002948 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
2950 struct nfs4_state_owner *sp;
2951 struct nfs4_state *state = NULL;
2952 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002953 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002954 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11002955 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04002956 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2957 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002958 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002959 struct iattr *sattr = c->sattr;
2960 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002961 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002962 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
2964 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002966 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2967 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2969 goto out_err;
2970 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002971 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002972 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002973 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002974 if (d_really_is_positive(dentry))
2975 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002976 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002977 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002978 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002979 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2980 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002981 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002982 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
David Quigley14c43f72013-05-22 12:50:43 -04002984 if (label) {
2985 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2986 if (IS_ERR(olabel)) {
2987 status = PTR_ERR(olabel);
2988 goto err_opendata_put;
2989 }
2990 }
2991
Trond Myklebuste911b812014-03-26 13:24:37 -07002992 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2993 if (!opendata->f_attr.mdsthreshold) {
2994 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2995 if (!opendata->f_attr.mdsthreshold)
2996 goto err_free_label;
2997 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002998 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002999 }
David Howells2b0143b2015-03-17 22:25:59 +00003000 if (d_really_is_positive(dentry))
3001 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003002
Trond Myklebust3efb9722013-05-29 13:17:04 -04003003 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003004 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003005 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003006 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003007
NeilBrownefcbc042015-07-30 13:00:56 +10003008 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003009 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003010 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003011 /*
3012 * send create attributes which was not set by open
3013 * with an extra setattr.
3014 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003015 if (attrs || label) {
3016 unsigned ia_old = sattr->ia_valid;
3017
3018 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003019 nfs_fattr_init(opendata->o_res.f_attr);
3020 status = nfs4_do_setattr(state->inode, cred,
3021 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003022 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003023 if (status == 0) {
3024 nfs_setattr_update_inode(state->inode, sattr,
3025 opendata->o_res.f_attr);
3026 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3027 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003028 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003029 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003030 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003031 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003032 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003033
Trond Myklebuste911b812014-03-26 13:24:37 -07003034 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003035 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003036 opendata->f_attr.mdsthreshold = NULL;
3037 }
Andy Adamson82be4172012-05-23 05:02:35 -04003038
David Quigley14c43f72013-05-22 12:50:43 -04003039 nfs4_label_free(olabel);
3040
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003041 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003044err_free_label:
3045 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003046err_opendata_put:
3047 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003048err_put_state_owner:
3049 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 return status;
3052}
3053
3054
Andy Adamson82be4172012-05-23 05:02:35 -04003055static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003056 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003057 int flags,
3058 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003059 struct nfs4_label *label,
3060 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003062 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 struct nfs4_exception exception = { };
3064 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003065 struct nfs4_open_createattrs c = {
3066 .label = label,
3067 .sattr = sattr,
3068 .verf = {
3069 [0] = (__u32)jiffies,
3070 [1] = (__u32)current->pid,
3071 },
3072 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 int status;
3074
3075 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003076 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003077 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003078 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 if (status == 0)
3080 break;
3081 /* NOTE: BAD_SEQID means the server and client disagree about the
3082 * book-keeping w.r.t. state-changing operations
3083 * (OPEN/CLOSE/LOCK/LOCKU...)
3084 * It is actually a sign of a bug on the client or on the server.
3085 *
3086 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003087 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 * have unhashed the old state_owner for us, and that we can
3089 * therefore safely retry using a new one. We should still warn
3090 * the user though...
3091 */
3092 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003093 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003094 " returned a bad sequence-id error!\n",
3095 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 exception.retry = 1;
3097 continue;
3098 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003099 /*
3100 * BAD_STATEID on OPEN means that the server cancelled our
3101 * state before it received the OPEN_CONFIRM.
3102 * Recover by retrying the request as per the discussion
3103 * on Page 181 of RFC3530.
3104 */
3105 if (status == -NFS4ERR_BAD_STATEID) {
3106 exception.retry = 1;
3107 continue;
3108 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003109 if (status == -EAGAIN) {
3110 /* We must have found a delegation */
3111 exception.retry = 1;
3112 continue;
3113 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003114 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3115 continue;
3116 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 status, &exception));
3118 } while (exception.retry);
3119 return res;
3120}
3121
Trond Myklebust8487c472016-06-26 08:44:35 -04003122static int _nfs4_do_setattr(struct inode *inode,
3123 struct nfs_setattrargs *arg,
3124 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003125 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003126 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003127{
3128 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003129 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3131 .rpc_argp = arg,
3132 .rpc_resp = res,
3133 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003134 };
NeilBrowna52458b2018-12-03 11:30:31 +11003135 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003136 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003137 bool truncate;
3138 int status;
3139
3140 nfs_fattr_init(res->fattr);
3141
3142 /* Servers should only apply open mode checks for file size changes */
3143 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003144 if (!truncate)
3145 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003146
Trond Myklebust991eedb2018-04-09 11:15:30 -04003147 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003148 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003149 } else if (ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003150 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003151 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003152 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003153 l_ctx = nfs_get_lock_context(ctx);
3154 if (IS_ERR(l_ctx))
3155 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003156 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3157 &arg->stateid, &delegation_cred);
3158 nfs_put_lock_context(l_ctx);
3159 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003160 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003161 } else {
3162zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003163 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003164 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003165 if (delegation_cred)
3166 msg.rpc_cred = delegation_cred;
3167
3168 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3169
NeilBrowna52458b2018-12-03 11:30:31 +11003170 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003171 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003172 renew_lease(server, timestamp);
3173 trace_nfs4_setattr(inode, &arg->stateid, status);
3174 return status;
3175}
3176
NeilBrowna52458b2018-12-03 11:30:31 +11003177static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003178 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003179 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003180 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003182 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003183 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003184 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003185 struct nfs_setattrargs arg = {
3186 .fh = NFS_FH(inode),
3187 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003189 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003190 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003191 };
3192 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003194 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003196 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003197 struct nfs4_exception exception = {
3198 .state = state,
3199 .inode = inode,
3200 .stateid = &arg.stateid,
3201 };
3202 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003205 nfs4_bitmap_copy_adjust_setattr(bitmask,
3206 nfs4_bitmask(server, olabel),
3207 inode);
3208
NeilBrown29b59f92016-10-13 15:26:47 +11003209 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003210 switch (err) {
3211 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003212 if (!(sattr->ia_valid & ATTR_SIZE)) {
3213 pr_warn_once("NFSv4: server %s is incorrectly "
3214 "applying open mode checks to "
3215 "a SETATTR that is not "
3216 "changing file size.\n",
3217 server->nfs_client->cl_hostname);
3218 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003219 if (state && !(state->state & FMODE_WRITE)) {
3220 err = -EBADF;
3221 if (sattr->ia_valid & ATTR_OPEN)
3222 err = -EACCES;
3223 goto out;
3224 }
3225 }
3226 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003228out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return err;
3230}
3231
Peng Tao500d7012015-09-22 11:35:22 +08003232static bool
3233nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3234{
3235 if (inode == NULL || !nfs_have_layout(inode))
3236 return false;
3237
3238 return pnfs_wait_on_layoutreturn(inode, task);
3239}
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241struct nfs4_closedata {
3242 struct inode *inode;
3243 struct nfs4_state *state;
3244 struct nfs_closeargs arg;
3245 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003246 struct {
3247 struct nfs4_layoutreturn_args arg;
3248 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003249 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003250 u32 roc_barrier;
3251 bool roc;
3252 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003253 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003254 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255};
3256
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003257static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003258{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003259 struct nfs4_closedata *calldata = data;
3260 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003261 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003262
Trond Myklebustcf805162016-11-15 14:56:07 -05003263 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003264 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3265 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003266 nfs4_put_open_state(calldata->state);
3267 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003268 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003269 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003270 kfree(calldata);
3271}
3272
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003273static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003275 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003278 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003279 struct nfs4_exception exception = {
3280 .state = state,
3281 .inode = calldata->inode,
3282 .stateid = &calldata->arg.stateid,
3283 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Chuck Levera3ca5652012-03-01 17:00:40 -05003285 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003286 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3287 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003288 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003289
3290 /* Handle Layoutreturn errors */
3291 if (calldata->arg.lr_args && task->tk_status != 0) {
3292 switch (calldata->res.lr_ret) {
3293 default:
3294 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3295 break;
3296 case 0:
3297 calldata->arg.lr_args = NULL;
3298 calldata->res.lr_res = NULL;
3299 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003300 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04003301 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04003302 &calldata->arg.lr_args->range,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003303 calldata->inode))
3304 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003305 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003306 case -NFS4ERR_ADMIN_REVOKED:
3307 case -NFS4ERR_DELEG_REVOKED:
3308 case -NFS4ERR_EXPIRED:
3309 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003310 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3311 case -NFS4ERR_WRONG_CRED:
3312 calldata->arg.lr_args = NULL;
3313 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003314 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003315 }
3316 }
3317
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003318 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 * the state_owner. we keep this around to process errors
3320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 switch (task->tk_status) {
3322 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003323 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003324 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003325 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003326 case -NFS4ERR_ACCESS:
3327 if (calldata->arg.bitmask != NULL) {
3328 calldata->arg.bitmask = NULL;
3329 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003330 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003331
3332 }
3333 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003334 case -NFS4ERR_OLD_STATEID:
3335 /* Did we race with OPEN? */
3336 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003337 state))
3338 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003339 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003340 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003342 case -NFS4ERR_EXPIRED:
3343 nfs4_free_revoked_stateid(server,
3344 &calldata->arg.stateid,
3345 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003346 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003347 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003350 task->tk_status = nfs4_async_handle_exception(task,
3351 server, task->tk_status, &exception);
3352 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003353 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003355 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3356 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003357out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003358 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003359 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003360 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003361 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003362 return;
3363lr_restart:
3364 calldata->res.lr_ret = 0;
3365out_restart:
3366 task->tk_status = 0;
3367 rpc_restart_call_prepare(task);
3368 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003371static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003373 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003374 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003375 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003376 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003377 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003378 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003379
Chuck Levera3ca5652012-03-01 17:00:40 -05003380 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003381 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003382 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003383
Trond Myklebust88069f72009-12-08 08:33:16 -05003384 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003385 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003386 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3387 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3388 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003389 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003390 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003391 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003392 if (state->n_rdonly == 0)
3393 call_close |= is_rdonly;
3394 else if (is_rdonly)
3395 calldata->arg.fmode |= FMODE_READ;
3396 if (state->n_wronly == 0)
3397 call_close |= is_wronly;
3398 else if (is_wronly)
3399 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003400 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3401 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003402 } else if (is_rdwr)
3403 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3404
Trond Myklebust5cc78612016-11-14 11:19:56 -05003405 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003406 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003407 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003408 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003409
3410 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003411 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003412 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003413 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003414
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003415 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003416 nfs_release_seqid(calldata->arg.seqid);
3417 goto out_wait;
3418 }
3419
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003420 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3421 if (lo && !pnfs_layout_is_valid(lo)) {
3422 calldata->arg.lr_args = NULL;
3423 calldata->res.lr_res = NULL;
3424 }
3425
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003426 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003427 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003428
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003429 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003430 /* Close-to-open cache consistency revalidation */
3431 if (!nfs4_have_delegation(inode, FMODE_READ))
3432 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3433 else
3434 calldata->arg.bitmask = NULL;
3435 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003436
Trond Myklebust6ae37332015-01-30 14:21:14 -05003437 calldata->arg.share_access =
3438 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3439 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003440
Trond Myklebustd8d84982016-12-19 12:14:44 -05003441 if (calldata->res.fattr == NULL)
3442 calldata->arg.bitmask = NULL;
3443 else if (calldata->arg.bitmask == NULL)
3444 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003445 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003446 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003447 &calldata->arg.seq_args,
3448 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003449 task) != 0)
3450 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003451 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003452 return;
3453out_no_action:
3454 task->tk_action = NULL;
3455out_wait:
3456 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003459static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003460 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003461 .rpc_call_done = nfs4_close_done,
3462 .rpc_release = nfs4_free_closedata,
3463};
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465/*
3466 * It is possible for data to be read/written from a mem-mapped file
3467 * after the sys_close call (which hits the vfs layer as a flush).
3468 * This means that we can't safely call nfsv4 close on a file until
3469 * the inode is cleared. This in turn means that we are not good
3470 * NFSv4 citizens - we do not indicate to the server to update the file's
3471 * share state even when we are done with one of the three share
3472 * stateid's in the inode.
3473 *
3474 * NOTE: Caller must be holding the sp->so_owner semaphore!
3475 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003476int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003478 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003479 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003481 struct nfs4_state_owner *sp = state->owner;
3482 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003483 struct rpc_message msg = {
3484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3485 .rpc_cred = state->owner->so_cred,
3486 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003487 struct rpc_task_setup task_setup_data = {
3488 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003489 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003490 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003491 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003492 .flags = RPC_TASK_ASYNC,
3493 };
Trond Myklebust95121352005-10-18 14:20:12 -07003494 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003496 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3497 &task_setup_data.rpc_client, &msg);
3498
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003499 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003501 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003502 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003503 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003505 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003506 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3507 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003509 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3510 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003511 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003512 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003513 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003514 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003515 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003516 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003517 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003518 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003519 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003520 calldata->lr.roc = pnfs_roc(state->inode,
3521 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3522 if (calldata->lr.roc) {
3523 calldata->arg.lr_args = &calldata->lr.arg;
3524 calldata->res.lr_res = &calldata->lr.res;
3525 }
Al Viro643168c2011-06-22 18:20:23 -04003526 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003527
Trond Myklebust1174dd12010-12-21 10:52:24 -05003528 msg.rpc_argp = &calldata->arg;
3529 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003530 task_setup_data.callback_data = calldata;
3531 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003532 if (IS_ERR(task))
3533 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003534 status = 0;
3535 if (wait)
3536 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003537 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003538 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003539out_free_calldata:
3540 kfree(calldata);
3541out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003542 nfs4_put_open_state(state);
3543 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003544 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
Trond Myklebust2b484292010-09-17 10:56:51 -04003547static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003548nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3549 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003552 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3553
3554 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
Trond Myklebust565277f2007-10-15 18:17:53 -04003556 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003557 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003558
3559 nfs4_label_release_security(label);
3560
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003561 if (IS_ERR(state))
3562 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003563 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564}
3565
Trond Myklebust1185a552009-12-03 15:54:02 -05003566static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003567{
3568 if (ctx->state == NULL)
3569 return;
3570 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003571 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003572 else
Al Viro643168c2011-06-22 18:20:23 -04003573 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003574}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Trond Myklebustb944dba2013-11-04 15:20:20 -05003576#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3577#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003578#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003579
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3581{
Kinglong Mee8c612822015-08-26 21:12:58 +08003582 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003583 struct nfs4_server_caps_arg args = {
3584 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003585 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003586 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 struct nfs4_server_caps_res res = {};
3588 struct rpc_message msg = {
3589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003590 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 .rpc_resp = &res,
3592 };
3593 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003594 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Kinglong Mee8c612822015-08-26 21:12:58 +08003596 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3597 FATTR4_WORD0_FH_EXPIRE_TYPE |
3598 FATTR4_WORD0_LINK_SUPPORT |
3599 FATTR4_WORD0_SYMLINK_SUPPORT |
3600 FATTR4_WORD0_ACLSUPPORT;
3601 if (minorversion)
3602 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3603
Bryan Schumaker7c513052011-03-24 17:12:24 +00003604 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003606 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003607 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003608 case 0:
3609 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3610 res.attr_bitmask[2] = 0;
3611 break;
3612 case 1:
3613 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3614 break;
3615 case 2:
3616 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003619 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3620 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3621 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3622 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003623 NFS_CAP_CTIME|NFS_CAP_MTIME|
3624 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003625 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3626 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 server->caps |= NFS_CAP_ACLS;
3628 if (res.has_links != 0)
3629 server->caps |= NFS_CAP_HARDLINKS;
3630 if (res.has_symlinks != 0)
3631 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003632 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3633 server->caps |= NFS_CAP_FILEID;
3634 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3635 server->caps |= NFS_CAP_MODE;
3636 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3637 server->caps |= NFS_CAP_NLINK;
3638 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3639 server->caps |= NFS_CAP_OWNER;
3640 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3641 server->caps |= NFS_CAP_OWNER_GROUP;
3642 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3643 server->caps |= NFS_CAP_ATIME;
3644 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3645 server->caps |= NFS_CAP_CTIME;
3646 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3647 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003648#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3649 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3650 server->caps |= NFS_CAP_SECURITY_LABEL;
3651#endif
3652 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3653 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003654 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003655
Trond Myklebusta65318b2009-03-11 14:10:28 -04003656 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3657 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3658 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003659 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003660
3661 /* Avoid a regression due to buggy server */
3662 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3663 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003664 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3665 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003668 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return status;
3672}
3673
Trond Myklebust55a97592006-06-09 09:34:19 -04003674int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675{
3676 struct nfs4_exception exception = { };
3677 int err;
3678 do {
3679 err = nfs4_handle_exception(server,
3680 _nfs4_server_capabilities(server, fhandle),
3681 &exception);
3682 } while (exception.retry);
3683 return err;
3684}
3685
3686static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3687 struct nfs_fsinfo *info)
3688{
David Quigleyaa9c2662013-05-22 12:50:44 -04003689 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003691 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 };
3693 struct nfs4_lookup_res res = {
3694 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003695 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 .fh = fhandle,
3697 };
3698 struct rpc_message msg = {
3699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3700 .rpc_argp = &args,
3701 .rpc_resp = &res,
3702 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003703
David Quigleyaa9c2662013-05-22 12:50:44 -04003704 bitmask[0] = nfs4_fattr_bitmap[0];
3705 bitmask[1] = nfs4_fattr_bitmap[1];
3706 /*
3707 * Process the label in the upcoming getfattr
3708 */
3709 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3710
Trond Myklebust0e574af2005-10-27 22:12:38 -04003711 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003712 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713}
3714
3715static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3716 struct nfs_fsinfo *info)
3717{
3718 struct nfs4_exception exception = { };
3719 int err;
3720 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003721 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003722 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003723 switch (err) {
3724 case 0:
3725 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003726 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003727 default:
3728 err = nfs4_handle_exception(server, err, &exception);
3729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003731out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 return err;
3733}
3734
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003735static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3736 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3737{
Trond Myklebustc2190662013-08-26 19:23:04 -04003738 struct rpc_auth_create_args auth_args = {
3739 .pseudoflavor = flavor,
3740 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003741 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003742
Trond Myklebustc2190662013-08-26 19:23:04 -04003743 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003744 if (IS_ERR(auth))
3745 return -EACCES;
3746 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003747}
3748
Chuck Lever9a744ba2013-03-16 15:56:02 -04003749/*
3750 * Retry pseudoroot lookup with various security flavors. We do this when:
3751 *
3752 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3753 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3754 *
3755 * Returns zero on success, or a negative NFS4ERR value, or a
3756 * negative errno value.
3757 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003758static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003759 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003761 /* Per 3530bis 15.33.5 */
3762 static const rpc_authflavor_t flav_array[] = {
3763 RPC_AUTH_GSS_KRB5P,
3764 RPC_AUTH_GSS_KRB5I,
3765 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003766 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003767 RPC_AUTH_NULL,
3768 };
3769 int status = -EPERM;
3770 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003772 if (server->auth_info.flavor_len > 0) {
3773 /* try each flavor specified by user */
3774 for (i = 0; i < server->auth_info.flavor_len; i++) {
3775 status = nfs4_lookup_root_sec(server, fhandle, info,
3776 server->auth_info.flavors[i]);
3777 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3778 continue;
3779 break;
3780 }
3781 } else {
3782 /* no flavors specified by user, try default list */
3783 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3784 status = nfs4_lookup_root_sec(server, fhandle, info,
3785 flav_array[i]);
3786 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3787 continue;
3788 break;
3789 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003790 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003791
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003792 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003793 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003794 * to access the mount. It could also mean that we tried to mount
3795 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3796 * existing mount programs don't handle -EACCES very well so it should
3797 * be mapped to -EPERM instead.
3798 */
3799 if (status == -EACCES)
3800 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003801 return status;
3802}
3803
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003804/**
3805 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3806 * @server: initialized nfs_server handle
3807 * @fhandle: we fill in the pseudo-fs root file handle
3808 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003809 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003810 *
3811 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003812 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003813int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003814 struct nfs_fsinfo *info,
3815 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003816{
Andre Przywarac7757072015-04-23 17:17:40 +01003817 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003818
Andre Przywarac7757072015-04-23 17:17:40 +01003819 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003820 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003821
3822 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003823 status = server->nfs_client->cl_mvops->find_root_sec(server,
3824 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003825
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 if (status == 0)
3827 status = nfs4_server_capabilities(server, fhandle);
3828 if (status == 0)
3829 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003830
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003831 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832}
3833
Bryan Schumakerbae36242012-05-10 15:07:31 -04003834static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3835 struct nfs_fsinfo *info)
3836{
3837 int error;
3838 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003839 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003840
3841 error = nfs4_server_capabilities(server, mntfh);
3842 if (error < 0) {
3843 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3844 return error;
3845 }
3846
David Quigley14c43f72013-05-22 12:50:43 -04003847 label = nfs4_label_alloc(server, GFP_KERNEL);
3848 if (IS_ERR(label))
3849 return PTR_ERR(label);
3850
Trond Myklebusta841b542018-04-07 13:50:59 -04003851 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003852 if (error < 0) {
3853 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003854 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003855 }
3856
3857 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3858 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3859 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3860
David Quigley14c43f72013-05-22 12:50:43 -04003861err_free_label:
3862 nfs4_label_free(label);
3863
Bryan Schumakerbae36242012-05-10 15:07:31 -04003864 return error;
3865}
3866
Manoj Naik6b97fd32006-06-09 09:34:29 -04003867/*
3868 * Get locations and (maybe) other attributes of a referral.
3869 * Note that we'll actually follow the referral later when
3870 * we detect fsid mismatch in inode revalidation
3871 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003872static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3873 const struct qstr *name, struct nfs_fattr *fattr,
3874 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003875{
3876 int status = -ENOMEM;
3877 struct page *page = NULL;
3878 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003879
3880 page = alloc_page(GFP_KERNEL);
3881 if (page == NULL)
3882 goto out;
3883 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3884 if (locations == NULL)
3885 goto out;
3886
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003887 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003888 if (status != 0)
3889 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003890
3891 /*
3892 * If the fsid didn't change, this is a migration event, not a
3893 * referral. Cause us to drop into the exception handler, which
3894 * will kick off migration recovery.
3895 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003896 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003897 dprintk("%s: server did not return a different fsid for"
3898 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003899 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003900 goto out;
3901 }
Andy Adamson533eb462011-06-13 18:25:56 -04003902 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3903 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003904
Andy Adamson533eb462011-06-13 18:25:56 -04003905 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003906 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003907 memset(fhandle, 0, sizeof(struct nfs_fh));
3908out:
3909 if (page)
3910 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003911 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003912 return status;
3913}
3914
David Quigley1775fd32013-05-22 12:50:42 -04003915static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003916 struct nfs_fattr *fattr, struct nfs4_label *label,
3917 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918{
Trond Myklebust771734f2018-04-07 13:54:23 -04003919 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 struct nfs4_getattr_arg args = {
3921 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04003922 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 };
3924 struct nfs4_getattr_res res = {
3925 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003926 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 .server = server,
3928 };
3929 struct rpc_message msg = {
3930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3931 .rpc_argp = &args,
3932 .rpc_resp = &res,
3933 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003934
Trond Myklebust771734f2018-04-07 13:54:23 -04003935 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04003936
Trond Myklebust0e574af2005-10-27 22:12:38 -04003937 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003938 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939}
3940
David Quigley1775fd32013-05-22 12:50:42 -04003941static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003942 struct nfs_fattr *fattr, struct nfs4_label *label,
3943 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944{
3945 struct nfs4_exception exception = { };
3946 int err;
3947 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04003948 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003949 trace_nfs4_getattr(server, fhandle, fattr, err);
3950 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 &exception);
3952 } while (exception.retry);
3953 return err;
3954}
3955
3956/*
3957 * The file is not closed if it is opened due to the a request to change
3958 * the size of the file. The open call will not be needed once the
3959 * VFS layer lookup-intents are implemented.
3960 *
3961 * Close is called when the inode is destroyed.
3962 * If we haven't opened the file for O_WRONLY, we
3963 * need to in the size_change case to obtain a stateid.
3964 *
3965 * Got race?
3966 * Because OPEN is always done by name in nfsv4, it is
3967 * possible that we opened a different file by the same
3968 * name. We can recognize this race condition, but we
3969 * can't do anything about it besides returning an error.
3970 *
3971 * This will be fixed with VFS changes (lookup-intent).
3972 */
3973static int
3974nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3975 struct iattr *sattr)
3976{
David Howells2b0143b2015-03-17 22:25:59 +00003977 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11003978 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003979 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003980 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 int status;
3982
Peng Tao88ac8152014-09-12 11:04:10 +08003983 if (pnfs_ld_layoutret_on_setattr(inode) &&
3984 sattr->ia_valid & ATTR_SIZE &&
3985 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003986 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003987
Trond Myklebust0e574af2005-10-27 22:12:38 -04003988 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Andy Adamson26699402012-05-30 16:12:24 -04003990 /* Deal with open(O_TRUNC) */
3991 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003992 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003993
3994 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003995 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003996 return 0;
3997
Trond Myklebustd5308382005-11-04 15:33:38 -05003998 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003999 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004000
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004001 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004002 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004003 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004004 }
4005
David Quigley14c43f72013-05-22 12:50:43 -04004006 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4007 if (IS_ERR(label))
4008 return PTR_ERR(label);
4009
Trond Myklebust199366f2018-03-20 16:43:18 -04004010 /* Return any delegations if we're going to change ACLs */
4011 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004012 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004013
NeilBrown29b59f92016-10-13 15:26:47 +11004014 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004015 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004016 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004017 nfs_setsecurity(inode, fattr, label);
4018 }
David Quigley14c43f72013-05-22 12:50:43 -04004019 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 return status;
4021}
4022
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004023static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4024 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004025 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004026{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004027 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004028 int status;
4029 struct nfs4_lookup_arg args = {
4030 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004031 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04004032 .name = name,
4033 };
4034 struct nfs4_lookup_res res = {
4035 .server = server,
4036 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004037 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004038 .fh = fhandle,
4039 };
4040 struct rpc_message msg = {
4041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4042 .rpc_argp = &args,
4043 .rpc_resp = &res,
4044 };
4045
David Quigleyaa9c2662013-05-22 12:50:44 -04004046 args.bitmask = nfs4_bitmask(server, label);
4047
David Howells2b3de442006-08-22 20:06:09 -04004048 nfs_fattr_init(fattr);
4049
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004051 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 dprintk("NFS reply lookup: %d\n", status);
4053 return status;
4054}
4055
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004056static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004057{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004058 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004059 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004060 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4061 fattr->nlink = 2;
4062}
4063
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004064static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004065 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004066 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004067{
4068 struct nfs4_exception exception = { };
4069 struct rpc_clnt *client = *clnt;
4070 int err;
4071 do {
David Quigley1775fd32013-05-22 12:50:42 -04004072 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004073 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004074 switch (err) {
4075 case -NFS4ERR_BADNAME:
4076 err = -ENOENT;
4077 goto out;
4078 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004079 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004080 if (err == -NFS4ERR_MOVED)
4081 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004082 goto out;
4083 case -NFS4ERR_WRONGSEC:
4084 err = -EPERM;
4085 if (client != *clnt)
4086 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004087 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004088 if (IS_ERR(client))
4089 return PTR_ERR(client);
4090
4091 exception.retry = 1;
4092 break;
4093 default:
4094 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4095 }
4096 } while (exception.retry);
4097
4098out:
4099 if (err == 0)
4100 *clnt = client;
4101 else if (client != *clnt)
4102 rpc_shutdown_client(client);
4103
4104 return err;
4105}
4106
Al Virobeffb8f2016-07-20 16:34:42 -04004107static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004108 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4109 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004111 int status;
4112 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004113
David Quigley1775fd32013-05-22 12:50:42 -04004114 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004115 if (client != NFS_CLIENT(dir)) {
4116 rpc_shutdown_client(client);
4117 nfs_fixup_secinfo_attributes(fattr);
4118 }
4119 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120}
4121
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004122struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004123nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004124 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4125{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004126 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004127 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004128
David Quigley1775fd32013-05-22 12:50:42 -04004129 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004130 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004131 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004132 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004133}
4134
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004135static int _nfs4_proc_lookupp(struct inode *inode,
4136 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4137 struct nfs4_label *label)
4138{
4139 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4140 struct nfs_server *server = NFS_SERVER(inode);
4141 int status;
4142 struct nfs4_lookupp_arg args = {
4143 .bitmask = server->attr_bitmask,
4144 .fh = NFS_FH(inode),
4145 };
4146 struct nfs4_lookupp_res res = {
4147 .server = server,
4148 .fattr = fattr,
4149 .label = label,
4150 .fh = fhandle,
4151 };
4152 struct rpc_message msg = {
4153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4154 .rpc_argp = &args,
4155 .rpc_resp = &res,
4156 };
4157
4158 args.bitmask = nfs4_bitmask(server, label);
4159
4160 nfs_fattr_init(fattr);
4161
4162 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4163 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4164 &res.seq_res, 0);
4165 dprintk("NFS reply lookupp: %d\n", status);
4166 return status;
4167}
4168
4169static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4170 struct nfs_fattr *fattr, struct nfs4_label *label)
4171{
4172 struct nfs4_exception exception = { };
4173 int err;
4174 do {
4175 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4176 trace_nfs4_lookupp(inode, err);
4177 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4178 &exception);
4179 } while (exception.retry);
4180 return err;
4181}
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4184{
Trond Myklebust76b32992007-08-10 17:45:11 -04004185 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 struct nfs4_accessargs args = {
4187 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004188 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004190 struct nfs4_accessres res = {
4191 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004192 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 struct rpc_message msg = {
4194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4195 .rpc_argp = &args,
4196 .rpc_resp = &res,
NeilBrowna52458b2018-12-03 11:30:31 +11004197 .rpc_cred = entry->cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004199 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
Trond Myklebust7c672652018-06-04 15:00:53 -04004201 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004202 res.fattr = nfs_alloc_fattr();
NeilBrowna52458b2018-12-03 11:30:31 +11004203 if (res.fattr == NULL)
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004204 return -ENOMEM;
4205 args.bitmask = server->cache_consistency_bitmask;
4206 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004207 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004209 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004210 if (res.fattr)
4211 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004213 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 return status;
4215}
4216
4217static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4218{
4219 struct nfs4_exception exception = { };
4220 int err;
4221 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004222 err = _nfs4_proc_access(inode, entry);
4223 trace_nfs4_access(inode, err);
4224 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 &exception);
4226 } while (exception.retry);
4227 return err;
4228}
4229
4230/*
4231 * TODO: For the time being, we don't try to get any attributes
4232 * along with any of the zero-copy operations READ, READDIR,
4233 * READLINK, WRITE.
4234 *
4235 * In the case of the first three, we want to put the GETATTR
4236 * after the read-type operation -- this is because it is hard
4237 * to predict the length of a GETATTR response in v4, and thus
4238 * align the READ data correctly. This means that the GETATTR
4239 * may end up partially falling into the page cache, and we should
4240 * shift it into the 'tail' of the xdr_buf before processing.
4241 * To do this efficiently, we need to know the total length
4242 * of data received, which doesn't seem to be available outside
4243 * of the RPC layer.
4244 *
4245 * In the case of WRITE, we also want to put the GETATTR after
4246 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004247 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 *
4249 * Both of these changes to the XDR layer would in fact be quite
4250 * minor, but I decided to leave them for a subsequent patch.
4251 */
4252static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4253 unsigned int pgbase, unsigned int pglen)
4254{
4255 struct nfs4_readlink args = {
4256 .fh = NFS_FH(inode),
4257 .pgbase = pgbase,
4258 .pglen = pglen,
4259 .pages = &page,
4260 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004261 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 struct rpc_message msg = {
4263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4264 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004265 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 };
4267
Bryan Schumaker7c513052011-03-24 17:12:24 +00004268 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 -07004269}
4270
4271static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4272 unsigned int pgbase, unsigned int pglen)
4273{
4274 struct nfs4_exception exception = { };
4275 int err;
4276 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004277 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4278 trace_nfs4_readlink(inode, err);
4279 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 &exception);
4281 } while (exception.retry);
4282 return err;
4283}
4284
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004286 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288static int
4289nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004290 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004292 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004293 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004294 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 int status = 0;
4297
NeilBrown532d4de2016-10-13 15:26:47 +11004298 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004299 if (IS_ERR(ctx))
4300 return PTR_ERR(ctx);
4301
David Quigleyaa9c2662013-05-22 12:50:44 -04004302 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4303
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004304 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4305 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004306 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 if (IS_ERR(state)) {
4308 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004312 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004313 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 return status;
4315}
4316
Trond Myklebust3c591172018-07-31 15:54:10 -04004317static int
4318_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319{
Trond Myklebust16e42952005-10-27 22:12:44 -04004320 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004321 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004323 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004325 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004326 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004327 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4330 .rpc_argp = &args,
4331 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004333 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004334 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004335
Bryan Schumaker7c513052011-03-24 17:12:24 +00004336 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004337 if (status == 0) {
4338 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004339 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004340 /* Removing a directory decrements nlink in the parent */
4341 if (ftype == NF4DIR && dir->i_nlink > 2)
4342 nfs4_dec_nlink_locked(dir);
4343 spin_unlock(&dir->i_lock);
4344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 return status;
4346}
4347
Trond Myklebust912678d2018-03-20 16:43:15 -04004348static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4349{
4350 struct nfs4_exception exception = { };
4351 struct inode *inode = d_inode(dentry);
4352 int err;
4353
Trond Myklebustc01d3642018-03-20 16:43:20 -04004354 if (inode) {
4355 if (inode->i_nlink == 1)
4356 nfs4_inode_return_delegation(inode);
4357 else
4358 nfs4_inode_make_writeable(inode);
4359 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004360 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004361 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004362 trace_nfs4_remove(dir, &dentry->d_name, err);
4363 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4364 &exception);
4365 } while (exception.retry);
4366 return err;
4367}
4368
4369static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
4371 struct nfs4_exception exception = { };
4372 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004375 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004376 trace_nfs4_remove(dir, name, err);
4377 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 &exception);
4379 } while (exception.retry);
4380 return err;
4381}
4382
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004383static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4384 struct dentry *dentry,
4385 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004387 struct nfs_removeargs *args = msg->rpc_argp;
4388 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
Trond Myklebust977fcc22018-03-20 16:43:17 -04004390 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004392 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004393
4394 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004395
4396 if (inode)
4397 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398}
4399
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004400static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4401{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004402 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004403 &data->args.seq_args,
4404 &data->res.seq_res,
4405 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406}
4407
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004408static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004410 struct nfs_unlinkdata *data = task->tk_calldata;
4411 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004412
Trond Myklebust14516c32010-07-31 14:29:06 -04004413 if (!nfs4_sequence_done(task, &res->seq_res))
4414 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004415 if (nfs4_async_handle_error(task, res->server, NULL,
4416 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004417 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004418 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004419 update_changeattr(dir, &res->cinfo,
4420 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004421 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422}
4423
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004424static void nfs4_proc_rename_setup(struct rpc_message *msg,
4425 struct dentry *old_dentry,
4426 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004427{
Jeff Laytond3d41522010-09-17 17:31:57 -04004428 struct nfs_renameargs *arg = msg->rpc_argp;
4429 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004430 struct inode *old_inode = d_inode(old_dentry);
4431 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004432
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004433 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004434 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004435 if (new_inode)
4436 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004437 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004438 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004439 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004440}
4441
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004442static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4443{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004444 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004445 &data->args.seq_args,
4446 &data->res.seq_res,
4447 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004448}
4449
4450static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4451 struct inode *new_dir)
4452{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004453 struct nfs_renamedata *data = task->tk_calldata;
4454 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004455
4456 if (!nfs4_sequence_done(task, &res->seq_res))
4457 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004458 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004459 return 0;
4460
Trond Myklebustc733c492017-01-11 12:32:26 -05004461 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004462 if (new_dir != old_dir) {
4463 /* Note: If we moved a directory, nlink will change */
4464 update_changeattr(old_dir, &res->old_cinfo,
4465 res->old_fattr->time_start,
4466 NFS_INO_INVALID_OTHER);
4467 update_changeattr(new_dir, &res->new_cinfo,
4468 res->new_fattr->time_start,
4469 NFS_INO_INVALID_OTHER);
4470 } else
4471 update_changeattr(old_dir, &res->old_cinfo,
4472 res->old_fattr->time_start,
4473 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004474 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004475 return 1;
4476}
4477
Al Virobeffb8f2016-07-20 16:34:42 -04004478static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004480 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004481 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 struct nfs4_link_arg arg = {
4483 .fh = NFS_FH(inode),
4484 .dir_fh = NFS_FH(dir),
4485 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004486 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004488 struct nfs4_link_res res = {
4489 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004490 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004491 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 struct rpc_message msg = {
4493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4494 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004495 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004497 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Trond Myklebust136f2622010-04-16 16:22:49 -04004499 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004500 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004501 goto out;
4502
David Quigley14c43f72013-05-22 12:50:43 -04004503 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4504 if (IS_ERR(res.label)) {
4505 status = PTR_ERR(res.label);
4506 goto out;
4507 }
4508
Trond Myklebustc01d3642018-03-20 16:43:20 -04004509 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004510 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004511
Bryan Schumaker7c513052011-03-24 17:12:24 +00004512 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004513 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004514 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004515 status = nfs_post_op_update_inode(inode, res.fattr);
4516 if (!status)
4517 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004518 }
David Quigley14c43f72013-05-22 12:50:43 -04004519
4520
4521 nfs4_label_free(res.label);
4522
Trond Myklebust136f2622010-04-16 16:22:49 -04004523out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004524 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 return status;
4526}
4527
Al Virobeffb8f2016-07-20 16:34:42 -04004528static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529{
4530 struct nfs4_exception exception = { };
4531 int err;
4532 do {
4533 err = nfs4_handle_exception(NFS_SERVER(inode),
4534 _nfs4_proc_link(inode, dir, name),
4535 &exception);
4536 } while (exception.retry);
4537 return err;
4538}
4539
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004540struct nfs4_createdata {
4541 struct rpc_message msg;
4542 struct nfs4_create_arg arg;
4543 struct nfs4_create_res res;
4544 struct nfs_fh fh;
4545 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004546 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004547};
4548
4549static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004550 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004551{
4552 struct nfs4_createdata *data;
4553
4554 data = kzalloc(sizeof(*data), GFP_KERNEL);
4555 if (data != NULL) {
4556 struct nfs_server *server = NFS_SERVER(dir);
4557
David Quigley14c43f72013-05-22 12:50:43 -04004558 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4559 if (IS_ERR(data->label))
4560 goto out_free;
4561
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004562 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4563 data->msg.rpc_argp = &data->arg;
4564 data->msg.rpc_resp = &data->res;
4565 data->arg.dir_fh = NFS_FH(dir);
4566 data->arg.server = server;
4567 data->arg.name = name;
4568 data->arg.attrs = sattr;
4569 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004570 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004571 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004572 data->res.server = server;
4573 data->res.fh = &data->fh;
4574 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004575 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004576 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004577 }
4578 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004579out_free:
4580 kfree(data);
4581 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004582}
4583
4584static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4585{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004586 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004587 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004588 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004589 spin_lock(&dir->i_lock);
4590 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004591 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004592 /* Creating a directory bumps nlink in the parent */
4593 if (data->arg.ftype == NF4DIR)
4594 nfs4_inc_nlink_locked(dir);
4595 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004596 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004597 }
4598 return status;
4599}
4600
4601static void nfs4_free_createdata(struct nfs4_createdata *data)
4602{
David Quigley14c43f72013-05-22 12:50:43 -04004603 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004604 kfree(data);
4605}
4606
Chuck Lever4f390c12006-08-22 20:06:22 -04004607static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004608 struct page *page, unsigned int len, struct iattr *sattr,
4609 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004611 struct nfs4_createdata *data;
4612 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Chuck Lever94a6d752006-08-22 20:06:23 -04004614 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004615 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004616
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004617 status = -ENOMEM;
4618 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4619 if (data == NULL)
4620 goto out;
4621
4622 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4623 data->arg.u.symlink.pages = &page;
4624 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004625 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004627 status = nfs4_do_create(dir, dentry, data);
4628
4629 nfs4_free_createdata(data);
4630out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 return status;
4632}
4633
Chuck Lever4f390c12006-08-22 20:06:22 -04004634static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004635 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636{
4637 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004638 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004640
4641 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004644 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4645 trace_nfs4_symlink(dir, &dentry->d_name, err);
4646 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 &exception);
4648 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004649
4650 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 return err;
4652}
4653
4654static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004655 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004657 struct nfs4_createdata *data;
4658 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004660 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4661 if (data == NULL)
4662 goto out;
4663
David Quigley1775fd32013-05-22 12:50:42 -04004664 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004665 status = nfs4_do_create(dir, dentry, data);
4666
4667 nfs4_free_createdata(data);
4668out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 return status;
4670}
4671
4672static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4673 struct iattr *sattr)
4674{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004675 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004677 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004679
David Quigleyaa9c2662013-05-22 12:50:44 -04004680 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4681
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004682 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4683 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004685 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4686 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4687 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 &exception);
4689 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004690 nfs4_label_release_security(label);
4691
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 return err;
4693}
4694
NeilBrown684f39b2018-12-03 11:30:30 +11004695static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004696 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
David Howells2b0143b2015-03-17 22:25:59 +00004698 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 struct nfs4_readdir_arg args = {
4700 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004701 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 .pgbase = 0,
4703 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004704 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004705 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 };
4707 struct nfs4_readdir_res res;
4708 struct rpc_message msg = {
4709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4710 .rpc_argp = &args,
4711 .rpc_resp = &res,
NeilBrowna52458b2018-12-03 11:30:31 +11004712 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 };
4714 int status;
4715
Al Viro6de14722013-09-16 10:53:17 -04004716 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4717 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004718 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004719 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004721 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 -05004722 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004723 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004724 status += args.pgbase;
4725 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004726
4727 nfs_invalidate_atime(dir);
4728
Harvey Harrison3110ff82008-05-02 13:42:44 -07004729 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 return status;
4731}
4732
NeilBrown684f39b2018-12-03 11:30:30 +11004733static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004734 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735{
4736 struct nfs4_exception exception = { };
4737 int err;
4738 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004739 err = _nfs4_proc_readdir(dentry, cred, cookie,
4740 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004741 trace_nfs4_readdir(d_inode(dentry), err);
4742 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 &exception);
4744 } while (exception.retry);
4745 return err;
4746}
4747
4748static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004749 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004751 struct nfs4_createdata *data;
4752 int mode = sattr->ia_mode;
4753 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004755 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4756 if (data == NULL)
4757 goto out;
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004760 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004762 data->arg.ftype = NF4BLK;
4763 data->arg.u.device.specdata1 = MAJOR(rdev);
4764 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 }
4766 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004767 data->arg.ftype = NF4CHR;
4768 data->arg.u.device.specdata1 = MAJOR(rdev);
4769 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004770 } else if (!S_ISSOCK(mode)) {
4771 status = -EINVAL;
4772 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 }
David Quigley1775fd32013-05-22 12:50:42 -04004774
David Quigleyaa9c2662013-05-22 12:50:44 -04004775 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004776 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004777out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004778 nfs4_free_createdata(data);
4779out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 return status;
4781}
4782
4783static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4784 struct iattr *sattr, dev_t rdev)
4785{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004786 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004788 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004790
David Quigleyaa9c2662013-05-22 12:50:44 -04004791 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4792
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004793 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4794 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004796 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4797 trace_nfs4_mknod(dir, &dentry->d_name, err);
4798 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 &exception);
4800 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004801
4802 nfs4_label_release_security(label);
4803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 return err;
4805}
4806
4807static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4808 struct nfs_fsstat *fsstat)
4809{
4810 struct nfs4_statfs_arg args = {
4811 .fh = fhandle,
4812 .bitmask = server->attr_bitmask,
4813 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004814 struct nfs4_statfs_res res = {
4815 .fsstat = fsstat,
4816 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 struct rpc_message msg = {
4818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4819 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004820 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 };
4822
Trond Myklebust0e574af2005-10-27 22:12:38 -04004823 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004824 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825}
4826
4827static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4828{
4829 struct nfs4_exception exception = { };
4830 int err;
4831 do {
4832 err = nfs4_handle_exception(server,
4833 _nfs4_proc_statfs(server, fhandle, fsstat),
4834 &exception);
4835 } while (exception.retry);
4836 return err;
4837}
4838
4839static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4840 struct nfs_fsinfo *fsinfo)
4841{
4842 struct nfs4_fsinfo_arg args = {
4843 .fh = fhandle,
4844 .bitmask = server->attr_bitmask,
4845 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004846 struct nfs4_fsinfo_res res = {
4847 .fsinfo = fsinfo,
4848 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 struct rpc_message msg = {
4850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4851 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004852 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 };
4854
Bryan Schumaker7c513052011-03-24 17:12:24 +00004855 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856}
4857
4858static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4859{
4860 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004861 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 int err;
4863
4864 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004865 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004866 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004867 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004868 nfs4_set_lease_period(server->nfs_client,
4869 fsinfo->lease_time * HZ,
4870 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004871 break;
4872 }
4873 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 } while (exception.retry);
4875 return err;
4876}
4877
4878static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4879{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004880 int error;
4881
Trond Myklebust0e574af2005-10-27 22:12:38 -04004882 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004883 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004884 if (error == 0) {
4885 /* block layout checks this! */
4886 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004887 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004888 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004889
4890 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891}
4892
4893static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4894 struct nfs_pathconf *pathconf)
4895{
4896 struct nfs4_pathconf_arg args = {
4897 .fh = fhandle,
4898 .bitmask = server->attr_bitmask,
4899 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004900 struct nfs4_pathconf_res res = {
4901 .pathconf = pathconf,
4902 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 struct rpc_message msg = {
4904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4905 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004906 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 };
4908
4909 /* None of the pathconf attributes are mandatory to implement */
4910 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4911 memset(pathconf, 0, sizeof(*pathconf));
4912 return 0;
4913 }
4914
Trond Myklebust0e574af2005-10-27 22:12:38 -04004915 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004916 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917}
4918
4919static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4920 struct nfs_pathconf *pathconf)
4921{
4922 struct nfs4_exception exception = { };
4923 int err;
4924
4925 do {
4926 err = nfs4_handle_exception(server,
4927 _nfs4_proc_pathconf(server, fhandle, pathconf),
4928 &exception);
4929 } while (exception.retry);
4930 return err;
4931}
4932
Trond Myklebust5521abf2013-03-16 20:54:34 -04004933int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004934 const struct nfs_open_context *ctx,
4935 const struct nfs_lock_context *l_ctx,
4936 fmode_t fmode)
4937{
NeilBrown17393472016-10-13 15:26:47 +11004938 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004939}
4940EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4941
Trond Myklebust5521abf2013-03-16 20:54:34 -04004942static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4943 const struct nfs_open_context *ctx,
4944 const struct nfs_lock_context *l_ctx,
4945 fmode_t fmode)
4946{
4947 nfs4_stateid current_stateid;
4948
Trond Myklebuste1253be2014-03-05 08:44:23 -05004949 /* If the current stateid represents a lost lock, then exit */
4950 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4951 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004952 return nfs4_stateid_match(stateid, &current_stateid);
4953}
4954
4955static bool nfs4_error_stateid_expired(int err)
4956{
4957 switch (err) {
4958 case -NFS4ERR_DELEG_REVOKED:
4959 case -NFS4ERR_ADMIN_REVOKED:
4960 case -NFS4ERR_BAD_STATEID:
4961 case -NFS4ERR_STALE_STATEID:
4962 case -NFS4ERR_OLD_STATEID:
4963 case -NFS4ERR_OPENMODE:
4964 case -NFS4ERR_EXPIRED:
4965 return true;
4966 }
4967 return false;
4968}
4969
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004970static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004972 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004974 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004975 if (task->tk_status < 0) {
4976 struct nfs4_exception exception = {
4977 .inode = hdr->inode,
4978 .state = hdr->args.context->state,
4979 .stateid = &hdr->args.stateid,
4980 };
4981 task->tk_status = nfs4_async_handle_exception(task,
4982 server, task->tk_status, &exception);
4983 if (exception.retry) {
4984 rpc_restart_call_prepare(task);
4985 return -EAGAIN;
4986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004990 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992}
4993
Trond Myklebust5521abf2013-03-16 20:54:34 -04004994static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004995 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004996{
4997
4998 if (!nfs4_error_stateid_expired(task->tk_status) ||
4999 nfs4_stateid_is_current(&args->stateid,
5000 args->context,
5001 args->lock_context,
5002 FMODE_READ))
5003 return false;
5004 rpc_restart_call_prepare(task);
5005 return true;
5006}
5007
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005008static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005009{
5010
5011 dprintk("--> %s\n", __func__);
5012
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005013 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005014 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005015 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005016 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005017 if (task->tk_status > 0)
5018 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005019 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5020 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005021}
5022
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005023static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5024 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005026 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005027 if (!hdr->pgio_done_cb)
5028 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005029 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005030 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031}
5032
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005033static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5034 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005035{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005036 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005037 &hdr->args.seq_args,
5038 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005039 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005040 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005041 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5042 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005043 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005044 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005045 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005046 return -EIO;
5047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048}
5049
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005050static int nfs4_write_done_cb(struct rpc_task *task,
5051 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005053 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005054
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005055 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005056 if (task->tk_status < 0) {
5057 struct nfs4_exception exception = {
5058 .inode = hdr->inode,
5059 .state = hdr->args.context->state,
5060 .stateid = &hdr->args.stateid,
5061 };
5062 task->tk_status = nfs4_async_handle_exception(task,
5063 NFS_SERVER(inode), task->tk_status,
5064 &exception);
5065 if (exception.retry) {
5066 rpc_restart_call_prepare(task);
5067 return -EAGAIN;
5068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005070 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005071 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005072 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005073 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005074 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075}
5076
Trond Myklebust5521abf2013-03-16 20:54:34 -04005077static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005078 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005079{
5080
5081 if (!nfs4_error_stateid_expired(task->tk_status) ||
5082 nfs4_stateid_is_current(&args->stateid,
5083 args->context,
5084 args->lock_context,
5085 FMODE_WRITE))
5086 return false;
5087 rpc_restart_call_prepare(task);
5088 return true;
5089}
5090
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005091static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005092{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005093 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005094 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005095 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005096 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005097 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5098 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005099}
5100
Trond Myklebust5a37f852012-04-28 14:55:16 -04005101static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005102bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005103{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005104 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005105 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005106 return false;
5107 /* Otherwise, request attributes if and only if we don't hold
5108 * a delegation
5109 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005110 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005111}
Fred Isamana69aef12011-03-03 15:13:47 +00005112
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005113static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005114 struct rpc_message *msg,
5115 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005117 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005118
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005119 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5120 hdr->args.bitmask = NULL;
5121 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005122 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005123 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005124
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005125 if (!hdr->pgio_done_cb)
5126 hdr->pgio_done_cb = nfs4_write_done_cb;
5127 hdr->res.server = server;
5128 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005130 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005131 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005132 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133}
5134
Fred Isaman0b7c0152012-04-20 14:47:39 -04005135static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5136{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005137 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005138 &data->args.seq_args,
5139 &data->res.seq_res,
5140 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141}
5142
Fred Isaman0b7c0152012-04-20 14:47:39 -04005143static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005146
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005147 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005148 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5149 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005150 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005151 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154}
5155
Fred Isaman0b7c0152012-04-20 14:47:39 -04005156static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005157{
5158 if (!nfs4_sequence_done(task, &data->res.seq_res))
5159 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005160 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005161}
5162
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005163static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5164 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005166 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005167
Fred Isaman0b7c0152012-04-20 14:47:39 -04005168 if (data->commit_done_cb == NULL)
5169 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005170 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005171 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005172 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005173 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174}
5175
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005176static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5177 struct nfs_commitres *res)
5178{
5179 struct inode *dst_inode = file_inode(dst);
5180 struct nfs_server *server = NFS_SERVER(dst_inode);
5181 struct rpc_message msg = {
5182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5183 .rpc_argp = args,
5184 .rpc_resp = res,
5185 };
5186
5187 args->fh = NFS_FH(dst_inode);
5188 return nfs4_call_sync(server->client, server, &msg,
5189 &args->seq_args, &res->seq_res, 1);
5190}
5191
5192int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5193{
5194 struct nfs_commitargs args = {
5195 .offset = offset,
5196 .count = count,
5197 };
5198 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5199 struct nfs4_exception exception = { };
5200 int status;
5201
5202 do {
5203 status = _nfs4_proc_commit(dst, &args, res);
5204 status = nfs4_handle_exception(dst_server, status, &exception);
5205 } while (exception.retry);
5206
5207 return status;
5208}
5209
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005210struct nfs4_renewdata {
5211 struct nfs_client *client;
5212 unsigned long timestamp;
5213};
5214
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215/*
5216 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5217 * standalone procedure for queueing an asynchronous RENEW.
5218 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005219static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005220{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005221 struct nfs4_renewdata *data = calldata;
5222 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005223
Elena Reshetova212bf412017-10-20 12:53:38 +03005224 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005225 nfs4_schedule_state_renewal(clp);
5226 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005227 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005228}
5229
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005230static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005232 struct nfs4_renewdata *data = calldata;
5233 struct nfs_client *clp = data->client;
5234 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005236 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005237 switch (task->tk_status) {
5238 case 0:
5239 break;
5240 case -NFS4ERR_LEASE_MOVED:
5241 nfs4_schedule_lease_moved_recovery(clp);
5242 break;
5243 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005244 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005245 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5246 return;
5247 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005248 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005249 return;
5250 }
5251 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005253 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254}
5255
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005256static const struct rpc_call_ops nfs4_renew_ops = {
5257 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005258 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005259};
5260
NeilBrowna52458b2018-12-03 11:30:31 +11005261static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262{
5263 struct rpc_message msg = {
5264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5265 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005266 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005268 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005270 if (renew_flags == 0)
5271 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005272 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005273 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005274 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005275 if (data == NULL) {
5276 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005277 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005278 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005279 data->client = clp;
5280 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005281 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005282 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283}
5284
NeilBrowna52458b2018-12-03 11:30:31 +11005285static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286{
5287 struct rpc_message msg = {
5288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5289 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005290 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 };
5292 unsigned long now = jiffies;
5293 int status;
5294
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005295 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 if (status < 0)
5297 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005298 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 return 0;
5300}
5301
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005302static inline int nfs4_server_supports_acls(struct nfs_server *server)
5303{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005304 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005305}
5306
Trond Myklebust21f498c2012-08-24 10:59:25 -04005307/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5308 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005309 * the stack.
5310 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005311#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005312
Neil Hormane9e3d722011-03-04 19:26:03 -05005313static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005314 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005315{
5316 struct page *newpage, **spages;
5317 int rc = 0;
5318 size_t len;
5319 spages = pages;
5320
5321 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005322 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005323 newpage = alloc_page(GFP_KERNEL);
5324
5325 if (newpage == NULL)
5326 goto unwind;
5327 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005328 buf += len;
5329 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005330 *pages++ = newpage;
5331 rc++;
5332 } while (buflen != 0);
5333
5334 return rc;
5335
5336unwind:
5337 for(; rc > 0; rc--)
5338 __free_page(spages[rc-1]);
5339 return -ENOMEM;
5340}
5341
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005342struct nfs4_cached_acl {
5343 int cached;
5344 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005345 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005346};
5347
5348static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005349{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005350 struct nfs_inode *nfsi = NFS_I(inode);
5351
5352 spin_lock(&inode->i_lock);
5353 kfree(nfsi->nfs4_acl);
5354 nfsi->nfs4_acl = acl;
5355 spin_unlock(&inode->i_lock);
5356}
5357
5358static void nfs4_zap_acl_attr(struct inode *inode)
5359{
5360 nfs4_set_cached_acl(inode, NULL);
5361}
5362
5363static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5364{
5365 struct nfs_inode *nfsi = NFS_I(inode);
5366 struct nfs4_cached_acl *acl;
5367 int ret = -ENOENT;
5368
5369 spin_lock(&inode->i_lock);
5370 acl = nfsi->nfs4_acl;
5371 if (acl == NULL)
5372 goto out;
5373 if (buf == NULL) /* user is just asking for length */
5374 goto out_len;
5375 if (acl->cached == 0)
5376 goto out;
5377 ret = -ERANGE; /* see getxattr(2) man page */
5378 if (acl->len > buflen)
5379 goto out;
5380 memcpy(buf, acl->data, acl->len);
5381out_len:
5382 ret = acl->len;
5383out:
5384 spin_unlock(&inode->i_lock);
5385 return ret;
5386}
5387
Sachin Prabhu5794d212012-04-17 14:36:40 +01005388static 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 +00005389{
5390 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005391 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005392
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005393 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005394 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005395 if (acl == NULL)
5396 goto out;
5397 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005398 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005399 } else {
5400 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5401 if (acl == NULL)
5402 goto out;
5403 acl->cached = 0;
5404 }
5405 acl->len = acl_len;
5406out:
5407 nfs4_set_cached_acl(inode, acl);
5408}
5409
Andy Adamsonbf118a32011-12-07 11:55:27 -05005410/*
5411 * The getxattr API returns the required buffer length when called with a
5412 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5413 * the required buf. On a NULL buf, we send a page of data to the server
5414 * guessing that the ACL request can be serviced by a page. If so, we cache
5415 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5416 * the cache. If not so, we throw away the page, and cache the required
5417 * length. The next getxattr call will then produce another round trip to
5418 * the server, this time with the input buf of the required size.
5419 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005420static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005421{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005422 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005423 struct nfs_getaclargs args = {
5424 .fh = NFS_FH(inode),
5425 .acl_pages = pages,
5426 .acl_len = buflen,
5427 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005428 struct nfs_getaclres res = {
5429 .acl_len = buflen,
5430 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005431 struct rpc_message msg = {
5432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5433 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005434 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005435 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005436 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005437 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005438
Trond Myklebust21f498c2012-08-24 10:59:25 -04005439 if (npages > ARRAY_SIZE(pages))
5440 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005441
Andy Adamsonbf118a32011-12-07 11:55:27 -05005442 for (i = 0; i < npages; i++) {
5443 pages[i] = alloc_page(GFP_KERNEL);
5444 if (!pages[i])
5445 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005446 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005447
5448 /* for decoding across pages */
5449 res.acl_scratch = alloc_page(GFP_KERNEL);
5450 if (!res.acl_scratch)
5451 goto out_free;
5452
Andy Adamsonbf118a32011-12-07 11:55:27 -05005453 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005454
Peng Taode040be2012-01-10 22:42:47 +08005455 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005456 __func__, buf, buflen, npages, args.acl_len);
5457 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5458 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005459 if (ret)
5460 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005461
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005462 /* Handle the case where the passed-in buffer is too short */
5463 if (res.acl_flags & NFS4_ACL_TRUNC) {
5464 /* Did the user only issue a request for the acl length? */
5465 if (buf == NULL)
5466 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005467 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005468 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005469 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005470 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005471 if (buf) {
5472 if (res.acl_len > buflen) {
5473 ret = -ERANGE;
5474 goto out_free;
5475 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005476 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005477 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005478out_ok:
5479 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005480out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005481 for (i = 0; i < npages; i++)
5482 if (pages[i])
5483 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005484 if (res.acl_scratch)
5485 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005486 return ret;
5487}
5488
Trond Myklebust16b42892006-08-24 12:27:15 -04005489static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5490{
5491 struct nfs4_exception exception = { };
5492 ssize_t ret;
5493 do {
5494 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005495 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005496 if (ret >= 0)
5497 break;
5498 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5499 } while (exception.retry);
5500 return ret;
5501}
5502
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005503static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5504{
5505 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005506 int ret;
5507
5508 if (!nfs4_server_supports_acls(server))
5509 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005510 ret = nfs_revalidate_inode(server, inode);
5511 if (ret < 0)
5512 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005513 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5514 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005515 ret = nfs4_read_cached_acl(inode, buf, buflen);
5516 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005517 /* -ENOENT is returned if there is no ACL or if there is an ACL
5518 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005519 return ret;
5520 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005521}
5522
Trond Myklebust16b42892006-08-24 12:27:15 -04005523static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005524{
5525 struct nfs_server *server = NFS_SERVER(inode);
5526 struct page *pages[NFS4ACL_MAXPAGES];
5527 struct nfs_setaclargs arg = {
5528 .fh = NFS_FH(inode),
5529 .acl_pages = pages,
5530 .acl_len = buflen,
5531 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005532 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005533 struct rpc_message msg = {
5534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5535 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005536 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005537 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005538 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005539 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005540
5541 if (!nfs4_server_supports_acls(server))
5542 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005543 if (npages > ARRAY_SIZE(pages))
5544 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005545 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005546 if (i < 0)
5547 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005548 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005549 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005550
5551 /*
5552 * Free each page after tx, so the only ref left is
5553 * held by the network stack
5554 */
5555 for (; i > 0; i--)
5556 put_page(pages[i-1]);
5557
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005558 /*
5559 * Acl update can result in inode attribute update.
5560 * so mark the attribute cache invalid.
5561 */
5562 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005563 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005564 | NFS_INO_INVALID_CTIME
5565 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005566 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005567 nfs_access_zap_cache(inode);
5568 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005569 return ret;
5570}
5571
Trond Myklebust16b42892006-08-24 12:27:15 -04005572static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5573{
5574 struct nfs4_exception exception = { };
5575 int err;
5576 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005577 err = __nfs4_proc_set_acl(inode, buf, buflen);
5578 trace_nfs4_set_acl(inode, err);
5579 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005580 &exception);
5581 } while (exception.retry);
5582 return err;
5583}
5584
David Quigleyaa9c2662013-05-22 12:50:44 -04005585#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5586static int _nfs4_get_security_label(struct inode *inode, void *buf,
5587 size_t buflen)
5588{
5589 struct nfs_server *server = NFS_SERVER(inode);
5590 struct nfs_fattr fattr;
5591 struct nfs4_label label = {0, 0, buflen, buf};
5592
5593 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005594 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005595 .fh = NFS_FH(inode),
5596 .bitmask = bitmask,
5597 };
5598 struct nfs4_getattr_res res = {
5599 .fattr = &fattr,
5600 .label = &label,
5601 .server = server,
5602 };
5603 struct rpc_message msg = {
5604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005605 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005606 .rpc_resp = &res,
5607 };
5608 int ret;
5609
5610 nfs_fattr_init(&fattr);
5611
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005612 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005613 if (ret)
5614 return ret;
5615 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5616 return -ENOENT;
5617 if (buflen < label.len)
5618 return -ERANGE;
5619 return 0;
5620}
5621
5622static int nfs4_get_security_label(struct inode *inode, void *buf,
5623 size_t buflen)
5624{
5625 struct nfs4_exception exception = { };
5626 int err;
5627
5628 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5629 return -EOPNOTSUPP;
5630
5631 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005632 err = _nfs4_get_security_label(inode, buf, buflen);
5633 trace_nfs4_get_security_label(inode, err);
5634 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005635 &exception);
5636 } while (exception.retry);
5637 return err;
5638}
5639
5640static int _nfs4_do_set_security_label(struct inode *inode,
5641 struct nfs4_label *ilabel,
5642 struct nfs_fattr *fattr,
5643 struct nfs4_label *olabel)
5644{
5645
5646 struct iattr sattr = {0};
5647 struct nfs_server *server = NFS_SERVER(inode);
5648 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005649 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005650 .fh = NFS_FH(inode),
5651 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005652 .server = server,
5653 .bitmask = bitmask,
5654 .label = ilabel,
5655 };
5656 struct nfs_setattrres res = {
5657 .fattr = fattr,
5658 .label = olabel,
5659 .server = server,
5660 };
5661 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5663 .rpc_argp = &arg,
5664 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005665 };
5666 int status;
5667
Jeff Layton12207f62013-11-01 10:49:32 -04005668 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005669
Jeff Layton12207f62013-11-01 10:49:32 -04005670 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005671 if (status)
5672 dprintk("%s failed: %d\n", __func__, status);
5673
5674 return status;
5675}
5676
5677static int nfs4_do_set_security_label(struct inode *inode,
5678 struct nfs4_label *ilabel,
5679 struct nfs_fattr *fattr,
5680 struct nfs4_label *olabel)
5681{
5682 struct nfs4_exception exception = { };
5683 int err;
5684
5685 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005686 err = _nfs4_do_set_security_label(inode, ilabel,
5687 fattr, olabel);
5688 trace_nfs4_set_security_label(inode, err);
5689 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005690 &exception);
5691 } while (exception.retry);
5692 return err;
5693}
5694
5695static int
Al Viro59301222016-05-27 10:19:30 -04005696nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005697{
5698 struct nfs4_label ilabel, *olabel = NULL;
5699 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005700 int status;
5701
5702 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5703 return -EOPNOTSUPP;
5704
5705 nfs_fattr_init(&fattr);
5706
5707 ilabel.pi = 0;
5708 ilabel.lfs = 0;
5709 ilabel.label = (char *)buf;
5710 ilabel.len = buflen;
5711
David Quigleyaa9c2662013-05-22 12:50:44 -04005712 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5713 if (IS_ERR(olabel)) {
5714 status = -PTR_ERR(olabel);
5715 goto out;
5716 }
5717
5718 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5719 if (status == 0)
5720 nfs_setsecurity(inode, &fattr, olabel);
5721
5722 nfs4_label_free(olabel);
5723out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005724 return status;
5725}
5726#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5727
5728
Chuck Leverf0920752012-05-21 22:45:41 -04005729static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5730 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005731{
5732 __be32 verf[2];
5733
Chuck Lever2c820d92012-05-21 22:45:33 -04005734 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5735 /* An impossible timestamp guarantees this value
5736 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005737 verf[0] = cpu_to_be32(U32_MAX);
5738 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005739 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005740 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005741 u64 ns = ktime_to_ns(nn->boot_time);
5742
5743 verf[0] = cpu_to_be32(ns >> 32);
5744 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005745 }
Chuck Levercd937102012-03-02 17:14:31 -05005746 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5747}
5748
Jeff Laytona3192682015-06-09 19:43:59 -04005749static int
5750nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005751{
Jeff Laytona3192682015-06-09 19:43:59 -04005752 size_t len;
5753 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005754
Trond Myklebustceb3a162015-01-03 15:16:04 -05005755 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005756 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005757
Jeff Laytona3192682015-06-09 19:43:59 -04005758 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005759 len = 14 +
5760 strlen(clp->cl_rpcclient->cl_nodename) +
5761 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005762 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005763 1;
5764 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005765 if (nfs4_client_id_uniquifier[0] != '\0')
5766 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005767 if (len > NFS4_OPAQUE_LIMIT + 1)
5768 return -EINVAL;
5769
5770 /*
5771 * Since this string is allocated at mount time, and held until the
5772 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5773 * about a memory-reclaim deadlock.
5774 */
5775 str = kmalloc(len, GFP_KERNEL);
5776 if (!str)
5777 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005778
Chuck Levere984a552012-09-14 17:24:21 -04005779 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005780 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005781 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005782 clp->cl_rpcclient->cl_nodename,
5783 nfs4_client_id_uniquifier,
5784 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005785 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005786 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005787 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005788 clp->cl_rpcclient->cl_nodename,
5789 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005790 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005791 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005792
Jeff Laytona3192682015-06-09 19:43:59 -04005793 clp->cl_owner_id = str;
5794 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005795}
5796
Jeff Layton873e3852015-06-09 19:44:00 -04005797static int
5798nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005799{
Jeff Layton873e3852015-06-09 19:44:00 -04005800 size_t len;
5801 char *str;
5802
5803 len = 10 + 10 + 1 + 10 + 1 +
5804 strlen(nfs4_client_id_uniquifier) + 1 +
5805 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5806
5807 if (len > NFS4_OPAQUE_LIMIT + 1)
5808 return -EINVAL;
5809
5810 /*
5811 * Since this string is allocated at mount time, and held until the
5812 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5813 * about a memory-reclaim deadlock.
5814 */
5815 str = kmalloc(len, GFP_KERNEL);
5816 if (!str)
5817 return -ENOMEM;
5818
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005819 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005820 clp->rpc_ops->version, clp->cl_minorversion,
5821 nfs4_client_id_uniquifier,
5822 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005823 clp->cl_owner_id = str;
5824 return 0;
5825}
5826
5827static int
5828nfs4_init_uniform_client_string(struct nfs_client *clp)
5829{
Jeff Layton873e3852015-06-09 19:44:00 -04005830 size_t len;
5831 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005832
5833 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005834 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005835
5836 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005837 return nfs4_init_uniquifier_client_string(clp);
5838
5839 len = 10 + 10 + 1 + 10 + 1 +
5840 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5841
5842 if (len > NFS4_OPAQUE_LIMIT + 1)
5843 return -EINVAL;
5844
5845 /*
5846 * Since this string is allocated at mount time, and held until the
5847 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5848 * about a memory-reclaim deadlock.
5849 */
5850 str = kmalloc(len, GFP_KERNEL);
5851 if (!str)
5852 return -ENOMEM;
5853
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005854 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005855 clp->rpc_ops->version, clp->cl_minorversion,
5856 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005857 clp->cl_owner_id = str;
5858 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005859}
5860
Chuck Lever706cb8d2014-03-12 12:51:47 -04005861/*
5862 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5863 * services. Advertise one based on the address family of the
5864 * clientaddr.
5865 */
5866static unsigned int
5867nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5868{
5869 if (strchr(clp->cl_ipaddr, ':') != NULL)
5870 return scnprintf(buf, len, "tcp6");
5871 else
5872 return scnprintf(buf, len, "tcp");
5873}
5874
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005875static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5876{
5877 struct nfs4_setclientid *sc = calldata;
5878
5879 if (task->tk_status == 0)
5880 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5881}
5882
5883static const struct rpc_call_ops nfs4_setclientid_ops = {
5884 .rpc_call_done = nfs4_setclientid_done,
5885};
5886
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005887/**
5888 * nfs4_proc_setclientid - Negotiate client ID
5889 * @clp: state data structure
5890 * @program: RPC program for NFSv4 callback service
5891 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11005892 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005893 * @res: where to place the result
5894 *
5895 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5896 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005897int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11005898 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005899 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900{
5901 nfs4_verifier sc_verifier;
5902 struct nfs4_setclientid setclientid = {
5903 .sc_verifier = &sc_verifier,
5904 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005905 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 };
5907 struct rpc_message msg = {
5908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5909 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005910 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005911 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005913 struct rpc_task *task;
5914 struct rpc_task_setup task_setup_data = {
5915 .rpc_client = clp->cl_rpcclient,
5916 .rpc_message = &msg,
5917 .callback_ops = &nfs4_setclientid_ops,
5918 .callback_data = &setclientid,
5919 .flags = RPC_TASK_TIMEOUT,
5920 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005921 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
Chuck Leverde734832012-07-11 16:30:50 -04005923 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005924 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005925
5926 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5927 status = nfs4_init_uniform_client_string(clp);
5928 else
Jeff Laytona3192682015-06-09 19:43:59 -04005929 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005930
5931 if (status)
5932 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005933
Chuck Leverde734832012-07-11 16:30:50 -04005934 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005935 setclientid.sc_netid_len =
5936 nfs4_init_callback_netid(clp,
5937 setclientid.sc_netid,
5938 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005939 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005940 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 clp->cl_ipaddr, port >> 8, port & 255);
5942
Jeff Layton3a6bb732015-06-09 19:43:57 -04005943 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005944 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005945 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005946 task = rpc_run_task(&task_setup_data);
5947 if (IS_ERR(task)) {
5948 status = PTR_ERR(task);
5949 goto out;
5950 }
5951 status = task->tk_status;
5952 if (setclientid.sc_cred) {
5953 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5954 put_rpccred(setclientid.sc_cred);
5955 }
5956 rpc_put_task(task);
5957out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005958 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005959 dprintk("NFS reply setclientid: %d\n", status);
5960 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961}
5962
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005963/**
5964 * nfs4_proc_setclientid_confirm - Confirm client ID
5965 * @clp: state data structure
5966 * @res: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11005967 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005968 *
5969 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5970 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005971int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005972 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11005973 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 struct rpc_message msg = {
5976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005977 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005978 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 int status;
5981
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005982 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5983 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5984 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005985 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005986 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005987 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 return status;
5989}
5990
Trond Myklebustfe650402006-01-03 09:55:18 +01005991struct nfs4_delegreturndata {
5992 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005993 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005994 struct nfs_fh fh;
5995 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005996 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005997 struct {
5998 struct nfs4_layoutreturn_args arg;
5999 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006000 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006001 u32 roc_barrier;
6002 bool roc;
6003 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006004 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006005 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006006 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006007};
6008
Trond Myklebustfe650402006-01-03 09:55:18 +01006009static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6010{
6011 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006012 struct nfs4_exception exception = {
6013 .inode = data->inode,
6014 .stateid = &data->stateid,
6015 };
Andy Adamson938e1012009-04-01 09:22:28 -04006016
Trond Myklebust14516c32010-07-31 14:29:06 -04006017 if (!nfs4_sequence_done(task, &data->res.seq_res))
6018 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006019
Trond Myklebustca8acf82013-08-13 10:36:56 -04006020 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006021
6022 /* Handle Layoutreturn errors */
6023 if (data->args.lr_args && task->tk_status != 0) {
6024 switch(data->res.lr_ret) {
6025 default:
6026 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6027 break;
6028 case 0:
6029 data->args.lr_args = NULL;
6030 data->res.lr_res = NULL;
6031 break;
Trond Myklebust73800202017-11-06 15:28:07 -05006032 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04006033 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04006034 &data->args.lr_args->range,
Trond Myklebust140087fd2017-11-06 15:28:10 -05006035 data->inode))
6036 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05006037 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05006038 case -NFS4ERR_ADMIN_REVOKED:
6039 case -NFS4ERR_DELEG_REVOKED:
6040 case -NFS4ERR_EXPIRED:
6041 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05006042 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6043 case -NFS4ERR_WRONG_CRED:
6044 data->args.lr_args = NULL;
6045 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006046 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006047 }
6048 }
6049
Ricardo Labiaga79708862009-12-07 09:23:21 -05006050 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006051 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006052 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006053 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006054 case -NFS4ERR_ADMIN_REVOKED:
6055 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006056 case -NFS4ERR_EXPIRED:
6057 nfs4_free_revoked_stateid(data->res.server,
6058 data->args.stateid,
6059 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006060 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006061 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006062 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006063 task->tk_status = 0;
6064 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006065 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05006066 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6067 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006068 task->tk_status = 0;
6069 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006070 case -NFS4ERR_ACCESS:
6071 if (data->args.bitmask) {
6072 data->args.bitmask = NULL;
6073 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006074 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006075 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006076 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006077 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006078 task->tk_status = nfs4_async_handle_exception(task,
6079 data->res.server, task->tk_status,
6080 &exception);
6081 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006082 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006083 }
6084 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006085 return;
6086lr_restart:
6087 data->res.lr_ret = 0;
6088out_restart:
6089 task->tk_status = 0;
6090 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006091}
6092
6093static void nfs4_delegreturn_release(void *calldata)
6094{
Peng Tao039b7562014-07-03 13:05:02 +08006095 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006096 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006097
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006098 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006099 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006100 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6101 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006102 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006103 nfs_iput_and_deactive(inode);
6104 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006105 kfree(calldata);
6106}
6107
Andy Adamson938e1012009-04-01 09:22:28 -04006108static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6109{
6110 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006111 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006112
6113 d_data = (struct nfs4_delegreturndata *)data;
6114
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006115 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08006116 return;
6117
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006118 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6119 if (lo && !pnfs_layout_is_valid(lo)) {
6120 d_data->args.lr_args = NULL;
6121 d_data->res.lr_res = NULL;
6122 }
6123
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006124 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006125 &d_data->args.seq_args,
6126 &d_data->res.seq_res,
6127 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006128}
Andy Adamson938e1012009-04-01 09:22:28 -04006129
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006130static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006131 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006132 .rpc_call_done = nfs4_delegreturn_done,
6133 .rpc_release = nfs4_delegreturn_release,
6134};
6135
NeilBrowna52458b2018-12-03 11:30:31 +11006136static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006137{
6138 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006139 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006140 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006141 struct rpc_message msg = {
6142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6143 .rpc_cred = cred,
6144 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006145 struct rpc_task_setup task_setup_data = {
6146 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006147 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006148 .callback_ops = &nfs4_delegreturn_ops,
6149 .flags = RPC_TASK_ASYNC,
6150 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006151 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006152
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006153 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006154 if (data == NULL)
6155 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006156 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006157
6158 nfs4_state_protect(server->nfs_client,
6159 NFS_SP4_MACH_CRED_CLEANUP,
6160 &task_setup_data.rpc_client, &msg);
6161
Trond Myklebustfe650402006-01-03 09:55:18 +01006162 data->args.fhandle = &data->fh;
6163 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006164 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006165 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006166 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006167 data->res.fattr = &data->fattr;
6168 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006169 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006170 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006171 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006172 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006173 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006174 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006175 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006176 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006177 if (data->lr.roc) {
6178 data->args.lr_args = &data->lr.arg;
6179 data->res.lr_res = &data->lr.res;
6180 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006181 } else if (data->lr.roc) {
6182 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6183 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006184 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006185
Trond Myklebustc970aa82007-07-14 15:39:59 -04006186 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006187 msg.rpc_argp = &data->args;
6188 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006189 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006190 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006191 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006192 if (!issync)
6193 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006194 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006195 if (status != 0)
6196 goto out;
6197 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006198out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006199 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006200 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201}
6202
NeilBrowna52458b2018-12-03 11:30:31 +11006203int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204{
6205 struct nfs_server *server = NFS_SERVER(inode);
6206 struct nfs4_exception exception = { };
6207 int err;
6208 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006209 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006210 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 switch (err) {
6212 case -NFS4ERR_STALE_STATEID:
6213 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 case 0:
6215 return 0;
6216 }
6217 err = nfs4_handle_exception(server, err, &exception);
6218 } while (exception.retry);
6219 return err;
6220}
6221
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6223{
6224 struct inode *inode = state->inode;
6225 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006226 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006227 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006229 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006231 struct nfs_lockt_res res = {
6232 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 };
6234 struct rpc_message msg = {
6235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006236 .rpc_argp = &arg,
6237 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 .rpc_cred = state->owner->so_cred,
6239 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 struct nfs4_lock_state *lsp;
6241 int status;
6242
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006243 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006244 status = nfs4_set_lock_state(state, request);
6245 if (status != 0)
6246 goto out;
6247 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006248 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006249 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006250 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006251 switch (status) {
6252 case 0:
6253 request->fl_type = F_UNLCK;
6254 break;
6255 case -NFS4ERR_DENIED:
6256 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006258 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006259 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006260out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 return status;
6262}
6263
6264static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6265{
6266 struct nfs4_exception exception = { };
6267 int err;
6268
6269 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006270 err = _nfs4_proc_getlk(state, cmd, request);
6271 trace_nfs4_get_lock(request, state, cmd, err);
6272 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 &exception);
6274 } while (exception.retry);
6275 return err;
6276}
6277
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006278struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006279 struct nfs_locku_args arg;
6280 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006281 struct nfs4_lock_state *lsp;
6282 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006283 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006284 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006285 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006286 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006287};
6288
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006289static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6290 struct nfs_open_context *ctx,
6291 struct nfs4_lock_state *lsp,
6292 struct nfs_seqid *seqid)
6293{
6294 struct nfs4_unlockdata *p;
6295 struct inode *inode = lsp->ls_state->inode;
6296
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006297 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006298 if (p == NULL)
6299 return NULL;
6300 p->arg.fh = NFS_FH(inode);
6301 p->arg.fl = &p->fl;
6302 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006303 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006304 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006305 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006306 /* Ensure we don't close file until we're done freeing locks! */
6307 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006308 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006309 memcpy(&p->fl, fl, sizeof(p->fl));
6310 p->server = NFS_SERVER(inode);
6311 return p;
6312}
6313
Trond Myklebust06f814a2006-01-03 09:55:07 +01006314static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006315{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006316 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006317 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006318 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006319 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006320 put_nfs_open_context(calldata->ctx);
6321 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006322}
6323
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006324static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006325{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006326 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006327 struct nfs4_exception exception = {
6328 .inode = calldata->lsp->ls_state->inode,
6329 .stateid = &calldata->arg.stateid,
6330 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006331
Trond Myklebust14516c32010-07-31 14:29:06 -04006332 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6333 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006334 switch (task->tk_status) {
6335 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006336 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006337 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006338 if (nfs4_update_lock_stateid(calldata->lsp,
6339 &calldata->res.stateid))
6340 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006341 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006342 case -NFS4ERR_ADMIN_REVOKED:
6343 case -NFS4ERR_EXPIRED:
6344 nfs4_free_revoked_stateid(calldata->server,
6345 &calldata->arg.stateid,
6346 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006347 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006348 case -NFS4ERR_BAD_STATEID:
6349 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006350 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006351 if (!nfs4_stateid_match(&calldata->arg.stateid,
6352 &calldata->lsp->ls_stateid))
6353 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006354 break;
6355 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006356 task->tk_status = nfs4_async_handle_exception(task,
6357 calldata->server, task->tk_status,
6358 &exception);
6359 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006360 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006361 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006362 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006363}
6364
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006365static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006366{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006367 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006369 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6370 nfs_async_iocounter_wait(task, calldata->l_ctx))
6371 return;
6372
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006373 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006374 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006375 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006376 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006377 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006378 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006379 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006380 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006381 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006382 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006383 &calldata->res.seq_res,
6384 task) != 0)
6385 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006386 return;
6387out_no_action:
6388 task->tk_action = NULL;
6389out_wait:
6390 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391}
6392
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006393static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006394 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006395 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006396 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006397};
6398
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006399static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6400 struct nfs_open_context *ctx,
6401 struct nfs4_lock_state *lsp,
6402 struct nfs_seqid *seqid)
6403{
6404 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006405 struct rpc_message msg = {
6406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6407 .rpc_cred = ctx->cred,
6408 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006409 struct rpc_task_setup task_setup_data = {
6410 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006411 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006412 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006413 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006414 .flags = RPC_TASK_ASYNC,
6415 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006416
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006417 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6418 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6419
Frank Filz137d6ac2007-07-09 15:32:29 -07006420 /* Ensure this is an unlock - when canceling a lock, the
6421 * canceled lock is passed in, and it won't be an unlock.
6422 */
6423 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006424 if (fl->fl_flags & FL_CLOSE)
6425 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006426
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006427 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6428 if (data == NULL) {
6429 nfs_free_seqid(seqid);
6430 return ERR_PTR(-ENOMEM);
6431 }
6432
Anna Schumakerfba83f32018-05-04 16:22:50 -04006433 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006434 msg.rpc_argp = &data->arg;
6435 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006436 task_setup_data.callback_data = data;
6437 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006438}
6439
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6441{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006442 struct inode *inode = state->inode;
6443 struct nfs4_state_owner *sp = state->owner;
6444 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006445 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006446 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006447 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006448 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006449 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006450 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451
Trond Myklebust9b073572006-06-29 16:38:34 -04006452 status = nfs4_set_lock_state(state, request);
6453 /* Unlock _before_ we do the RPC call */
6454 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006455 /* Exclude nfs_delegation_claim_locks() */
6456 mutex_lock(&sp->so_delegreturn_mutex);
6457 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006458 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006459 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006460 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006461 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006462 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006463 }
6464 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006465 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006466 if (status != 0)
6467 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006468 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006469 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006470 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6471 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006472 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6473 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006474 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006475 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006476 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006477 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006478 status = PTR_ERR(task);
6479 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006480 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006481 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006482 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006483out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006484 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006485 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006486 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487}
6488
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006489struct nfs4_lockdata {
6490 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006491 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006492 struct nfs4_lock_state *lsp;
6493 struct nfs_open_context *ctx;
6494 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006495 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006496 int rpc_status;
6497 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006498 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006499};
6500
6501static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006502 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6503 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006504{
6505 struct nfs4_lockdata *p;
6506 struct inode *inode = lsp->ls_state->inode;
6507 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006508 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006509
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006510 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006511 if (p == NULL)
6512 return NULL;
6513
6514 p->arg.fh = NFS_FH(inode);
6515 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006516 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006517 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006518 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006519 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6520 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006521 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006522 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006523 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006524 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006525 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006526 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006527 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006528 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006529 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006530 p->ctx = get_nfs_open_context(ctx);
6531 memcpy(&p->fl, fl, sizeof(p->fl));
6532 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006533out_free_seqid:
6534 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006535out_free:
6536 kfree(p);
6537 return NULL;
6538}
6539
6540static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6541{
6542 struct nfs4_lockdata *data = calldata;
6543 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544
Harvey Harrison3110ff82008-05-02 13:42:44 -07006545 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006546 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006547 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006548 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006549 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006550 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006551 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006552 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006553 nfs4_stateid_copy(&data->arg.open_stateid,
6554 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006555 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006556 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006557 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006558 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006559 nfs4_stateid_copy(&data->arg.lock_stateid,
6560 &data->lsp->ls_stateid);
6561 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006562 if (!nfs4_valid_open_stateid(state)) {
6563 data->rpc_status = -EBADF;
6564 task->tk_action = NULL;
6565 goto out_release_open_seqid;
6566 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006567 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006568 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006569 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006570 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006571 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006572 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006573out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006574 nfs_release_seqid(data->arg.open_seqid);
6575out_release_lock_seqid:
6576 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006577out_wait:
6578 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006579 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006580}
6581
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006582static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6583{
6584 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006585 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006586
Harvey Harrison3110ff82008-05-02 13:42:44 -07006587 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006588
Trond Myklebust14516c32010-07-31 14:29:06 -04006589 if (!nfs4_sequence_done(task, &data->res.seq_res))
6590 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006591
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006592 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006593 switch (task->tk_status) {
6594 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006595 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006596 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006597 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006598 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006599 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006600 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006601 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006602 if (data->arg.new_lock_owner != 0) {
6603 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6604 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6605 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006606 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6607 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006608 break;
6609 case -NFS4ERR_BAD_STATEID:
6610 case -NFS4ERR_OLD_STATEID:
6611 case -NFS4ERR_STALE_STATEID:
6612 case -NFS4ERR_EXPIRED:
6613 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006614 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006615 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006616 goto out_restart;
6617 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006618 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006619 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006620 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006621out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006622 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006623 return;
6624out_restart:
6625 if (!data->cancelled)
6626 rpc_restart_call_prepare(task);
6627 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006628}
6629
6630static void nfs4_lock_release(void *calldata)
6631{
6632 struct nfs4_lockdata *data = calldata;
6633
Harvey Harrison3110ff82008-05-02 13:42:44 -07006634 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006635 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006636 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006637 struct rpc_task *task;
6638 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6639 data->arg.lock_seqid);
6640 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006641 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006642 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006643 } else
6644 nfs_free_seqid(data->arg.lock_seqid);
6645 nfs4_put_lock_state(data->lsp);
6646 put_nfs_open_context(data->ctx);
6647 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006648 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006649}
6650
6651static const struct rpc_call_ops nfs4_lock_ops = {
6652 .rpc_call_prepare = nfs4_lock_prepare,
6653 .rpc_call_done = nfs4_lock_done,
6654 .rpc_release = nfs4_lock_release,
6655};
6656
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006657static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6658{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006659 switch (error) {
6660 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006661 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006662 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006663 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006664 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006665 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006666 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006667 break;
6668 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006669 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006670 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006671 };
6672}
6673
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006674static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006675{
6676 struct nfs4_lockdata *data;
6677 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006678 struct rpc_message msg = {
6679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6680 .rpc_cred = state->owner->so_cred,
6681 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006682 struct rpc_task_setup task_setup_data = {
6683 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006684 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006685 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006686 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006687 .flags = RPC_TASK_ASYNC,
6688 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006689 int ret;
6690
Harvey Harrison3110ff82008-05-02 13:42:44 -07006691 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006692 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006693 fl->fl_u.nfs4_fl.owner,
6694 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006695 if (data == NULL)
6696 return -ENOMEM;
6697 if (IS_SETLKW(cmd))
6698 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006699 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6700 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006701 msg.rpc_argp = &data->arg;
6702 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006703 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006704 if (recovery_type > NFS_LOCK_NEW) {
6705 if (recovery_type == NFS_LOCK_RECLAIM)
6706 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006707 } else
6708 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006709 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006710 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006711 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006712 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006713 if (ret == 0) {
6714 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006715 if (ret)
6716 nfs4_handle_setlk_error(data->server, data->lsp,
6717 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006718 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006719 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006720 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006721 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006722 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724}
6725
6726static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6727{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006728 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006729 struct nfs4_exception exception = {
6730 .inode = state->inode,
6731 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006732 int err;
6733
6734 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006735 /* Cache the lock if possible... */
6736 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6737 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006738 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006739 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006740 break;
6741 nfs4_handle_exception(server, err, &exception);
6742 } while (exception.retry);
6743 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744}
6745
6746static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6747{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006748 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006749 struct nfs4_exception exception = {
6750 .inode = state->inode,
6751 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006752 int err;
6753
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006754 err = nfs4_set_lock_state(state, request);
6755 if (err != 0)
6756 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006757 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006758 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6759 return 0;
6760 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006761 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006762 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6763 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006764 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006765 switch (err) {
6766 default:
6767 goto out;
6768 case -NFS4ERR_GRACE:
6769 case -NFS4ERR_DELAY:
6770 nfs4_handle_exception(server, err, &exception);
6771 err = 0;
6772 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006773 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006774out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776}
6777
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006778#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006779static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6780{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006781 struct nfs4_lock_state *lsp;
6782 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006783
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006784 status = nfs4_set_lock_state(state, request);
6785 if (status != 0)
6786 return status;
6787 lsp = request->fl_u.nfs4_fl.owner;
6788 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6789 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6790 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006791 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006792}
6793#endif
6794
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6796{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006797 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006798 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006799 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006800 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801
Trond Myklebust01c3b862006-06-29 16:38:39 -04006802 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006803 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006804 if (status < 0)
6805 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006806 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006807 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006808 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006809 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006810 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006811 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006812 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006813 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006814 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006815 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006816 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006817 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006818 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006819 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006820out:
6821 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 return status;
6823}
6824
6825static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6826{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006827 struct nfs4_exception exception = {
6828 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006829 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006830 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 int err;
6832
6833 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006834 err = _nfs4_proc_setlk(state, cmd, request);
6835 if (err == -NFS4ERR_DENIED)
6836 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006838 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 } while (exception.retry);
6840 return err;
6841}
6842
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006843#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6844#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6845
6846static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006847nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6848 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006849{
6850 int status = -ERESTARTSYS;
6851 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6852
6853 while(!signalled()) {
6854 status = nfs4_proc_setlk(state, cmd, request);
6855 if ((status != -EAGAIN) || IS_SETLK(cmd))
6856 break;
6857 freezable_schedule_timeout_interruptible(timeout);
6858 timeout *= 2;
6859 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6860 status = -ERESTARTSYS;
6861 }
6862 return status;
6863}
6864
Jeff Laytona1d617d82016-09-17 18:17:39 -04006865#ifdef CONFIG_NFS_V4_1
6866struct nfs4_lock_waiter {
6867 struct task_struct *task;
6868 struct inode *inode;
6869 struct nfs_lowner *owner;
6870 bool notified;
6871};
6872
6873static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006874nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006875{
6876 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006877 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006878
Jeff Layton57174592018-03-18 08:37:03 -04006879 /* NULL key means to wake up everyone */
6880 if (key) {
6881 struct cb_notify_lock_args *cbnl = key;
6882 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6883 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006884
Jeff Layton57174592018-03-18 08:37:03 -04006885 /* Only wake if the callback was for the same owner. */
6886 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6887 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006888
Jeff Layton57174592018-03-18 08:37:03 -04006889 /* Make sure it's for the right inode */
6890 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6891 return 0;
6892
6893 waiter->notified = true;
6894 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006895
6896 /* override "private" so we can use default_wake_function */
6897 wait->private = waiter->task;
6898 ret = autoremove_wake_function(wait, mode, flags, key);
6899 wait->private = waiter;
6900 return ret;
6901}
6902
6903static int
6904nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6905{
6906 int status = -ERESTARTSYS;
6907 unsigned long flags;
6908 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6909 struct nfs_server *server = NFS_SERVER(state->inode);
6910 struct nfs_client *clp = server->nfs_client;
6911 wait_queue_head_t *q = &clp->cl_lock_waitq;
6912 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6913 .id = lsp->ls_seqid.owner_id,
6914 .s_dev = server->s_dev };
6915 struct nfs4_lock_waiter waiter = { .task = current,
6916 .inode = state->inode,
6917 .owner = &owner,
6918 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006919 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006920
6921 /* Don't bother with waitqueue if we don't expect a callback */
6922 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6923 return nfs4_retry_setlk_simple(state, cmd, request);
6924
6925 init_wait(&wait);
6926 wait.private = &waiter;
6927 wait.func = nfs4_wake_lock_waiter;
6928 add_wait_queue(q, &wait);
6929
6930 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006931 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006932 status = nfs4_proc_setlk(state, cmd, request);
6933 if ((status != -EAGAIN) || IS_SETLK(cmd))
6934 break;
6935
6936 status = -ERESTARTSYS;
6937 spin_lock_irqsave(&q->lock, flags);
6938 if (waiter.notified) {
6939 spin_unlock_irqrestore(&q->lock, flags);
6940 continue;
6941 }
6942 set_current_state(TASK_INTERRUPTIBLE);
6943 spin_unlock_irqrestore(&q->lock, flags);
6944
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006945 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006946 }
6947
6948 finish_wait(q, &wait);
6949 return status;
6950}
6951#else /* !CONFIG_NFS_V4_1 */
6952static inline int
6953nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6954{
6955 return nfs4_retry_setlk_simple(state, cmd, request);
6956}
6957#endif
6958
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959static int
6960nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6961{
6962 struct nfs_open_context *ctx;
6963 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 int status;
6965
6966 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006967 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 state = ctx->state;
6969
Trond Myklebustd9531262009-07-21 19:22:38 -04006970 if (IS_GETLK(cmd)) {
6971 if (state != NULL)
6972 return nfs4_proc_getlk(state, F_GETLK, request);
6973 return 0;
6974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975
6976 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6977 return -EINVAL;
6978
Trond Myklebustd9531262009-07-21 19:22:38 -04006979 if (request->fl_type == F_UNLCK) {
6980 if (state != NULL)
6981 return nfs4_proc_unlck(state, cmd, request);
6982 return 0;
6983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984
Trond Myklebustd9531262009-07-21 19:22:38 -04006985 if (state == NULL)
6986 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006987
6988 if ((request->fl_flags & FL_POSIX) &&
6989 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6990 return -ENOLCK;
6991
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006992 /*
6993 * Don't rely on the VFS having checked the file open mode,
6994 * since it won't do this for flock() locks.
6995 */
6996 switch (request->fl_type) {
6997 case F_RDLCK:
6998 if (!(filp->f_mode & FMODE_READ))
6999 return -EBADF;
7000 break;
7001 case F_WRLCK:
7002 if (!(filp->f_mode & FMODE_WRITE))
7003 return -EBADF;
7004 }
7005
Jeff Layton1ea67db2016-09-17 18:17:37 -04007006 status = nfs4_set_lock_state(state, request);
7007 if (status != 0)
7008 return status;
7009
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007010 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011}
7012
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007013int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007014{
7015 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007016 int err;
7017
7018 err = nfs4_set_lock_state(state, fl);
7019 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007020 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007021 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007022 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007023}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007024
Trond Myklebustcf470c32012-03-07 13:49:12 -05007025struct nfs_release_lockowner_data {
7026 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007027 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007028 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007029 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007030 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007031};
7032
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007033static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7034{
7035 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007036 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007037 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7038 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007039 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007040 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007041}
7042
7043static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7044{
7045 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007046 struct nfs_server *server = data->server;
7047
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007048 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007049
7050 switch (task->tk_status) {
7051 case 0:
7052 renew_lease(server, data->timestamp);
7053 break;
7054 case -NFS4ERR_STALE_CLIENTID:
7055 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007056 nfs4_schedule_lease_recovery(server->nfs_client);
7057 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007058 case -NFS4ERR_LEASE_MOVED:
7059 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007060 if (nfs4_async_handle_error(task, server,
7061 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007062 rpc_restart_call_prepare(task);
7063 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007064}
7065
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007066static void nfs4_release_lockowner_release(void *calldata)
7067{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007068 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007069 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007070 kfree(calldata);
7071}
7072
Trond Myklebust17280172012-03-11 13:11:00 -04007073static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007074 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7075 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007076 .rpc_release = nfs4_release_lockowner_release,
7077};
7078
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007079static void
7080nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007081{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007082 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007083 struct rpc_message msg = {
7084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7085 };
7086
7087 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007088 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007089
Trond Myklebustcf470c32012-03-07 13:49:12 -05007090 data = kmalloc(sizeof(*data), GFP_NOFS);
7091 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007092 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007093 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007094 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007095 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7096 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7097 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007098
Trond Myklebustcf470c32012-03-07 13:49:12 -05007099 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007100 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007101 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007102 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007103}
7104
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007105#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7106
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007107static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007108 struct dentry *unused, struct inode *inode,
7109 const char *key, const void *buf,
7110 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007111{
Al Viro59301222016-05-27 10:19:30 -04007112 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007113}
7114
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007115static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007116 struct dentry *unused, struct inode *inode,
7117 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007118{
Al Virob2968212016-04-10 20:48:24 -04007119 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007120}
7121
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007122static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007123{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007124 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007125}
7126
David Quigleyc9bccef2013-05-22 12:50:45 -04007127#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007128
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007129static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007130 struct dentry *unused, struct inode *inode,
7131 const char *key, const void *buf,
7132 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007133{
7134 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007135 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007136
7137 return -EOPNOTSUPP;
7138}
7139
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007140static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007141 struct dentry *unused, struct inode *inode,
7142 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007143{
7144 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007145 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007146 return -EOPNOTSUPP;
7147}
7148
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007149static ssize_t
7150nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007151{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007152 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007153
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007154 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7155 len = security_inode_listsecurity(inode, list, list_len);
7156 if (list_len && len > list_len)
7157 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007158 }
7159 return len;
7160}
7161
7162static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7163 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007164 .get = nfs4_xattr_get_nfs4_label,
7165 .set = nfs4_xattr_set_nfs4_label,
7166};
David Quigleyc9bccef2013-05-22 12:50:45 -04007167
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007168#else
7169
7170static ssize_t
7171nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7172{
7173 return 0;
7174}
7175
7176#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007177
Andy Adamson533eb462011-06-13 18:25:56 -04007178/*
7179 * nfs_fhget will use either the mounted_on_fileid or the fileid
7180 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007181static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7182{
Andy Adamson533eb462011-06-13 18:25:56 -04007183 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7184 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7185 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007186 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007187 return;
7188
7189 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007190 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007191 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7192 fattr->nlink = 2;
7193}
7194
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007195static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7196 const struct qstr *name,
7197 struct nfs4_fs_locations *fs_locations,
7198 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007199{
7200 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007201 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007202 struct nfs4_fs_locations_arg args = {
7203 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007204 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007205 .page = page,
7206 .bitmask = bitmask,
7207 };
Benny Halevy22958462009-04-01 09:22:02 -04007208 struct nfs4_fs_locations_res res = {
7209 .fs_locations = fs_locations,
7210 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007211 struct rpc_message msg = {
7212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7213 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007214 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007215 };
7216 int status;
7217
Harvey Harrison3110ff82008-05-02 13:42:44 -07007218 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007219
Chuck Leverc05cefc2017-11-05 15:45:22 -05007220 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7221 bitmask[1] = nfs4_fattr_bitmap[1];
7222
Andy Adamson533eb462011-06-13 18:25:56 -04007223 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7224 * is not supported */
7225 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007226 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007227 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007228 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007229
Trond Myklebustc228fd32007-01-13 02:28:11 -05007230 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007231 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007232 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007233 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007234 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007235 return status;
7236}
7237
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007238int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7239 const struct qstr *name,
7240 struct nfs4_fs_locations *fs_locations,
7241 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007242{
7243 struct nfs4_exception exception = { };
7244 int err;
7245 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007246 err = _nfs4_proc_fs_locations(client, dir, name,
7247 fs_locations, page);
7248 trace_nfs4_get_fs_locations(dir, name, err);
7249 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007250 &exception);
7251 } while (exception.retry);
7252 return err;
7253}
7254
Chuck Leverb03d7352013-10-17 14:12:50 -04007255/*
7256 * This operation also signals the server that this client is
7257 * performing migration recovery. The server can stop returning
7258 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7259 * appended to this compound to identify the client ID which is
7260 * performing recovery.
7261 */
7262static int _nfs40_proc_get_locations(struct inode *inode,
7263 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007264 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007265{
7266 struct nfs_server *server = NFS_SERVER(inode);
7267 struct rpc_clnt *clnt = server->client;
7268 u32 bitmask[2] = {
7269 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7270 };
7271 struct nfs4_fs_locations_arg args = {
7272 .clientid = server->nfs_client->cl_clientid,
7273 .fh = NFS_FH(inode),
7274 .page = page,
7275 .bitmask = bitmask,
7276 .migration = 1, /* skip LOOKUP */
7277 .renew = 1, /* append RENEW */
7278 };
7279 struct nfs4_fs_locations_res res = {
7280 .fs_locations = locations,
7281 .migration = 1,
7282 .renew = 1,
7283 };
7284 struct rpc_message msg = {
7285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7286 .rpc_argp = &args,
7287 .rpc_resp = &res,
7288 .rpc_cred = cred,
7289 };
7290 unsigned long now = jiffies;
7291 int status;
7292
7293 nfs_fattr_init(&locations->fattr);
7294 locations->server = server;
7295 locations->nlocations = 0;
7296
Anna Schumakerfba83f32018-05-04 16:22:50 -04007297 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007298 status = nfs4_call_sync_sequence(clnt, server, &msg,
7299 &args.seq_args, &res.seq_res);
7300 if (status)
7301 return status;
7302
7303 renew_lease(server, now);
7304 return 0;
7305}
7306
7307#ifdef CONFIG_NFS_V4_1
7308
7309/*
7310 * This operation also signals the server that this client is
7311 * performing migration recovery. The server can stop asserting
7312 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7313 * performing this operation is identified in the SEQUENCE
7314 * operation in this compound.
7315 *
7316 * When the client supports GETATTR(fs_locations_info), it can
7317 * be plumbed in here.
7318 */
7319static int _nfs41_proc_get_locations(struct inode *inode,
7320 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007321 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007322{
7323 struct nfs_server *server = NFS_SERVER(inode);
7324 struct rpc_clnt *clnt = server->client;
7325 u32 bitmask[2] = {
7326 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7327 };
7328 struct nfs4_fs_locations_arg args = {
7329 .fh = NFS_FH(inode),
7330 .page = page,
7331 .bitmask = bitmask,
7332 .migration = 1, /* skip LOOKUP */
7333 };
7334 struct nfs4_fs_locations_res res = {
7335 .fs_locations = locations,
7336 .migration = 1,
7337 };
7338 struct rpc_message msg = {
7339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7340 .rpc_argp = &args,
7341 .rpc_resp = &res,
7342 .rpc_cred = cred,
7343 };
7344 int status;
7345
7346 nfs_fattr_init(&locations->fattr);
7347 locations->server = server;
7348 locations->nlocations = 0;
7349
Anna Schumakerfba83f32018-05-04 16:22:50 -04007350 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007351 status = nfs4_call_sync_sequence(clnt, server, &msg,
7352 &args.seq_args, &res.seq_res);
7353 if (status == NFS4_OK &&
7354 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7355 status = -NFS4ERR_LEASE_MOVED;
7356 return status;
7357}
7358
7359#endif /* CONFIG_NFS_V4_1 */
7360
7361/**
7362 * nfs4_proc_get_locations - discover locations for a migrated FSID
7363 * @inode: inode on FSID that is migrating
7364 * @locations: result of query
7365 * @page: buffer
7366 * @cred: credential to use for this operation
7367 *
7368 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7369 * operation failed, or a negative errno if a local error occurred.
7370 *
7371 * On success, "locations" is filled in, but if the server has
7372 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7373 * asserted.
7374 *
7375 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7376 * from this client that require migration recovery.
7377 */
7378int nfs4_proc_get_locations(struct inode *inode,
7379 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007380 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007381{
7382 struct nfs_server *server = NFS_SERVER(inode);
7383 struct nfs_client *clp = server->nfs_client;
7384 const struct nfs4_mig_recovery_ops *ops =
7385 clp->cl_mvops->mig_recovery_ops;
7386 struct nfs4_exception exception = { };
7387 int status;
7388
7389 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7390 (unsigned long long)server->fsid.major,
7391 (unsigned long long)server->fsid.minor,
7392 clp->cl_hostname);
7393 nfs_display_fhandle(NFS_FH(inode), __func__);
7394
7395 do {
7396 status = ops->get_locations(inode, locations, page, cred);
7397 if (status != -NFS4ERR_DELAY)
7398 break;
7399 nfs4_handle_exception(server, status, &exception);
7400 } while (exception.retry);
7401 return status;
7402}
7403
Chuck Lever44c99932013-10-17 14:13:30 -04007404/*
7405 * This operation also signals the server that this client is
7406 * performing "lease moved" recovery. The server can stop
7407 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7408 * is appended to this compound to identify the client ID which is
7409 * performing recovery.
7410 */
NeilBrowna52458b2018-12-03 11:30:31 +11007411static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007412{
7413 struct nfs_server *server = NFS_SERVER(inode);
7414 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7415 struct rpc_clnt *clnt = server->client;
7416 struct nfs4_fsid_present_arg args = {
7417 .fh = NFS_FH(inode),
7418 .clientid = clp->cl_clientid,
7419 .renew = 1, /* append RENEW */
7420 };
7421 struct nfs4_fsid_present_res res = {
7422 .renew = 1,
7423 };
7424 struct rpc_message msg = {
7425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7426 .rpc_argp = &args,
7427 .rpc_resp = &res,
7428 .rpc_cred = cred,
7429 };
7430 unsigned long now = jiffies;
7431 int status;
7432
7433 res.fh = nfs_alloc_fhandle();
7434 if (res.fh == NULL)
7435 return -ENOMEM;
7436
Anna Schumakerfba83f32018-05-04 16:22:50 -04007437 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007438 status = nfs4_call_sync_sequence(clnt, server, &msg,
7439 &args.seq_args, &res.seq_res);
7440 nfs_free_fhandle(res.fh);
7441 if (status)
7442 return status;
7443
7444 do_renew_lease(clp, now);
7445 return 0;
7446}
7447
7448#ifdef CONFIG_NFS_V4_1
7449
7450/*
7451 * This operation also signals the server that this client is
7452 * performing "lease moved" recovery. The server can stop asserting
7453 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7454 * this operation is identified in the SEQUENCE operation in this
7455 * compound.
7456 */
NeilBrowna52458b2018-12-03 11:30:31 +11007457static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007458{
7459 struct nfs_server *server = NFS_SERVER(inode);
7460 struct rpc_clnt *clnt = server->client;
7461 struct nfs4_fsid_present_arg args = {
7462 .fh = NFS_FH(inode),
7463 };
7464 struct nfs4_fsid_present_res res = {
7465 };
7466 struct rpc_message msg = {
7467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7468 .rpc_argp = &args,
7469 .rpc_resp = &res,
7470 .rpc_cred = cred,
7471 };
7472 int status;
7473
7474 res.fh = nfs_alloc_fhandle();
7475 if (res.fh == NULL)
7476 return -ENOMEM;
7477
Anna Schumakerfba83f32018-05-04 16:22:50 -04007478 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007479 status = nfs4_call_sync_sequence(clnt, server, &msg,
7480 &args.seq_args, &res.seq_res);
7481 nfs_free_fhandle(res.fh);
7482 if (status == NFS4_OK &&
7483 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7484 status = -NFS4ERR_LEASE_MOVED;
7485 return status;
7486}
7487
7488#endif /* CONFIG_NFS_V4_1 */
7489
7490/**
7491 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7492 * @inode: inode on FSID to check
7493 * @cred: credential to use for this operation
7494 *
7495 * Server indicates whether the FSID is present, moved, or not
7496 * recognized. This operation is necessary to clear a LEASE_MOVED
7497 * condition for this client ID.
7498 *
7499 * Returns NFS4_OK if the FSID is present on this server,
7500 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7501 * NFS4ERR code if some error occurred on the server, or a
7502 * negative errno if a local failure occurred.
7503 */
NeilBrowna52458b2018-12-03 11:30:31 +11007504int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007505{
7506 struct nfs_server *server = NFS_SERVER(inode);
7507 struct nfs_client *clp = server->nfs_client;
7508 const struct nfs4_mig_recovery_ops *ops =
7509 clp->cl_mvops->mig_recovery_ops;
7510 struct nfs4_exception exception = { };
7511 int status;
7512
7513 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7514 (unsigned long long)server->fsid.major,
7515 (unsigned long long)server->fsid.minor,
7516 clp->cl_hostname);
7517 nfs_display_fhandle(NFS_FH(inode), __func__);
7518
7519 do {
7520 status = ops->fsid_present(inode, cred);
7521 if (status != -NFS4ERR_DELAY)
7522 break;
7523 nfs4_handle_exception(server, status, &exception);
7524 } while (exception.retry);
7525 return status;
7526}
7527
Andy Adamson5ec16a82013-08-08 10:57:55 -04007528/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007529 * If 'use_integrity' is true and the state managment nfs_client
7530 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7531 * and the machine credential as per RFC3530bis and RFC5661 Security
7532 * Considerations sections. Otherwise, just use the user cred with the
7533 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007534 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007535static 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 +00007536{
7537 int status;
7538 struct nfs4_secinfo_arg args = {
7539 .dir_fh = NFS_FH(dir),
7540 .name = name,
7541 };
7542 struct nfs4_secinfo_res res = {
7543 .flavors = flavors,
7544 };
7545 struct rpc_message msg = {
7546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7547 .rpc_argp = &args,
7548 .rpc_resp = &res,
7549 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007550 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
NeilBrowna52458b2018-12-03 11:30:31 +11007551 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007552
7553 if (use_integrity) {
7554 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007555 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7556 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007557 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007558
7559 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007560
7561 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7562 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7563
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007564 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7565 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007566 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007567
NeilBrowna52458b2018-12-03 11:30:31 +11007568 put_cred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007569
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007570 return status;
7571}
7572
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007573int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7574 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007575{
7576 struct nfs4_exception exception = { };
7577 int err;
7578 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007579 err = -NFS4ERR_WRONGSEC;
7580
7581 /* try to use integrity protection with machine cred */
7582 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7583 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7584
7585 /*
7586 * if unable to use integrity protection, or SECINFO with
7587 * integrity protection returns NFS4ERR_WRONGSEC (which is
7588 * disallowed by spec, but exists in deployed servers) use
7589 * the current filesystem's rpc_client and the user cred.
7590 */
7591 if (err == -NFS4ERR_WRONGSEC)
7592 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7593
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007594 trace_nfs4_secinfo(dir, name, err);
7595 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007596 &exception);
7597 } while (exception.retry);
7598 return err;
7599}
7600
Andy Adamson557134a2009-04-01 09:21:53 -04007601#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007602/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007603 * Check the exchange flags returned by the server for invalid flags, having
7604 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7605 * DS flags set.
7606 */
7607static int nfs4_check_cl_exchange_flags(u32 flags)
7608{
7609 if (flags & ~EXCHGID4_FLAG_MASK_R)
7610 goto out_inval;
7611 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7612 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7613 goto out_inval;
7614 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7615 goto out_inval;
7616 return NFS_OK;
7617out_inval:
7618 return -NFS4ERR_INVAL;
7619}
7620
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007621static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007622nfs41_same_server_scope(struct nfs41_server_scope *a,
7623 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007624{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007625 if (a->server_scope_sz != b->server_scope_sz)
7626 return false;
7627 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007628}
7629
Andy Adamson02a95de2016-02-05 16:08:37 -05007630static void
7631nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7632{
7633}
7634
7635static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7636 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7637};
7638
Andy Adamson357f54d2010-12-14 10:11:57 -05007639/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007640 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007641 *
7642 * The 4.1 client currently uses the same TCP connection for the
7643 * fore and backchannel.
7644 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007645static
7646int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7647 struct rpc_xprt *xprt,
7648 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007649 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007650{
7651 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007652 struct nfs41_bind_conn_to_session_args args = {
7653 .client = clp,
7654 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7655 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007656 struct nfs41_bind_conn_to_session_res res;
7657 struct rpc_message msg = {
7658 .rpc_proc =
7659 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007660 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007661 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007662 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007663 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007664 struct rpc_task_setup task_setup_data = {
7665 .rpc_client = clnt,
7666 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007667 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007668 .rpc_message = &msg,
7669 .flags = RPC_TASK_TIMEOUT,
7670 };
7671 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007672
Trond Myklebust71a097c2015-02-18 09:27:18 -08007673 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7674 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7675 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007676
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007677 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7678 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7679 args.dir = NFS4_CDFC4_FORE;
7680
7681 task = rpc_run_task(&task_setup_data);
7682 if (!IS_ERR(task)) {
7683 status = task->tk_status;
7684 rpc_put_task(task);
7685 } else
7686 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007687 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007688 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007689 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007690 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7691 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007692 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007693 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007694 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007695 dprintk("NFS: %s: Unexpected direction from server\n",
7696 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007697 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007698 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007699 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007700 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7701 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007702 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007703 }
7704 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007705
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007706 return status;
7707}
7708
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007709struct rpc_bind_conn_calldata {
7710 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007711 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007712};
7713
7714static int
7715nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7716 struct rpc_xprt *xprt,
7717 void *calldata)
7718{
7719 struct rpc_bind_conn_calldata *p = calldata;
7720
7721 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7722}
7723
NeilBrowna52458b2018-12-03 11:30:31 +11007724int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007725{
7726 struct rpc_bind_conn_calldata data = {
7727 .clp = clp,
7728 .cred = cred,
7729 };
7730 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7731 nfs4_proc_bind_conn_to_session_callback, &data);
7732}
7733
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007734/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007735 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7736 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007737 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007738static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7739 .how = SP4_MACH_CRED,
7740 .enforce.u.words = {
7741 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7742 1 << (OP_EXCHANGE_ID - 32) |
7743 1 << (OP_CREATE_SESSION - 32) |
7744 1 << (OP_DESTROY_SESSION - 32) |
7745 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007746 },
7747 .allow.u.words = {
7748 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007749 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007750 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007751 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007752 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007753 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007754 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007755 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007756 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007757 1 << (OP_FREE_STATEID - 32) |
7758 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007759 }
7760};
7761
7762/*
7763 * Select the state protection mode for client `clp' given the server results
7764 * from exchange_id in `sp'.
7765 *
7766 * Returns 0 on success, negative errno otherwise.
7767 */
7768static int nfs4_sp4_select_mode(struct nfs_client *clp,
7769 struct nfs41_state_protection *sp)
7770{
7771 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7772 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7773 1 << (OP_EXCHANGE_ID - 32) |
7774 1 << (OP_CREATE_SESSION - 32) |
7775 1 << (OP_DESTROY_SESSION - 32) |
7776 1 << (OP_DESTROY_CLIENTID - 32)
7777 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007778 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007779 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007780 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007781
7782 if (sp->how == SP4_MACH_CRED) {
7783 /* Print state protect result */
7784 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7785 for (i = 0; i <= LAST_NFS4_OP; i++) {
7786 if (test_bit(i, sp->enforce.u.longs))
7787 dfprintk(MOUNT, " enforce op %d\n", i);
7788 if (test_bit(i, sp->allow.u.longs))
7789 dfprintk(MOUNT, " allow op %d\n", i);
7790 }
7791
7792 /* make sure nothing is on enforce list that isn't supported */
7793 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7794 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7795 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007796 ret = -EINVAL;
7797 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007798 }
7799 }
7800
7801 /*
7802 * Minimal mode - state operations are allowed to use machine
7803 * credential. Note this already happens by default, so the
7804 * client doesn't have to do anything more than the negotiation.
7805 *
7806 * NOTE: we don't care if EXCHANGE_ID is in the list -
7807 * we're already using the machine cred for exchange_id
7808 * and will never use a different cred.
7809 */
7810 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7811 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7812 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7813 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7814 dfprintk(MOUNT, "sp4_mach_cred:\n");
7815 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007816 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007817 } else {
7818 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007819 ret = -EINVAL;
7820 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007821 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007822
7823 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007824 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7825 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007826 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7827 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007828 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007829 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007830
Andrew Elble99ade3c2015-12-02 09:39:51 -05007831 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7832 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007833 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007834 }
7835
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007836 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7837 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7838 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007839 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007840 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007841
7842 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7843 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7844 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007845 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007846 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007847
7848 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7849 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007850 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007851 }
7852
7853 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7854 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007855 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007856 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007857 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007858out:
7859 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00007860 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007861}
7862
Andy Adamson8d89bd72016-09-09 09:22:18 -04007863struct nfs41_exchange_id_data {
7864 struct nfs41_exchange_id_res res;
7865 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007866};
7867
Andy Adamson8d89bd72016-09-09 09:22:18 -04007868static void nfs4_exchange_id_release(void *data)
7869{
7870 struct nfs41_exchange_id_data *cdata =
7871 (struct nfs41_exchange_id_data *)data;
7872
Olga Kornievskaia63513232017-03-13 10:36:19 -04007873 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007874 kfree(cdata->res.impl_id);
7875 kfree(cdata->res.server_scope);
7876 kfree(cdata->res.server_owner);
7877 kfree(cdata);
7878}
7879
7880static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007881 .rpc_release = nfs4_exchange_id_release,
7882};
7883
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007884/*
7885 * _nfs4_proc_exchange_id()
7886 *
7887 * Wrapper for EXCHANGE_ID operation.
7888 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007889static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11007890nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007891 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007892{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007893 struct rpc_message msg = {
7894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007895 .rpc_cred = cred,
7896 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007897 struct rpc_task_setup task_setup_data = {
7898 .rpc_client = clp->cl_rpcclient,
7899 .callback_ops = &nfs4_exchange_id_call_ops,
7900 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007901 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007902 };
7903 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007904 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007905
Elena Reshetova212bf412017-10-20 12:53:38 +03007906 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007907 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007908
Trond Myklebust9c760d12017-07-31 18:38:50 -04007909 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007910 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007911 if (!calldata)
7912 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007913
Trond Myklebustfd405592017-08-01 16:02:47 -04007914 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007915
7916 status = nfs4_init_uniform_client_string(clp);
7917 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007918 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007919
Andy Adamson8d89bd72016-09-09 09:22:18 -04007920 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7921 GFP_NOFS);
7922 status = -ENOMEM;
7923 if (unlikely(calldata->res.server_owner == NULL))
7924 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007925
Andy Adamson8d89bd72016-09-09 09:22:18 -04007926 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007927 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007928 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007929 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007930
Andy Adamson8d89bd72016-09-09 09:22:18 -04007931 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7932 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007933 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007934
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007935 switch (sp4_how) {
7936 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007937 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007938 break;
7939
7940 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007941 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007942 break;
7943
7944 default:
7945 /* unsupported! */
7946 WARN_ON_ONCE(1);
7947 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007948 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007949 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007950 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007951 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007952 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007953 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7954 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007955 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007956 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007957 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7958 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007959#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007960 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007961#endif
7962 msg.rpc_argp = &calldata->args;
7963 msg.rpc_resp = &calldata->res;
7964 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007965
Trond Myklebust9c760d12017-07-31 18:38:50 -04007966 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007967
7968out_impl_id:
7969 kfree(calldata->res.impl_id);
7970out_server_scope:
7971 kfree(calldata->res.server_scope);
7972out_server_owner:
7973 kfree(calldata->res.server_owner);
7974out_calldata:
7975 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007976out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007977 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007978 return ERR_PTR(status);
7979}
7980
7981/*
7982 * _nfs4_proc_exchange_id()
7983 *
7984 * Wrapper for EXCHANGE_ID operation.
7985 */
NeilBrowna52458b2018-12-03 11:30:31 +11007986static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04007987 u32 sp4_how)
7988{
7989 struct rpc_task *task;
7990 struct nfs41_exchange_id_args *argp;
7991 struct nfs41_exchange_id_res *resp;
7992 int status;
7993
7994 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7995 if (IS_ERR(task))
7996 return PTR_ERR(task);
7997
7998 argp = task->tk_msg.rpc_argp;
7999 resp = task->tk_msg.rpc_resp;
8000 status = task->tk_status;
8001 if (status != 0)
8002 goto out;
8003
8004 status = nfs4_check_cl_exchange_flags(resp->flags);
8005 if (status != 0)
8006 goto out;
8007
8008 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8009 if (status != 0)
8010 goto out;
8011
8012 clp->cl_clientid = resp->clientid;
8013 clp->cl_exchange_flags = resp->flags;
8014 clp->cl_seqid = resp->seqid;
8015 /* Client ID is not confirmed */
8016 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8017 clear_bit(NFS4_SESSION_ESTABLISHED,
8018 &clp->cl_session->session_state);
8019
8020 if (clp->cl_serverscope != NULL &&
8021 !nfs41_same_server_scope(clp->cl_serverscope,
8022 resp->server_scope)) {
8023 dprintk("%s: server_scope mismatch detected\n",
8024 __func__);
8025 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8026 }
8027
8028 swap(clp->cl_serverowner, resp->server_owner);
8029 swap(clp->cl_serverscope, resp->server_scope);
8030 swap(clp->cl_implid, resp->impl_id);
8031
8032 /* Save the EXCHANGE_ID verifier session trunk tests */
8033 memcpy(clp->cl_confirm.data, argp->verifier.data,
8034 sizeof(clp->cl_confirm.data));
8035out:
8036 trace_nfs4_exchange_id(clp, status);
8037 rpc_put_task(task);
8038 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008039}
8040
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008041/*
8042 * nfs4_proc_exchange_id()
8043 *
8044 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8045 *
8046 * Since the clientid has expired, all compounds using sessions
8047 * associated with the stale clientid will be returning
8048 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8049 * be in some phase of session reset.
8050 *
8051 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8052 */
NeilBrowna52458b2018-12-03 11:30:31 +11008053int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008054{
8055 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8056 int status;
8057
8058 /* try SP4_MACH_CRED if krb5i/p */
8059 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8060 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008061 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008062 if (!status)
8063 return 0;
8064 }
8065
8066 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008067 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008068}
8069
Andy Adamson04fa2c62016-09-09 09:22:29 -04008070/**
8071 * nfs4_test_session_trunk
8072 *
8073 * This is an add_xprt_test() test function called from
8074 * rpc_clnt_setup_test_and_add_xprt.
8075 *
8076 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8077 * and is dereferrenced in nfs4_exchange_id_release
8078 *
8079 * Upon success, add the new transport to the rpc_clnt
8080 *
8081 * @clnt: struct rpc_clnt to get new transport
8082 * @xprt: the rpc_xprt to test
8083 * @data: call data for _nfs4_proc_exchange_id.
8084 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308085void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008086 void *data)
8087{
8088 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008089 struct rpc_task *task;
8090 int status;
8091
Andy Adamson04fa2c62016-09-09 09:22:29 -04008092 u32 sp4_how;
8093
8094 dprintk("--> %s try %s\n", __func__,
8095 xprt->address_strings[RPC_DISPLAY_ADDR]);
8096
8097 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8098
8099 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008100 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8101 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308102 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008103
8104 status = task->tk_status;
8105 if (status == 0)
8106 status = nfs4_detect_session_trunking(adata->clp,
8107 task->tk_msg.rpc_resp, xprt);
8108
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308109 if (status == 0)
8110 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8111
Trond Myklebust9c760d12017-07-31 18:38:50 -04008112 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008113}
8114EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8115
Trond Myklebust66245532012-05-25 17:18:09 -04008116static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008117 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008118{
8119 struct rpc_message msg = {
8120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8121 .rpc_argp = clp,
8122 .rpc_cred = cred,
8123 };
8124 int status;
8125
8126 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008127 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008128 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008129 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008130 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8131 return status;
8132}
8133
8134static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008135 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008136{
8137 unsigned int loop;
8138 int ret;
8139
8140 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8141 ret = _nfs4_proc_destroy_clientid(clp, cred);
8142 switch (ret) {
8143 case -NFS4ERR_DELAY:
8144 case -NFS4ERR_CLIENTID_BUSY:
8145 ssleep(1);
8146 break;
8147 default:
8148 return ret;
8149 }
8150 }
8151 return 0;
8152}
8153
8154int nfs4_destroy_clientid(struct nfs_client *clp)
8155{
NeilBrowna52458b2018-12-03 11:30:31 +11008156 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008157 int ret = 0;
8158
8159 if (clp->cl_mvops->minor_version < 1)
8160 goto out;
8161 if (clp->cl_exchange_flags == 0)
8162 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008163 if (clp->cl_preserve_clid)
8164 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008165 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008166 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008167 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008168 switch (ret) {
8169 case 0:
8170 case -NFS4ERR_STALE_CLIENTID:
8171 clp->cl_exchange_flags = 0;
8172 }
8173out:
8174 return ret;
8175}
8176
Andy Adamson2050f0c2009-04-01 09:22:30 -04008177struct nfs4_get_lease_time_data {
8178 struct nfs4_get_lease_time_args *args;
8179 struct nfs4_get_lease_time_res *res;
8180 struct nfs_client *clp;
8181};
8182
8183static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8184 void *calldata)
8185{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008186 struct nfs4_get_lease_time_data *data =
8187 (struct nfs4_get_lease_time_data *)calldata;
8188
8189 dprintk("--> %s\n", __func__);
8190 /* just setup sequence, do not trigger session recovery
8191 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008192 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008193 &data->args->la_seq_args,
8194 &data->res->lr_seq_res,
8195 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008196 dprintk("<-- %s\n", __func__);
8197}
8198
8199/*
8200 * Called from nfs4_state_manager thread for session setup, so don't recover
8201 * from sequence operation or clientid errors.
8202 */
8203static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8204{
8205 struct nfs4_get_lease_time_data *data =
8206 (struct nfs4_get_lease_time_data *)calldata;
8207
8208 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008209 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8210 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008211 switch (task->tk_status) {
8212 case -NFS4ERR_DELAY:
8213 case -NFS4ERR_GRACE:
8214 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8215 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8216 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008217 /* fall through */
8218 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008219 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008220 return;
8221 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008222 dprintk("<-- %s\n", __func__);
8223}
8224
Trond Myklebust17280172012-03-11 13:11:00 -04008225static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008226 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8227 .rpc_call_done = nfs4_get_lease_time_done,
8228};
8229
8230int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8231{
8232 struct rpc_task *task;
8233 struct nfs4_get_lease_time_args args;
8234 struct nfs4_get_lease_time_res res = {
8235 .lr_fsinfo = fsinfo,
8236 };
8237 struct nfs4_get_lease_time_data data = {
8238 .args = &args,
8239 .res = &res,
8240 .clp = clp,
8241 };
8242 struct rpc_message msg = {
8243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8244 .rpc_argp = &args,
8245 .rpc_resp = &res,
8246 };
8247 struct rpc_task_setup task_setup = {
8248 .rpc_client = clp->cl_rpcclient,
8249 .rpc_message = &msg,
8250 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008251 .callback_data = &data,
8252 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008253 };
8254 int status;
8255
Anna Schumakerfba83f32018-05-04 16:22:50 -04008256 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008257 task = rpc_run_task(&task_setup);
8258
8259 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008260 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008261
Anna Schumakerf6148712017-04-07 14:15:23 -04008262 status = task->tk_status;
8263 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008264 return status;
8265}
8266
Andy Adamsonfc931582009-04-01 09:22:31 -04008267/*
8268 * Initialize the values to be used by the client in CREATE_SESSION
8269 * If nfs4_init_session set the fore channel request and response sizes,
8270 * use them.
8271 *
8272 * Set the back channel max_resp_sz_cached to zero to force the client to
8273 * always set csa_cachethis to FALSE because the current implementation
8274 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8275 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008276static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8277 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008278{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008279 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008280 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008281
Andy Adamson18aad3d2013-06-26 12:21:49 -04008282 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8283 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8284
Andy Adamsonfc931582009-04-01 09:22:31 -04008285 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008286 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8287 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008288 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008289 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008290
8291 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008292 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008293 __func__,
8294 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008295 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008296
8297 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008298 args->bc_attrs.max_rqst_sz = max_bc_payload;
8299 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008300 args->bc_attrs.max_resp_sz_cached = 0;
8301 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008302 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008303
8304 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8305 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8306 __func__,
8307 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8308 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8309 args->bc_attrs.max_reqs);
8310}
8311
Trond Myklebust79969dd12015-02-18 11:30:18 -08008312static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8313 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008314{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008315 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008316 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008317
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008318 if (rcvd->max_resp_sz > sent->max_resp_sz)
8319 return -EINVAL;
8320 /*
8321 * Our requested max_ops is the minimum we need; we're not
8322 * prepared to break up compounds into smaller pieces than that.
8323 * So, no point even trying to continue if the server won't
8324 * cooperate:
8325 */
8326 if (rcvd->max_ops < sent->max_ops)
8327 return -EINVAL;
8328 if (rcvd->max_reqs == 0)
8329 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008330 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8331 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008332 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008333}
8334
Trond Myklebust79969dd12015-02-18 11:30:18 -08008335static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8336 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008337{
8338 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008339 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008340
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008341 if (!(res->flags & SESSION4_BACK_CHAN))
8342 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008343 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8344 return -EINVAL;
8345 if (rcvd->max_resp_sz < sent->max_resp_sz)
8346 return -EINVAL;
8347 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8348 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008349 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008350 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008351 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008352 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008353out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008354 return 0;
8355}
Andy Adamson8d353012009-04-01 09:22:32 -04008356
Andy Adamson8d353012009-04-01 09:22:32 -04008357static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008358 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008359{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008360 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008361
Trond Myklebust79969dd12015-02-18 11:30:18 -08008362 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008363 if (ret)
8364 return ret;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008365 return nfs4_verify_back_channel_attrs(args, res);
8366}
8367
8368static void nfs4_update_session(struct nfs4_session *session,
8369 struct nfs41_create_session_res *res)
8370{
8371 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008372 /* Mark client id and session as being confirmed */
8373 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8374 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd12015-02-18 11:30:18 -08008375 session->flags = res->flags;
8376 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008377 if (res->flags & SESSION4_BACK_CHAN)
8378 memcpy(&session->bc_attrs, &res->bc_attrs,
8379 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008380}
8381
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008382static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008383 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008384{
8385 struct nfs4_session *session = clp->cl_session;
8386 struct nfs41_create_session_args args = {
8387 .client = clp,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008388 .clientid = clp->cl_clientid,
8389 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008390 .cb_program = NFS4_CALLBACK,
8391 };
Trond Myklebust79969dd12015-02-18 11:30:18 -08008392 struct nfs41_create_session_res res;
8393
Andy Adamsonfc931582009-04-01 09:22:31 -04008394 struct rpc_message msg = {
8395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8396 .rpc_argp = &args,
8397 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008398 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008399 };
8400 int status;
8401
Chuck Lever6b26cc82016-05-02 14:40:40 -04008402 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008403 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008404
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008405 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008406 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008407
Trond Myklebustb519d402016-09-11 14:50:01 -04008408 switch (status) {
8409 case -NFS4ERR_STALE_CLIENTID:
8410 case -NFS4ERR_DELAY:
8411 case -ETIMEDOUT:
8412 case -EACCES:
8413 case -EAGAIN:
8414 goto out;
8415 };
8416
8417 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008418 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008419 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008420 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008421 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008422 if (status)
8423 goto out;
8424 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008425 }
Trond Myklebust79969dd12015-02-18 11:30:18 -08008426out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008427 return status;
8428}
8429
8430/*
8431 * Issues a CREATE_SESSION operation to the server.
8432 * It is the responsibility of the caller to verify the session is
8433 * expired before calling this routine.
8434 */
NeilBrowna52458b2018-12-03 11:30:31 +11008435int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008436{
8437 int status;
8438 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008439 struct nfs4_session *session = clp->cl_session;
8440
8441 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8442
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008443 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008444 if (status)
8445 goto out;
8446
Andy Adamsonaacd5532011-11-09 13:58:21 -05008447 /* Init or reset the session slot tables */
8448 status = nfs4_setup_session_slot_tables(session);
8449 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008450 if (status)
8451 goto out;
8452
8453 ptr = (unsigned *)&session->sess_id.data[0];
8454 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8455 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008456out:
8457 dprintk("<-- %s\n", __func__);
8458 return status;
8459}
8460
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008461/*
8462 * Issue the over-the-wire RPC DESTROY_SESSION.
8463 * The caller must serialize access to this routine.
8464 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008465int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008466 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008467{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008468 struct rpc_message msg = {
8469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8470 .rpc_argp = session,
8471 .rpc_cred = cred,
8472 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008473 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008474
8475 dprintk("--> nfs4_proc_destroy_session\n");
8476
8477 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008478 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8479 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008480
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008481 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008482 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008483
8484 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008485 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008486 "Session has been destroyed regardless...\n", status);
8487
8488 dprintk("<-- nfs4_proc_destroy_session\n");
8489 return status;
8490}
8491
Trond Myklebust7b38c362012-05-23 13:23:31 -04008492/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008493 * Renew the cl_session lease.
8494 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008495struct nfs4_sequence_data {
8496 struct nfs_client *clp;
8497 struct nfs4_sequence_args args;
8498 struct nfs4_sequence_res res;
8499};
8500
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008501static void nfs41_sequence_release(void *data)
8502{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008503 struct nfs4_sequence_data *calldata = data;
8504 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008505
Elena Reshetova212bf412017-10-20 12:53:38 +03008506 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008507 nfs4_schedule_state_renewal(clp);
8508 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008509 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008510}
8511
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008512static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8513{
8514 switch(task->tk_status) {
8515 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008516 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8517 return -EAGAIN;
8518 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008519 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008520 }
8521 return 0;
8522}
8523
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008524static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008525{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008526 struct nfs4_sequence_data *calldata = data;
8527 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008528
Trond Myklebust14516c32010-07-31 14:29:06 -04008529 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8530 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008531
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008532 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008533 if (task->tk_status < 0) {
8534 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008535 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008536 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008537
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008538 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8539 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008540 return;
8541 }
8542 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008543 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008544out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008545 dprintk("<-- %s\n", __func__);
8546}
8547
8548static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8549{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008550 struct nfs4_sequence_data *calldata = data;
8551 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008552 struct nfs4_sequence_args *args;
8553 struct nfs4_sequence_res *res;
8554
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008555 args = task->tk_msg.rpc_argp;
8556 res = task->tk_msg.rpc_resp;
8557
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008558 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008559}
8560
8561static const struct rpc_call_ops nfs41_sequence_ops = {
8562 .rpc_call_done = nfs41_sequence_call_done,
8563 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008564 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008565};
8566
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008567static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008568 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008569 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008570 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008571{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008572 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008573 struct rpc_message msg = {
8574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8575 .rpc_cred = cred,
8576 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008577 struct rpc_task_setup task_setup_data = {
8578 .rpc_client = clp->cl_rpcclient,
8579 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008580 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008581 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008582 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008583 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008584
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008585 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008586 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008587 goto out_err;
8588
8589 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008590 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008591 if (calldata == NULL)
8592 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008593 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008594 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008595 msg.rpc_argp = &calldata->args;
8596 msg.rpc_resp = &calldata->res;
8597 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008598 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008599
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008600 ret = rpc_run_task(&task_setup_data);
8601 if (IS_ERR(ret))
8602 goto out_err;
8603 return ret;
8604out_put_clp:
8605 nfs_put_client(clp);
8606out_err:
8607 nfs41_release_slot(slot);
8608 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008609}
8610
NeilBrowna52458b2018-12-03 11:30:31 +11008611static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008612{
8613 struct rpc_task *task;
8614 int ret = 0;
8615
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008616 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008617 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008618 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008619 if (IS_ERR(task))
8620 ret = PTR_ERR(task);
8621 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008622 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008623 dprintk("<-- %s status=%d\n", __func__, ret);
8624 return ret;
8625}
8626
NeilBrowna52458b2018-12-03 11:30:31 +11008627static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008628{
8629 struct rpc_task *task;
8630 int ret;
8631
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008632 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008633 if (IS_ERR(task)) {
8634 ret = PTR_ERR(task);
8635 goto out;
8636 }
8637 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008638 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008639 ret = task->tk_status;
8640 rpc_put_task(task);
8641out:
8642 dprintk("<-- %s status=%d\n", __func__, ret);
8643 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008644}
8645
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008646struct nfs4_reclaim_complete_data {
8647 struct nfs_client *clp;
8648 struct nfs41_reclaim_complete_args arg;
8649 struct nfs41_reclaim_complete_res res;
8650};
8651
8652static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8653{
8654 struct nfs4_reclaim_complete_data *calldata = data;
8655
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008656 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008657 &calldata->arg.seq_args,
8658 &calldata->res.seq_res,
8659 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008660}
8661
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008662static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8663{
8664 switch(task->tk_status) {
8665 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008666 wake_up_all(&clp->cl_lock_waitq);
8667 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008668 case -NFS4ERR_COMPLETE_ALREADY:
8669 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8670 break;
8671 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008672 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008673 /* fall through */
8674 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008675 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008676 case -NFS4ERR_BADSESSION:
8677 case -NFS4ERR_DEADSESSION:
8678 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8679 nfs4_schedule_session_recovery(clp->cl_session,
8680 task->tk_status);
8681 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008682 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008683 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008684 }
8685 return 0;
8686}
8687
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008688static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8689{
8690 struct nfs4_reclaim_complete_data *calldata = data;
8691 struct nfs_client *clp = calldata->clp;
8692 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8693
8694 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008695 if (!nfs41_sequence_done(task, res))
8696 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008697
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008698 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008699 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8700 rpc_restart_call_prepare(task);
8701 return;
8702 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008703 dprintk("<-- %s\n", __func__);
8704}
8705
8706static void nfs4_free_reclaim_complete_data(void *data)
8707{
8708 struct nfs4_reclaim_complete_data *calldata = data;
8709
8710 kfree(calldata);
8711}
8712
8713static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8714 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8715 .rpc_call_done = nfs4_reclaim_complete_done,
8716 .rpc_release = nfs4_free_reclaim_complete_data,
8717};
8718
8719/*
8720 * Issue a global reclaim complete.
8721 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008722static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008723 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008724{
8725 struct nfs4_reclaim_complete_data *calldata;
8726 struct rpc_task *task;
8727 struct rpc_message msg = {
8728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008729 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008730 };
8731 struct rpc_task_setup task_setup_data = {
8732 .rpc_client = clp->cl_rpcclient,
8733 .rpc_message = &msg,
8734 .callback_ops = &nfs4_reclaim_complete_call_ops,
8735 .flags = RPC_TASK_ASYNC,
8736 };
8737 int status = -ENOMEM;
8738
8739 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008740 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008741 if (calldata == NULL)
8742 goto out;
8743 calldata->clp = clp;
8744 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008745
Anna Schumakerfba83f32018-05-04 16:22:50 -04008746 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008747 msg.rpc_argp = &calldata->arg;
8748 msg.rpc_resp = &calldata->res;
8749 task_setup_data.callback_data = calldata;
8750 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008751 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008752 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008753 goto out;
8754 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008755 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008756 if (status == 0)
8757 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008758 rpc_put_task(task);
8759out:
8760 dprintk("<-- %s status=%d\n", __func__, status);
8761 return status;
8762}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008763
8764static void
8765nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8766{
8767 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008768 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008769
8770 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008771 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008772 &lgp->res.seq_res, task);
8773 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008774}
8775
8776static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8777{
8778 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008779
8780 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008781 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008782 dprintk("<-- %s\n", __func__);
8783}
8784
8785static int
8786nfs4_layoutget_handle_exception(struct rpc_task *task,
8787 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8788{
Trond Myklebustee314c22012-10-01 17:25:48 -07008789 struct inode *inode = lgp->args.inode;
8790 struct nfs_server *server = NFS_SERVER(inode);
8791 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008792 int nfs4err = task->tk_status;
8793 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008794 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008795
Boaz Harroshed7e5422014-01-22 20:34:54 +02008796 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008797
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008798 nfs4_sequence_free_slot(&lgp->res.seq_res);
8799
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008800 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008801 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008802 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008803
8804 /*
8805 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8806 * on the file. set tk_status to -ENODATA to tell upper layer to
8807 * retry go inband.
8808 */
8809 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008810 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008811 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008812 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008813 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8814 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8815 */
8816 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008817 status = -EOVERFLOW;
8818 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008819 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008820 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008821 * (or clients) writing to the same RAID stripe except when
8822 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008823 *
8824 * Treat it like we would RECALLCONFLICT -- we retry for a little
8825 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008826 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008827 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008828 if (lgp->args.minlength == 0) {
8829 status = -EOVERFLOW;
8830 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008831 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008832 status = -EBUSY;
8833 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008834 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008835 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008836 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008837 case -NFS4ERR_DELEG_REVOKED:
8838 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008839 case -NFS4ERR_EXPIRED:
8840 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008841 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008842 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008843 lo = NFS_I(inode)->layout;
8844 /* If the open stateid was bad, then recover it. */
8845 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008846 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008847 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008848 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008849 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008850 break;
8851 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008852
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008853 /*
8854 * Mark the bad layout state as invalid, then retry
8855 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008856 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008857 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008858 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008859 pnfs_free_lseg_list(&head);
8860 status = -EAGAIN;
8861 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008862 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008863
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008864 err = nfs4_handle_exception(server, nfs4err, exception);
8865 if (!status) {
8866 if (exception->retry)
8867 status = -EAGAIN;
8868 else
8869 status = err;
8870 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008871out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008872 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008873 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008874}
8875
Fred Isamandacb4522016-09-19 17:47:09 -04008876size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03008877{
8878 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8879 return nfs_page_array_len(0, max_resp_sz);
8880}
8881
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008882static void nfs4_layoutget_release(void *calldata)
8883{
8884 struct nfs4_layoutget *lgp = calldata;
8885
8886 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008887 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04008888 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008889 dprintk("<-- %s\n", __func__);
8890}
8891
8892static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8893 .rpc_call_prepare = nfs4_layoutget_prepare,
8894 .rpc_call_done = nfs4_layoutget_done,
8895 .rpc_release = nfs4_layoutget_release,
8896};
8897
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008898struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04008899nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008900{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008901 struct inode *inode = lgp->args.inode;
8902 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008903 struct rpc_task *task;
8904 struct rpc_message msg = {
8905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8906 .rpc_argp = &lgp->args,
8907 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008908 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008909 };
8910 struct rpc_task_setup task_setup_data = {
8911 .rpc_client = server->client,
8912 .rpc_message = &msg,
8913 .callback_ops = &nfs4_layoutget_call_ops,
8914 .callback_data = lgp,
8915 .flags = RPC_TASK_ASYNC,
8916 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008917 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008918 struct nfs4_exception exception = {
8919 .inode = inode,
8920 .timeout = *timeout,
8921 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008922 int status = 0;
8923
8924 dprintk("--> %s\n", __func__);
8925
Peng Tao4bd5a982014-11-17 11:05:17 +08008926 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8927 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8928
Anna Schumakerfba83f32018-05-04 16:22:50 -04008929 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008930
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008931 task = rpc_run_task(&task_setup_data);
8932 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008933 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008934 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008935 if (status != 0)
8936 goto out;
8937
8938 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8939 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04008940 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8941 *timeout = exception.timeout;
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008942 } else
8943 lseg = pnfs_layout_process(lgp);
8944out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008945 trace_nfs4_layoutget(lgp->args.ctx,
8946 &lgp->args.range,
8947 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008948 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008949 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008950
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008951 rpc_put_task(task);
8952 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008953 if (status)
8954 return ERR_PTR(status);
8955 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008956}
8957
Benny Halevycbe82602011-05-22 19:52:37 +03008958static void
8959nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8960{
8961 struct nfs4_layoutreturn *lrp = calldata;
8962
8963 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008964 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008965 &lrp->args.seq_args,
8966 &lrp->res.seq_res,
8967 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04008968 if (!pnfs_layout_is_valid(lrp->args.layout))
8969 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008970}
8971
8972static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8973{
8974 struct nfs4_layoutreturn *lrp = calldata;
8975 struct nfs_server *server;
8976
8977 dprintk("--> %s\n", __func__);
8978
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008979 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008980 return;
8981
8982 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008983 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008984 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04008985 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04008986 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05008987 lrp->args.inode))
8988 goto out_restart;
8989 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008990 default:
8991 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008992 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008993 case 0:
8994 break;
8995 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008996 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008997 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008998 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008999 }
Benny Halevycbe82602011-05-22 19:52:37 +03009000 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009001 return;
9002out_restart:
9003 task->tk_status = 0;
9004 nfs4_sequence_free_slot(&lrp->res.seq_res);
9005 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009006}
9007
9008static void nfs4_layoutreturn_release(void *calldata)
9009{
9010 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009011 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009012
9013 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009014 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009015 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009016 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009017 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9018 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009019 pnfs_put_layout_hdr(lrp->args.layout);
9020 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009021 kfree(calldata);
9022 dprintk("<-- %s\n", __func__);
9023}
9024
9025static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9026 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9027 .rpc_call_done = nfs4_layoutreturn_done,
9028 .rpc_release = nfs4_layoutreturn_release,
9029};
9030
Peng Tao6c166052014-11-17 09:30:40 +08009031int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009032{
9033 struct rpc_task *task;
9034 struct rpc_message msg = {
9035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9036 .rpc_argp = &lrp->args,
9037 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009038 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009039 };
9040 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c5772013-07-22 12:42:05 -04009041 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009042 .rpc_message = &msg,
9043 .callback_ops = &nfs4_layoutreturn_call_ops,
9044 .callback_data = lrp,
9045 };
Peng Tao6c166052014-11-17 09:30:40 +08009046 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009047
Andrew Elble99ade3c2015-12-02 09:39:51 -05009048 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9049 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9050 &task_setup_data.rpc_client, &msg);
9051
Benny Halevycbe82602011-05-22 19:52:37 +03009052 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009053 if (!sync) {
9054 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9055 if (!lrp->inode) {
9056 nfs4_layoutreturn_release(lrp);
9057 return -EAGAIN;
9058 }
9059 task_setup_data.flags |= RPC_TASK_ASYNC;
9060 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009061 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009062 task = rpc_run_task(&task_setup_data);
9063 if (IS_ERR(task))
9064 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009065 if (sync)
9066 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009067 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009068 dprintk("<-- %s status=%d\n", __func__, status);
9069 rpc_put_task(task);
9070 return status;
9071}
9072
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009073static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009074_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9075 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009076 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009077{
9078 struct nfs4_getdeviceinfo_args args = {
9079 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009080 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9081 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009082 };
9083 struct nfs4_getdeviceinfo_res res = {
9084 .pdev = pdev,
9085 };
9086 struct rpc_message msg = {
9087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9088 .rpc_argp = &args,
9089 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009090 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009091 };
9092 int status;
9093
9094 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009095 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009096 if (res.notification & ~args.notify_types)
9097 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009098 if (res.notification != args.notify_types)
9099 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009100
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009101 dprintk("<-- %s status=%d\n", __func__, status);
9102
9103 return status;
9104}
9105
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009106int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9107 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009108 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009109{
9110 struct nfs4_exception exception = { };
9111 int err;
9112
9113 do {
9114 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009115 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009116 &exception);
9117 } while (exception.retry);
9118 return err;
9119}
9120EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9121
Andy Adamson863a3c62011-03-23 13:27:54 +00009122static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9123{
9124 struct nfs4_layoutcommit_data *data = calldata;
9125 struct nfs_server *server = NFS_SERVER(data->args.inode);
9126
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009127 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009128 &data->args.seq_args,
9129 &data->res.seq_res,
9130 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009131}
9132
9133static void
9134nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9135{
9136 struct nfs4_layoutcommit_data *data = calldata;
9137 struct nfs_server *server = NFS_SERVER(data->args.inode);
9138
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009139 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009140 return;
9141
9142 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009143 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9144 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9145 case -NFS4ERR_BADLAYOUT: /* no layout */
9146 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009147 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009148 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009149 break;
9150 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009151 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009152 rpc_restart_call_prepare(task);
9153 return;
9154 }
9155 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009156}
9157
9158static void nfs4_layoutcommit_release(void *calldata)
9159{
9160 struct nfs4_layoutcommit_data *data = calldata;
9161
Andy Adamsondb29c082011-07-30 20:52:38 -04009162 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009163 nfs_post_op_update_inode_force_wcc(data->args.inode,
9164 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009165 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009166 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009167 kfree(data);
9168}
9169
9170static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9171 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9172 .rpc_call_done = nfs4_layoutcommit_done,
9173 .rpc_release = nfs4_layoutcommit_release,
9174};
9175
9176int
Andy Adamsonef311532011-03-12 02:58:10 -05009177nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009178{
9179 struct rpc_message msg = {
9180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9181 .rpc_argp = &data->args,
9182 .rpc_resp = &data->res,
9183 .rpc_cred = data->cred,
9184 };
9185 struct rpc_task_setup task_setup_data = {
9186 .task = &data->task,
9187 .rpc_client = NFS_CLIENT(data->args.inode),
9188 .rpc_message = &msg,
9189 .callback_ops = &nfs4_layoutcommit_ops,
9190 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009191 };
9192 struct rpc_task *task;
9193 int status = 0;
9194
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009195 dprintk("NFS: initiating layoutcommit call. sync %d "
9196 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009197 data->args.lastbytewritten,
9198 data->args.inode->i_ino);
9199
Trond Myklebust472e2592015-02-05 16:50:30 -05009200 if (!sync) {
9201 data->inode = nfs_igrab_and_active(data->args.inode);
9202 if (data->inode == NULL) {
9203 nfs4_layoutcommit_release(data);
9204 return -EAGAIN;
9205 }
9206 task_setup_data.flags = RPC_TASK_ASYNC;
9207 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009208 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009209 task = rpc_run_task(&task_setup_data);
9210 if (IS_ERR(task))
9211 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009212 if (sync)
9213 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009214 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009215 dprintk("%s: status %d\n", __func__, status);
9216 rpc_put_task(task);
9217 return status;
9218}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009219
Andy Adamson97431202013-08-08 10:57:56 -04009220/**
9221 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9222 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9223 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009224static int
9225_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009226 struct nfs_fsinfo *info,
9227 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009228{
9229 struct nfs41_secinfo_no_name_args args = {
9230 .style = SECINFO_STYLE_CURRENT_FH,
9231 };
9232 struct nfs4_secinfo_res res = {
9233 .flavors = flavors,
9234 };
9235 struct rpc_message msg = {
9236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9237 .rpc_argp = &args,
9238 .rpc_resp = &res,
9239 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009240 struct rpc_clnt *clnt = server->client;
NeilBrowna52458b2018-12-03 11:30:31 +11009241 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009242 int status;
9243
9244 if (use_integrity) {
9245 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009246 cred = nfs4_get_clid_cred(server->nfs_client);
9247 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009248 }
9249
9250 dprintk("--> %s\n", __func__);
9251 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9252 &res.seq_res, 0);
9253 dprintk("<-- %s status=%d\n", __func__, status);
9254
NeilBrowna52458b2018-12-03 11:30:31 +11009255 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009256
9257 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009258}
9259
9260static int
9261nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9262 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9263{
9264 struct nfs4_exception exception = { };
9265 int err;
9266 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009267 /* first try using integrity protection */
9268 err = -NFS4ERR_WRONGSEC;
9269
9270 /* try to use integrity protection with machine cred */
9271 if (_nfs4_is_integrity_protected(server->nfs_client))
9272 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9273 flavors, true);
9274
9275 /*
9276 * if unable to use integrity protection, or SECINFO with
9277 * integrity protection returns NFS4ERR_WRONGSEC (which is
9278 * disallowed by spec, but exists in deployed servers) use
9279 * the current filesystem's rpc_client and the user cred.
9280 */
9281 if (err == -NFS4ERR_WRONGSEC)
9282 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9283 flavors, false);
9284
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009285 switch (err) {
9286 case 0:
9287 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009288 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009289 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009290 default:
9291 err = nfs4_handle_exception(server, err, &exception);
9292 }
9293 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009294out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009295 return err;
9296}
9297
9298static int
9299nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9300 struct nfs_fsinfo *info)
9301{
9302 int err;
9303 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009304 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009305 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009306 struct nfs4_secinfo4 *secinfo;
9307 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009308
9309 page = alloc_page(GFP_KERNEL);
9310 if (!page) {
9311 err = -ENOMEM;
9312 goto out;
9313 }
9314
9315 flavors = page_address(page);
9316 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9317
9318 /*
9319 * Fall back on "guess and check" method if
9320 * the server doesn't support SECINFO_NO_NAME
9321 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009322 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009323 err = nfs4_find_root_sec(server, fhandle, info);
9324 goto out_freepage;
9325 }
9326 if (err)
9327 goto out_freepage;
9328
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009329 for (i = 0; i < flavors->num_flavors; i++) {
9330 secinfo = &flavors->flavors[i];
9331
9332 switch (secinfo->flavor) {
9333 case RPC_AUTH_NULL:
9334 case RPC_AUTH_UNIX:
9335 case RPC_AUTH_GSS:
9336 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9337 &secinfo->flavor_info);
9338 break;
9339 default:
9340 flavor = RPC_AUTH_MAXFLAVOR;
9341 break;
9342 }
9343
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009344 if (!nfs_auth_info_match(&server->auth_info, flavor))
9345 flavor = RPC_AUTH_MAXFLAVOR;
9346
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009347 if (flavor != RPC_AUTH_MAXFLAVOR) {
9348 err = nfs4_lookup_root_sec(server, fhandle,
9349 info, flavor);
9350 if (!err)
9351 break;
9352 }
9353 }
9354
9355 if (flavor == RPC_AUTH_MAXFLAVOR)
9356 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009357
9358out_freepage:
9359 put_page(page);
9360 if (err == -EACCES)
9361 return -EPERM;
9362out:
9363 return err;
9364}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009365
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009366static int _nfs41_test_stateid(struct nfs_server *server,
9367 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009368 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009369{
9370 int status;
9371 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009372 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009373 };
9374 struct nfs41_test_stateid_res res;
9375 struct rpc_message msg = {
9376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9377 .rpc_argp = &args,
9378 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009379 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009380 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009381 struct rpc_clnt *rpc_client = server->client;
9382
9383 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9384 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009385
Chuck Lever38527b12012-07-11 16:30:23 -04009386 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009387 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009388 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009389 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009390 if (status != NFS_OK) {
9391 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009392 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009393 }
9394 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009395 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009396}
9397
Trond Myklebust43912bb2016-09-22 13:38:56 -04009398static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9399 int err, struct nfs4_exception *exception)
9400{
9401 exception->retry = 0;
9402 switch(err) {
9403 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009404 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009405 nfs4_handle_exception(server, err, exception);
9406 break;
9407 case -NFS4ERR_BADSESSION:
9408 case -NFS4ERR_BADSLOT:
9409 case -NFS4ERR_BAD_HIGH_SLOT:
9410 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9411 case -NFS4ERR_DEADSESSION:
9412 nfs4_do_handle_exception(server, err, exception);
9413 }
9414}
9415
Chuck Lever38527b12012-07-11 16:30:23 -04009416/**
9417 * nfs41_test_stateid - perform a TEST_STATEID operation
9418 *
9419 * @server: server / transport on which to perform the operation
9420 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009421 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009422 *
9423 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9424 * Otherwise a negative NFS4ERR value is returned if the operation
9425 * failed or the state ID is not currently valid.
9426 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009427static int nfs41_test_stateid(struct nfs_server *server,
9428 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009429 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009430{
9431 struct nfs4_exception exception = { };
9432 int err;
9433 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009434 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009435 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009436 } while (exception.retry);
9437 return err;
9438}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009439
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009440struct nfs_free_stateid_data {
9441 struct nfs_server *server;
9442 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009443 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009444};
9445
9446static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9447{
9448 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009449 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009450 &data->args.seq_args,
9451 &data->res.seq_res,
9452 task);
9453}
9454
9455static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9456{
9457 struct nfs_free_stateid_data *data = calldata;
9458
9459 nfs41_sequence_done(task, &data->res.seq_res);
9460
9461 switch (task->tk_status) {
9462 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009463 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009464 rpc_restart_call_prepare(task);
9465 }
9466}
9467
9468static void nfs41_free_stateid_release(void *calldata)
9469{
9470 kfree(calldata);
9471}
9472
Trond Myklebust17f26b12013-08-21 15:48:42 -04009473static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009474 .rpc_call_prepare = nfs41_free_stateid_prepare,
9475 .rpc_call_done = nfs41_free_stateid_done,
9476 .rpc_release = nfs41_free_stateid_release,
9477};
9478
Anna Schumaker2f261022018-05-15 13:03:39 -04009479/**
9480 * nfs41_free_stateid - perform a FREE_STATEID operation
9481 *
9482 * @server: server / transport on which to perform the operation
9483 * @stateid: state ID to release
9484 * @cred: credential
9485 * @is_recovery: set to true if this call needs to be privileged
9486 *
9487 * Note: this function is always asynchronous.
9488 */
9489static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009490 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009491 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009492 bool privileged)
9493{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009494 struct rpc_message msg = {
9495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009496 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009497 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009498 struct rpc_task_setup task_setup = {
9499 .rpc_client = server->client,
9500 .rpc_message = &msg,
9501 .callback_ops = &nfs41_free_stateid_ops,
9502 .flags = RPC_TASK_ASYNC,
9503 };
9504 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009505 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009506
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009507 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9508 &task_setup.rpc_client, &msg);
9509
Chuck Lever38527b12012-07-11 16:30:23 -04009510 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009511 data = kmalloc(sizeof(*data), GFP_NOFS);
9512 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009513 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009514 data->server = server;
9515 nfs4_stateid_copy(&data->args.stateid, stateid);
9516
9517 task_setup.callback_data = data;
9518
9519 msg.rpc_argp = &data->args;
9520 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009521 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009522 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009523 if (IS_ERR(task))
9524 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009525 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009526 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009527}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009528
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009529static void
9530nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009531{
NeilBrowna52458b2018-12-03 11:30:31 +11009532 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009533
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009534 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009535 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009536}
9537
Trond Myklebust36281ca2012-03-04 18:13:56 -05009538static bool nfs41_match_stateid(const nfs4_stateid *s1,
9539 const nfs4_stateid *s2)
9540{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009541 if (s1->type != s2->type)
9542 return false;
9543
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009544 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009545 return false;
9546
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009547 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009548 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009549
Anna Schumaker045c5512017-01-11 16:59:48 -05009550 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009551}
9552
Andy Adamson557134a2009-04-01 09:21:53 -04009553#endif /* CONFIG_NFS_V4_1 */
9554
Trond Myklebust36281ca2012-03-04 18:13:56 -05009555static bool nfs4_match_stateid(const nfs4_stateid *s1,
9556 const nfs4_stateid *s2)
9557{
Trond Myklebustf597c532012-03-04 18:13:56 -05009558 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009559}
9560
9561
Trond Myklebust17280172012-03-11 13:11:00 -04009562static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009563 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009564 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009565 .recover_open = nfs4_open_reclaim,
9566 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009567 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009568 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009569};
9570
Andy Adamson591d71c2009-04-01 09:22:47 -04009571#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009572static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009573 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9574 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9575 .recover_open = nfs4_open_reclaim,
9576 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009577 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009578 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009579 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009580};
9581#endif /* CONFIG_NFS_V4_1 */
9582
Trond Myklebust17280172012-03-11 13:11:00 -04009583static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009584 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009585 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009586 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009588 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009589};
9590
Andy Adamson591d71c2009-04-01 09:22:47 -04009591#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009592static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009593 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9594 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009595 .recover_open = nfs41_open_expired,
9596 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009597 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009598};
9599#endif /* CONFIG_NFS_V4_1 */
9600
Trond Myklebust17280172012-03-11 13:11:00 -04009601static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009602 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009603 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009604 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009605};
9606
9607#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009608static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009609 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009610 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009611 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009612};
9613#endif
9614
Chuck Leverec011fe2013-10-17 14:12:39 -04009615static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009616 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009617 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009618};
9619
9620#if defined(CONFIG_NFS_V4_1)
9621static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009622 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009623 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009624};
9625#endif /* CONFIG_NFS_V4_1 */
9626
Trond Myklebust97dc1352010-06-16 09:52:26 -04009627static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9628 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009629 .init_caps = NFS_CAP_READDIRPLUS
9630 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009631 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009632 .init_client = nfs40_init_client,
9633 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009634 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009635 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009636 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009637 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009638 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009639 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009640 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9641 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9642 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009643 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009644};
9645
9646#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009647static struct nfs_seqid *
9648nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9649{
9650 return NULL;
9651}
9652
Trond Myklebust97dc1352010-06-16 09:52:26 -04009653static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9654 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009655 .init_caps = NFS_CAP_READDIRPLUS
9656 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009657 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009658 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009659 | NFS_CAP_ATOMIC_OPEN_V1
9660 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009661 .init_client = nfs41_init_client,
9662 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009663 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009664 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009665 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009666 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009667 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009668 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009669 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009670 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9671 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9672 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009673 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009674};
9675#endif
9676
Steve Dickson42c2c422013-05-22 12:50:38 -04009677#if defined(CONFIG_NFS_V4_2)
9678static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9679 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009680 .init_caps = NFS_CAP_READDIRPLUS
9681 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009682 | NFS_CAP_POSIX_LOCK
9683 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009684 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009685 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009686 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009687 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009688 | NFS_CAP_OFFLOAD_CANCEL
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009689 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009690 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009691 | NFS_CAP_LAYOUTSTATS
9692 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009693 .init_client = nfs41_init_client,
9694 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009695 .match_stateid = nfs41_match_stateid,
9696 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009697 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009698 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009699 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009700 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009701 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009702 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9703 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9704 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009705 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009706};
9707#endif
9708
Trond Myklebust97dc1352010-06-16 09:52:26 -04009709const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9710 [0] = &nfs_v4_0_minor_ops,
9711#if defined(CONFIG_NFS_V4_1)
9712 [1] = &nfs_v4_1_minor_ops,
9713#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009714#if defined(CONFIG_NFS_V4_2)
9715 [2] = &nfs_v4_2_minor_ops,
9716#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009717};
9718
Trond Myklebust13997822016-07-24 17:10:52 -04009719static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009720{
9721 ssize_t error, error2;
9722
9723 error = generic_listxattr(dentry, list, size);
9724 if (error < 0)
9725 return error;
9726 if (list) {
9727 list += error;
9728 size -= error;
9729 }
9730
9731 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9732 if (error2 < 0)
9733 return error2;
9734 return error + error2;
9735}
9736
Trond Myklebust17f26b12013-08-21 15:48:42 -04009737static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009738 .create = nfs_create,
9739 .lookup = nfs_lookup,
9740 .atomic_open = nfs_atomic_open,
9741 .link = nfs_link,
9742 .unlink = nfs_unlink,
9743 .symlink = nfs_symlink,
9744 .mkdir = nfs_mkdir,
9745 .rmdir = nfs_rmdir,
9746 .mknod = nfs_mknod,
9747 .rename = nfs_rename,
9748 .permission = nfs_permission,
9749 .getattr = nfs_getattr,
9750 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009751 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009752};
9753
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009754static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009755 .permission = nfs_permission,
9756 .getattr = nfs_getattr,
9757 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009758 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009759};
9760
David Howells509de812006-08-22 20:06:11 -04009761const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009762 .version = 4, /* protocol version */
9763 .dentry_ops = &nfs4_dentry_operations,
9764 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009765 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009766 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009768 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009769 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009770 .getattr = nfs4_proc_getattr,
9771 .setattr = nfs4_proc_setattr,
9772 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009773 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009774 .access = nfs4_proc_access,
9775 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 .create = nfs4_proc_create,
9777 .remove = nfs4_proc_remove,
9778 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009779 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009781 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009782 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009783 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784 .link = nfs4_proc_link,
9785 .symlink = nfs4_proc_symlink,
9786 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009787 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 .readdir = nfs4_proc_readdir,
9789 .mknod = nfs4_proc_mknod,
9790 .statfs = nfs4_proc_statfs,
9791 .fsinfo = nfs4_proc_fsinfo,
9792 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009793 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009795 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009797 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009799 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009801 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009802 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009804 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009805 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009806 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009807 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009808 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009809 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009810 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009811 .create_server = nfs4_create_server,
9812 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813};
9814
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009815static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009816 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009817 .list = nfs4_xattr_list_nfs4_acl,
9818 .get = nfs4_xattr_get_nfs4_acl,
9819 .set = nfs4_xattr_set_nfs4_acl,
9820};
9821
9822const struct xattr_handler *nfs4_xattr_handlers[] = {
9823 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009824#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9825 &nfs4_xattr_nfs4_label_handler,
9826#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009827 NULL
9828};
9829
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830/*
9831 * Local variables:
9832 * c-basic-offset: 8
9833 * End:
9834 */