blob: a64ce95187768c97c7dc723d60bcd788bc026e5d [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 Myklebust0688e642019-04-07 13:59:09 -0400403static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500404{
Trond Myklebust65de8722008-12-23 15:21:44 -0500405 might_sleep();
406
NeilBrown8478eaa2014-09-18 16:09:27 +1000407 freezable_schedule_timeout_killable_unsafe(
408 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400409 if (!__fatal_signal_pending(current))
410 return 0;
411 return -EINTR;
412}
413
414static int nfs4_delay_interruptible(long *timeout)
415{
416 might_sleep();
417
418 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
419 if (!signal_pending(current))
420 return 0;
421 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
422}
423
424static int nfs4_delay(long *timeout, bool interruptible)
425{
426 if (interruptible)
427 return nfs4_delay_interruptible(timeout);
428 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500429}
430
Trond Myklebust50c80002019-07-11 19:02:18 -0400431static const nfs4_stateid *
432nfs4_recoverable_stateid(const nfs4_stateid *stateid)
433{
434 if (!stateid)
435 return NULL;
436 switch (stateid->type) {
437 case NFS4_OPEN_STATEID_TYPE:
438 case NFS4_LOCK_STATEID_TYPE:
439 case NFS4_DELEGATION_STATEID_TYPE:
440 return stateid;
441 default:
442 break;
443 }
444 return NULL;
445}
446
Trond Myklebust65de8722008-12-23 15:21:44 -0500447/* This is the error handling routine for processes that are allowed
448 * to sleep.
449 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400450static int nfs4_do_handle_exception(struct nfs_server *server,
451 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500452{
453 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500454 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400455 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500456 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500457 int ret = errorcode;
458
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400459 exception->delay = 0;
460 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400462
Trond Myklebust50c80002019-07-11 19:02:18 -0400463 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400464 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400465 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400466
Trond Myklebust65de8722008-12-23 15:21:44 -0500467 switch(errorcode) {
468 case 0:
469 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400470 case -NFS4ERR_BADHANDLE:
471 case -ESTALE:
472 if (inode != NULL && S_ISREG(inode->i_mode))
473 pnfs_destroy_layout(NFS_I(inode));
474 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400475 case -NFS4ERR_DELEG_REVOKED:
476 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400477 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400478 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400479 if (inode != NULL && stateid != NULL) {
480 nfs_inode_find_state_and_recover(inode,
481 stateid);
482 goto wait_on_recovery;
483 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500484 /* Fall through */
Trond Myklebust272289a2016-09-22 13:39:15 -0400485 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400486 if (inode) {
487 int err;
488
489 err = nfs_async_inode_return_delegation(inode,
490 stateid);
491 if (err == 0)
492 goto wait_on_recovery;
493 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
494 exception->retry = 1;
495 break;
496 }
497 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500498 if (state == NULL)
499 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400500 ret = nfs4_schedule_stateid_recovery(server, state);
501 if (ret < 0)
502 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500503 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500504 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500505 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500506 nfs4_schedule_lease_recovery(clp);
507 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400508 case -NFS4ERR_MOVED:
509 ret = nfs4_schedule_migration_recovery(server);
510 if (ret < 0)
511 break;
512 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400513 case -NFS4ERR_LEASE_MOVED:
514 nfs4_schedule_lease_moved_recovery(clp);
515 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500516#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400517 case -NFS4ERR_BADSESSION:
518 case -NFS4ERR_BADSLOT:
519 case -NFS4ERR_BAD_HIGH_SLOT:
520 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
521 case -NFS4ERR_DEADSESSION:
522 case -NFS4ERR_SEQ_FALSE_RETRY:
523 case -NFS4ERR_SEQ_MISORDERED:
524 dprintk("%s ERROR: %d Reset session\n", __func__,
525 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400526 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400527 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500528#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500529 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500530 if (exception->timeout > HZ) {
531 /* We have retried a decent amount, time to
532 * fail
533 */
534 ret = -EBUSY;
535 break;
536 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500537 /* Fall through */
Trond Myklebust65de8722008-12-23 15:21:44 -0500538 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400539 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500540 /* Fall through */
Trond Myklebust2598ed32015-09-20 16:10:18 -0400541 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400542 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400543 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400544 exception->delay = 1;
545 return 0;
546
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400547 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500548 case -NFS4ERR_OLD_STATEID:
549 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800550 break;
551 case -NFS4ERR_BADOWNER:
552 /* The following works around a Linux server bug! */
553 case -NFS4ERR_BADNAME:
554 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
555 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
556 exception->retry = 1;
557 printk(KERN_WARNING "NFS: v4 server %s "
558 "does not accept raw "
559 "uid/gids. "
560 "Reenabling the idmapper.\n",
561 server->nfs_client->cl_hostname);
562 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500563 }
564 /* We failed to handle the error */
565 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500566wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400567 exception->recovering = 1;
568 return 0;
569}
570
571/* This is the error handling routine for processes that are allowed
572 * to sleep.
573 */
574int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
575{
576 struct nfs_client *clp = server->nfs_client;
577 int ret;
578
579 ret = nfs4_do_handle_exception(server, errorcode, exception);
580 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400581 ret = nfs4_delay(&exception->timeout,
582 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400583 goto out_retry;
584 }
585 if (exception->recovering) {
586 ret = nfs4_wait_clnt_recover(clp);
587 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
588 return -EIO;
589 goto out_retry;
590 }
591 return ret;
592out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500593 if (ret == 0)
594 exception->retry = 1;
595 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500596}
597
Trond Myklebust037fc982015-09-20 15:51:00 -0400598static int
599nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
600 int errorcode, struct nfs4_exception *exception)
601{
602 struct nfs_client *clp = server->nfs_client;
603 int ret;
604
605 ret = nfs4_do_handle_exception(server, errorcode, exception);
606 if (exception->delay) {
607 rpc_delay(task, nfs4_update_delay(&exception->timeout));
608 goto out_retry;
609 }
610 if (exception->recovering) {
611 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
612 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
613 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
614 goto out_retry;
615 }
616 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
617 ret = -EIO;
618 return ret;
619out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500620 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400621 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500622 /*
623 * For NFS4ERR_MOVED, the client transport will need to
624 * be recomputed after migration recovery has completed.
625 */
626 if (errorcode == -NFS4ERR_MOVED)
627 rpc_task_release_transport(task);
628 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400629 return ret;
630}
631
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400632int
Trond Myklebust037fc982015-09-20 15:51:00 -0400633nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
634 struct nfs4_state *state, long *timeout)
635{
636 struct nfs4_exception exception = {
637 .state = state,
638 };
639
640 if (task->tk_status >= 0)
641 return 0;
642 if (timeout)
643 exception.timeout = *timeout;
644 task->tk_status = nfs4_async_handle_exception(task, server,
645 task->tk_status,
646 &exception);
647 if (exception.delay && timeout)
648 *timeout = exception.timeout;
649 if (exception.retry)
650 return -EAGAIN;
651 return 0;
652}
653
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400654/*
655 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
656 * or 'false' otherwise.
657 */
658static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
659{
660 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500661 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400662}
Trond Myklebust65de8722008-12-23 15:21:44 -0500663
Trond Myklebust452e9352010-07-31 14:29:06 -0400664static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 spin_lock(&clp->cl_lock);
667 if (time_before(clp->cl_last_renewal,timestamp))
668 clp->cl_last_renewal = timestamp;
669 spin_unlock(&clp->cl_lock);
670}
671
Trond Myklebust452e9352010-07-31 14:29:06 -0400672static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
673{
Trond Myklebustbe824162015-07-05 14:50:46 -0400674 struct nfs_client *clp = server->nfs_client;
675
676 if (!nfs4_has_session(clp))
677 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400678}
679
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400680struct nfs4_call_sync_data {
681 const struct nfs_server *seq_server;
682 struct nfs4_sequence_args *seq_args;
683 struct nfs4_sequence_res *seq_res;
684};
685
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800686void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400687 struct nfs4_sequence_res *res, int cache_reply,
688 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400689{
690 args->sa_slot = NULL;
691 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400692 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400693
694 res->sr_slot = NULL;
695}
696
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400697static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400698{
699 struct nfs4_slot *slot = res->sr_slot;
700 struct nfs4_slot_table *tbl;
701
Chuck Lever3bd23842013-08-09 12:49:19 -0400702 tbl = slot->table;
703 spin_lock(&tbl->slot_tbl_lock);
704 if (!nfs41_wake_and_assign_slot(tbl, slot))
705 nfs4_free_slot(tbl, slot);
706 spin_unlock(&tbl->slot_tbl_lock);
707
708 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400709}
710
711static int nfs40_sequence_done(struct rpc_task *task,
712 struct nfs4_sequence_res *res)
713{
714 if (res->sr_slot != NULL)
715 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400716 return 1;
717}
718
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400719#if defined(CONFIG_NFS_V4_1)
720
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400721static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400722{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500723 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400724 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500725 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400726
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400727 if (!slot)
728 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500729 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500730 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500731
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400732 /* Bump the slot sequence number */
733 if (slot->seq_done)
734 slot->seq_nr++;
735 slot->seq_done = 0;
736
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500737 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500738 /* Be nice to the server: try to ensure that the last transmitted
739 * value for highest_user_slotid <= target_highest_slotid
740 */
741 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
742 send_new_highest_used_slotid = true;
743
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500744 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500745 send_new_highest_used_slotid = false;
746 goto out_unlock;
747 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500748 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500749
750 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
751 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500752out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500753 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500754 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400755 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400756 if (waitqueue_active(&tbl->slot_waitq))
757 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400758}
759
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400760static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
761{
762 nfs41_release_slot(res->sr_slot);
763 res->sr_slot = NULL;
764}
765
Trond Myklebust3453d572018-06-20 17:53:34 -0400766static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
767 u32 seqnr)
768{
769 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
770 slot->seq_nr_highest_sent = seqnr;
771}
772static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
773 u32 seqnr)
774{
775 slot->seq_nr_highest_sent = seqnr;
776 slot->seq_nr_last_acked = seqnr;
777}
778
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400779static int nfs41_sequence_process(struct rpc_task *task,
780 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400781{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500782 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500783 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400784 struct nfs_client *clp;
Trond Myklebust85563072012-12-11 10:31:12 -0500785 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400786
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500787 if (slot == NULL)
788 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400789 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500790 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400791 goto out;
792
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500793 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500794
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400795 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500796 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400797 switch (res->sr_status) {
798 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400799 /* Mark this sequence number as having been acked */
800 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400801 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400802 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500803 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500804 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500805 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400806 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
807 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500808 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400809 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500810 case 1:
811 /*
812 * sr_status remains 1 if an RPC level error occurred.
813 * The server may or may not have processed the sequence
814 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500815 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400816 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
817 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500818 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400819 case -NFS4ERR_DELAY:
820 /* The server detected a resend of the RPC call and
821 * returned NFS4ERR_DELAY as per Section 2.10.6.2
822 * of RFC5661.
823 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500824 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400825 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500826 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500827 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400828 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400829 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400830 case -NFS4ERR_RETRY_UNCACHED_REP:
831 case -NFS4ERR_SEQ_FALSE_RETRY:
832 /*
833 * The server thinks we tried to replay a request.
834 * Retry the call after bumping the sequence ID.
835 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400836 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400837 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500838 case -NFS4ERR_BADSLOT:
839 /*
840 * The slot id we used was probably retired. Try again
841 * using a different slot id.
842 */
Trond Myklebust99589102018-06-09 12:50:50 -0400843 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400844 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500845 goto retry_nowait;
846 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400847 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500848 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400849 * Were one or more calls using this slot interrupted?
850 * If the server never received the request, then our
851 * transmitted slot sequence number may be too high.
Trond Myklebustac20d162012-12-15 15:36:07 -0500852 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400853 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
854 slot->seq_nr--;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500855 goto retry_nowait;
856 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400857 /*
858 * RFC5661:
859 * A retry might be sent while the original request is
860 * still in progress on the replier. The replier SHOULD
861 * deal with the issue by returning NFS4ERR_DELAY as the
862 * reply to SEQUENCE or CB_SEQUENCE operation, but
863 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
864 *
865 * Restart the search after a delay.
866 */
867 slot->seq_nr = slot->seq_nr_highest_sent;
868 goto out_retry;
Trond Myklebust14516c32010-07-31 14:29:06 -0400869 default:
870 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400871 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400872 }
873out:
874 /* The session may be reset by one of the error handlers. */
875 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500876out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500877 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400878session_recover:
879 nfs4_schedule_session_recovery(session, res->sr_status);
880 goto retry_nowait;
881retry_new_seq:
882 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500883retry_nowait:
884 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400885 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500886 task->tk_status = 0;
887 ret = 0;
888 }
889 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400890out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400891 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400892 goto out;
893 rpc_delay(task, NFS4_POLL_RETRY_MAX);
894 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400895}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400896
897int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
898{
899 if (!nfs41_sequence_process(task, res))
900 return 0;
901 if (res->sr_slot != NULL)
902 nfs41_sequence_free_slot(res);
903 return 1;
904
905}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500906EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400907
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400908static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
909{
910 if (res->sr_slot == NULL)
911 return 1;
912 if (res->sr_slot->table->session != NULL)
913 return nfs41_sequence_process(task, res);
914 return nfs40_sequence_done(task, res);
915}
916
917static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
918{
919 if (res->sr_slot != NULL) {
920 if (res->sr_slot->table->session != NULL)
921 nfs41_sequence_free_slot(res);
922 else
923 nfs40_sequence_free_slot(res);
924 }
925}
926
Peng Tao2c4b1312014-06-11 05:24:16 +0800927int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400928{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500929 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400930 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400931 if (!res->sr_slot->table->session)
932 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400933 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400934}
Peng Tao2c4b1312014-06-11 05:24:16 +0800935EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400936
Andy Adamsonce5039c2009-04-01 09:22:13 -0400937static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
938{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400939 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400940
Trond Myklebust035168ab2010-06-16 09:52:26 -0400941 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
942
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500943 nfs4_setup_sequence(data->seq_server->nfs_client,
944 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400945}
946
Andy Adamson69ab40c2009-04-01 09:22:19 -0400947static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
948{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400949 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400950
Trond Myklebust14516c32010-07-31 14:29:06 -0400951 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400952}
953
Trond Myklebust17280172012-03-11 13:11:00 -0400954static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400956 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400957};
958
Chuck Lever3bd23842013-08-09 12:49:19 -0400959#else /* !CONFIG_NFS_V4_1 */
960
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400961static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
962{
963 return nfs40_sequence_done(task, res);
964}
965
966static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
967{
968 if (res->sr_slot != NULL)
969 nfs40_sequence_free_slot(res);
970}
971
Peng Tao2c4b1312014-06-11 05:24:16 +0800972int nfs4_sequence_done(struct rpc_task *task,
973 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400974{
Chuck Lever3bd23842013-08-09 12:49:19 -0400975 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400976}
Peng Tao2c4b1312014-06-11 05:24:16 +0800977EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400978
979#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400980
Trond Myklebustc1dffe02019-03-01 12:13:34 -0500981static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
982{
983 res->sr_timestamp = jiffies;
984 res->sr_status_flags = 0;
985 res->sr_status = 1;
986}
987
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400988static
989void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
990 struct nfs4_sequence_res *res,
991 struct nfs4_slot *slot)
992{
993 if (!slot)
994 return;
995 slot->privileged = args->sa_privileged ? 1 : 0;
996 args->sa_slot = slot;
997
998 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400999}
1000
1001int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001002 struct nfs4_sequence_args *args,
1003 struct nfs4_sequence_res *res,
1004 struct rpc_task *task)
1005{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001006 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001007 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001008 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001009
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001010 /* slot already allocated? */
1011 if (res->sr_slot != NULL)
1012 goto out_start;
1013
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001014 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001015 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001016
Trond Myklebust3453d572018-06-20 17:53:34 -04001017 spin_lock(&tbl->slot_tbl_lock);
1018 /* The state manager will wait until the slot table is empty */
1019 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1020 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001021
Trond Myklebust3453d572018-06-20 17:53:34 -04001022 slot = nfs4_alloc_slot(tbl);
1023 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001024 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001025 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001026 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001027 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001028 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001029
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001030 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001031
Anna Schumakerad05cc02017-01-11 13:37:06 -05001032 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001033out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001034 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001035 rpc_call_start(task);
1036 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001037out_sleep_timeout:
1038 /* Try again in 1/4 second */
1039 if (args->sa_privileged)
1040 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1041 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1042 else
1043 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1044 NULL, jiffies + (HZ >> 2));
1045 spin_unlock(&tbl->slot_tbl_lock);
1046 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001047out_sleep:
1048 if (args->sa_privileged)
1049 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001050 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001051 else
1052 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1053 spin_unlock(&tbl->slot_tbl_lock);
1054 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001055}
1056EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1057
Chuck Lever9915ea72013-08-09 12:48:27 -04001058static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1059{
1060 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001061 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001062 data->seq_args, data->seq_res, task);
1063}
1064
1065static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1066{
1067 struct nfs4_call_sync_data *data = calldata;
1068 nfs4_sequence_done(task, data->seq_res);
1069}
1070
1071static const struct rpc_call_ops nfs40_call_sync_ops = {
1072 .rpc_call_prepare = nfs40_call_sync_prepare,
1073 .rpc_call_done = nfs40_call_sync_done,
1074};
1075
Anna Schumaker48c05852019-08-14 15:27:00 -04001076static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1077{
1078 int ret;
1079 struct rpc_task *task;
1080
1081 task = rpc_run_task(task_setup);
1082 if (IS_ERR(task))
1083 return PTR_ERR(task);
1084
1085 ret = task->tk_status;
1086 rpc_put_task(task);
1087 return ret;
1088}
1089
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001090static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001091 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001092 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001093 struct nfs4_sequence_args *args,
1094 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001095{
Chuck Lever9915ea72013-08-09 12:48:27 -04001096 struct nfs_client *clp = server->nfs_client;
1097 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001098 .seq_server = server,
1099 .seq_args = args,
1100 .seq_res = res,
1101 };
1102 struct rpc_task_setup task_setup = {
1103 .rpc_client = clnt,
1104 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001105 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001106 .callback_data = &data
1107 };
1108
Anna Schumaker48c05852019-08-14 15:27:00 -04001109 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110}
1111
Bryan Schumaker7c513052011-03-24 17:12:24 +00001112int nfs4_call_sync(struct rpc_clnt *clnt,
1113 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001114 struct rpc_message *msg,
1115 struct nfs4_sequence_args *args,
1116 struct nfs4_sequence_res *res,
1117 int cache_reply)
1118{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001119 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001120 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001121}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Trond Myklebust3c591172018-07-31 15:54:10 -04001123static void
1124nfs4_inc_nlink_locked(struct inode *inode)
1125{
1126 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1127 inc_nlink(inode);
1128}
1129
1130static void
1131nfs4_dec_nlink_locked(struct inode *inode)
1132{
1133 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1134 drop_nlink(inode);
1135}
1136
1137static void
1138update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001139 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 struct nfs_inode *nfsi = NFS_I(dir);
1142
Trond Myklebust16e14372018-03-20 16:53:31 -04001143 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1144 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001145 | NFS_INO_INVALID_DATA
1146 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001147 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001148 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1149 nfsi->attrtimeo_timestamp = jiffies;
1150 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001152 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001153 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1154 NFS_INO_INVALID_ACL;
1155 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001156 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001157 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001158 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001159 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001160 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001161}
1162
1163static void
1164update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001165 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001166{
1167 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001168 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 spin_unlock(&dir->i_lock);
1170}
1171
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001172struct nfs4_open_createattrs {
1173 struct nfs4_label *label;
1174 struct iattr *sattr;
1175 const __u32 verf[2];
1176};
1177
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001178static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1179 int err, struct nfs4_exception *exception)
1180{
1181 if (err != -EINVAL)
1182 return false;
1183 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1184 return false;
1185 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1186 exception->retry = 1;
1187 return true;
1188}
1189
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001190static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1191{
1192 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1193}
1194
1195static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1196{
1197 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1198
1199 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1200}
1201
Trond Myklebust6ae37332015-01-30 14:21:14 -05001202static u32
1203nfs4_map_atomic_open_share(struct nfs_server *server,
1204 fmode_t fmode, int openflags)
1205{
1206 u32 res = 0;
1207
1208 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1209 case FMODE_READ:
1210 res = NFS4_SHARE_ACCESS_READ;
1211 break;
1212 case FMODE_WRITE:
1213 res = NFS4_SHARE_ACCESS_WRITE;
1214 break;
1215 case FMODE_READ|FMODE_WRITE:
1216 res = NFS4_SHARE_ACCESS_BOTH;
1217 }
1218 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1219 goto out;
1220 /* Want no delegation if we're using O_DIRECT */
1221 if (openflags & O_DIRECT)
1222 res |= NFS4_SHARE_WANT_NO_DELEG;
1223out:
1224 return res;
1225}
1226
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001227static enum open_claim_type4
1228nfs4_map_atomic_open_claim(struct nfs_server *server,
1229 enum open_claim_type4 claim)
1230{
1231 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1232 return claim;
1233 switch (claim) {
1234 default:
1235 return claim;
1236 case NFS4_OPEN_CLAIM_FH:
1237 return NFS4_OPEN_CLAIM_NULL;
1238 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1239 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1240 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1241 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1242 }
1243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246{
1247 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001248 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001249 p->o_res.seqid = p->o_arg.seqid;
1250 p->c_res.seqid = p->c_arg.seqid;
1251 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001252 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001253 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001254 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001255}
1256
Al Viro82a2c1b2011-06-22 18:30:55 -04001257static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001258 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001259 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001260 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001261 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001262{
Al Viro82a2c1b2011-06-22 18:30:55 -04001263 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001264 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001266 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001267 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268 struct nfs4_opendata *p;
1269
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001270 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 if (p == NULL)
1272 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001273
1274 p->f_label = nfs4_label_alloc(server, gfp_mask);
1275 if (IS_ERR(p->f_label))
1276 goto err_free_p;
1277
Kinglong Meea49c2692015-07-27 15:31:38 +08001278 p->a_label = nfs4_label_alloc(server, gfp_mask);
1279 if (IS_ERR(p->a_label))
1280 goto err_free_f;
1281
Trond Myklebust63f5f792015-01-23 19:19:25 -05001282 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1283 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001284 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001285 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001286 nfs_sb_active(dentry->d_sb);
1287 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001288 p->dir = parent;
1289 p->owner = sp;
1290 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 p->o_arg.open_flags = flags;
1292 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001293 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001294 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1295 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001296 if (flags & O_CREAT) {
1297 p->o_arg.umask = current_umask();
1298 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1299 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1300 p->o_arg.u.attrs = &p->attrs;
1301 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1302
1303 memcpy(p->o_arg.u.verifier.data, c->verf,
1304 sizeof(p->o_arg.u.verifier.data));
1305 }
1306 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001307 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1308 * will return permission denied for all bits until close */
1309 if (!(flags & O_EXCL)) {
1310 /* ask server to check for all possible rights as results
1311 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001312 switch (p->o_arg.claim) {
1313 default:
1314 break;
1315 case NFS4_OPEN_CLAIM_NULL:
1316 case NFS4_OPEN_CLAIM_FH:
1317 p->o_arg.access = NFS4_ACCESS_READ |
1318 NFS4_ACCESS_MODIFY |
1319 NFS4_ACCESS_EXTEND |
1320 NFS4_ACCESS_EXECUTE;
1321 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001322 }
David Howells7539bba2006-08-22 20:06:09 -04001323 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001324 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1325 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001326 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001327 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001328 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001329 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001330 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001331 case NFS4_OPEN_CLAIM_NULL:
1332 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1333 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1334 p->o_arg.fh = NFS_FH(dir);
1335 break;
1336 case NFS4_OPEN_CLAIM_PREVIOUS:
1337 case NFS4_OPEN_CLAIM_FH:
1338 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1339 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001340 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001341 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001342 p->c_arg.fh = &p->o_res.fh;
1343 p->c_arg.stateid = &p->o_res.stateid;
1344 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001345 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001346 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001347 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001348
1349err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001350 nfs4_label_free(p->a_label);
1351err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001352 nfs4_label_free(p->f_label);
1353err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001354 kfree(p);
1355err:
1356 dput(parent);
1357 return NULL;
1358}
1359
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001360static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001361{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001362 struct nfs4_opendata *p = container_of(kref,
1363 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001364 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001365
Fred Isaman30ae2412016-10-18 13:39:51 -04001366 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001367 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001368 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001369 if (p->state != NULL)
1370 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001371 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001372
Kinglong Meea49c2692015-07-27 15:31:38 +08001373 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001374 nfs4_label_free(p->f_label);
1375
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001376 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001377 dput(p->dentry);
1378 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001379 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001380 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001381 kfree(p);
1382}
1383
1384static void nfs4_opendata_put(struct nfs4_opendata *p)
1385{
1386 if (p != NULL)
1387 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001388}
1389
Trond Myklebust24311f82015-09-20 10:50:17 -04001390static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1391 fmode_t fmode)
1392{
1393 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1394 case FMODE_READ|FMODE_WRITE:
1395 return state->n_rdwr != 0;
1396 case FMODE_WRITE:
1397 return state->n_wronly != 0;
1398 case FMODE_READ:
1399 return state->n_rdonly != 0;
1400 }
1401 WARN_ON_ONCE(1);
1402 return false;
1403}
1404
Trond Myklebustbe189f72018-09-27 17:12:33 -04001405static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1406 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001407{
1408 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001409
Trond Myklebust536e43d2012-01-17 22:04:26 -05001410 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001411 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001412 switch (claim) {
1413 case NFS4_OPEN_CLAIM_NULL:
1414 case NFS4_OPEN_CLAIM_FH:
1415 goto out;
1416 default:
1417 break;
1418 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001419 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001420 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001421 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1422 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001423 break;
1424 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001425 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1426 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001427 break;
1428 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001429 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1430 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001431 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001432out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001433 return ret;
1434}
1435
Trond Myklebust2a606182015-08-19 22:30:00 -05001436static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1437 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001438{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001439 if (delegation == NULL)
1440 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001442 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001443 switch (claim) {
1444 case NFS4_OPEN_CLAIM_NULL:
1445 case NFS4_OPEN_CLAIM_FH:
1446 break;
1447 case NFS4_OPEN_CLAIM_PREVIOUS:
1448 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1449 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001450 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001451 default:
1452 return 0;
1453 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001454 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001455 return 1;
1456}
1457
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001458static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001459{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001460 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001461 case FMODE_WRITE:
1462 state->n_wronly++;
1463 break;
1464 case FMODE_READ:
1465 state->n_rdonly++;
1466 break;
1467 case FMODE_READ|FMODE_WRITE:
1468 state->n_rdwr++;
1469 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001470 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001471}
1472
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001473#ifdef CONFIG_NFS_V4_1
1474static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1475{
1476 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1477 return true;
1478 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1479 return true;
1480 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1481 return true;
1482 return false;
1483}
1484#endif /* CONFIG_NFS_V4_1 */
1485
Trond Myklebustc9399f22017-11-06 15:28:01 -05001486static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1487{
1488 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1489 wake_up_all(&state->waitq);
1490}
1491
1492static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1493 const nfs4_stateid *stateid)
1494{
1495 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1496 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1497
1498 if (stateid_seqid == state_seqid + 1U ||
1499 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1500 nfs_state_log_update_open_stateid(state);
1501 else
1502 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1503}
1504
Trond Myklebust4f14c192014-02-12 19:15:06 -05001505static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001506{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001507 struct nfs_client *clp = state->owner->so_server->nfs_client;
1508 bool need_recover = false;
1509
1510 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1511 need_recover = true;
1512 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1513 need_recover = true;
1514 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1515 need_recover = true;
1516 if (need_recover)
1517 nfs4_state_mark_reclaim_nograce(clp, state);
1518}
1519
Trond Myklebustc9399f22017-11-06 15:28:01 -05001520/*
1521 * Check for whether or not the caller may update the open stateid
1522 * to the value passed in by stateid.
1523 *
1524 * Note: This function relies heavily on the server implementing
1525 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1526 * correctly.
1527 * i.e. The stateid seqids have to be initialised to 1, and
1528 * are then incremented on every state transition.
1529 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001530static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001531 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001532{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001533 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1534 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1535 if (stateid->seqid == cpu_to_be32(1))
1536 nfs_state_log_update_open_stateid(state);
1537 else
1538 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001539 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001540 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001541
1542 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1543 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001544 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001545 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001546 return false;
1547}
1548
Trond Myklebustf95549c2015-01-23 18:06:09 -05001549static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1550{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001551 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1552 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001553 if (state->n_wronly)
1554 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1555 if (state->n_rdonly)
1556 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1557 if (state->n_rdwr)
1558 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001559 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001560}
1561
Trond Myklebust226056c2014-02-11 10:41:07 -05001562static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1563 nfs4_stateid *stateid, fmode_t fmode)
1564{
1565 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1566 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1567 case FMODE_WRITE:
1568 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1569 break;
1570 case FMODE_READ:
1571 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1572 break;
1573 case 0:
1574 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1575 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1576 clear_bit(NFS_OPEN_STATE, &state->flags);
1577 }
1578 if (stateid == NULL)
1579 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001580 /* Handle OPEN+OPEN_DOWNGRADE races */
1581 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1582 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001583 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001584 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001585 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001586 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001587 nfs4_stateid_copy(&state->stateid, stateid);
1588 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001589 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001590out:
1591 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001592}
1593
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001594static void nfs_clear_open_stateid(struct nfs4_state *state,
1595 nfs4_stateid *arg_stateid,
1596 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001597{
1598 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001599 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1600 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1601 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001602 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001603 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1604 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001605}
1606
Trond Myklebust1393d962016-09-22 13:39:13 -04001607static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001608 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001609 __must_hold(&state->owner->so_lock)
1610 __must_hold(&state->seqlock)
1611 __must_hold(RCU)
1612
Trond Myklebust003707c2007-07-05 18:07:55 -04001613{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001614 DEFINE_WAIT(wait);
1615 int status = 0;
1616 for (;;) {
1617
1618 if (!nfs_need_update_open_stateid(state, stateid))
1619 return;
1620 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001621 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001622 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001623 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001624 /* Rely on seqids for serialisation with NFSv4.0 */
1625 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1626 break;
1627
1628 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1629 /*
1630 * Ensure we process the state changes in the same order
1631 * in which the server processed them by delaying the
1632 * update of the stateid until we are in sequence.
1633 */
1634 write_sequnlock(&state->seqlock);
1635 spin_unlock(&state->owner->so_lock);
1636 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001637 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001638 if (!signal_pending(current)) {
1639 if (schedule_timeout(5*HZ) == 0)
1640 status = -EAGAIN;
1641 else
1642 status = 0;
1643 } else
1644 status = -EINTR;
1645 finish_wait(&state->waitq, &wait);
1646 rcu_read_lock();
1647 spin_lock(&state->owner->so_lock);
1648 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001649 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001650
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001651 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1652 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001653 nfs4_stateid_copy(freeme, &state->open_stateid);
1654 nfs_test_and_clear_all_open_stateid(state);
1655 }
1656
Trond Myklebuste999e802014-02-10 18:20:47 -05001657 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1658 nfs4_stateid_copy(&state->stateid, stateid);
1659 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001660 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001661 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001662}
1663
Trond Myklebustc9399f22017-11-06 15:28:01 -05001664static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001665 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001666 fmode_t fmode,
1667 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001669 /*
1670 * Protect the call to nfs4_state_set_mode_locked and
1671 * serialise the stateid update
1672 */
1673 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001674 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1675 switch (fmode) {
1676 case FMODE_READ:
1677 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1678 break;
1679 case FMODE_WRITE:
1680 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1681 break;
1682 case FMODE_READ|FMODE_WRITE:
1683 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001684 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001685 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001686 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001687}
1688
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001689static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1690{
1691 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1692 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1693 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1694 clear_bit(NFS_OPEN_STATE, &state->flags);
1695}
1696
Trond Myklebustc9399f22017-11-06 15:28:01 -05001697static void nfs_state_set_delegation(struct nfs4_state *state,
1698 const nfs4_stateid *deleg_stateid,
1699 fmode_t fmode)
1700{
1701 /*
1702 * Protect the call to nfs4_state_set_mode_locked and
1703 * serialise the stateid update
1704 */
1705 write_seqlock(&state->seqlock);
1706 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1707 set_bit(NFS_DELEGATED_STATE, &state->flags);
1708 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001711static void nfs_state_clear_delegation(struct nfs4_state *state)
1712{
1713 write_seqlock(&state->seqlock);
1714 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1715 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1716 write_sequnlock(&state->seqlock);
1717}
1718
Trond Myklebust1393d962016-09-22 13:39:13 -04001719static int update_open_stateid(struct nfs4_state *state,
1720 const nfs4_stateid *open_stateid,
1721 const nfs4_stateid *delegation,
1722 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001723{
Trond Myklebust1393d962016-09-22 13:39:13 -04001724 struct nfs_server *server = NFS_SERVER(state->inode);
1725 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001726 struct nfs_inode *nfsi = NFS_I(state->inode);
1727 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001728 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001729 int ret = 0;
1730
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001731 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001732
1733 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001734 spin_lock(&state->owner->so_lock);
1735 if (open_stateid != NULL) {
1736 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1737 ret = 1;
1738 }
1739
Trond Myklebust333ac782019-10-22 12:12:17 -04001740 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001741 if (deleg_cur == NULL)
1742 goto no_delegation;
1743
1744 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001745 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001746 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001747 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001748 goto no_delegation_unlock;
1749
1750 if (delegation == NULL)
1751 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001752 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001753 goto no_delegation_unlock;
1754
Trond Myklebustb7391f42008-12-23 15:21:52 -05001755 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001756 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001757 ret = 1;
1758no_delegation_unlock:
1759 spin_unlock(&deleg_cur->lock);
1760no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001761 if (ret)
1762 update_open_stateflags(state, fmode);
1763 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001764 rcu_read_unlock();
1765
Trond Myklebust4f14c192014-02-12 19:15:06 -05001766 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001767 nfs4_schedule_state_manager(clp);
1768 if (freeme.type != 0)
1769 nfs4_test_and_free_stateid(server, &freeme,
1770 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001771
1772 return ret;
1773}
1774
Trond Myklebust39071e62015-01-24 15:07:56 -05001775static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1776 const nfs4_stateid *stateid)
1777{
1778 struct nfs4_state *state = lsp->ls_state;
1779 bool ret = false;
1780
1781 spin_lock(&state->state_lock);
1782 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1783 goto out_noupdate;
1784 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1785 goto out_noupdate;
1786 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1787 ret = true;
1788out_noupdate:
1789 spin_unlock(&state->state_lock);
1790 return ret;
1791}
Trond Myklebust34310432008-12-23 15:21:38 -05001792
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001793static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001794{
1795 struct nfs_delegation *delegation;
1796
Trond Myklebustf5086242018-03-20 16:43:13 -04001797 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001798 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001799 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001800 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001801 rcu_read_unlock();
1802 return;
1803 }
1804 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001805 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001806}
1807
Trond Myklebust6ee41262007-07-08 14:11:36 -04001808static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001809{
1810 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001811 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001812 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001813 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001814 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001815 nfs4_stateid stateid;
1816 int ret = -EAGAIN;
1817
Trond Myklebustaac00a82007-07-05 19:02:21 -04001818 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001819 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001820 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001821 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001822 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001823 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001824 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001825 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001826 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001827 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001828 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001829 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001830 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001831 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001832 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001833 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001834 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001835 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001836 if (!opendata->is_recover) {
1837 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1838 if (ret != 0)
1839 goto out;
1840 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001841 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001842
1843 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001844 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001845 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001846 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001847out:
1848 return ERR_PTR(ret);
1849out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001850 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001851 return state;
1852}
1853
Andy Adamsone23008e2012-10-02 21:07:32 -04001854static void
1855nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1856{
1857 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1858 struct nfs_delegation *delegation;
1859 int delegation_flags = 0;
1860
1861 rcu_read_lock();
1862 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1863 if (delegation)
1864 delegation_flags = delegation->flags;
1865 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001866 switch (data->o_arg.claim) {
1867 default:
1868 break;
1869 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1870 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001871 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1872 "returning a delegation for "
1873 "OPEN(CLAIM_DELEGATE_CUR)\n",
1874 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001875 return;
1876 }
1877 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001878 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001879 data->owner->so_cred,
1880 data->o_res.delegation_type,
1881 &data->o_res.delegation,
1882 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001883 else
1884 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001885 data->owner->so_cred,
1886 data->o_res.delegation_type,
1887 &data->o_res.delegation,
1888 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001889
1890 if (data->o_res.do_recall)
1891 nfs_async_inode_return_delegation(state->inode,
1892 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001893}
1894
1895/*
1896 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1897 * and update the nfs4_state.
1898 */
1899static struct nfs4_state *
1900_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1901{
1902 struct inode *inode = data->state->inode;
1903 struct nfs4_state *state = data->state;
1904 int ret;
1905
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001906 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001907 if (data->rpc_status)
1908 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001909 /* cached opens have already been processed */
1910 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001911 }
1912
Andy Adamsone23008e2012-10-02 21:07:32 -04001913 ret = nfs_refresh_inode(inode, &data->f_attr);
1914 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001915 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001916
1917 if (data->o_res.delegation_type != 0)
1918 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001919update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001920 if (!update_open_stateid(state, &data->o_res.stateid,
1921 NULL, data->o_arg.fmode))
1922 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001923 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001924
1925 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001926}
1927
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001928static struct inode *
1929nfs4_opendata_get_inode(struct nfs4_opendata *data)
1930{
1931 struct inode *inode;
1932
1933 switch (data->o_arg.claim) {
1934 case NFS4_OPEN_CLAIM_NULL:
1935 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1936 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1937 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1938 return ERR_PTR(-EAGAIN);
1939 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1940 &data->f_attr, data->f_label);
1941 break;
1942 default:
1943 inode = d_inode(data->dentry);
1944 ihold(inode);
1945 nfs_refresh_inode(inode, &data->f_attr);
1946 }
1947 return inode;
1948}
1949
Andy Adamsone23008e2012-10-02 21:07:32 -04001950static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001951nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1952{
1953 struct nfs4_state *state;
1954 struct inode *inode;
1955
1956 inode = nfs4_opendata_get_inode(data);
1957 if (IS_ERR(inode))
1958 return ERR_CAST(inode);
1959 if (data->state != NULL && data->state->inode == inode) {
1960 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001961 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04001962 } else
1963 state = nfs4_get_open_state(inode, data->owner);
1964 iput(inode);
1965 if (state == NULL)
1966 state = ERR_PTR(-ENOMEM);
1967 return state;
1968}
1969
Andy Adamsone23008e2012-10-02 21:07:32 -04001970static struct nfs4_state *
1971_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001972{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001973 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001974
Trond Myklebustaac00a82007-07-05 19:02:21 -04001975 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001976 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001977 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001978 goto out;
1979 }
1980
Trond Myklebust75e8c482017-08-08 10:38:07 -04001981 state = nfs4_opendata_find_nfs4_state(data);
1982 if (IS_ERR(state))
1983 goto out;
1984
Andy Adamsone23008e2012-10-02 21:07:32 -04001985 if (data->o_res.delegation_type != 0)
1986 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001987 if (!update_open_stateid(state, &data->o_res.stateid,
1988 NULL, data->o_arg.fmode)) {
1989 nfs4_put_open_state(state);
1990 state = ERR_PTR(-EAGAIN);
1991 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001992out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001993 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001994 return state;
1995}
1996
Andy Adamsone23008e2012-10-02 21:07:32 -04001997static struct nfs4_state *
1998nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1999{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002000 struct nfs4_state *ret;
2001
Andy Adamsone23008e2012-10-02 21:07:32 -04002002 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002003 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2004 else
2005 ret = _nfs4_opendata_to_nfs4_state(data);
2006 nfs4_sequence_free_slot(&data->o_res.seq_res);
2007 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002008}
2009
Trond Myklebust0de43972018-09-02 15:57:01 -04002010static struct nfs_open_context *
2011nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002012{
2013 struct nfs_inode *nfsi = NFS_I(state->inode);
2014 struct nfs_open_context *ctx;
2015
Trond Myklebust0de43972018-09-02 15:57:01 -04002016 rcu_read_lock();
2017 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002018 if (ctx->state != state)
2019 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002020 if ((ctx->mode & mode) != mode)
2021 continue;
2022 if (!get_nfs_open_context(ctx))
2023 continue;
2024 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002025 return ctx;
2026 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002027 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002028 return ERR_PTR(-ENOENT);
2029}
2030
Trond Myklebust0de43972018-09-02 15:57:01 -04002031static struct nfs_open_context *
2032nfs4_state_find_open_context(struct nfs4_state *state)
2033{
2034 struct nfs_open_context *ctx;
2035
2036 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2037 if (!IS_ERR(ctx))
2038 return ctx;
2039 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2040 if (!IS_ERR(ctx))
2041 return ctx;
2042 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2043}
2044
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002045static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2046 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002047{
2048 struct nfs4_opendata *opendata;
2049
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002050 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002051 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002052 if (opendata == NULL)
2053 return ERR_PTR(-ENOMEM);
2054 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002055 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002056 return opendata;
2057}
2058
Trond Myklebust24311f82015-09-20 10:50:17 -04002059static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2060 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002061{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002062 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002063 int ret;
2064
Trond Myklebust24311f82015-09-20 10:50:17 -04002065 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002066 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002067 opendata->o_arg.open_flags = 0;
2068 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002069 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2070 NFS_SB(opendata->dentry->d_sb),
2071 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002072 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2073 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2074 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002075 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002076 if (ret != 0)
2077 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002078 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002079 if (IS_ERR(newstate))
2080 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002081 if (newstate != opendata->state)
2082 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002083 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002084 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002085}
2086
2087static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2088{
Trond Myklebust864472e2006-01-03 09:55:15 +01002089 int ret;
2090
2091 /* memory barrier prior to reading state->n_* */
2092 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002093 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2094 if (ret != 0)
2095 return ret;
2096 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2097 if (ret != 0)
2098 return ret;
2099 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2100 if (ret != 0)
2101 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002102 /*
2103 * We may have performed cached opens for all three recoveries.
2104 * Check if we need to update the current stateid.
2105 */
2106 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002107 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002108 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002109 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002110 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002111 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002112 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002113 return 0;
2114}
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116/*
2117 * OPEN_RECLAIM:
2118 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002120static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002122 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002123 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002124 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 int status;
2126
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002127 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2128 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002129 if (IS_ERR(opendata))
2130 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002131 rcu_read_lock();
2132 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002133 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002134 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002135 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002136 opendata->o_arg.u.delegation_type = delegation_type;
2137 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002138 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return status;
2140}
2141
Trond Myklebust539cd032007-06-05 11:46:42 -04002142static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
2144 struct nfs_server *server = NFS_SERVER(state->inode);
2145 struct nfs4_exception exception = { };
2146 int err;
2147 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002148 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002149 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002150 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2151 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002152 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002153 break;
2154 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 } while (exception.retry);
2156 return err;
2157}
2158
Trond Myklebust864472e2006-01-03 09:55:15 +01002159static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2160{
2161 struct nfs_open_context *ctx;
2162 int ret;
2163
2164 ctx = nfs4_state_find_open_context(state);
2165 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002166 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002167 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2168 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002169 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002170 put_nfs_open_context(ctx);
2171 return ret;
2172}
2173
NeilBrowndce26302017-12-13 09:57:09 +11002174static 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 -07002175{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002176 switch (err) {
2177 default:
2178 printk(KERN_ERR "NFS: %s: unhandled error "
2179 "%d.\n", __func__, err);
2180 case 0:
2181 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002182 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002183 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002184 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002185 break;
2186 case -NFS4ERR_BADSESSION:
2187 case -NFS4ERR_BADSLOT:
2188 case -NFS4ERR_BAD_HIGH_SLOT:
2189 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2190 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002191 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2192 return -EAGAIN;
2193 case -NFS4ERR_STALE_CLIENTID:
2194 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002195 /* Don't recall a delegation if it was lost */
2196 nfs4_schedule_lease_recovery(server->nfs_client);
2197 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002198 case -NFS4ERR_MOVED:
2199 nfs4_schedule_migration_recovery(server);
2200 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002201 case -NFS4ERR_LEASE_MOVED:
2202 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2203 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002204 case -NFS4ERR_DELEG_REVOKED:
2205 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002206 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002207 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002208 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002209 nfs_inode_find_state_and_recover(state->inode,
2210 stateid);
2211 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002212 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002213 case -NFS4ERR_DELAY:
2214 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002215 ssleep(1);
2216 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002217 case -ENOMEM:
2218 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002219 if (fl) {
2220 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2221 if (lsp)
2222 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2223 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002224 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return err;
2227}
2228
Trond Myklebust24311f82015-09-20 10:50:17 -04002229int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002230 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002231{
2232 struct nfs_server *server = NFS_SERVER(state->inode);
2233 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002234 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002235
2236 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2237 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2238 if (IS_ERR(opendata))
2239 return PTR_ERR(opendata);
2240 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002241 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002242 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2243 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002244 goto out;
2245 }
2246 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002247 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2248 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002249 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002250 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002251 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2252 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2253 if (err)
2254 goto out;
2255 }
2256 nfs_state_clear_delegation(state);
2257out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002258 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002259 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002260}
2261
Chuck Leverbe05c862013-08-09 12:49:47 -04002262static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2263{
2264 struct nfs4_opendata *data = calldata;
2265
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002266 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2267 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002268}
2269
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002270static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2271{
2272 struct nfs4_opendata *data = calldata;
2273
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002274 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002275
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002276 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002277 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002278 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002279 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002280 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002281 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002282 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002283}
2284
2285static void nfs4_open_confirm_release(void *calldata)
2286{
2287 struct nfs4_opendata *data = calldata;
2288 struct nfs4_state *state = NULL;
2289
2290 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002291 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002292 goto out_free;
2293 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002294 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002295 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002296 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002297 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002298 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002299out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002300 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002301}
2302
2303static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002304 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002305 .rpc_call_done = nfs4_open_confirm_done,
2306 .rpc_release = nfs4_open_confirm_release,
2307};
2308
2309/*
2310 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2311 */
2312static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2313{
David Howells2b0143b2015-03-17 22:25:59 +00002314 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002315 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002316 struct rpc_message msg = {
2317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2318 .rpc_argp = &data->c_arg,
2319 .rpc_resp = &data->c_res,
2320 .rpc_cred = data->owner->so_cred,
2321 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002322 struct rpc_task_setup task_setup_data = {
2323 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002324 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002325 .callback_ops = &nfs4_open_confirm_ops,
2326 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002327 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002328 .flags = RPC_TASK_ASYNC,
2329 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 int status;
2331
Anna Schumakerfba83f32018-05-04 16:22:50 -04002332 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2333 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002334 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002335 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002336 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002337 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002338 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002339 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002340 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002341 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002342 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002343 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002344 smp_wmb();
2345 } else
2346 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002347 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return status;
2349}
2350
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002351static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002353 struct nfs4_opendata *data = calldata;
2354 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002355 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002356 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002357
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002358 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002359 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002360 /*
2361 * Check if we still need to send an OPEN call, or if we can use
2362 * a delegation instead.
2363 */
2364 if (data->state != NULL) {
2365 struct nfs_delegation *delegation;
2366
Trond Myklebustbe189f72018-09-27 17:12:33 -04002367 if (can_open_cached(data->state, data->o_arg.fmode,
2368 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002369 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002370 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002371 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002372 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002373 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002374 rcu_read_unlock();
2375 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002376 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002377 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002378 switch (claim) {
2379 default:
2380 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002381 case NFS4_OPEN_CLAIM_PREVIOUS:
2382 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2383 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002384 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002385 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002386 case NFS4_OPEN_CLAIM_FH:
2387 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002388 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002389 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002390 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002391 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002392 &data->o_res.seq_res,
2393 task) != 0)
2394 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002395
2396 /* Set the create mode (note dependency on the session type) */
2397 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2398 if (data->o_arg.open_flags & O_EXCL) {
2399 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2400 if (nfs4_has_persistent_session(clp))
2401 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2402 else if (clp->cl_mvops->minor_version > 0)
2403 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2404 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002405 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002406unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002407 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002408 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002409out_no_action:
2410 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002411out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002412 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002413}
2414
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002415static void nfs4_open_done(struct rpc_task *task, void *calldata)
2416{
2417 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002419 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002420
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002421 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002422 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002423
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002424 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002425 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2426 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002427 case S_IFREG:
2428 break;
2429 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002430 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002431 break;
2432 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002433 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002434 break;
2435 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002436 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002437 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002438 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002439 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002440 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2441 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002442 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002443 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002444}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002445
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002446static void nfs4_open_release(void *calldata)
2447{
2448 struct nfs4_opendata *data = calldata;
2449 struct nfs4_state *state = NULL;
2450
2451 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002452 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002453 goto out_free;
2454 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002455 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002456 goto out_free;
2457 /* In case we need an open_confirm, no cleanup! */
2458 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2459 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002460 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002461 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002462 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002463out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002464 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002465}
2466
2467static const struct rpc_call_ops nfs4_open_ops = {
2468 .rpc_call_prepare = nfs4_open_prepare,
2469 .rpc_call_done = nfs4_open_done,
2470 .rpc_release = nfs4_open_release,
2471};
2472
Fred Isaman3b65a302016-09-19 10:06:49 -04002473static int nfs4_run_open_task(struct nfs4_opendata *data,
2474 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002475{
David Howells2b0143b2015-03-17 22:25:59 +00002476 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002477 struct nfs_server *server = NFS_SERVER(dir);
2478 struct nfs_openargs *o_arg = &data->o_arg;
2479 struct nfs_openres *o_res = &data->o_res;
2480 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002481 struct rpc_message msg = {
2482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2483 .rpc_argp = o_arg,
2484 .rpc_resp = o_res,
2485 .rpc_cred = data->owner->so_cred,
2486 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002487 struct rpc_task_setup task_setup_data = {
2488 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002489 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002490 .callback_ops = &nfs4_open_ops,
2491 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002492 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002493 .flags = RPC_TASK_ASYNC,
2494 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002495 int status;
2496
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002497 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002498 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002499 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002500 data->cancelled = false;
2501 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002502 if (!ctx) {
2503 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002504 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002505 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002506 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002507 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002508 pnfs_lgopen_prepare(data, ctx);
2509 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002510 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002511 if (IS_ERR(task))
2512 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002513 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002514 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002515 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002516 smp_wmb();
2517 } else
2518 status = data->rpc_status;
2519 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002520
2521 return status;
2522}
2523
2524static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2525{
David Howells2b0143b2015-03-17 22:25:59 +00002526 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002527 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002528 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002529
Fred Isaman3b65a302016-09-19 10:06:49 -04002530 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002531 if (status != 0 || !data->rpc_done)
2532 return status;
2533
Trond Myklebust6926afd2012-01-07 13:22:46 -05002534 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2535
Anna Schumakerd7e98252017-01-11 16:13:29 -05002536 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002537 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002538
2539 return status;
2540}
2541
Trond Myklebustf3792d62014-07-10 08:54:32 -04002542/*
2543 * Additional permission checks in order to distinguish between an
2544 * open for read, and an open for execute. This works around the
2545 * fact that NFSv4 OPEN treats read and execute permissions as being
2546 * the same.
2547 * Note that in the non-execute case, we want to turn off permission
2548 * checking if we just created a new file (POSIX open() semantics).
2549 */
NeilBrowna52458b2018-12-03 11:30:31 +11002550static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002551 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002552 struct nfs4_state *state, fmode_t fmode,
2553 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002554{
2555 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002556 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002557
2558 /* access call failed or for some reason the server doesn't
2559 * support any access modes -- defer access call until later */
2560 if (opendata->o_res.access_supported == 0)
2561 return 0;
2562
2563 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002564 /*
2565 * Use openflags to check for exec, because fmode won't
2566 * always have FMODE_EXEC set when file open for exec.
2567 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002568 if (openflags & __FMODE_EXEC) {
2569 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002570 if (S_ISDIR(state->inode->i_mode))
2571 mask = NFS4_ACCESS_LOOKUP;
2572 else
2573 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002574 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002575 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002576
2577 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002578 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2579 nfs_access_add_cache(state->inode, &cache);
2580
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002581 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2582 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002583 return 0;
2584
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002585 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002586}
2587
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002588/*
2589 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2590 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002591static int _nfs4_proc_open(struct nfs4_opendata *data,
2592 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002593{
David Howells2b0143b2015-03-17 22:25:59 +00002594 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002595 struct nfs_server *server = NFS_SERVER(dir);
2596 struct nfs_openargs *o_arg = &data->o_arg;
2597 struct nfs_openres *o_res = &data->o_res;
2598 int status;
2599
Fred Isaman3b65a302016-09-19 10:06:49 -04002600 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002601 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002602 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002603 if (status != 0) {
2604 if (status == -NFS4ERR_BADNAME &&
2605 !(o_arg->open_flags & O_CREAT))
2606 return -ENOENT;
2607 return status;
2608 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002609
Trond Myklebust6926afd2012-01-07 13:22:46 -05002610 nfs_fattr_map_and_free_names(server, &data->f_attr);
2611
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002612 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002613 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002614 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002615 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002616 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002617 if (data->file_created ||
2618 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002619 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002620 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002621 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002622 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2623 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002625 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002627 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002629 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2630 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002631 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2632 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002633 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637/*
2638 * OPEN_EXPIRED:
2639 * reclaim state on the server after a network partition.
2640 * Assumes caller holds the appropriate lock
2641 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002642static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002644 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002645 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002647 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002648 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002649 if (IS_ERR(opendata))
2650 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002651 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002652 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002653 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002654 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002655 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002658static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002659{
Trond Myklebust539cd032007-06-05 11:46:42 -04002660 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002661 struct nfs4_exception exception = { };
2662 int err;
2663
2664 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002665 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002666 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002667 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2668 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002669 switch (err) {
2670 default:
2671 goto out;
2672 case -NFS4ERR_GRACE:
2673 case -NFS4ERR_DELAY:
2674 nfs4_handle_exception(server, err, &exception);
2675 err = 0;
2676 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002677 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002678out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002679 return err;
2680}
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2683{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002685 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Trond Myklebust864472e2006-01-03 09:55:15 +01002687 ctx = nfs4_state_find_open_context(state);
2688 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002689 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002690 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002691 put_nfs_open_context(ctx);
2692 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
Trond Myklebust41020b62016-09-22 13:38:58 -04002695static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2696 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002697{
Trond Myklebust41020b62016-09-22 13:38:58 -04002698 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002699 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002700}
2701
2702static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2703{
2704 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002705 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002706}
2707
2708static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2709{
2710 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2711 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002712 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002713 return nfs4_open_expired(sp, state);
2714}
2715
Trond Myklebust45870d62016-09-22 13:38:59 -04002716static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2717 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002718 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002719{
2720 return -NFS4ERR_BAD_STATEID;
2721}
2722
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002723#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002724static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2725 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002726 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002727{
2728 int status;
2729
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002730 switch (stateid->type) {
2731 default:
2732 break;
2733 case NFS4_INVALID_STATEID_TYPE:
2734 case NFS4_SPECIAL_STATEID_TYPE:
2735 return -NFS4ERR_BAD_STATEID;
2736 case NFS4_REVOKED_STATEID_TYPE:
2737 goto out_free;
2738 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002739
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002740 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002741 switch (status) {
2742 case -NFS4ERR_EXPIRED:
2743 case -NFS4ERR_ADMIN_REVOKED:
2744 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002745 break;
2746 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002747 return status;
2748 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002749out_free:
2750 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002751 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002752 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002753}
2754
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002755static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002756{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002757 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002758 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002759 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002760 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002761 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002762
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002763 /* Get the delegation credential for use by test/free_stateid */
2764 rcu_read_lock();
2765 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002766 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002767 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002768 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002769 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002770 }
2771
2772 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002773
Trond Myklebust994b15b2018-09-05 14:07:14 -04002774 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2775 &delegation->flags)) {
2776 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002777 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002778 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002779
NeilBrowna52458b2018-12-03 11:30:31 +11002780 if (delegation->cred)
2781 cred = get_cred(delegation->cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002782 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002783 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002784 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002785 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002786 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002787 else
2788 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002789
Trond Myklebust8c39a392019-07-19 13:48:44 -04002790 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002791 return ret;
2792}
2793
2794static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2795{
2796 nfs4_stateid tmp;
2797
2798 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2799 nfs4_copy_delegation_stateid(state->inode, state->state,
2800 &tmp, NULL) &&
2801 nfs4_stateid_match_other(&state->stateid, &tmp))
2802 nfs_state_set_delegation(state, &tmp, state->state);
2803 else
2804 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002805}
2806
2807/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002808 * nfs41_check_expired_locks - possibly free a lock stateid
2809 *
2810 * @state: NFSv4 state for an inode
2811 *
2812 * Returns NFS_OK if recovery for this stateid is now finished.
2813 * Otherwise a negative NFS4ERR value is returned.
2814 */
2815static int nfs41_check_expired_locks(struct nfs4_state *state)
2816{
2817 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002818 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002819 struct nfs_server *server = NFS_SERVER(state->inode);
2820
2821 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2822 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002823
2824 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002825 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2826 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002827 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002828
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002829 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002830 spin_unlock(&state->state_lock);
2831
2832 nfs4_put_lock_state(prev);
2833 prev = lsp;
2834
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002835 status = nfs41_test_and_free_expired_stateid(server,
2836 &lsp->ls_stateid,
2837 cred);
2838 trace_nfs4_test_lock_stateid(state, lsp, status);
2839 if (status == -NFS4ERR_EXPIRED ||
2840 status == -NFS4ERR_BAD_STATEID) {
2841 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002842 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002843 if (!recover_lost_locks)
2844 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2845 } else if (status != NFS_OK) {
2846 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002847 nfs4_put_lock_state(prev);
2848 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002849 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002850 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002851 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002852 }
2853 spin_unlock(&state->state_lock);
2854 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002855out:
2856 return ret;
2857}
2858
2859/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002860 * nfs41_check_open_stateid - possibly free an open stateid
2861 *
2862 * @state: NFSv4 state for an inode
2863 *
2864 * Returns NFS_OK if recovery for this stateid is now finished.
2865 * Otherwise a negative NFS4ERR value is returned.
2866 */
2867static int nfs41_check_open_stateid(struct nfs4_state *state)
2868{
2869 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002870 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002871 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002872 int status;
2873
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002874 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002875 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002876 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002877 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002878 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002879 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002880 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002881 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002882 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002883 if (nfs_open_stateid_recover_openmode(state))
2884 return -NFS4ERR_OPENMODE;
2885 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002886}
2887
2888static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2889{
Chuck Levereb64cf92012-07-11 16:30:05 -04002890 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002891
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002892 status = nfs41_check_delegation_stateid(state);
2893 if (status != NFS_OK)
2894 return status;
2895 nfs41_delegation_recover_stateid(state);
2896
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002897 status = nfs41_check_expired_locks(state);
2898 if (status != NFS_OK)
2899 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002900 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002901 if (status != NFS_OK)
2902 status = nfs4_open_expired(sp, state);
2903 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002904}
2905#endif
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002908 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2909 * fields corresponding to attributes that were used to store the verifier.
2910 * Make sure we clobber those fields in the later setattr call
2911 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002912static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002913 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002914{
Trond Myklebust609339c2018-03-28 16:18:17 -04002915 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2916 __u32 attrset[3];
2917 unsigned ret;
2918 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002919
Trond Myklebust609339c2018-03-28 16:18:17 -04002920 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2921 attrset[i] = opendata->o_res.attrset[i];
2922 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2923 attrset[i] &= ~bitmask[i];
2924 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002925
Trond Myklebust609339c2018-03-28 16:18:17 -04002926 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2927 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002928
Trond Myklebust609339c2018-03-28 16:18:17 -04002929 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2930 if (sattr->ia_valid & ATTR_ATIME_SET)
2931 ret |= ATTR_ATIME_SET;
2932 else
2933 ret |= ATTR_ATIME;
2934 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002935
Trond Myklebust609339c2018-03-28 16:18:17 -04002936 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2937 if (sattr->ia_valid & ATTR_MTIME_SET)
2938 ret |= ATTR_MTIME_SET;
2939 else
2940 ret |= ATTR_MTIME;
2941 }
2942
2943 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002944 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002945 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002946}
2947
Trond Myklebustc21443c2013-02-07 14:26:21 -05002948static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002949 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002950{
2951 struct nfs4_state_owner *sp = opendata->owner;
2952 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002953 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002954 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002955 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002956 unsigned int seq;
2957 int ret;
2958
2959 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2960
Fred Isaman3b65a302016-09-19 10:06:49 -04002961 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002962 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002963 goto out;
2964
Trond Myklebustae55e592018-05-22 11:17:16 -04002965 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002966 ret = PTR_ERR(state);
2967 if (IS_ERR(state))
2968 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002969 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002970 if (server->caps & NFS_CAP_POSIX_LOCK)
2971 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002972 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2973 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002974
Trond Myklebust275bb302013-05-29 13:11:28 -04002975 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002976 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002977 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002978 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002979 alias = d_exact_alias(dentry, state->inode);
2980 if (!alias)
2981 alias = d_splice_alias(igrab(state->inode), dentry);
2982 /* d_splice_alias() can't fail here - it's a non-directory */
2983 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002984 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002985 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002986 }
2987 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002988 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002989 }
2990
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04002991 /* Parse layoutget results before we check for access */
2992 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2993
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002994 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
2995 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002996 if (ret != 0)
2997 goto out;
2998
David Howells2b0143b2015-03-17 22:25:59 +00002999 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003000 nfs_inode_attach_open_context(ctx);
3001 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3002 nfs4_schedule_stateid_recovery(server, state);
3003 }
Fred Isaman2409a972016-10-06 12:11:21 -04003004
Trond Myklebustc21443c2013-02-07 14:26:21 -05003005out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003006 if (!opendata->cancelled)
3007 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003008 return ret;
3009}
3010
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003011/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003012 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 */
Andy Adamson82be4172012-05-23 05:02:35 -04003014static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003015 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003016 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003017 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003018 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 struct nfs4_state_owner *sp;
3021 struct nfs4_state *state = NULL;
3022 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003023 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003024 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003025 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003026 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003027 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003028 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003029 struct iattr *sattr = c->sattr;
3030 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003031 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003032 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003036 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3037 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3039 goto out_err;
3040 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003041 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003042 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003043 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003044 if (d_really_is_positive(dentry))
3045 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003046 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003047 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003048 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003049 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3050 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003051 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003052 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
David Quigley14c43f72013-05-22 12:50:43 -04003054 if (label) {
3055 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3056 if (IS_ERR(olabel)) {
3057 status = PTR_ERR(olabel);
3058 goto err_opendata_put;
3059 }
3060 }
3061
Trond Myklebuste911b812014-03-26 13:24:37 -07003062 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3063 if (!opendata->f_attr.mdsthreshold) {
3064 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3065 if (!opendata->f_attr.mdsthreshold)
3066 goto err_free_label;
3067 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003068 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003069 }
David Howells2b0143b2015-03-17 22:25:59 +00003070 if (d_really_is_positive(dentry))
3071 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003072
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003073 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003074 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003075 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003076 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003077
NeilBrownefcbc042015-07-30 13:00:56 +10003078 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003079 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003080 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003081 /*
3082 * send create attributes which was not set by open
3083 * with an extra setattr.
3084 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003085 if (attrs || label) {
3086 unsigned ia_old = sattr->ia_valid;
3087
3088 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003089 nfs_fattr_init(opendata->o_res.f_attr);
3090 status = nfs4_do_setattr(state->inode, cred,
3091 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003092 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003093 if (status == 0) {
3094 nfs_setattr_update_inode(state->inode, sattr,
3095 opendata->o_res.f_attr);
3096 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3097 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003098 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003099 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003100 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003101 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003102 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003103
Trond Myklebuste911b812014-03-26 13:24:37 -07003104 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003105 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003106 opendata->f_attr.mdsthreshold = NULL;
3107 }
Andy Adamson82be4172012-05-23 05:02:35 -04003108
David Quigley14c43f72013-05-22 12:50:43 -04003109 nfs4_label_free(olabel);
3110
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003111 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003114err_free_label:
3115 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003116err_opendata_put:
3117 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003118err_put_state_owner:
3119 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return status;
3122}
3123
3124
Andy Adamson82be4172012-05-23 05:02:35 -04003125static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003126 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003127 int flags,
3128 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003129 struct nfs4_label *label,
3130 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003132 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003133 struct nfs4_exception exception = {
3134 .interruptible = true,
3135 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003137 struct nfs4_open_createattrs c = {
3138 .label = label,
3139 .sattr = sattr,
3140 .verf = {
3141 [0] = (__u32)jiffies,
3142 [1] = (__u32)current->pid,
3143 },
3144 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 int status;
3146
3147 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003148 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003149 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003150 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (status == 0)
3152 break;
3153 /* NOTE: BAD_SEQID means the server and client disagree about the
3154 * book-keeping w.r.t. state-changing operations
3155 * (OPEN/CLOSE/LOCK/LOCKU...)
3156 * It is actually a sign of a bug on the client or on the server.
3157 *
3158 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003159 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 * have unhashed the old state_owner for us, and that we can
3161 * therefore safely retry using a new one. We should still warn
3162 * the user though...
3163 */
3164 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003165 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003166 " returned a bad sequence-id error!\n",
3167 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 exception.retry = 1;
3169 continue;
3170 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003171 /*
3172 * BAD_STATEID on OPEN means that the server cancelled our
3173 * state before it received the OPEN_CONFIRM.
3174 * Recover by retrying the request as per the discussion
3175 * on Page 181 of RFC3530.
3176 */
3177 if (status == -NFS4ERR_BAD_STATEID) {
3178 exception.retry = 1;
3179 continue;
3180 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003181 if (status == -EAGAIN) {
3182 /* We must have found a delegation */
3183 exception.retry = 1;
3184 continue;
3185 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003186 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3187 continue;
3188 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 status, &exception));
3190 } while (exception.retry);
3191 return res;
3192}
3193
Trond Myklebust8487c472016-06-26 08:44:35 -04003194static int _nfs4_do_setattr(struct inode *inode,
3195 struct nfs_setattrargs *arg,
3196 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003197 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003198 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003199{
3200 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003201 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3203 .rpc_argp = arg,
3204 .rpc_resp = res,
3205 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003206 };
NeilBrowna52458b2018-12-03 11:30:31 +11003207 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003208 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003209 bool truncate;
3210 int status;
3211
3212 nfs_fattr_init(res->fattr);
3213
3214 /* Servers should only apply open mode checks for file size changes */
3215 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003216 if (!truncate)
3217 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003218
Trond Myklebust991eedb2018-04-09 11:15:30 -04003219 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003220 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003221 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003222 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003223 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003224 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003225 l_ctx = nfs_get_lock_context(ctx);
3226 if (IS_ERR(l_ctx))
3227 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003228 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3229 &arg->stateid, &delegation_cred);
3230 nfs_put_lock_context(l_ctx);
3231 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003232 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003233 } else {
3234zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003235 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003236 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003237 if (delegation_cred)
3238 msg.rpc_cred = delegation_cred;
3239
3240 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3241
NeilBrowna52458b2018-12-03 11:30:31 +11003242 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003243 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003244 renew_lease(server, timestamp);
3245 trace_nfs4_setattr(inode, &arg->stateid, status);
3246 return status;
3247}
3248
NeilBrowna52458b2018-12-03 11:30:31 +11003249static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003250 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003251 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003252 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003254 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003255 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003256 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003257 struct nfs_setattrargs arg = {
3258 .fh = NFS_FH(inode),
3259 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003261 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003262 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003263 };
3264 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003266 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003268 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003269 struct nfs4_exception exception = {
3270 .state = state,
3271 .inode = inode,
3272 .stateid = &arg.stateid,
3273 };
3274 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003277 nfs4_bitmap_copy_adjust_setattr(bitmask,
3278 nfs4_bitmask(server, olabel),
3279 inode);
3280
NeilBrown29b59f92016-10-13 15:26:47 +11003281 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003282 switch (err) {
3283 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003284 if (!(sattr->ia_valid & ATTR_SIZE)) {
3285 pr_warn_once("NFSv4: server %s is incorrectly "
3286 "applying open mode checks to "
3287 "a SETATTR that is not "
3288 "changing file size.\n",
3289 server->nfs_client->cl_hostname);
3290 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003291 if (state && !(state->state & FMODE_WRITE)) {
3292 err = -EBADF;
3293 if (sattr->ia_valid & ATTR_OPEN)
3294 err = -EACCES;
3295 goto out;
3296 }
3297 }
3298 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003300out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 return err;
3302}
3303
Peng Tao500d7012015-09-22 11:35:22 +08003304static bool
3305nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3306{
3307 if (inode == NULL || !nfs_have_layout(inode))
3308 return false;
3309
3310 return pnfs_wait_on_layoutreturn(inode, task);
3311}
3312
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003313/*
3314 * Update the seqid of an open stateid
3315 */
3316static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3317 struct nfs4_state *state)
3318{
3319 __be32 seqid_open;
3320 u32 dst_seqid;
3321 int seq;
3322
3323 for (;;) {
3324 if (!nfs4_valid_open_stateid(state))
3325 break;
3326 seq = read_seqbegin(&state->seqlock);
3327 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3328 nfs4_stateid_copy(dst, &state->open_stateid);
3329 if (read_seqretry(&state->seqlock, seq))
3330 continue;
3331 break;
3332 }
3333 seqid_open = state->open_stateid.seqid;
3334 if (read_seqretry(&state->seqlock, seq))
3335 continue;
3336
3337 dst_seqid = be32_to_cpu(dst->seqid);
3338 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3339 dst->seqid = seqid_open;
3340 break;
3341 }
3342}
3343
3344/*
3345 * Update the seqid of an open stateid after receiving
3346 * NFS4ERR_OLD_STATEID
3347 */
3348static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3349 struct nfs4_state *state)
3350{
3351 __be32 seqid_open;
3352 u32 dst_seqid;
3353 bool ret;
3354 int seq;
3355
3356 for (;;) {
3357 ret = false;
3358 if (!nfs4_valid_open_stateid(state))
3359 break;
3360 seq = read_seqbegin(&state->seqlock);
3361 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3362 if (read_seqretry(&state->seqlock, seq))
3363 continue;
3364 break;
3365 }
3366 seqid_open = state->open_stateid.seqid;
3367 if (read_seqretry(&state->seqlock, seq))
3368 continue;
3369
3370 dst_seqid = be32_to_cpu(dst->seqid);
3371 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3372 dst->seqid = cpu_to_be32(dst_seqid + 1);
3373 else
3374 dst->seqid = seqid_open;
3375 ret = true;
3376 break;
3377 }
3378
3379 return ret;
3380}
3381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382struct nfs4_closedata {
3383 struct inode *inode;
3384 struct nfs4_state *state;
3385 struct nfs_closeargs arg;
3386 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003387 struct {
3388 struct nfs4_layoutreturn_args arg;
3389 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003390 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003391 u32 roc_barrier;
3392 bool roc;
3393 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003394 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003395 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396};
3397
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003398static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003399{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003400 struct nfs4_closedata *calldata = data;
3401 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003402 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003403
Trond Myklebustcf805162016-11-15 14:56:07 -05003404 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003405 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3406 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003407 nfs4_put_open_state(calldata->state);
3408 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003409 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003410 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003411 kfree(calldata);
3412}
3413
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003414static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003416 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003419 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003420 struct nfs4_exception exception = {
3421 .state = state,
3422 .inode = calldata->inode,
3423 .stateid = &calldata->arg.stateid,
3424 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Chuck Levera3ca5652012-03-01 17:00:40 -05003426 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003427 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3428 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003429 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003430
3431 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04003432 if (pnfs_roc_done(task, calldata->inode,
3433 &calldata->arg.lr_args,
3434 &calldata->res.lr_res,
3435 &calldata->res.lr_ret) == -EAGAIN)
3436 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003437
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003438 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 * the state_owner. we keep this around to process errors
3440 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 switch (task->tk_status) {
3442 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003443 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003444 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003445 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003446 case -NFS4ERR_ACCESS:
3447 if (calldata->arg.bitmask != NULL) {
3448 calldata->arg.bitmask = NULL;
3449 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003450 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003451
3452 }
3453 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003454 case -NFS4ERR_OLD_STATEID:
3455 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003456 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003457 state))
3458 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003459 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003460 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003462 case -NFS4ERR_EXPIRED:
3463 nfs4_free_revoked_stateid(server,
3464 &calldata->arg.stateid,
3465 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003466 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003467 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003468 if (calldata->arg.fmode == 0)
3469 break;
3470 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003472 task->tk_status = nfs4_async_handle_exception(task,
3473 server, task->tk_status, &exception);
3474 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003475 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003477 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3478 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003479out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003480 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003481 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003482 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003483 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003484 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003485out_restart:
3486 task->tk_status = 0;
3487 rpc_restart_call_prepare(task);
3488 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003491static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003493 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003494 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003495 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003496 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003497 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003498 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003499
Chuck Levera3ca5652012-03-01 17:00:40 -05003500 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003501 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003502 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003503
Trond Myklebust88069f72009-12-08 08:33:16 -05003504 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003505 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003506 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3507 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3508 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003509 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003510 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003511 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003512 if (state->n_rdonly == 0)
3513 call_close |= is_rdonly;
3514 else if (is_rdonly)
3515 calldata->arg.fmode |= FMODE_READ;
3516 if (state->n_wronly == 0)
3517 call_close |= is_wronly;
3518 else if (is_wronly)
3519 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003520 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3521 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003522 } else if (is_rdwr)
3523 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3524
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003525 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3526 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003527 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003528 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003529
3530 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003531 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003532 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003533 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003534
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003535 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003536 nfs_release_seqid(calldata->arg.seqid);
3537 goto out_wait;
3538 }
3539
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003540 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3541 if (lo && !pnfs_layout_is_valid(lo)) {
3542 calldata->arg.lr_args = NULL;
3543 calldata->res.lr_res = NULL;
3544 }
3545
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003546 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003547 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003548
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003549 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003550 /* Close-to-open cache consistency revalidation */
3551 if (!nfs4_have_delegation(inode, FMODE_READ))
3552 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3553 else
3554 calldata->arg.bitmask = NULL;
3555 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003556
Trond Myklebust6ae37332015-01-30 14:21:14 -05003557 calldata->arg.share_access =
3558 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3559 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003560
Trond Myklebustd8d84982016-12-19 12:14:44 -05003561 if (calldata->res.fattr == NULL)
3562 calldata->arg.bitmask = NULL;
3563 else if (calldata->arg.bitmask == NULL)
3564 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003565 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003566 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003567 &calldata->arg.seq_args,
3568 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003569 task) != 0)
3570 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003571 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003572 return;
3573out_no_action:
3574 task->tk_action = NULL;
3575out_wait:
3576 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003579static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003580 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003581 .rpc_call_done = nfs4_close_done,
3582 .rpc_release = nfs4_free_closedata,
3583};
3584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585/*
3586 * It is possible for data to be read/written from a mem-mapped file
3587 * after the sys_close call (which hits the vfs layer as a flush).
3588 * This means that we can't safely call nfsv4 close on a file until
3589 * the inode is cleared. This in turn means that we are not good
3590 * NFSv4 citizens - we do not indicate to the server to update the file's
3591 * share state even when we are done with one of the three share
3592 * stateid's in the inode.
3593 *
3594 * NOTE: Caller must be holding the sp->so_owner semaphore!
3595 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003596int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003598 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003599 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003601 struct nfs4_state_owner *sp = state->owner;
3602 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003603 struct rpc_message msg = {
3604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3605 .rpc_cred = state->owner->so_cred,
3606 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003607 struct rpc_task_setup task_setup_data = {
3608 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003609 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003610 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003611 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003612 .flags = RPC_TASK_ASYNC,
3613 };
Trond Myklebust95121352005-10-18 14:20:12 -07003614 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003616 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3617 &task_setup_data.rpc_client, &msg);
3618
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003619 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003621 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003622 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003623 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003625 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003626 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3627 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003629 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3630 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003631 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003632 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003633 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003634 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003635 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003636 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003637 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003638 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003639 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003640 calldata->lr.roc = pnfs_roc(state->inode,
3641 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3642 if (calldata->lr.roc) {
3643 calldata->arg.lr_args = &calldata->lr.arg;
3644 calldata->res.lr_res = &calldata->lr.res;
3645 }
Al Viro643168c2011-06-22 18:20:23 -04003646 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003647
Trond Myklebust1174dd12010-12-21 10:52:24 -05003648 msg.rpc_argp = &calldata->arg;
3649 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003650 task_setup_data.callback_data = calldata;
3651 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003652 if (IS_ERR(task))
3653 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003654 status = 0;
3655 if (wait)
3656 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003657 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003658 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003659out_free_calldata:
3660 kfree(calldata);
3661out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003662 nfs4_put_open_state(state);
3663 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003664 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665}
3666
Trond Myklebust2b484292010-09-17 10:56:51 -04003667static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003668nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3669 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003672 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3673
3674 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Trond Myklebust565277f2007-10-15 18:17:53 -04003676 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003677 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003678
3679 nfs4_label_release_security(label);
3680
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003681 if (IS_ERR(state))
3682 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003683 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684}
3685
Trond Myklebust1185a552009-12-03 15:54:02 -05003686static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003687{
3688 if (ctx->state == NULL)
3689 return;
3690 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003691 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003692 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003693 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003694}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695
Trond Myklebustb944dba2013-11-04 15:20:20 -05003696#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3697#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003698#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3701{
Kinglong Mee8c612822015-08-26 21:12:58 +08003702 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003703 struct nfs4_server_caps_arg args = {
3704 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003705 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003706 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 struct nfs4_server_caps_res res = {};
3708 struct rpc_message msg = {
3709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003710 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 .rpc_resp = &res,
3712 };
3713 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003714 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Kinglong Mee8c612822015-08-26 21:12:58 +08003716 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3717 FATTR4_WORD0_FH_EXPIRE_TYPE |
3718 FATTR4_WORD0_LINK_SUPPORT |
3719 FATTR4_WORD0_SYMLINK_SUPPORT |
3720 FATTR4_WORD0_ACLSUPPORT;
3721 if (minorversion)
3722 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3723
Bryan Schumaker7c513052011-03-24 17:12:24 +00003724 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003726 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003727 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003728 case 0:
3729 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3730 res.attr_bitmask[2] = 0;
3731 break;
3732 case 1:
3733 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3734 break;
3735 case 2:
3736 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003739 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3740 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3741 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3742 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003743 NFS_CAP_CTIME|NFS_CAP_MTIME|
3744 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003745 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3746 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 server->caps |= NFS_CAP_ACLS;
3748 if (res.has_links != 0)
3749 server->caps |= NFS_CAP_HARDLINKS;
3750 if (res.has_symlinks != 0)
3751 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003752 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3753 server->caps |= NFS_CAP_FILEID;
3754 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3755 server->caps |= NFS_CAP_MODE;
3756 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3757 server->caps |= NFS_CAP_NLINK;
3758 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3759 server->caps |= NFS_CAP_OWNER;
3760 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3761 server->caps |= NFS_CAP_OWNER_GROUP;
3762 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3763 server->caps |= NFS_CAP_ATIME;
3764 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3765 server->caps |= NFS_CAP_CTIME;
3766 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3767 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003768#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3769 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3770 server->caps |= NFS_CAP_SECURITY_LABEL;
3771#endif
3772 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3773 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003774 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003775
Trond Myklebusta65318b2009-03-11 14:10:28 -04003776 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3777 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3778 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003779 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003780
3781 /* Avoid a regression due to buggy server */
3782 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3783 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003784 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3785 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003788 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003790
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return status;
3792}
3793
Trond Myklebust55a97592006-06-09 09:34:19 -04003794int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
Trond Myklebust0688e642019-04-07 13:59:09 -04003796 struct nfs4_exception exception = {
3797 .interruptible = true,
3798 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 int err;
3800 do {
3801 err = nfs4_handle_exception(server,
3802 _nfs4_server_capabilities(server, fhandle),
3803 &exception);
3804 } while (exception.retry);
3805 return err;
3806}
3807
3808static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3809 struct nfs_fsinfo *info)
3810{
David Quigleyaa9c2662013-05-22 12:50:44 -04003811 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003813 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 };
3815 struct nfs4_lookup_res res = {
3816 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003817 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 .fh = fhandle,
3819 };
3820 struct rpc_message msg = {
3821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3822 .rpc_argp = &args,
3823 .rpc_resp = &res,
3824 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003825
David Quigleyaa9c2662013-05-22 12:50:44 -04003826 bitmask[0] = nfs4_fattr_bitmap[0];
3827 bitmask[1] = nfs4_fattr_bitmap[1];
3828 /*
3829 * Process the label in the upcoming getfattr
3830 */
3831 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3832
Trond Myklebust0e574af2005-10-27 22:12:38 -04003833 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003834 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835}
3836
3837static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3838 struct nfs_fsinfo *info)
3839{
Trond Myklebust0688e642019-04-07 13:59:09 -04003840 struct nfs4_exception exception = {
3841 .interruptible = true,
3842 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 int err;
3844 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003845 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003846 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003847 switch (err) {
3848 case 0:
3849 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003850 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003851 default:
3852 err = nfs4_handle_exception(server, err, &exception);
3853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003855out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 return err;
3857}
3858
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003859static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3860 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3861{
Trond Myklebustc2190662013-08-26 19:23:04 -04003862 struct rpc_auth_create_args auth_args = {
3863 .pseudoflavor = flavor,
3864 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003865 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003866
Trond Myklebustc2190662013-08-26 19:23:04 -04003867 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003868 if (IS_ERR(auth))
3869 return -EACCES;
3870 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003871}
3872
Chuck Lever9a744ba2013-03-16 15:56:02 -04003873/*
3874 * Retry pseudoroot lookup with various security flavors. We do this when:
3875 *
3876 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3877 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3878 *
3879 * Returns zero on success, or a negative NFS4ERR value, or a
3880 * negative errno value.
3881 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003882static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003883 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003885 /* Per 3530bis 15.33.5 */
3886 static const rpc_authflavor_t flav_array[] = {
3887 RPC_AUTH_GSS_KRB5P,
3888 RPC_AUTH_GSS_KRB5I,
3889 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003890 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003891 RPC_AUTH_NULL,
3892 };
3893 int status = -EPERM;
3894 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003896 if (server->auth_info.flavor_len > 0) {
3897 /* try each flavor specified by user */
3898 for (i = 0; i < server->auth_info.flavor_len; i++) {
3899 status = nfs4_lookup_root_sec(server, fhandle, info,
3900 server->auth_info.flavors[i]);
3901 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3902 continue;
3903 break;
3904 }
3905 } else {
3906 /* no flavors specified by user, try default list */
3907 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3908 status = nfs4_lookup_root_sec(server, fhandle, info,
3909 flav_array[i]);
3910 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3911 continue;
3912 break;
3913 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003914 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003915
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003916 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003917 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003918 * to access the mount. It could also mean that we tried to mount
3919 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3920 * existing mount programs don't handle -EACCES very well so it should
3921 * be mapped to -EPERM instead.
3922 */
3923 if (status == -EACCES)
3924 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003925 return status;
3926}
3927
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003928/**
3929 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3930 * @server: initialized nfs_server handle
3931 * @fhandle: we fill in the pseudo-fs root file handle
3932 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003933 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003934 *
3935 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003936 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003937int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003938 struct nfs_fsinfo *info,
3939 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003940{
Andre Przywarac7757072015-04-23 17:17:40 +01003941 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003942
Andre Przywarac7757072015-04-23 17:17:40 +01003943 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003944 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003945
3946 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003947 status = server->nfs_client->cl_mvops->find_root_sec(server,
3948 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003949
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 if (status == 0)
3951 status = nfs4_server_capabilities(server, fhandle);
3952 if (status == 0)
3953 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003954
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003955 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956}
3957
Bryan Schumakerbae36242012-05-10 15:07:31 -04003958static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3959 struct nfs_fsinfo *info)
3960{
3961 int error;
3962 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003963 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003964
3965 error = nfs4_server_capabilities(server, mntfh);
3966 if (error < 0) {
3967 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3968 return error;
3969 }
3970
David Quigley14c43f72013-05-22 12:50:43 -04003971 label = nfs4_label_alloc(server, GFP_KERNEL);
3972 if (IS_ERR(label))
3973 return PTR_ERR(label);
3974
Trond Myklebusta841b542018-04-07 13:50:59 -04003975 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003976 if (error < 0) {
3977 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003978 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003979 }
3980
3981 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3982 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3983 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3984
David Quigley14c43f72013-05-22 12:50:43 -04003985err_free_label:
3986 nfs4_label_free(label);
3987
Bryan Schumakerbae36242012-05-10 15:07:31 -04003988 return error;
3989}
3990
Manoj Naik6b97fd32006-06-09 09:34:29 -04003991/*
3992 * Get locations and (maybe) other attributes of a referral.
3993 * Note that we'll actually follow the referral later when
3994 * we detect fsid mismatch in inode revalidation
3995 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003996static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3997 const struct qstr *name, struct nfs_fattr *fattr,
3998 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003999{
4000 int status = -ENOMEM;
4001 struct page *page = NULL;
4002 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004003
4004 page = alloc_page(GFP_KERNEL);
4005 if (page == NULL)
4006 goto out;
4007 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4008 if (locations == NULL)
4009 goto out;
4010
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004011 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004012 if (status != 0)
4013 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004014
4015 /*
4016 * If the fsid didn't change, this is a migration event, not a
4017 * referral. Cause us to drop into the exception handler, which
4018 * will kick off migration recovery.
4019 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004020 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004021 dprintk("%s: server did not return a different fsid for"
4022 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004023 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004024 goto out;
4025 }
Andy Adamson533eb462011-06-13 18:25:56 -04004026 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4027 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004028
Andy Adamson533eb462011-06-13 18:25:56 -04004029 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004030 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004031 memset(fhandle, 0, sizeof(struct nfs_fh));
4032out:
4033 if (page)
4034 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004035 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004036 return status;
4037}
4038
David Quigley1775fd32013-05-22 12:50:42 -04004039static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004040 struct nfs_fattr *fattr, struct nfs4_label *label,
4041 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042{
Trond Myklebust771734f2018-04-07 13:54:23 -04004043 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 struct nfs4_getattr_arg args = {
4045 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004046 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 };
4048 struct nfs4_getattr_res res = {
4049 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004050 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 .server = server,
4052 };
4053 struct rpc_message msg = {
4054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4055 .rpc_argp = &args,
4056 .rpc_resp = &res,
4057 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004058
Trond Myklebust771734f2018-04-07 13:54:23 -04004059 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004060
Trond Myklebust0e574af2005-10-27 22:12:38 -04004061 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004062 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063}
4064
David Quigley1775fd32013-05-22 12:50:42 -04004065static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004066 struct nfs_fattr *fattr, struct nfs4_label *label,
4067 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068{
Trond Myklebust0688e642019-04-07 13:59:09 -04004069 struct nfs4_exception exception = {
4070 .interruptible = true,
4071 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 int err;
4073 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004074 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004075 trace_nfs4_getattr(server, fhandle, fattr, err);
4076 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 &exception);
4078 } while (exception.retry);
4079 return err;
4080}
4081
4082/*
4083 * The file is not closed if it is opened due to the a request to change
4084 * the size of the file. The open call will not be needed once the
4085 * VFS layer lookup-intents are implemented.
4086 *
4087 * Close is called when the inode is destroyed.
4088 * If we haven't opened the file for O_WRONLY, we
4089 * need to in the size_change case to obtain a stateid.
4090 *
4091 * Got race?
4092 * Because OPEN is always done by name in nfsv4, it is
4093 * possible that we opened a different file by the same
4094 * name. We can recognize this race condition, but we
4095 * can't do anything about it besides returning an error.
4096 *
4097 * This will be fixed with VFS changes (lookup-intent).
4098 */
4099static int
4100nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4101 struct iattr *sattr)
4102{
David Howells2b0143b2015-03-17 22:25:59 +00004103 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004104 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004105 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004106 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 int status;
4108
Peng Tao88ac8152014-09-12 11:04:10 +08004109 if (pnfs_ld_layoutret_on_setattr(inode) &&
4110 sattr->ia_valid & ATTR_SIZE &&
4111 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004112 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004113
Trond Myklebust0e574af2005-10-27 22:12:38 -04004114 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Andy Adamson26699402012-05-30 16:12:24 -04004116 /* Deal with open(O_TRUNC) */
4117 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004118 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004119
4120 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004121 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004122 return 0;
4123
Trond Myklebustd5308382005-11-04 15:33:38 -05004124 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004125 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004126
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004127 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004128 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004129 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004130 }
4131
David Quigley14c43f72013-05-22 12:50:43 -04004132 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4133 if (IS_ERR(label))
4134 return PTR_ERR(label);
4135
Trond Myklebust199366f2018-03-20 16:43:18 -04004136 /* Return any delegations if we're going to change ACLs */
4137 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004138 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004139
NeilBrown29b59f92016-10-13 15:26:47 +11004140 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004141 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004142 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004143 nfs_setsecurity(inode, fattr, label);
4144 }
David Quigley14c43f72013-05-22 12:50:43 -04004145 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 return status;
4147}
4148
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004149static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4150 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004151 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004152{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004153 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004154 int status;
4155 struct nfs4_lookup_arg args = {
4156 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004157 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04004158 .name = name,
4159 };
4160 struct nfs4_lookup_res res = {
4161 .server = server,
4162 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004163 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004164 .fh = fhandle,
4165 };
4166 struct rpc_message msg = {
4167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4168 .rpc_argp = &args,
4169 .rpc_resp = &res,
4170 };
4171
David Quigleyaa9c2662013-05-22 12:50:44 -04004172 args.bitmask = nfs4_bitmask(server, label);
4173
David Howells2b3de442006-08-22 20:06:09 -04004174 nfs_fattr_init(fattr);
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004177 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 dprintk("NFS reply lookup: %d\n", status);
4179 return status;
4180}
4181
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004182static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004183{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004184 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004185 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004186 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4187 fattr->nlink = 2;
4188}
4189
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004190static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004191 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004192 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004193{
Trond Myklebust0688e642019-04-07 13:59:09 -04004194 struct nfs4_exception exception = {
4195 .interruptible = true,
4196 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004197 struct rpc_clnt *client = *clnt;
4198 int err;
4199 do {
David Quigley1775fd32013-05-22 12:50:42 -04004200 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004201 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004202 switch (err) {
4203 case -NFS4ERR_BADNAME:
4204 err = -ENOENT;
4205 goto out;
4206 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004207 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004208 if (err == -NFS4ERR_MOVED)
4209 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004210 goto out;
4211 case -NFS4ERR_WRONGSEC:
4212 err = -EPERM;
4213 if (client != *clnt)
4214 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004215 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004216 if (IS_ERR(client))
4217 return PTR_ERR(client);
4218
4219 exception.retry = 1;
4220 break;
4221 default:
4222 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4223 }
4224 } while (exception.retry);
4225
4226out:
4227 if (err == 0)
4228 *clnt = client;
4229 else if (client != *clnt)
4230 rpc_shutdown_client(client);
4231
4232 return err;
4233}
4234
Al Virobeffb8f2016-07-20 16:34:42 -04004235static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004236 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4237 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004239 int status;
4240 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004241
David Quigley1775fd32013-05-22 12:50:42 -04004242 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004243 if (client != NFS_CLIENT(dir)) {
4244 rpc_shutdown_client(client);
4245 nfs_fixup_secinfo_attributes(fattr);
4246 }
4247 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248}
4249
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004250struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004251nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004252 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4253{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004254 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004255 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004256
David Quigley1775fd32013-05-22 12:50:42 -04004257 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004258 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004259 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004260 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004261}
4262
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004263static int _nfs4_proc_lookupp(struct inode *inode,
4264 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4265 struct nfs4_label *label)
4266{
4267 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4268 struct nfs_server *server = NFS_SERVER(inode);
4269 int status;
4270 struct nfs4_lookupp_arg args = {
4271 .bitmask = server->attr_bitmask,
4272 .fh = NFS_FH(inode),
4273 };
4274 struct nfs4_lookupp_res res = {
4275 .server = server,
4276 .fattr = fattr,
4277 .label = label,
4278 .fh = fhandle,
4279 };
4280 struct rpc_message msg = {
4281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4282 .rpc_argp = &args,
4283 .rpc_resp = &res,
4284 };
4285
4286 args.bitmask = nfs4_bitmask(server, label);
4287
4288 nfs_fattr_init(fattr);
4289
4290 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4291 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4292 &res.seq_res, 0);
4293 dprintk("NFS reply lookupp: %d\n", status);
4294 return status;
4295}
4296
4297static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4298 struct nfs_fattr *fattr, struct nfs4_label *label)
4299{
Trond Myklebust0688e642019-04-07 13:59:09 -04004300 struct nfs4_exception exception = {
4301 .interruptible = true,
4302 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004303 int err;
4304 do {
4305 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4306 trace_nfs4_lookupp(inode, err);
4307 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4308 &exception);
4309 } while (exception.retry);
4310 return err;
4311}
4312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4314{
Trond Myklebust76b32992007-08-10 17:45:11 -04004315 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 struct nfs4_accessargs args = {
4317 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004318 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004320 struct nfs4_accessres res = {
4321 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004322 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 struct rpc_message msg = {
4324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4325 .rpc_argp = &args,
4326 .rpc_resp = &res,
4327 .rpc_cred = entry->cred,
4328 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004329 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Trond Myklebust7c672652018-06-04 15:00:53 -04004331 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004332 res.fattr = nfs_alloc_fattr();
4333 if (res.fattr == NULL)
4334 return -ENOMEM;
4335 args.bitmask = server->cache_consistency_bitmask;
4336 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004337 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004339 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004340 if (res.fattr)
4341 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004343 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 return status;
4345}
4346
4347static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4348{
Trond Myklebust0688e642019-04-07 13:59:09 -04004349 struct nfs4_exception exception = {
4350 .interruptible = true,
4351 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 int err;
4353 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004354 err = _nfs4_proc_access(inode, entry);
4355 trace_nfs4_access(inode, err);
4356 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 &exception);
4358 } while (exception.retry);
4359 return err;
4360}
4361
4362/*
4363 * TODO: For the time being, we don't try to get any attributes
4364 * along with any of the zero-copy operations READ, READDIR,
4365 * READLINK, WRITE.
4366 *
4367 * In the case of the first three, we want to put the GETATTR
4368 * after the read-type operation -- this is because it is hard
4369 * to predict the length of a GETATTR response in v4, and thus
4370 * align the READ data correctly. This means that the GETATTR
4371 * may end up partially falling into the page cache, and we should
4372 * shift it into the 'tail' of the xdr_buf before processing.
4373 * To do this efficiently, we need to know the total length
4374 * of data received, which doesn't seem to be available outside
4375 * of the RPC layer.
4376 *
4377 * In the case of WRITE, we also want to put the GETATTR after
4378 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004379 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 *
4381 * Both of these changes to the XDR layer would in fact be quite
4382 * minor, but I decided to leave them for a subsequent patch.
4383 */
4384static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4385 unsigned int pgbase, unsigned int pglen)
4386{
4387 struct nfs4_readlink args = {
4388 .fh = NFS_FH(inode),
4389 .pgbase = pgbase,
4390 .pglen = pglen,
4391 .pages = &page,
4392 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004393 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 struct rpc_message msg = {
4395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4396 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004397 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 };
4399
Bryan Schumaker7c513052011-03-24 17:12:24 +00004400 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 -07004401}
4402
4403static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4404 unsigned int pgbase, unsigned int pglen)
4405{
Trond Myklebust0688e642019-04-07 13:59:09 -04004406 struct nfs4_exception exception = {
4407 .interruptible = true,
4408 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 int err;
4410 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004411 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4412 trace_nfs4_readlink(inode, err);
4413 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 &exception);
4415 } while (exception.retry);
4416 return err;
4417}
4418
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004420 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422static int
4423nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004424 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004426 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004427 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004428 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 int status = 0;
4431
NeilBrown532d4de2016-10-13 15:26:47 +11004432 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004433 if (IS_ERR(ctx))
4434 return PTR_ERR(ctx);
4435
David Quigleyaa9c2662013-05-22 12:50:44 -04004436 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4437
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004438 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4439 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004440 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 if (IS_ERR(state)) {
4442 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004443 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004446 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004447 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return status;
4449}
4450
Trond Myklebust3c591172018-07-31 15:54:10 -04004451static int
4452_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453{
Trond Myklebust16e42952005-10-27 22:12:44 -04004454 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004455 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004457 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004459 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004460 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004461 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4464 .rpc_argp = &args,
4465 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004467 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004468 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004469
Bryan Schumaker7c513052011-03-24 17:12:24 +00004470 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004471 if (status == 0) {
4472 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004473 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004474 /* Removing a directory decrements nlink in the parent */
4475 if (ftype == NF4DIR && dir->i_nlink > 2)
4476 nfs4_dec_nlink_locked(dir);
4477 spin_unlock(&dir->i_lock);
4478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 return status;
4480}
4481
Trond Myklebust912678d2018-03-20 16:43:15 -04004482static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4483{
Trond Myklebust0688e642019-04-07 13:59:09 -04004484 struct nfs4_exception exception = {
4485 .interruptible = true,
4486 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004487 struct inode *inode = d_inode(dentry);
4488 int err;
4489
Trond Myklebustc01d3642018-03-20 16:43:20 -04004490 if (inode) {
4491 if (inode->i_nlink == 1)
4492 nfs4_inode_return_delegation(inode);
4493 else
4494 nfs4_inode_make_writeable(inode);
4495 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004496 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004497 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004498 trace_nfs4_remove(dir, &dentry->d_name, err);
4499 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4500 &exception);
4501 } while (exception.retry);
4502 return err;
4503}
4504
4505static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506{
Trond Myklebust0688e642019-04-07 13:59:09 -04004507 struct nfs4_exception exception = {
4508 .interruptible = true,
4509 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004511
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004513 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004514 trace_nfs4_remove(dir, name, err);
4515 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 &exception);
4517 } while (exception.retry);
4518 return err;
4519}
4520
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004521static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4522 struct dentry *dentry,
4523 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004525 struct nfs_removeargs *args = msg->rpc_argp;
4526 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Trond Myklebust977fcc22018-03-20 16:43:17 -04004528 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004530 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004531
4532 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004533
4534 if (inode)
4535 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536}
4537
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004538static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4539{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004540 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004541 &data->args.seq_args,
4542 &data->res.seq_res,
4543 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544}
4545
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004546static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004548 struct nfs_unlinkdata *data = task->tk_calldata;
4549 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004550
Trond Myklebust14516c32010-07-31 14:29:06 -04004551 if (!nfs4_sequence_done(task, &res->seq_res))
4552 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004553 if (nfs4_async_handle_error(task, res->server, NULL,
4554 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004555 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004556 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004557 update_changeattr(dir, &res->cinfo,
4558 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004559 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560}
4561
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004562static void nfs4_proc_rename_setup(struct rpc_message *msg,
4563 struct dentry *old_dentry,
4564 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004565{
Jeff Laytond3d41522010-09-17 17:31:57 -04004566 struct nfs_renameargs *arg = msg->rpc_argp;
4567 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004568 struct inode *old_inode = d_inode(old_dentry);
4569 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004570
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004571 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004572 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004573 if (new_inode)
4574 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004575 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004576 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004577 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004578}
4579
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004580static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4581{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004582 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004583 &data->args.seq_args,
4584 &data->res.seq_res,
4585 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004586}
4587
4588static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4589 struct inode *new_dir)
4590{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004591 struct nfs_renamedata *data = task->tk_calldata;
4592 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004593
4594 if (!nfs4_sequence_done(task, &res->seq_res))
4595 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004596 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004597 return 0;
4598
Trond Myklebustc733c492017-01-11 12:32:26 -05004599 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004600 if (new_dir != old_dir) {
4601 /* Note: If we moved a directory, nlink will change */
4602 update_changeattr(old_dir, &res->old_cinfo,
4603 res->old_fattr->time_start,
4604 NFS_INO_INVALID_OTHER);
4605 update_changeattr(new_dir, &res->new_cinfo,
4606 res->new_fattr->time_start,
4607 NFS_INO_INVALID_OTHER);
4608 } else
4609 update_changeattr(old_dir, &res->old_cinfo,
4610 res->old_fattr->time_start,
4611 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004612 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004613 return 1;
4614}
4615
Al Virobeffb8f2016-07-20 16:34:42 -04004616static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004618 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004619 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 struct nfs4_link_arg arg = {
4621 .fh = NFS_FH(inode),
4622 .dir_fh = NFS_FH(dir),
4623 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004624 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004626 struct nfs4_link_res res = {
4627 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004628 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004629 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 struct rpc_message msg = {
4631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4632 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004633 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004635 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Trond Myklebust136f2622010-04-16 16:22:49 -04004637 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004638 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004639 goto out;
4640
David Quigley14c43f72013-05-22 12:50:43 -04004641 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4642 if (IS_ERR(res.label)) {
4643 status = PTR_ERR(res.label);
4644 goto out;
4645 }
4646
Trond Myklebustc01d3642018-03-20 16:43:20 -04004647 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004648 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004649
Bryan Schumaker7c513052011-03-24 17:12:24 +00004650 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004651 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004652 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004653 status = nfs_post_op_update_inode(inode, res.fattr);
4654 if (!status)
4655 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004656 }
David Quigley14c43f72013-05-22 12:50:43 -04004657
4658
4659 nfs4_label_free(res.label);
4660
Trond Myklebust136f2622010-04-16 16:22:49 -04004661out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004662 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 return status;
4664}
4665
Al Virobeffb8f2016-07-20 16:34:42 -04004666static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667{
Trond Myklebust0688e642019-04-07 13:59:09 -04004668 struct nfs4_exception exception = {
4669 .interruptible = true,
4670 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 int err;
4672 do {
4673 err = nfs4_handle_exception(NFS_SERVER(inode),
4674 _nfs4_proc_link(inode, dir, name),
4675 &exception);
4676 } while (exception.retry);
4677 return err;
4678}
4679
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004680struct nfs4_createdata {
4681 struct rpc_message msg;
4682 struct nfs4_create_arg arg;
4683 struct nfs4_create_res res;
4684 struct nfs_fh fh;
4685 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004686 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004687};
4688
4689static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004690 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004691{
4692 struct nfs4_createdata *data;
4693
4694 data = kzalloc(sizeof(*data), GFP_KERNEL);
4695 if (data != NULL) {
4696 struct nfs_server *server = NFS_SERVER(dir);
4697
David Quigley14c43f72013-05-22 12:50:43 -04004698 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4699 if (IS_ERR(data->label))
4700 goto out_free;
4701
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004702 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4703 data->msg.rpc_argp = &data->arg;
4704 data->msg.rpc_resp = &data->res;
4705 data->arg.dir_fh = NFS_FH(dir);
4706 data->arg.server = server;
4707 data->arg.name = name;
4708 data->arg.attrs = sattr;
4709 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004710 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004711 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004712 data->res.server = server;
4713 data->res.fh = &data->fh;
4714 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004715 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004716 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004717 }
4718 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004719out_free:
4720 kfree(data);
4721 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004722}
4723
4724static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4725{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004726 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004727 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004728 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004729 spin_lock(&dir->i_lock);
4730 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004731 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004732 /* Creating a directory bumps nlink in the parent */
4733 if (data->arg.ftype == NF4DIR)
4734 nfs4_inc_nlink_locked(dir);
4735 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004736 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004737 }
4738 return status;
4739}
4740
4741static void nfs4_free_createdata(struct nfs4_createdata *data)
4742{
David Quigley14c43f72013-05-22 12:50:43 -04004743 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004744 kfree(data);
4745}
4746
Chuck Lever4f390c12006-08-22 20:06:22 -04004747static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004748 struct page *page, unsigned int len, struct iattr *sattr,
4749 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004751 struct nfs4_createdata *data;
4752 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Chuck Lever94a6d752006-08-22 20:06:23 -04004754 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004755 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004756
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004757 status = -ENOMEM;
4758 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4759 if (data == NULL)
4760 goto out;
4761
4762 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4763 data->arg.u.symlink.pages = &page;
4764 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004765 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004767 status = nfs4_do_create(dir, dentry, data);
4768
4769 nfs4_free_createdata(data);
4770out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 return status;
4772}
4773
Chuck Lever4f390c12006-08-22 20:06:22 -04004774static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004775 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
Trond Myklebust0688e642019-04-07 13:59:09 -04004777 struct nfs4_exception exception = {
4778 .interruptible = true,
4779 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004780 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004782
4783 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4784
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004786 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4787 trace_nfs4_symlink(dir, &dentry->d_name, err);
4788 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 &exception);
4790 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004791
4792 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 return err;
4794}
4795
4796static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004797 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004799 struct nfs4_createdata *data;
4800 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004802 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4803 if (data == NULL)
4804 goto out;
4805
David Quigley1775fd32013-05-22 12:50:42 -04004806 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004807 status = nfs4_do_create(dir, dentry, data);
4808
4809 nfs4_free_createdata(data);
4810out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 return status;
4812}
4813
4814static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4815 struct iattr *sattr)
4816{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004817 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004818 struct nfs4_exception exception = {
4819 .interruptible = true,
4820 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004821 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004823
David Quigleyaa9c2662013-05-22 12:50:44 -04004824 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4825
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004826 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4827 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004829 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4830 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4831 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 &exception);
4833 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004834 nfs4_label_release_security(label);
4835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 return err;
4837}
4838
NeilBrown684f39b2018-12-03 11:30:30 +11004839static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004840 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841{
David Howells2b0143b2015-03-17 22:25:59 +00004842 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 struct nfs4_readdir_arg args = {
4844 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004845 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 .pgbase = 0,
4847 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004848 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004849 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 };
4851 struct nfs4_readdir_res res;
4852 struct rpc_message msg = {
4853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4854 .rpc_argp = &args,
4855 .rpc_resp = &res,
4856 .rpc_cred = cred,
4857 };
4858 int status;
4859
Al Viro6de14722013-09-16 10:53:17 -04004860 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4861 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004862 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004863 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004865 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 -05004866 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004867 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004868 status += args.pgbase;
4869 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004870
4871 nfs_invalidate_atime(dir);
4872
Harvey Harrison3110ff82008-05-02 13:42:44 -07004873 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 return status;
4875}
4876
NeilBrown684f39b2018-12-03 11:30:30 +11004877static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004878 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879{
Trond Myklebust0688e642019-04-07 13:59:09 -04004880 struct nfs4_exception exception = {
4881 .interruptible = true,
4882 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 int err;
4884 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004885 err = _nfs4_proc_readdir(dentry, cred, cookie,
4886 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004887 trace_nfs4_readdir(d_inode(dentry), err);
4888 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 &exception);
4890 } while (exception.retry);
4891 return err;
4892}
4893
4894static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004895 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004897 struct nfs4_createdata *data;
4898 int mode = sattr->ia_mode;
4899 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004901 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4902 if (data == NULL)
4903 goto out;
4904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004906 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004908 data->arg.ftype = NF4BLK;
4909 data->arg.u.device.specdata1 = MAJOR(rdev);
4910 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 }
4912 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004913 data->arg.ftype = NF4CHR;
4914 data->arg.u.device.specdata1 = MAJOR(rdev);
4915 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004916 } else if (!S_ISSOCK(mode)) {
4917 status = -EINVAL;
4918 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 }
David Quigley1775fd32013-05-22 12:50:42 -04004920
David Quigleyaa9c2662013-05-22 12:50:44 -04004921 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004922 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004923out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004924 nfs4_free_createdata(data);
4925out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 return status;
4927}
4928
4929static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4930 struct iattr *sattr, dev_t rdev)
4931{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004932 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004933 struct nfs4_exception exception = {
4934 .interruptible = true,
4935 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004936 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004938
David Quigleyaa9c2662013-05-22 12:50:44 -04004939 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4940
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004941 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4942 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004944 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4945 trace_nfs4_mknod(dir, &dentry->d_name, err);
4946 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 &exception);
4948 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004949
4950 nfs4_label_release_security(label);
4951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 return err;
4953}
4954
4955static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4956 struct nfs_fsstat *fsstat)
4957{
4958 struct nfs4_statfs_arg args = {
4959 .fh = fhandle,
4960 .bitmask = server->attr_bitmask,
4961 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004962 struct nfs4_statfs_res res = {
4963 .fsstat = fsstat,
4964 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 struct rpc_message msg = {
4966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4967 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004968 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 };
4970
Trond Myklebust0e574af2005-10-27 22:12:38 -04004971 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004972 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973}
4974
4975static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4976{
Trond Myklebust0688e642019-04-07 13:59:09 -04004977 struct nfs4_exception exception = {
4978 .interruptible = true,
4979 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 int err;
4981 do {
4982 err = nfs4_handle_exception(server,
4983 _nfs4_proc_statfs(server, fhandle, fsstat),
4984 &exception);
4985 } while (exception.retry);
4986 return err;
4987}
4988
4989static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4990 struct nfs_fsinfo *fsinfo)
4991{
4992 struct nfs4_fsinfo_arg args = {
4993 .fh = fhandle,
4994 .bitmask = server->attr_bitmask,
4995 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004996 struct nfs4_fsinfo_res res = {
4997 .fsinfo = fsinfo,
4998 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 struct rpc_message msg = {
5000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5001 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005002 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 };
5004
Bryan Schumaker7c513052011-03-24 17:12:24 +00005005 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006}
5007
5008static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5009{
Trond Myklebust0688e642019-04-07 13:59:09 -04005010 struct nfs4_exception exception = {
5011 .interruptible = true,
5012 };
Chuck Lever83ca7f52013-03-16 15:55:53 -04005013 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 int err;
5015
5016 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005017 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005018 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005019 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04005020 nfs4_set_lease_period(server->nfs_client,
5021 fsinfo->lease_time * HZ,
5022 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005023 break;
5024 }
5025 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 } while (exception.retry);
5027 return err;
5028}
5029
5030static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5031{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005032 int error;
5033
Trond Myklebust0e574af2005-10-27 22:12:38 -04005034 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005035 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005036 if (error == 0) {
5037 /* block layout checks this! */
5038 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005039 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005040 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005041
5042 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043}
5044
5045static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5046 struct nfs_pathconf *pathconf)
5047{
5048 struct nfs4_pathconf_arg args = {
5049 .fh = fhandle,
5050 .bitmask = server->attr_bitmask,
5051 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005052 struct nfs4_pathconf_res res = {
5053 .pathconf = pathconf,
5054 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 struct rpc_message msg = {
5056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5057 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005058 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 };
5060
5061 /* None of the pathconf attributes are mandatory to implement */
5062 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5063 memset(pathconf, 0, sizeof(*pathconf));
5064 return 0;
5065 }
5066
Trond Myklebust0e574af2005-10-27 22:12:38 -04005067 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005068 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069}
5070
5071static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5072 struct nfs_pathconf *pathconf)
5073{
Trond Myklebust0688e642019-04-07 13:59:09 -04005074 struct nfs4_exception exception = {
5075 .interruptible = true,
5076 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 int err;
5078
5079 do {
5080 err = nfs4_handle_exception(server,
5081 _nfs4_proc_pathconf(server, fhandle, pathconf),
5082 &exception);
5083 } while (exception.retry);
5084 return err;
5085}
5086
Trond Myklebust5521abf2013-03-16 20:54:34 -04005087int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005088 const struct nfs_open_context *ctx,
5089 const struct nfs_lock_context *l_ctx,
5090 fmode_t fmode)
5091{
NeilBrown17393472016-10-13 15:26:47 +11005092 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005093}
5094EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5095
Trond Myklebust5521abf2013-03-16 20:54:34 -04005096static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5097 const struct nfs_open_context *ctx,
5098 const struct nfs_lock_context *l_ctx,
5099 fmode_t fmode)
5100{
5101 nfs4_stateid current_stateid;
5102
Trond Myklebuste1253be2014-03-05 08:44:23 -05005103 /* If the current stateid represents a lost lock, then exit */
5104 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
5105 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005106 return nfs4_stateid_match(stateid, &current_stateid);
5107}
5108
5109static bool nfs4_error_stateid_expired(int err)
5110{
5111 switch (err) {
5112 case -NFS4ERR_DELEG_REVOKED:
5113 case -NFS4ERR_ADMIN_REVOKED:
5114 case -NFS4ERR_BAD_STATEID:
5115 case -NFS4ERR_STALE_STATEID:
5116 case -NFS4ERR_OLD_STATEID:
5117 case -NFS4ERR_OPENMODE:
5118 case -NFS4ERR_EXPIRED:
5119 return true;
5120 }
5121 return false;
5122}
5123
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005124static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005126 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005128 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005129 if (task->tk_status < 0) {
5130 struct nfs4_exception exception = {
5131 .inode = hdr->inode,
5132 .state = hdr->args.context->state,
5133 .stateid = &hdr->args.stateid,
5134 };
5135 task->tk_status = nfs4_async_handle_exception(task,
5136 server, task->tk_status, &exception);
5137 if (exception.retry) {
5138 rpc_restart_call_prepare(task);
5139 return -EAGAIN;
5140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005144 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146}
5147
Trond Myklebust5521abf2013-03-16 20:54:34 -04005148static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005149 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005150{
5151
5152 if (!nfs4_error_stateid_expired(task->tk_status) ||
5153 nfs4_stateid_is_current(&args->stateid,
5154 args->context,
5155 args->lock_context,
5156 FMODE_READ))
5157 return false;
5158 rpc_restart_call_prepare(task);
5159 return true;
5160}
5161
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005162static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005163{
5164
5165 dprintk("--> %s\n", __func__);
5166
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005167 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005168 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005169 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005170 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005171 if (task->tk_status > 0)
5172 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005173 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5174 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005175}
5176
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005177static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5178 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005180 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005181 if (!hdr->pgio_done_cb)
5182 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005183 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005184 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185}
5186
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005187static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5188 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005189{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005190 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005191 &hdr->args.seq_args,
5192 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005193 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005194 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005195 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5196 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005197 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005198 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005199 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005200 return -EIO;
5201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202}
5203
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005204static int nfs4_write_done_cb(struct rpc_task *task,
5205 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005207 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005208
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005209 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005210 if (task->tk_status < 0) {
5211 struct nfs4_exception exception = {
5212 .inode = hdr->inode,
5213 .state = hdr->args.context->state,
5214 .stateid = &hdr->args.stateid,
5215 };
5216 task->tk_status = nfs4_async_handle_exception(task,
5217 NFS_SERVER(inode), task->tk_status,
5218 &exception);
5219 if (exception.retry) {
5220 rpc_restart_call_prepare(task);
5221 return -EAGAIN;
5222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005224 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005225 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005226 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005227 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229}
5230
Trond Myklebust5521abf2013-03-16 20:54:34 -04005231static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005232 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005233{
5234
5235 if (!nfs4_error_stateid_expired(task->tk_status) ||
5236 nfs4_stateid_is_current(&args->stateid,
5237 args->context,
5238 args->lock_context,
5239 FMODE_WRITE))
5240 return false;
5241 rpc_restart_call_prepare(task);
5242 return true;
5243}
5244
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005245static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005246{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005247 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005248 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005249 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005250 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005251 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5252 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005253}
5254
Trond Myklebust5a37f852012-04-28 14:55:16 -04005255static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005256bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005257{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005258 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005259 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005260 return false;
5261 /* Otherwise, request attributes if and only if we don't hold
5262 * a delegation
5263 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005264 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005265}
Fred Isamana69aef12011-03-03 15:13:47 +00005266
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005267static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005268 struct rpc_message *msg,
5269 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005271 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005272
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005273 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5274 hdr->args.bitmask = NULL;
5275 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005276 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005277 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005278
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005279 if (!hdr->pgio_done_cb)
5280 hdr->pgio_done_cb = nfs4_write_done_cb;
5281 hdr->res.server = server;
5282 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005284 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005285 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005286 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287}
5288
Fred Isaman0b7c0152012-04-20 14:47:39 -04005289static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5290{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005291 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005292 &data->args.seq_args,
5293 &data->res.seq_res,
5294 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295}
5296
Fred Isaman0b7c0152012-04-20 14:47:39 -04005297static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005300
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005301 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005302 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5303 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005304 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005305 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308}
5309
Fred Isaman0b7c0152012-04-20 14:47:39 -04005310static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005311{
5312 if (!nfs4_sequence_done(task, &data->res.seq_res))
5313 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005314 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005315}
5316
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005317static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5318 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005320 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005321
Fred Isaman0b7c0152012-04-20 14:47:39 -04005322 if (data->commit_done_cb == NULL)
5323 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005324 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005325 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005326 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005327 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328}
5329
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005330static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5331 struct nfs_commitres *res)
5332{
5333 struct inode *dst_inode = file_inode(dst);
5334 struct nfs_server *server = NFS_SERVER(dst_inode);
5335 struct rpc_message msg = {
5336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5337 .rpc_argp = args,
5338 .rpc_resp = res,
5339 };
5340
5341 args->fh = NFS_FH(dst_inode);
5342 return nfs4_call_sync(server->client, server, &msg,
5343 &args->seq_args, &res->seq_res, 1);
5344}
5345
5346int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5347{
5348 struct nfs_commitargs args = {
5349 .offset = offset,
5350 .count = count,
5351 };
5352 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5353 struct nfs4_exception exception = { };
5354 int status;
5355
5356 do {
5357 status = _nfs4_proc_commit(dst, &args, res);
5358 status = nfs4_handle_exception(dst_server, status, &exception);
5359 } while (exception.retry);
5360
5361 return status;
5362}
5363
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005364struct nfs4_renewdata {
5365 struct nfs_client *client;
5366 unsigned long timestamp;
5367};
5368
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369/*
5370 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5371 * standalone procedure for queueing an asynchronous RENEW.
5372 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005373static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005374{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005375 struct nfs4_renewdata *data = calldata;
5376 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005377
Elena Reshetova212bf412017-10-20 12:53:38 +03005378 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005379 nfs4_schedule_state_renewal(clp);
5380 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005381 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005382}
5383
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005384static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005386 struct nfs4_renewdata *data = calldata;
5387 struct nfs_client *clp = data->client;
5388 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005390 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005391 switch (task->tk_status) {
5392 case 0:
5393 break;
5394 case -NFS4ERR_LEASE_MOVED:
5395 nfs4_schedule_lease_moved_recovery(clp);
5396 break;
5397 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005398 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005399 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5400 return;
5401 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005402 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005403 return;
5404 }
5405 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005407 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408}
5409
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005410static const struct rpc_call_ops nfs4_renew_ops = {
5411 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005412 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005413};
5414
NeilBrowna52458b2018-12-03 11:30:31 +11005415static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
5417 struct rpc_message msg = {
5418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5419 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005420 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005422 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005424 if (renew_flags == 0)
5425 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005426 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005427 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005428 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005429 if (data == NULL) {
5430 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005431 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005432 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005433 data->client = clp;
5434 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005435 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005436 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437}
5438
NeilBrowna52458b2018-12-03 11:30:31 +11005439static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440{
5441 struct rpc_message msg = {
5442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5443 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005444 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 };
5446 unsigned long now = jiffies;
5447 int status;
5448
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005449 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 if (status < 0)
5451 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005452 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 return 0;
5454}
5455
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005456static inline int nfs4_server_supports_acls(struct nfs_server *server)
5457{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005458 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005459}
5460
Trond Myklebust21f498c2012-08-24 10:59:25 -04005461/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5462 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005463 * the stack.
5464 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005465#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005466
Neil Hormane9e3d722011-03-04 19:26:03 -05005467static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005468 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005469{
5470 struct page *newpage, **spages;
5471 int rc = 0;
5472 size_t len;
5473 spages = pages;
5474
5475 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005476 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005477 newpage = alloc_page(GFP_KERNEL);
5478
5479 if (newpage == NULL)
5480 goto unwind;
5481 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005482 buf += len;
5483 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005484 *pages++ = newpage;
5485 rc++;
5486 } while (buflen != 0);
5487
5488 return rc;
5489
5490unwind:
5491 for(; rc > 0; rc--)
5492 __free_page(spages[rc-1]);
5493 return -ENOMEM;
5494}
5495
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005496struct nfs4_cached_acl {
5497 int cached;
5498 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005499 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005500};
5501
5502static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005503{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005504 struct nfs_inode *nfsi = NFS_I(inode);
5505
5506 spin_lock(&inode->i_lock);
5507 kfree(nfsi->nfs4_acl);
5508 nfsi->nfs4_acl = acl;
5509 spin_unlock(&inode->i_lock);
5510}
5511
5512static void nfs4_zap_acl_attr(struct inode *inode)
5513{
5514 nfs4_set_cached_acl(inode, NULL);
5515}
5516
5517static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5518{
5519 struct nfs_inode *nfsi = NFS_I(inode);
5520 struct nfs4_cached_acl *acl;
5521 int ret = -ENOENT;
5522
5523 spin_lock(&inode->i_lock);
5524 acl = nfsi->nfs4_acl;
5525 if (acl == NULL)
5526 goto out;
5527 if (buf == NULL) /* user is just asking for length */
5528 goto out_len;
5529 if (acl->cached == 0)
5530 goto out;
5531 ret = -ERANGE; /* see getxattr(2) man page */
5532 if (acl->len > buflen)
5533 goto out;
5534 memcpy(buf, acl->data, acl->len);
5535out_len:
5536 ret = acl->len;
5537out:
5538 spin_unlock(&inode->i_lock);
5539 return ret;
5540}
5541
Sachin Prabhu5794d212012-04-17 14:36:40 +01005542static 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 +00005543{
5544 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005545 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005546
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005547 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005548 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005549 if (acl == NULL)
5550 goto out;
5551 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005552 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005553 } else {
5554 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5555 if (acl == NULL)
5556 goto out;
5557 acl->cached = 0;
5558 }
5559 acl->len = acl_len;
5560out:
5561 nfs4_set_cached_acl(inode, acl);
5562}
5563
Andy Adamsonbf118a32011-12-07 11:55:27 -05005564/*
5565 * The getxattr API returns the required buffer length when called with a
5566 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5567 * the required buf. On a NULL buf, we send a page of data to the server
5568 * guessing that the ACL request can be serviced by a page. If so, we cache
5569 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5570 * the cache. If not so, we throw away the page, and cache the required
5571 * length. The next getxattr call will then produce another round trip to
5572 * the server, this time with the input buf of the required size.
5573 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005574static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005575{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005576 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005577 struct nfs_getaclargs args = {
5578 .fh = NFS_FH(inode),
5579 .acl_pages = pages,
5580 .acl_len = buflen,
5581 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005582 struct nfs_getaclres res = {
5583 .acl_len = buflen,
5584 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005585 struct rpc_message msg = {
5586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5587 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005588 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005589 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005590 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005591 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005592
Trond Myklebust21f498c2012-08-24 10:59:25 -04005593 if (npages > ARRAY_SIZE(pages))
5594 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005595
Andy Adamsonbf118a32011-12-07 11:55:27 -05005596 for (i = 0; i < npages; i++) {
5597 pages[i] = alloc_page(GFP_KERNEL);
5598 if (!pages[i])
5599 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005600 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005601
5602 /* for decoding across pages */
5603 res.acl_scratch = alloc_page(GFP_KERNEL);
5604 if (!res.acl_scratch)
5605 goto out_free;
5606
Andy Adamsonbf118a32011-12-07 11:55:27 -05005607 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005608
Peng Taode040be2012-01-10 22:42:47 +08005609 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005610 __func__, buf, buflen, npages, args.acl_len);
5611 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5612 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005613 if (ret)
5614 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005615
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005616 /* Handle the case where the passed-in buffer is too short */
5617 if (res.acl_flags & NFS4_ACL_TRUNC) {
5618 /* Did the user only issue a request for the acl length? */
5619 if (buf == NULL)
5620 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005621 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005622 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005623 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005624 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005625 if (buf) {
5626 if (res.acl_len > buflen) {
5627 ret = -ERANGE;
5628 goto out_free;
5629 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005630 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005631 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005632out_ok:
5633 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005634out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005635 for (i = 0; i < npages; i++)
5636 if (pages[i])
5637 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005638 if (res.acl_scratch)
5639 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005640 return ret;
5641}
5642
Trond Myklebust16b42892006-08-24 12:27:15 -04005643static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5644{
Trond Myklebust0688e642019-04-07 13:59:09 -04005645 struct nfs4_exception exception = {
5646 .interruptible = true,
5647 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005648 ssize_t ret;
5649 do {
5650 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005651 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005652 if (ret >= 0)
5653 break;
5654 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5655 } while (exception.retry);
5656 return ret;
5657}
5658
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005659static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5660{
5661 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005662 int ret;
5663
5664 if (!nfs4_server_supports_acls(server))
5665 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005666 ret = nfs_revalidate_inode(server, inode);
5667 if (ret < 0)
5668 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005669 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5670 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005671 ret = nfs4_read_cached_acl(inode, buf, buflen);
5672 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005673 /* -ENOENT is returned if there is no ACL or if there is an ACL
5674 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005675 return ret;
5676 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005677}
5678
Trond Myklebust16b42892006-08-24 12:27:15 -04005679static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005680{
5681 struct nfs_server *server = NFS_SERVER(inode);
5682 struct page *pages[NFS4ACL_MAXPAGES];
5683 struct nfs_setaclargs arg = {
5684 .fh = NFS_FH(inode),
5685 .acl_pages = pages,
5686 .acl_len = buflen,
5687 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005688 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005689 struct rpc_message msg = {
5690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5691 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005692 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005693 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005694 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005695 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005696
5697 if (!nfs4_server_supports_acls(server))
5698 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005699 if (npages > ARRAY_SIZE(pages))
5700 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005701 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005702 if (i < 0)
5703 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005704 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005705 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005706
5707 /*
5708 * Free each page after tx, so the only ref left is
5709 * held by the network stack
5710 */
5711 for (; i > 0; i--)
5712 put_page(pages[i-1]);
5713
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005714 /*
5715 * Acl update can result in inode attribute update.
5716 * so mark the attribute cache invalid.
5717 */
5718 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005719 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005720 | NFS_INO_INVALID_CTIME
5721 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005722 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005723 nfs_access_zap_cache(inode);
5724 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005725 return ret;
5726}
5727
Trond Myklebust16b42892006-08-24 12:27:15 -04005728static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5729{
5730 struct nfs4_exception exception = { };
5731 int err;
5732 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005733 err = __nfs4_proc_set_acl(inode, buf, buflen);
5734 trace_nfs4_set_acl(inode, err);
5735 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005736 &exception);
5737 } while (exception.retry);
5738 return err;
5739}
5740
David Quigleyaa9c2662013-05-22 12:50:44 -04005741#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5742static int _nfs4_get_security_label(struct inode *inode, void *buf,
5743 size_t buflen)
5744{
5745 struct nfs_server *server = NFS_SERVER(inode);
5746 struct nfs_fattr fattr;
5747 struct nfs4_label label = {0, 0, buflen, buf};
5748
5749 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005750 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005751 .fh = NFS_FH(inode),
5752 .bitmask = bitmask,
5753 };
5754 struct nfs4_getattr_res res = {
5755 .fattr = &fattr,
5756 .label = &label,
5757 .server = server,
5758 };
5759 struct rpc_message msg = {
5760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005761 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005762 .rpc_resp = &res,
5763 };
5764 int ret;
5765
5766 nfs_fattr_init(&fattr);
5767
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005768 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005769 if (ret)
5770 return ret;
5771 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5772 return -ENOENT;
5773 if (buflen < label.len)
5774 return -ERANGE;
5775 return 0;
5776}
5777
5778static int nfs4_get_security_label(struct inode *inode, void *buf,
5779 size_t buflen)
5780{
Trond Myklebust0688e642019-04-07 13:59:09 -04005781 struct nfs4_exception exception = {
5782 .interruptible = true,
5783 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005784 int err;
5785
5786 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5787 return -EOPNOTSUPP;
5788
5789 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005790 err = _nfs4_get_security_label(inode, buf, buflen);
5791 trace_nfs4_get_security_label(inode, err);
5792 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005793 &exception);
5794 } while (exception.retry);
5795 return err;
5796}
5797
5798static int _nfs4_do_set_security_label(struct inode *inode,
5799 struct nfs4_label *ilabel,
5800 struct nfs_fattr *fattr,
5801 struct nfs4_label *olabel)
5802{
5803
5804 struct iattr sattr = {0};
5805 struct nfs_server *server = NFS_SERVER(inode);
5806 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005807 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005808 .fh = NFS_FH(inode),
5809 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005810 .server = server,
5811 .bitmask = bitmask,
5812 .label = ilabel,
5813 };
5814 struct nfs_setattrres res = {
5815 .fattr = fattr,
5816 .label = olabel,
5817 .server = server,
5818 };
5819 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005820 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5821 .rpc_argp = &arg,
5822 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005823 };
5824 int status;
5825
Jeff Layton12207f62013-11-01 10:49:32 -04005826 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005827
Jeff Layton12207f62013-11-01 10:49:32 -04005828 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005829 if (status)
5830 dprintk("%s failed: %d\n", __func__, status);
5831
5832 return status;
5833}
5834
5835static int nfs4_do_set_security_label(struct inode *inode,
5836 struct nfs4_label *ilabel,
5837 struct nfs_fattr *fattr,
5838 struct nfs4_label *olabel)
5839{
5840 struct nfs4_exception exception = { };
5841 int err;
5842
5843 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005844 err = _nfs4_do_set_security_label(inode, ilabel,
5845 fattr, olabel);
5846 trace_nfs4_set_security_label(inode, err);
5847 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005848 &exception);
5849 } while (exception.retry);
5850 return err;
5851}
5852
5853static int
Al Viro59301222016-05-27 10:19:30 -04005854nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005855{
5856 struct nfs4_label ilabel, *olabel = NULL;
5857 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005858 int status;
5859
5860 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5861 return -EOPNOTSUPP;
5862
5863 nfs_fattr_init(&fattr);
5864
5865 ilabel.pi = 0;
5866 ilabel.lfs = 0;
5867 ilabel.label = (char *)buf;
5868 ilabel.len = buflen;
5869
David Quigleyaa9c2662013-05-22 12:50:44 -04005870 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5871 if (IS_ERR(olabel)) {
5872 status = -PTR_ERR(olabel);
5873 goto out;
5874 }
5875
5876 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5877 if (status == 0)
5878 nfs_setsecurity(inode, &fattr, olabel);
5879
5880 nfs4_label_free(olabel);
5881out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005882 return status;
5883}
5884#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5885
5886
Chuck Leverf0920752012-05-21 22:45:41 -04005887static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5888 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005889{
5890 __be32 verf[2];
5891
Chuck Lever2c820d92012-05-21 22:45:33 -04005892 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5893 /* An impossible timestamp guarantees this value
5894 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005895 verf[0] = cpu_to_be32(U32_MAX);
5896 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005897 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005898 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005899 u64 ns = ktime_to_ns(nn->boot_time);
5900
5901 verf[0] = cpu_to_be32(ns >> 32);
5902 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005903 }
Chuck Levercd937102012-03-02 17:14:31 -05005904 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5905}
5906
Jeff Laytona3192682015-06-09 19:43:59 -04005907static int
5908nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005909{
Jeff Laytona3192682015-06-09 19:43:59 -04005910 size_t len;
5911 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005912
Trond Myklebustceb3a162015-01-03 15:16:04 -05005913 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005914 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005915
Jeff Laytona3192682015-06-09 19:43:59 -04005916 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005917 len = 14 +
5918 strlen(clp->cl_rpcclient->cl_nodename) +
5919 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005920 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005921 1;
5922 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005923 if (nfs4_client_id_uniquifier[0] != '\0')
5924 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005925 if (len > NFS4_OPAQUE_LIMIT + 1)
5926 return -EINVAL;
5927
5928 /*
5929 * Since this string is allocated at mount time, and held until the
5930 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5931 * about a memory-reclaim deadlock.
5932 */
5933 str = kmalloc(len, GFP_KERNEL);
5934 if (!str)
5935 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005936
Chuck Levere984a552012-09-14 17:24:21 -04005937 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005938 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005939 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005940 clp->cl_rpcclient->cl_nodename,
5941 nfs4_client_id_uniquifier,
5942 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005943 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005944 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005945 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005946 clp->cl_rpcclient->cl_nodename,
5947 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005948 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005949 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005950
Jeff Laytona3192682015-06-09 19:43:59 -04005951 clp->cl_owner_id = str;
5952 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005953}
5954
Jeff Layton873e3852015-06-09 19:44:00 -04005955static int
5956nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005957{
Jeff Layton873e3852015-06-09 19:44:00 -04005958 size_t len;
5959 char *str;
5960
5961 len = 10 + 10 + 1 + 10 + 1 +
5962 strlen(nfs4_client_id_uniquifier) + 1 +
5963 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5964
5965 if (len > NFS4_OPAQUE_LIMIT + 1)
5966 return -EINVAL;
5967
5968 /*
5969 * Since this string is allocated at mount time, and held until the
5970 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5971 * about a memory-reclaim deadlock.
5972 */
5973 str = kmalloc(len, GFP_KERNEL);
5974 if (!str)
5975 return -ENOMEM;
5976
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005977 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005978 clp->rpc_ops->version, clp->cl_minorversion,
5979 nfs4_client_id_uniquifier,
5980 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005981 clp->cl_owner_id = str;
5982 return 0;
5983}
5984
5985static int
5986nfs4_init_uniform_client_string(struct nfs_client *clp)
5987{
Jeff Layton873e3852015-06-09 19:44:00 -04005988 size_t len;
5989 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005990
5991 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005992 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005993
5994 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005995 return nfs4_init_uniquifier_client_string(clp);
5996
5997 len = 10 + 10 + 1 + 10 + 1 +
5998 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5999
6000 if (len > NFS4_OPAQUE_LIMIT + 1)
6001 return -EINVAL;
6002
6003 /*
6004 * Since this string is allocated at mount time, and held until the
6005 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6006 * about a memory-reclaim deadlock.
6007 */
6008 str = kmalloc(len, GFP_KERNEL);
6009 if (!str)
6010 return -ENOMEM;
6011
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006012 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04006013 clp->rpc_ops->version, clp->cl_minorversion,
6014 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006015 clp->cl_owner_id = str;
6016 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006017}
6018
Chuck Lever706cb8d2014-03-12 12:51:47 -04006019/*
6020 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6021 * services. Advertise one based on the address family of the
6022 * clientaddr.
6023 */
6024static unsigned int
6025nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6026{
6027 if (strchr(clp->cl_ipaddr, ':') != NULL)
6028 return scnprintf(buf, len, "tcp6");
6029 else
6030 return scnprintf(buf, len, "tcp");
6031}
6032
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006033static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6034{
6035 struct nfs4_setclientid *sc = calldata;
6036
6037 if (task->tk_status == 0)
6038 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6039}
6040
6041static const struct rpc_call_ops nfs4_setclientid_ops = {
6042 .rpc_call_done = nfs4_setclientid_done,
6043};
6044
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006045/**
6046 * nfs4_proc_setclientid - Negotiate client ID
6047 * @clp: state data structure
6048 * @program: RPC program for NFSv4 callback service
6049 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006050 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006051 * @res: where to place the result
6052 *
6053 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6054 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006055int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006056 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006057 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058{
6059 nfs4_verifier sc_verifier;
6060 struct nfs4_setclientid setclientid = {
6061 .sc_verifier = &sc_verifier,
6062 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006063 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 };
6065 struct rpc_message msg = {
6066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6067 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006068 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006069 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006071 struct rpc_task_setup task_setup_data = {
6072 .rpc_client = clp->cl_rpcclient,
6073 .rpc_message = &msg,
6074 .callback_ops = &nfs4_setclientid_ops,
6075 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006076 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006077 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006078 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
Chuck Leverde734832012-07-11 16:30:50 -04006080 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006081 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006082
6083 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6084 status = nfs4_init_uniform_client_string(clp);
6085 else
Jeff Laytona3192682015-06-09 19:43:59 -04006086 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006087
6088 if (status)
6089 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006090
Chuck Leverde734832012-07-11 16:30:50 -04006091 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006092 setclientid.sc_netid_len =
6093 nfs4_init_callback_netid(clp,
6094 setclientid.sc_netid,
6095 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006096 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006097 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 clp->cl_ipaddr, port >> 8, port & 255);
6099
Jeff Layton3a6bb732015-06-09 19:43:57 -04006100 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006101 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006102 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006103
6104 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006105 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006106 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006107 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6108 put_rpccred(setclientid.sc_cred);
6109 }
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006110out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006111 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006112 dprintk("NFS reply setclientid: %d\n", status);
6113 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114}
6115
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006116/**
6117 * nfs4_proc_setclientid_confirm - Confirm client ID
6118 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006119 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006120 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006121 *
6122 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6123 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006124int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006125 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006126 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 struct rpc_message msg = {
6129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006130 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006131 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 int status;
6134
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006135 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6136 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6137 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006138 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6139 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006140 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006141 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 return status;
6143}
6144
Trond Myklebustfe650402006-01-03 09:55:18 +01006145struct nfs4_delegreturndata {
6146 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006147 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006148 struct nfs_fh fh;
6149 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006150 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006151 struct {
6152 struct nfs4_layoutreturn_args arg;
6153 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006154 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006155 u32 roc_barrier;
6156 bool roc;
6157 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006158 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006159 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006160 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006161};
6162
Trond Myklebustfe650402006-01-03 09:55:18 +01006163static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6164{
6165 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006166 struct nfs4_exception exception = {
6167 .inode = data->inode,
6168 .stateid = &data->stateid,
6169 };
Andy Adamson938e1012009-04-01 09:22:28 -04006170
Trond Myklebust14516c32010-07-31 14:29:06 -04006171 if (!nfs4_sequence_done(task, &data->res.seq_res))
6172 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006173
Trond Myklebustca8acf82013-08-13 10:36:56 -04006174 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006175
6176 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04006177 if (pnfs_roc_done(task, data->inode,
6178 &data->args.lr_args,
6179 &data->res.lr_res,
6180 &data->res.lr_ret) == -EAGAIN)
6181 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006182
Ricardo Labiaga79708862009-12-07 09:23:21 -05006183 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006184 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006185 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006186 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006187 case -NFS4ERR_ADMIN_REVOKED:
6188 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006189 case -NFS4ERR_EXPIRED:
6190 nfs4_free_revoked_stateid(data->res.server,
6191 data->args.stateid,
6192 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006193 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006194 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006195 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006196 task->tk_status = 0;
6197 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006198 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006199 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6200 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006201 if (data->args.bitmask) {
6202 data->args.bitmask = NULL;
6203 data->res.fattr = NULL;
6204 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006205 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006206 case -NFS4ERR_ACCESS:
6207 if (data->args.bitmask) {
6208 data->args.bitmask = NULL;
6209 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006210 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006211 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006212 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006213 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006214 task->tk_status = nfs4_async_handle_exception(task,
6215 data->res.server, task->tk_status,
6216 &exception);
6217 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006218 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006219 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006220 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006221 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006222 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006223out_restart:
6224 task->tk_status = 0;
6225 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006226}
6227
6228static void nfs4_delegreturn_release(void *calldata)
6229{
Peng Tao039b7562014-07-03 13:05:02 +08006230 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006231 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006232
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006233 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006234 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006235 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6236 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006237 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006238 nfs_iput_and_deactive(inode);
6239 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006240 kfree(calldata);
6241}
6242
Andy Adamson938e1012009-04-01 09:22:28 -04006243static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6244{
6245 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006246 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006247
6248 d_data = (struct nfs4_delegreturndata *)data;
6249
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006250 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08006251 return;
6252
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006253 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6254 if (lo && !pnfs_layout_is_valid(lo)) {
6255 d_data->args.lr_args = NULL;
6256 d_data->res.lr_res = NULL;
6257 }
6258
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006259 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006260 &d_data->args.seq_args,
6261 &d_data->res.seq_res,
6262 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006263}
Andy Adamson938e1012009-04-01 09:22:28 -04006264
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006265static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006266 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006267 .rpc_call_done = nfs4_delegreturn_done,
6268 .rpc_release = nfs4_delegreturn_release,
6269};
6270
NeilBrowna52458b2018-12-03 11:30:31 +11006271static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006272{
6273 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006274 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006275 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006276 struct rpc_message msg = {
6277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6278 .rpc_cred = cred,
6279 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006280 struct rpc_task_setup task_setup_data = {
6281 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006282 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006283 .callback_ops = &nfs4_delegreturn_ops,
6284 .flags = RPC_TASK_ASYNC,
6285 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006286 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006287
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006288 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006289 if (data == NULL)
6290 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006291 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006292
6293 nfs4_state_protect(server->nfs_client,
6294 NFS_SP4_MACH_CRED_CLEANUP,
6295 &task_setup_data.rpc_client, &msg);
6296
Trond Myklebustfe650402006-01-03 09:55:18 +01006297 data->args.fhandle = &data->fh;
6298 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006299 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006300 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006301 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006302 data->res.fattr = &data->fattr;
6303 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006304 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006305 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006306 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006307 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006308 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006309 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006310 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006311 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006312 if (data->lr.roc) {
6313 data->args.lr_args = &data->lr.arg;
6314 data->res.lr_res = &data->lr.res;
6315 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006316 } else if (data->lr.roc) {
6317 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6318 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006319 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006320
Trond Myklebustc970aa82007-07-14 15:39:59 -04006321 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006322 msg.rpc_argp = &data->args;
6323 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006324 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006325 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006326 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006327 if (!issync)
6328 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006329 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006330 if (status != 0)
6331 goto out;
6332 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006333out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006334 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006335 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336}
6337
NeilBrowna52458b2018-12-03 11:30:31 +11006338int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339{
6340 struct nfs_server *server = NFS_SERVER(inode);
6341 struct nfs4_exception exception = { };
6342 int err;
6343 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006344 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006345 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 switch (err) {
6347 case -NFS4ERR_STALE_STATEID:
6348 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 case 0:
6350 return 0;
6351 }
6352 err = nfs4_handle_exception(server, err, &exception);
6353 } while (exception.retry);
6354 return err;
6355}
6356
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6358{
6359 struct inode *inode = state->inode;
6360 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006361 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006362 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006364 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006366 struct nfs_lockt_res res = {
6367 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 };
6369 struct rpc_message msg = {
6370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006371 .rpc_argp = &arg,
6372 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 .rpc_cred = state->owner->so_cred,
6374 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 struct nfs4_lock_state *lsp;
6376 int status;
6377
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006378 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006379 status = nfs4_set_lock_state(state, request);
6380 if (status != 0)
6381 goto out;
6382 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006383 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006384 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006385 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006386 switch (status) {
6387 case 0:
6388 request->fl_type = F_UNLCK;
6389 break;
6390 case -NFS4ERR_DENIED:
6391 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006393 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006394 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006395out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 return status;
6397}
6398
6399static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6400{
Trond Myklebust0688e642019-04-07 13:59:09 -04006401 struct nfs4_exception exception = {
6402 .interruptible = true,
6403 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 int err;
6405
6406 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006407 err = _nfs4_proc_getlk(state, cmd, request);
6408 trace_nfs4_get_lock(request, state, cmd, err);
6409 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 &exception);
6411 } while (exception.retry);
6412 return err;
6413}
6414
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006415/*
6416 * Update the seqid of a lock stateid after receiving
6417 * NFS4ERR_OLD_STATEID
6418 */
6419static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6420 struct nfs4_lock_state *lsp)
6421{
6422 struct nfs4_state *state = lsp->ls_state;
6423 bool ret = false;
6424
6425 spin_lock(&state->state_lock);
6426 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6427 goto out;
6428 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6429 nfs4_stateid_seqid_inc(dst);
6430 else
6431 dst->seqid = lsp->ls_stateid.seqid;
6432 ret = true;
6433out:
6434 spin_unlock(&state->state_lock);
6435 return ret;
6436}
6437
6438static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6439 struct nfs4_lock_state *lsp)
6440{
6441 struct nfs4_state *state = lsp->ls_state;
6442 bool ret;
6443
6444 spin_lock(&state->state_lock);
6445 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6446 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6447 spin_unlock(&state->state_lock);
6448 return ret;
6449}
6450
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006451struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006452 struct nfs_locku_args arg;
6453 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006454 struct nfs4_lock_state *lsp;
6455 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006456 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006457 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006458 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006459 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006460};
6461
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006462static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6463 struct nfs_open_context *ctx,
6464 struct nfs4_lock_state *lsp,
6465 struct nfs_seqid *seqid)
6466{
6467 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006468 struct nfs4_state *state = lsp->ls_state;
6469 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006470
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006471 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006472 if (p == NULL)
6473 return NULL;
6474 p->arg.fh = NFS_FH(inode);
6475 p->arg.fl = &p->fl;
6476 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006477 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006478 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006479 /* Ensure we don't close file until we're done freeing locks! */
6480 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006481 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006482 locks_init_lock(&p->fl);
6483 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006484 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006485 spin_lock(&state->state_lock);
6486 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6487 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006488 return p;
6489}
6490
Trond Myklebust06f814a2006-01-03 09:55:07 +01006491static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006492{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006493 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006494 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006495 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006496 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006497 put_nfs_open_context(calldata->ctx);
6498 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006499}
6500
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006501static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006502{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006503 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006504 struct nfs4_exception exception = {
6505 .inode = calldata->lsp->ls_state->inode,
6506 .stateid = &calldata->arg.stateid,
6507 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006508
Trond Myklebust14516c32010-07-31 14:29:06 -04006509 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6510 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006511 switch (task->tk_status) {
6512 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006513 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006514 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006515 if (nfs4_update_lock_stateid(calldata->lsp,
6516 &calldata->res.stateid))
6517 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006518 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006519 case -NFS4ERR_ADMIN_REVOKED:
6520 case -NFS4ERR_EXPIRED:
6521 nfs4_free_revoked_stateid(calldata->server,
6522 &calldata->arg.stateid,
6523 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006524 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006525 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006526 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006527 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6528 calldata->lsp))
6529 rpc_restart_call_prepare(task);
6530 break;
6531 case -NFS4ERR_OLD_STATEID:
6532 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6533 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006534 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006535 break;
6536 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006537 task->tk_status = nfs4_async_handle_exception(task,
6538 calldata->server, task->tk_status,
6539 &exception);
6540 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006541 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006542 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006543 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006544}
6545
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006546static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006547{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006548 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006550 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6551 nfs_async_iocounter_wait(task, calldata->l_ctx))
6552 return;
6553
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006554 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006555 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006556 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006557 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006558 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006559 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006560 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006561 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006562 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006563 &calldata->res.seq_res,
6564 task) != 0)
6565 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006566 return;
6567out_no_action:
6568 task->tk_action = NULL;
6569out_wait:
6570 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571}
6572
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006573static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006574 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006575 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006576 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006577};
6578
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006579static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6580 struct nfs_open_context *ctx,
6581 struct nfs4_lock_state *lsp,
6582 struct nfs_seqid *seqid)
6583{
6584 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006585 struct rpc_message msg = {
6586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6587 .rpc_cred = ctx->cred,
6588 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006589 struct rpc_task_setup task_setup_data = {
6590 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006591 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006592 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006593 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006594 .flags = RPC_TASK_ASYNC,
6595 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006596
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006597 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6598 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6599
Frank Filz137d6ac2007-07-09 15:32:29 -07006600 /* Ensure this is an unlock - when canceling a lock, the
6601 * canceled lock is passed in, and it won't be an unlock.
6602 */
6603 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006604 if (fl->fl_flags & FL_CLOSE)
6605 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006606
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006607 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6608 if (data == NULL) {
6609 nfs_free_seqid(seqid);
6610 return ERR_PTR(-ENOMEM);
6611 }
6612
Anna Schumakerfba83f32018-05-04 16:22:50 -04006613 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006614 msg.rpc_argp = &data->arg;
6615 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006616 task_setup_data.callback_data = data;
6617 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006618}
6619
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6621{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006622 struct inode *inode = state->inode;
6623 struct nfs4_state_owner *sp = state->owner;
6624 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006625 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006626 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006627 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006628 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006629 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006630 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631
Trond Myklebust9b073572006-06-29 16:38:34 -04006632 status = nfs4_set_lock_state(state, request);
6633 /* Unlock _before_ we do the RPC call */
6634 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006635 /* Exclude nfs_delegation_claim_locks() */
6636 mutex_lock(&sp->so_delegreturn_mutex);
6637 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006638 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006639 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006640 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006641 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006642 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006643 }
6644 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006645 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006646 if (status != 0)
6647 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006648 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006649 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006650 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6651 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006652 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6653 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006654 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006655 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006656 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006657 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006658 status = PTR_ERR(task);
6659 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006660 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006661 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006662 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006663out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006664 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006665 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006666 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667}
6668
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006669struct nfs4_lockdata {
6670 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006671 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006672 struct nfs4_lock_state *lsp;
6673 struct nfs_open_context *ctx;
6674 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006675 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006676 int rpc_status;
6677 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006678 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006679};
6680
6681static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006682 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6683 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006684{
6685 struct nfs4_lockdata *p;
6686 struct inode *inode = lsp->ls_state->inode;
6687 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006688 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006689
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006690 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006691 if (p == NULL)
6692 return NULL;
6693
6694 p->arg.fh = NFS_FH(inode);
6695 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006696 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006697 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006698 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006699 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6700 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006701 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006702 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006703 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006704 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006705 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006706 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006707 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006708 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006709 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006710 locks_init_lock(&p->fl);
6711 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006712 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006713out_free_seqid:
6714 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006715out_free:
6716 kfree(p);
6717 return NULL;
6718}
6719
6720static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6721{
6722 struct nfs4_lockdata *data = calldata;
6723 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724
Harvey Harrison3110ff82008-05-02 13:42:44 -07006725 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006726 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006727 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006728 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006729 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006730 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006731 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006732 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006733 nfs4_stateid_copy(&data->arg.open_stateid,
6734 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006735 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006736 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006737 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006738 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006739 nfs4_stateid_copy(&data->arg.lock_stateid,
6740 &data->lsp->ls_stateid);
6741 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006742 if (!nfs4_valid_open_stateid(state)) {
6743 data->rpc_status = -EBADF;
6744 task->tk_action = NULL;
6745 goto out_release_open_seqid;
6746 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006747 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006748 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006749 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006750 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006751 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006752 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006753out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006754 nfs_release_seqid(data->arg.open_seqid);
6755out_release_lock_seqid:
6756 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006757out_wait:
6758 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006759 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006760}
6761
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006762static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6763{
6764 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006765 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006766
Harvey Harrison3110ff82008-05-02 13:42:44 -07006767 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006768
Trond Myklebust14516c32010-07-31 14:29:06 -04006769 if (!nfs4_sequence_done(task, &data->res.seq_res))
6770 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006771
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006772 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006773 switch (task->tk_status) {
6774 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006775 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006776 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006777 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006778 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006779 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006780 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006781 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006782 if (data->arg.new_lock_owner != 0) {
6783 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6784 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6785 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006786 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6787 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006788 break;
6789 case -NFS4ERR_BAD_STATEID:
6790 case -NFS4ERR_OLD_STATEID:
6791 case -NFS4ERR_STALE_STATEID:
6792 case -NFS4ERR_EXPIRED:
6793 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006794 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006795 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006796 goto out_restart;
6797 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006798 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006799 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006800 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006801out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006802 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006803 return;
6804out_restart:
6805 if (!data->cancelled)
6806 rpc_restart_call_prepare(task);
6807 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006808}
6809
6810static void nfs4_lock_release(void *calldata)
6811{
6812 struct nfs4_lockdata *data = calldata;
6813
Harvey Harrison3110ff82008-05-02 13:42:44 -07006814 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006815 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006816 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006817 struct rpc_task *task;
6818 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6819 data->arg.lock_seqid);
6820 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006821 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006822 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006823 } else
6824 nfs_free_seqid(data->arg.lock_seqid);
6825 nfs4_put_lock_state(data->lsp);
6826 put_nfs_open_context(data->ctx);
6827 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006828 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006829}
6830
6831static const struct rpc_call_ops nfs4_lock_ops = {
6832 .rpc_call_prepare = nfs4_lock_prepare,
6833 .rpc_call_done = nfs4_lock_done,
6834 .rpc_release = nfs4_lock_release,
6835};
6836
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006837static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6838{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006839 switch (error) {
6840 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006841 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006842 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006843 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006844 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006845 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006846 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006847 break;
6848 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006849 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006850 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006851 };
6852}
6853
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006854static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006855{
6856 struct nfs4_lockdata *data;
6857 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006858 struct rpc_message msg = {
6859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6860 .rpc_cred = state->owner->so_cred,
6861 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006862 struct rpc_task_setup task_setup_data = {
6863 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006864 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006865 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006866 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006867 .flags = RPC_TASK_ASYNC,
6868 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006869 int ret;
6870
Harvey Harrison3110ff82008-05-02 13:42:44 -07006871 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006872 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006873 fl->fl_u.nfs4_fl.owner,
6874 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006875 if (data == NULL)
6876 return -ENOMEM;
6877 if (IS_SETLKW(cmd))
6878 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006879 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6880 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006881 msg.rpc_argp = &data->arg;
6882 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006883 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006884 if (recovery_type > NFS_LOCK_NEW) {
6885 if (recovery_type == NFS_LOCK_RECLAIM)
6886 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006887 } else
6888 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006889 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006890 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006891 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006892 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006893 if (ret == 0) {
6894 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006895 if (ret)
6896 nfs4_handle_setlk_error(data->server, data->lsp,
6897 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006898 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006899 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006900 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006901 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006902 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006903 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904}
6905
6906static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6907{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006908 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006909 struct nfs4_exception exception = {
6910 .inode = state->inode,
6911 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006912 int err;
6913
6914 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006915 /* Cache the lock if possible... */
6916 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6917 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006918 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006919 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006920 break;
6921 nfs4_handle_exception(server, err, &exception);
6922 } while (exception.retry);
6923 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924}
6925
6926static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6927{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006928 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006929 struct nfs4_exception exception = {
6930 .inode = state->inode,
6931 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006932 int err;
6933
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006934 err = nfs4_set_lock_state(state, request);
6935 if (err != 0)
6936 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006937 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006938 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6939 return 0;
6940 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006941 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006942 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6943 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006944 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006945 switch (err) {
6946 default:
6947 goto out;
6948 case -NFS4ERR_GRACE:
6949 case -NFS4ERR_DELAY:
6950 nfs4_handle_exception(server, err, &exception);
6951 err = 0;
6952 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006953 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006954out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006955 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956}
6957
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006958#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006959static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6960{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006961 struct nfs4_lock_state *lsp;
6962 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006963
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006964 status = nfs4_set_lock_state(state, request);
6965 if (status != 0)
6966 return status;
6967 lsp = request->fl_u.nfs4_fl.owner;
6968 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6969 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6970 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006971 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006972}
6973#endif
6974
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6976{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006977 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006978 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006979 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006980 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
Trond Myklebust01c3b862006-06-29 16:38:39 -04006982 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006983 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006984 if (status < 0)
6985 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006986 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006987 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006988 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006989 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006990 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006991 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006992 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006993 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006994 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006995 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006996 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006997 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006998 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006999 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007000out:
7001 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 return status;
7003}
7004
7005static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7006{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007007 struct nfs4_exception exception = {
7008 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007009 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007010 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007011 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 int err;
7013
7014 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007015 err = _nfs4_proc_setlk(state, cmd, request);
7016 if (err == -NFS4ERR_DENIED)
7017 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007019 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 } while (exception.retry);
7021 return err;
7022}
7023
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007024#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7025#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7026
7027static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007028nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7029 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007030{
7031 int status = -ERESTARTSYS;
7032 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7033
7034 while(!signalled()) {
7035 status = nfs4_proc_setlk(state, cmd, request);
7036 if ((status != -EAGAIN) || IS_SETLK(cmd))
7037 break;
7038 freezable_schedule_timeout_interruptible(timeout);
7039 timeout *= 2;
7040 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7041 status = -ERESTARTSYS;
7042 }
7043 return status;
7044}
7045
Jeff Laytona1d617d82016-09-17 18:17:39 -04007046#ifdef CONFIG_NFS_V4_1
7047struct nfs4_lock_waiter {
7048 struct task_struct *task;
7049 struct inode *inode;
7050 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007051};
7052
7053static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007054nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007055{
7056 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007057 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007058
Jeff Layton57174592018-03-18 08:37:03 -04007059 /* NULL key means to wake up everyone */
7060 if (key) {
7061 struct cb_notify_lock_args *cbnl = key;
7062 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7063 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007064
Jeff Layton57174592018-03-18 08:37:03 -04007065 /* Only wake if the callback was for the same owner. */
7066 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7067 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007068
Jeff Layton57174592018-03-18 08:37:03 -04007069 /* Make sure it's for the right inode */
7070 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7071 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007072 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007073
7074 /* override "private" so we can use default_wake_function */
7075 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007076 ret = woken_wake_function(wait, mode, flags, key);
7077 if (ret)
7078 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007079 wait->private = waiter;
7080 return ret;
7081}
7082
7083static int
7084nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7085{
7086 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007087 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7088 struct nfs_server *server = NFS_SERVER(state->inode);
7089 struct nfs_client *clp = server->nfs_client;
7090 wait_queue_head_t *q = &clp->cl_lock_waitq;
7091 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7092 .id = lsp->ls_seqid.owner_id,
7093 .s_dev = server->s_dev };
7094 struct nfs4_lock_waiter waiter = { .task = current,
7095 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007096 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007097 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007098
7099 /* Don't bother with waitqueue if we don't expect a callback */
7100 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7101 return nfs4_retry_setlk_simple(state, cmd, request);
7102
7103 init_wait(&wait);
7104 wait.private = &waiter;
7105 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007106
7107 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007108 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007109 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007110 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7111 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007112 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007113 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007114
7115 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007116 freezer_do_not_count();
7117 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7118 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007119 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007120 }
7121
Jeff Laytona1d617d82016-09-17 18:17:39 -04007122 return status;
7123}
7124#else /* !CONFIG_NFS_V4_1 */
7125static inline int
7126nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7127{
7128 return nfs4_retry_setlk_simple(state, cmd, request);
7129}
7130#endif
7131
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132static int
7133nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7134{
7135 struct nfs_open_context *ctx;
7136 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 int status;
7138
7139 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007140 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 state = ctx->state;
7142
Trond Myklebustd9531262009-07-21 19:22:38 -04007143 if (IS_GETLK(cmd)) {
7144 if (state != NULL)
7145 return nfs4_proc_getlk(state, F_GETLK, request);
7146 return 0;
7147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148
7149 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7150 return -EINVAL;
7151
Trond Myklebustd9531262009-07-21 19:22:38 -04007152 if (request->fl_type == F_UNLCK) {
7153 if (state != NULL)
7154 return nfs4_proc_unlck(state, cmd, request);
7155 return 0;
7156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157
Trond Myklebustd9531262009-07-21 19:22:38 -04007158 if (state == NULL)
7159 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007160
7161 if ((request->fl_flags & FL_POSIX) &&
7162 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7163 return -ENOLCK;
7164
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007165 /*
7166 * Don't rely on the VFS having checked the file open mode,
7167 * since it won't do this for flock() locks.
7168 */
7169 switch (request->fl_type) {
7170 case F_RDLCK:
7171 if (!(filp->f_mode & FMODE_READ))
7172 return -EBADF;
7173 break;
7174 case F_WRLCK:
7175 if (!(filp->f_mode & FMODE_WRITE))
7176 return -EBADF;
7177 }
7178
Jeff Layton1ea67db2016-09-17 18:17:37 -04007179 status = nfs4_set_lock_state(state, request);
7180 if (status != 0)
7181 return status;
7182
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007183 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184}
7185
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007186int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007187{
7188 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007189 int err;
7190
7191 err = nfs4_set_lock_state(state, fl);
7192 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007193 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007194 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007195 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007196}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007197
Trond Myklebustcf470c32012-03-07 13:49:12 -05007198struct nfs_release_lockowner_data {
7199 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007200 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007201 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007202 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007203 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007204};
7205
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007206static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7207{
7208 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007209 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007210 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7211 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007212 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007213 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007214}
7215
7216static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7217{
7218 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007219 struct nfs_server *server = data->server;
7220
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007221 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007222
7223 switch (task->tk_status) {
7224 case 0:
7225 renew_lease(server, data->timestamp);
7226 break;
7227 case -NFS4ERR_STALE_CLIENTID:
7228 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007229 nfs4_schedule_lease_recovery(server->nfs_client);
7230 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007231 case -NFS4ERR_LEASE_MOVED:
7232 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007233 if (nfs4_async_handle_error(task, server,
7234 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007235 rpc_restart_call_prepare(task);
7236 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007237}
7238
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007239static void nfs4_release_lockowner_release(void *calldata)
7240{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007241 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007242 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007243 kfree(calldata);
7244}
7245
Trond Myklebust17280172012-03-11 13:11:00 -04007246static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007247 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7248 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007249 .rpc_release = nfs4_release_lockowner_release,
7250};
7251
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007252static void
7253nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007254{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007255 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007256 struct rpc_message msg = {
7257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7258 };
7259
7260 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007261 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007262
Trond Myklebustcf470c32012-03-07 13:49:12 -05007263 data = kmalloc(sizeof(*data), GFP_NOFS);
7264 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007265 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007266 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007267 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007268 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7269 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7270 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007271
Trond Myklebustcf470c32012-03-07 13:49:12 -05007272 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007273 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007274 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007275 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007276}
7277
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007278#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7279
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007280static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007281 struct dentry *unused, struct inode *inode,
7282 const char *key, const void *buf,
7283 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007284{
Al Viro59301222016-05-27 10:19:30 -04007285 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007286}
7287
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007288static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007289 struct dentry *unused, struct inode *inode,
7290 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007291{
Al Virob2968212016-04-10 20:48:24 -04007292 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007293}
7294
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007295static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007296{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007297 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007298}
7299
David Quigleyc9bccef2013-05-22 12:50:45 -04007300#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007301
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007302static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007303 struct dentry *unused, struct inode *inode,
7304 const char *key, const void *buf,
7305 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007306{
7307 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007308 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007309
7310 return -EOPNOTSUPP;
7311}
7312
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007313static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007314 struct dentry *unused, struct inode *inode,
7315 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007316{
7317 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007318 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007319 return -EOPNOTSUPP;
7320}
7321
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007322static ssize_t
7323nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007324{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007325 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007326
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007327 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7328 len = security_inode_listsecurity(inode, list, list_len);
7329 if (list_len && len > list_len)
7330 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007331 }
7332 return len;
7333}
7334
7335static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7336 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007337 .get = nfs4_xattr_get_nfs4_label,
7338 .set = nfs4_xattr_set_nfs4_label,
7339};
David Quigleyc9bccef2013-05-22 12:50:45 -04007340
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007341#else
7342
7343static ssize_t
7344nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7345{
7346 return 0;
7347}
7348
7349#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007350
Andy Adamson533eb462011-06-13 18:25:56 -04007351/*
7352 * nfs_fhget will use either the mounted_on_fileid or the fileid
7353 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007354static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7355{
Andy Adamson533eb462011-06-13 18:25:56 -04007356 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7357 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7358 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007359 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007360 return;
7361
7362 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007363 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007364 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7365 fattr->nlink = 2;
7366}
7367
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007368static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7369 const struct qstr *name,
7370 struct nfs4_fs_locations *fs_locations,
7371 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007372{
7373 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007374 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007375 struct nfs4_fs_locations_arg args = {
7376 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007377 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007378 .page = page,
7379 .bitmask = bitmask,
7380 };
Benny Halevy22958462009-04-01 09:22:02 -04007381 struct nfs4_fs_locations_res res = {
7382 .fs_locations = fs_locations,
7383 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007384 struct rpc_message msg = {
7385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7386 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007387 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007388 };
7389 int status;
7390
Harvey Harrison3110ff82008-05-02 13:42:44 -07007391 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007392
Chuck Leverc05cefc2017-11-05 15:45:22 -05007393 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7394 bitmask[1] = nfs4_fattr_bitmap[1];
7395
Andy Adamson533eb462011-06-13 18:25:56 -04007396 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7397 * is not supported */
7398 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007399 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007400 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007401 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007402
Trond Myklebustc228fd32007-01-13 02:28:11 -05007403 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007404 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007405 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007406 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007407 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007408 return status;
7409}
7410
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007411int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7412 const struct qstr *name,
7413 struct nfs4_fs_locations *fs_locations,
7414 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007415{
Trond Myklebust0688e642019-04-07 13:59:09 -04007416 struct nfs4_exception exception = {
7417 .interruptible = true,
7418 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007419 int err;
7420 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007421 err = _nfs4_proc_fs_locations(client, dir, name,
7422 fs_locations, page);
7423 trace_nfs4_get_fs_locations(dir, name, err);
7424 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007425 &exception);
7426 } while (exception.retry);
7427 return err;
7428}
7429
Chuck Leverb03d7352013-10-17 14:12:50 -04007430/*
7431 * This operation also signals the server that this client is
7432 * performing migration recovery. The server can stop returning
7433 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7434 * appended to this compound to identify the client ID which is
7435 * performing recovery.
7436 */
7437static int _nfs40_proc_get_locations(struct inode *inode,
7438 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007439 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007440{
7441 struct nfs_server *server = NFS_SERVER(inode);
7442 struct rpc_clnt *clnt = server->client;
7443 u32 bitmask[2] = {
7444 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7445 };
7446 struct nfs4_fs_locations_arg args = {
7447 .clientid = server->nfs_client->cl_clientid,
7448 .fh = NFS_FH(inode),
7449 .page = page,
7450 .bitmask = bitmask,
7451 .migration = 1, /* skip LOOKUP */
7452 .renew = 1, /* append RENEW */
7453 };
7454 struct nfs4_fs_locations_res res = {
7455 .fs_locations = locations,
7456 .migration = 1,
7457 .renew = 1,
7458 };
7459 struct rpc_message msg = {
7460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7461 .rpc_argp = &args,
7462 .rpc_resp = &res,
7463 .rpc_cred = cred,
7464 };
7465 unsigned long now = jiffies;
7466 int status;
7467
7468 nfs_fattr_init(&locations->fattr);
7469 locations->server = server;
7470 locations->nlocations = 0;
7471
Anna Schumakerfba83f32018-05-04 16:22:50 -04007472 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007473 status = nfs4_call_sync_sequence(clnt, server, &msg,
7474 &args.seq_args, &res.seq_res);
7475 if (status)
7476 return status;
7477
7478 renew_lease(server, now);
7479 return 0;
7480}
7481
7482#ifdef CONFIG_NFS_V4_1
7483
7484/*
7485 * This operation also signals the server that this client is
7486 * performing migration recovery. The server can stop asserting
7487 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7488 * performing this operation is identified in the SEQUENCE
7489 * operation in this compound.
7490 *
7491 * When the client supports GETATTR(fs_locations_info), it can
7492 * be plumbed in here.
7493 */
7494static int _nfs41_proc_get_locations(struct inode *inode,
7495 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007496 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007497{
7498 struct nfs_server *server = NFS_SERVER(inode);
7499 struct rpc_clnt *clnt = server->client;
7500 u32 bitmask[2] = {
7501 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7502 };
7503 struct nfs4_fs_locations_arg args = {
7504 .fh = NFS_FH(inode),
7505 .page = page,
7506 .bitmask = bitmask,
7507 .migration = 1, /* skip LOOKUP */
7508 };
7509 struct nfs4_fs_locations_res res = {
7510 .fs_locations = locations,
7511 .migration = 1,
7512 };
7513 struct rpc_message msg = {
7514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7515 .rpc_argp = &args,
7516 .rpc_resp = &res,
7517 .rpc_cred = cred,
7518 };
7519 int status;
7520
7521 nfs_fattr_init(&locations->fattr);
7522 locations->server = server;
7523 locations->nlocations = 0;
7524
Anna Schumakerfba83f32018-05-04 16:22:50 -04007525 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007526 status = nfs4_call_sync_sequence(clnt, server, &msg,
7527 &args.seq_args, &res.seq_res);
7528 if (status == NFS4_OK &&
7529 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7530 status = -NFS4ERR_LEASE_MOVED;
7531 return status;
7532}
7533
7534#endif /* CONFIG_NFS_V4_1 */
7535
7536/**
7537 * nfs4_proc_get_locations - discover locations for a migrated FSID
7538 * @inode: inode on FSID that is migrating
7539 * @locations: result of query
7540 * @page: buffer
7541 * @cred: credential to use for this operation
7542 *
7543 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7544 * operation failed, or a negative errno if a local error occurred.
7545 *
7546 * On success, "locations" is filled in, but if the server has
7547 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7548 * asserted.
7549 *
7550 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7551 * from this client that require migration recovery.
7552 */
7553int nfs4_proc_get_locations(struct inode *inode,
7554 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007555 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007556{
7557 struct nfs_server *server = NFS_SERVER(inode);
7558 struct nfs_client *clp = server->nfs_client;
7559 const struct nfs4_mig_recovery_ops *ops =
7560 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007561 struct nfs4_exception exception = {
7562 .interruptible = true,
7563 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007564 int status;
7565
7566 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7567 (unsigned long long)server->fsid.major,
7568 (unsigned long long)server->fsid.minor,
7569 clp->cl_hostname);
7570 nfs_display_fhandle(NFS_FH(inode), __func__);
7571
7572 do {
7573 status = ops->get_locations(inode, locations, page, cred);
7574 if (status != -NFS4ERR_DELAY)
7575 break;
7576 nfs4_handle_exception(server, status, &exception);
7577 } while (exception.retry);
7578 return status;
7579}
7580
Chuck Lever44c99932013-10-17 14:13:30 -04007581/*
7582 * This operation also signals the server that this client is
7583 * performing "lease moved" recovery. The server can stop
7584 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7585 * is appended to this compound to identify the client ID which is
7586 * performing recovery.
7587 */
NeilBrowna52458b2018-12-03 11:30:31 +11007588static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007589{
7590 struct nfs_server *server = NFS_SERVER(inode);
7591 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7592 struct rpc_clnt *clnt = server->client;
7593 struct nfs4_fsid_present_arg args = {
7594 .fh = NFS_FH(inode),
7595 .clientid = clp->cl_clientid,
7596 .renew = 1, /* append RENEW */
7597 };
7598 struct nfs4_fsid_present_res res = {
7599 .renew = 1,
7600 };
7601 struct rpc_message msg = {
7602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7603 .rpc_argp = &args,
7604 .rpc_resp = &res,
7605 .rpc_cred = cred,
7606 };
7607 unsigned long now = jiffies;
7608 int status;
7609
7610 res.fh = nfs_alloc_fhandle();
7611 if (res.fh == NULL)
7612 return -ENOMEM;
7613
Anna Schumakerfba83f32018-05-04 16:22:50 -04007614 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007615 status = nfs4_call_sync_sequence(clnt, server, &msg,
7616 &args.seq_args, &res.seq_res);
7617 nfs_free_fhandle(res.fh);
7618 if (status)
7619 return status;
7620
7621 do_renew_lease(clp, now);
7622 return 0;
7623}
7624
7625#ifdef CONFIG_NFS_V4_1
7626
7627/*
7628 * This operation also signals the server that this client is
7629 * performing "lease moved" recovery. The server can stop asserting
7630 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7631 * this operation is identified in the SEQUENCE operation in this
7632 * compound.
7633 */
NeilBrowna52458b2018-12-03 11:30:31 +11007634static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007635{
7636 struct nfs_server *server = NFS_SERVER(inode);
7637 struct rpc_clnt *clnt = server->client;
7638 struct nfs4_fsid_present_arg args = {
7639 .fh = NFS_FH(inode),
7640 };
7641 struct nfs4_fsid_present_res res = {
7642 };
7643 struct rpc_message msg = {
7644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7645 .rpc_argp = &args,
7646 .rpc_resp = &res,
7647 .rpc_cred = cred,
7648 };
7649 int status;
7650
7651 res.fh = nfs_alloc_fhandle();
7652 if (res.fh == NULL)
7653 return -ENOMEM;
7654
Anna Schumakerfba83f32018-05-04 16:22:50 -04007655 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007656 status = nfs4_call_sync_sequence(clnt, server, &msg,
7657 &args.seq_args, &res.seq_res);
7658 nfs_free_fhandle(res.fh);
7659 if (status == NFS4_OK &&
7660 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7661 status = -NFS4ERR_LEASE_MOVED;
7662 return status;
7663}
7664
7665#endif /* CONFIG_NFS_V4_1 */
7666
7667/**
7668 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7669 * @inode: inode on FSID to check
7670 * @cred: credential to use for this operation
7671 *
7672 * Server indicates whether the FSID is present, moved, or not
7673 * recognized. This operation is necessary to clear a LEASE_MOVED
7674 * condition for this client ID.
7675 *
7676 * Returns NFS4_OK if the FSID is present on this server,
7677 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7678 * NFS4ERR code if some error occurred on the server, or a
7679 * negative errno if a local failure occurred.
7680 */
NeilBrowna52458b2018-12-03 11:30:31 +11007681int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007682{
7683 struct nfs_server *server = NFS_SERVER(inode);
7684 struct nfs_client *clp = server->nfs_client;
7685 const struct nfs4_mig_recovery_ops *ops =
7686 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007687 struct nfs4_exception exception = {
7688 .interruptible = true,
7689 };
Chuck Lever44c99932013-10-17 14:13:30 -04007690 int status;
7691
7692 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7693 (unsigned long long)server->fsid.major,
7694 (unsigned long long)server->fsid.minor,
7695 clp->cl_hostname);
7696 nfs_display_fhandle(NFS_FH(inode), __func__);
7697
7698 do {
7699 status = ops->fsid_present(inode, cred);
7700 if (status != -NFS4ERR_DELAY)
7701 break;
7702 nfs4_handle_exception(server, status, &exception);
7703 } while (exception.retry);
7704 return status;
7705}
7706
Trond Myklebust302fad72019-02-18 13:32:38 -05007707/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007708 * If 'use_integrity' is true and the state managment nfs_client
7709 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7710 * and the machine credential as per RFC3530bis and RFC5661 Security
7711 * Considerations sections. Otherwise, just use the user cred with the
7712 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007713 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007714static 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 +00007715{
7716 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04007717 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7718 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007719 struct nfs4_secinfo_arg args = {
7720 .dir_fh = NFS_FH(dir),
7721 .name = name,
7722 };
7723 struct nfs4_secinfo_res res = {
7724 .flavors = flavors,
7725 };
7726 struct rpc_message msg = {
7727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7728 .rpc_argp = &args,
7729 .rpc_resp = &res,
7730 };
Anna Schumaker50493362019-08-14 15:30:16 -04007731 struct nfs4_call_sync_data data = {
7732 .seq_server = NFS_SERVER(dir),
7733 .seq_args = &args.seq_args,
7734 .seq_res = &res.seq_res,
7735 };
7736 struct rpc_task_setup task_setup = {
7737 .rpc_client = clnt,
7738 .rpc_message = &msg,
7739 .callback_ops = clp->cl_mvops->call_sync_ops,
7740 .callback_data = &data,
7741 .flags = RPC_TASK_NO_ROUND_ROBIN,
7742 };
NeilBrowna52458b2018-12-03 11:30:31 +11007743 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007744
7745 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04007746 clnt = clp->cl_rpcclient;
7747 task_setup.rpc_client = clnt;
7748
7749 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007750 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007751 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007752
7753 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007754
Anna Schumaker50493362019-08-14 15:30:16 -04007755 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7756 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7757 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007758
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007759 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007760
NeilBrowna52458b2018-12-03 11:30:31 +11007761 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007762 return status;
7763}
7764
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007765int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7766 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007767{
Trond Myklebust0688e642019-04-07 13:59:09 -04007768 struct nfs4_exception exception = {
7769 .interruptible = true,
7770 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007771 int err;
7772 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007773 err = -NFS4ERR_WRONGSEC;
7774
7775 /* try to use integrity protection with machine cred */
7776 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7777 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7778
7779 /*
7780 * if unable to use integrity protection, or SECINFO with
7781 * integrity protection returns NFS4ERR_WRONGSEC (which is
7782 * disallowed by spec, but exists in deployed servers) use
7783 * the current filesystem's rpc_client and the user cred.
7784 */
7785 if (err == -NFS4ERR_WRONGSEC)
7786 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7787
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007788 trace_nfs4_secinfo(dir, name, err);
7789 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007790 &exception);
7791 } while (exception.retry);
7792 return err;
7793}
7794
Andy Adamson557134a2009-04-01 09:21:53 -04007795#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007796/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007797 * Check the exchange flags returned by the server for invalid flags, having
7798 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7799 * DS flags set.
7800 */
7801static int nfs4_check_cl_exchange_flags(u32 flags)
7802{
7803 if (flags & ~EXCHGID4_FLAG_MASK_R)
7804 goto out_inval;
7805 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7806 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7807 goto out_inval;
7808 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7809 goto out_inval;
7810 return NFS_OK;
7811out_inval:
7812 return -NFS4ERR_INVAL;
7813}
7814
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007815static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007816nfs41_same_server_scope(struct nfs41_server_scope *a,
7817 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007818{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007819 if (a->server_scope_sz != b->server_scope_sz)
7820 return false;
7821 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007822}
7823
Andy Adamson02a95de2016-02-05 16:08:37 -05007824static void
7825nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7826{
7827}
7828
7829static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7830 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7831};
7832
Andy Adamson357f54d2010-12-14 10:11:57 -05007833/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007834 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007835 *
7836 * The 4.1 client currently uses the same TCP connection for the
7837 * fore and backchannel.
7838 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007839static
7840int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7841 struct rpc_xprt *xprt,
7842 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007843 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007844{
7845 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007846 struct nfs41_bind_conn_to_session_args args = {
7847 .client = clp,
7848 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7849 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007850 struct nfs41_bind_conn_to_session_res res;
7851 struct rpc_message msg = {
7852 .rpc_proc =
7853 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007854 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007855 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007856 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007857 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007858 struct rpc_task_setup task_setup_data = {
7859 .rpc_client = clnt,
7860 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007861 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007862 .rpc_message = &msg,
7863 .flags = RPC_TASK_TIMEOUT,
7864 };
7865 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007866
Trond Myklebust71a097c2015-02-18 09:27:18 -08007867 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7868 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7869 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007870
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007871 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7872 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7873 args.dir = NFS4_CDFC4_FORE;
7874
7875 task = rpc_run_task(&task_setup_data);
7876 if (!IS_ERR(task)) {
7877 status = task->tk_status;
7878 rpc_put_task(task);
7879 } else
7880 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007881 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007882 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007883 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007884 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7885 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007886 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007887 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007888 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007889 dprintk("NFS: %s: Unexpected direction from server\n",
7890 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007891 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007892 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007893 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007894 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7895 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007896 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007897 }
7898 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007899
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007900 return status;
7901}
7902
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007903struct rpc_bind_conn_calldata {
7904 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007905 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007906};
7907
7908static int
7909nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7910 struct rpc_xprt *xprt,
7911 void *calldata)
7912{
7913 struct rpc_bind_conn_calldata *p = calldata;
7914
7915 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7916}
7917
NeilBrowna52458b2018-12-03 11:30:31 +11007918int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007919{
7920 struct rpc_bind_conn_calldata data = {
7921 .clp = clp,
7922 .cred = cred,
7923 };
7924 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7925 nfs4_proc_bind_conn_to_session_callback, &data);
7926}
7927
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007928/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007929 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7930 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007931 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007932static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7933 .how = SP4_MACH_CRED,
7934 .enforce.u.words = {
7935 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7936 1 << (OP_EXCHANGE_ID - 32) |
7937 1 << (OP_CREATE_SESSION - 32) |
7938 1 << (OP_DESTROY_SESSION - 32) |
7939 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007940 },
7941 .allow.u.words = {
7942 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007943 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007944 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007945 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007946 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007947 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007948 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007949 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007950 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007951 1 << (OP_FREE_STATEID - 32) |
7952 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007953 }
7954};
7955
7956/*
7957 * Select the state protection mode for client `clp' given the server results
7958 * from exchange_id in `sp'.
7959 *
7960 * Returns 0 on success, negative errno otherwise.
7961 */
7962static int nfs4_sp4_select_mode(struct nfs_client *clp,
7963 struct nfs41_state_protection *sp)
7964{
7965 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7966 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7967 1 << (OP_EXCHANGE_ID - 32) |
7968 1 << (OP_CREATE_SESSION - 32) |
7969 1 << (OP_DESTROY_SESSION - 32) |
7970 1 << (OP_DESTROY_CLIENTID - 32)
7971 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007972 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007973 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007974 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007975
7976 if (sp->how == SP4_MACH_CRED) {
7977 /* Print state protect result */
7978 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7979 for (i = 0; i <= LAST_NFS4_OP; i++) {
7980 if (test_bit(i, sp->enforce.u.longs))
7981 dfprintk(MOUNT, " enforce op %d\n", i);
7982 if (test_bit(i, sp->allow.u.longs))
7983 dfprintk(MOUNT, " allow op %d\n", i);
7984 }
7985
7986 /* make sure nothing is on enforce list that isn't supported */
7987 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7988 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7989 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007990 ret = -EINVAL;
7991 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007992 }
7993 }
7994
7995 /*
7996 * Minimal mode - state operations are allowed to use machine
7997 * credential. Note this already happens by default, so the
7998 * client doesn't have to do anything more than the negotiation.
7999 *
8000 * NOTE: we don't care if EXCHANGE_ID is in the list -
8001 * we're already using the machine cred for exchange_id
8002 * and will never use a different cred.
8003 */
8004 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8005 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8006 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8007 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8008 dfprintk(MOUNT, "sp4_mach_cred:\n");
8009 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008010 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008011 } else {
8012 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008013 ret = -EINVAL;
8014 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008015 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008016
8017 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008018 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8019 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008020 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8021 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008022 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008023 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008024
Andrew Elble99ade3c2015-12-02 09:39:51 -05008025 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8026 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008027 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008028 }
8029
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008030 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8031 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8032 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008033 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008034 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008035
8036 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8037 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8038 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008039 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008040 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008041
8042 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8043 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008044 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008045 }
8046
8047 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8048 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008049 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008050 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008051 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008052out:
8053 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008054 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008055}
8056
Andy Adamson8d89bd72016-09-09 09:22:18 -04008057struct nfs41_exchange_id_data {
8058 struct nfs41_exchange_id_res res;
8059 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008060};
8061
Andy Adamson8d89bd72016-09-09 09:22:18 -04008062static void nfs4_exchange_id_release(void *data)
8063{
8064 struct nfs41_exchange_id_data *cdata =
8065 (struct nfs41_exchange_id_data *)data;
8066
Olga Kornievskaia63513232017-03-13 10:36:19 -04008067 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008068 kfree(cdata->res.impl_id);
8069 kfree(cdata->res.server_scope);
8070 kfree(cdata->res.server_owner);
8071 kfree(cdata);
8072}
8073
8074static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008075 .rpc_release = nfs4_exchange_id_release,
8076};
8077
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008078/*
8079 * _nfs4_proc_exchange_id()
8080 *
8081 * Wrapper for EXCHANGE_ID operation.
8082 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008083static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008084nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008085 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008086{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008087 struct rpc_message msg = {
8088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008089 .rpc_cred = cred,
8090 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008091 struct rpc_task_setup task_setup_data = {
8092 .rpc_client = clp->cl_rpcclient,
8093 .callback_ops = &nfs4_exchange_id_call_ops,
8094 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008095 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008096 };
8097 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008098 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008099
Elena Reshetova212bf412017-10-20 12:53:38 +03008100 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008101 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008102
Trond Myklebust9c760d12017-07-31 18:38:50 -04008103 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008104 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008105 if (!calldata)
8106 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008107
Trond Myklebustfd405592017-08-01 16:02:47 -04008108 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008109
8110 status = nfs4_init_uniform_client_string(clp);
8111 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008112 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008113
Andy Adamson8d89bd72016-09-09 09:22:18 -04008114 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8115 GFP_NOFS);
8116 status = -ENOMEM;
8117 if (unlikely(calldata->res.server_owner == NULL))
8118 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008119
Andy Adamson8d89bd72016-09-09 09:22:18 -04008120 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008121 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008122 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008123 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008124
Andy Adamson8d89bd72016-09-09 09:22:18 -04008125 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8126 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008127 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008128
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008129 switch (sp4_how) {
8130 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008131 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008132 break;
8133
8134 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008135 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008136 break;
8137
8138 default:
8139 /* unsupported! */
8140 WARN_ON_ONCE(1);
8141 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008142 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008143 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008144 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008145 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008146 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008147 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8148 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008149 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008150 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008151 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8152 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008153#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008154 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008155#endif
8156 msg.rpc_argp = &calldata->args;
8157 msg.rpc_resp = &calldata->res;
8158 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008159
Trond Myklebust9c760d12017-07-31 18:38:50 -04008160 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008161
8162out_impl_id:
8163 kfree(calldata->res.impl_id);
8164out_server_scope:
8165 kfree(calldata->res.server_scope);
8166out_server_owner:
8167 kfree(calldata->res.server_owner);
8168out_calldata:
8169 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008170out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008171 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008172 return ERR_PTR(status);
8173}
8174
8175/*
8176 * _nfs4_proc_exchange_id()
8177 *
8178 * Wrapper for EXCHANGE_ID operation.
8179 */
NeilBrowna52458b2018-12-03 11:30:31 +11008180static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008181 u32 sp4_how)
8182{
8183 struct rpc_task *task;
8184 struct nfs41_exchange_id_args *argp;
8185 struct nfs41_exchange_id_res *resp;
8186 int status;
8187
8188 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8189 if (IS_ERR(task))
8190 return PTR_ERR(task);
8191
8192 argp = task->tk_msg.rpc_argp;
8193 resp = task->tk_msg.rpc_resp;
8194 status = task->tk_status;
8195 if (status != 0)
8196 goto out;
8197
8198 status = nfs4_check_cl_exchange_flags(resp->flags);
8199 if (status != 0)
8200 goto out;
8201
8202 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8203 if (status != 0)
8204 goto out;
8205
8206 clp->cl_clientid = resp->clientid;
8207 clp->cl_exchange_flags = resp->flags;
8208 clp->cl_seqid = resp->seqid;
8209 /* Client ID is not confirmed */
8210 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8211 clear_bit(NFS4_SESSION_ESTABLISHED,
8212 &clp->cl_session->session_state);
8213
8214 if (clp->cl_serverscope != NULL &&
8215 !nfs41_same_server_scope(clp->cl_serverscope,
8216 resp->server_scope)) {
8217 dprintk("%s: server_scope mismatch detected\n",
8218 __func__);
8219 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8220 }
8221
8222 swap(clp->cl_serverowner, resp->server_owner);
8223 swap(clp->cl_serverscope, resp->server_scope);
8224 swap(clp->cl_implid, resp->impl_id);
8225
8226 /* Save the EXCHANGE_ID verifier session trunk tests */
8227 memcpy(clp->cl_confirm.data, argp->verifier.data,
8228 sizeof(clp->cl_confirm.data));
8229out:
8230 trace_nfs4_exchange_id(clp, status);
8231 rpc_put_task(task);
8232 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008233}
8234
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008235/*
8236 * nfs4_proc_exchange_id()
8237 *
8238 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8239 *
8240 * Since the clientid has expired, all compounds using sessions
8241 * associated with the stale clientid will be returning
8242 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8243 * be in some phase of session reset.
8244 *
8245 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8246 */
NeilBrowna52458b2018-12-03 11:30:31 +11008247int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008248{
8249 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8250 int status;
8251
8252 /* try SP4_MACH_CRED if krb5i/p */
8253 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8254 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008255 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008256 if (!status)
8257 return 0;
8258 }
8259
8260 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008261 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008262}
8263
Andy Adamson04fa2c62016-09-09 09:22:29 -04008264/**
8265 * nfs4_test_session_trunk
8266 *
8267 * This is an add_xprt_test() test function called from
8268 * rpc_clnt_setup_test_and_add_xprt.
8269 *
8270 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8271 * and is dereferrenced in nfs4_exchange_id_release
8272 *
8273 * Upon success, add the new transport to the rpc_clnt
8274 *
8275 * @clnt: struct rpc_clnt to get new transport
8276 * @xprt: the rpc_xprt to test
8277 * @data: call data for _nfs4_proc_exchange_id.
8278 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308279void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008280 void *data)
8281{
8282 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008283 struct rpc_task *task;
8284 int status;
8285
Andy Adamson04fa2c62016-09-09 09:22:29 -04008286 u32 sp4_how;
8287
8288 dprintk("--> %s try %s\n", __func__,
8289 xprt->address_strings[RPC_DISPLAY_ADDR]);
8290
8291 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8292
8293 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008294 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8295 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308296 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008297
8298 status = task->tk_status;
8299 if (status == 0)
8300 status = nfs4_detect_session_trunking(adata->clp,
8301 task->tk_msg.rpc_resp, xprt);
8302
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308303 if (status == 0)
8304 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8305
Trond Myklebust9c760d12017-07-31 18:38:50 -04008306 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008307}
8308EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8309
Trond Myklebust66245532012-05-25 17:18:09 -04008310static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008311 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008312{
8313 struct rpc_message msg = {
8314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8315 .rpc_argp = clp,
8316 .rpc_cred = cred,
8317 };
8318 int status;
8319
NeilBrown5a0c2572019-05-30 10:41:28 +10008320 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8321 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008322 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008323 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008324 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008325 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8326 return status;
8327}
8328
8329static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008330 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008331{
8332 unsigned int loop;
8333 int ret;
8334
8335 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8336 ret = _nfs4_proc_destroy_clientid(clp, cred);
8337 switch (ret) {
8338 case -NFS4ERR_DELAY:
8339 case -NFS4ERR_CLIENTID_BUSY:
8340 ssleep(1);
8341 break;
8342 default:
8343 return ret;
8344 }
8345 }
8346 return 0;
8347}
8348
8349int nfs4_destroy_clientid(struct nfs_client *clp)
8350{
NeilBrowna52458b2018-12-03 11:30:31 +11008351 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008352 int ret = 0;
8353
8354 if (clp->cl_mvops->minor_version < 1)
8355 goto out;
8356 if (clp->cl_exchange_flags == 0)
8357 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008358 if (clp->cl_preserve_clid)
8359 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008360 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008361 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008362 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008363 switch (ret) {
8364 case 0:
8365 case -NFS4ERR_STALE_CLIENTID:
8366 clp->cl_exchange_flags = 0;
8367 }
8368out:
8369 return ret;
8370}
8371
Donald Buczek0efb01b2019-07-07 21:26:08 +02008372#endif /* CONFIG_NFS_V4_1 */
8373
Andy Adamson2050f0c2009-04-01 09:22:30 -04008374struct nfs4_get_lease_time_data {
8375 struct nfs4_get_lease_time_args *args;
8376 struct nfs4_get_lease_time_res *res;
8377 struct nfs_client *clp;
8378};
8379
8380static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8381 void *calldata)
8382{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008383 struct nfs4_get_lease_time_data *data =
8384 (struct nfs4_get_lease_time_data *)calldata;
8385
8386 dprintk("--> %s\n", __func__);
8387 /* just setup sequence, do not trigger session recovery
8388 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008389 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008390 &data->args->la_seq_args,
8391 &data->res->lr_seq_res,
8392 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008393 dprintk("<-- %s\n", __func__);
8394}
8395
8396/*
8397 * Called from nfs4_state_manager thread for session setup, so don't recover
8398 * from sequence operation or clientid errors.
8399 */
8400static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8401{
8402 struct nfs4_get_lease_time_data *data =
8403 (struct nfs4_get_lease_time_data *)calldata;
8404
8405 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008406 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008407 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008408 switch (task->tk_status) {
8409 case -NFS4ERR_DELAY:
8410 case -NFS4ERR_GRACE:
8411 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8412 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8413 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008414 /* fall through */
8415 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008416 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008417 return;
8418 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008419 dprintk("<-- %s\n", __func__);
8420}
8421
Trond Myklebust17280172012-03-11 13:11:00 -04008422static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008423 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8424 .rpc_call_done = nfs4_get_lease_time_done,
8425};
8426
8427int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8428{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008429 struct nfs4_get_lease_time_args args;
8430 struct nfs4_get_lease_time_res res = {
8431 .lr_fsinfo = fsinfo,
8432 };
8433 struct nfs4_get_lease_time_data data = {
8434 .args = &args,
8435 .res = &res,
8436 .clp = clp,
8437 };
8438 struct rpc_message msg = {
8439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8440 .rpc_argp = &args,
8441 .rpc_resp = &res,
8442 };
8443 struct rpc_task_setup task_setup = {
8444 .rpc_client = clp->cl_rpcclient,
8445 .rpc_message = &msg,
8446 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008447 .callback_data = &data,
8448 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008449 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008450
Anna Schumakerfba83f32018-05-04 16:22:50 -04008451 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008452 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008453}
8454
Donald Buczek0efb01b2019-07-07 21:26:08 +02008455#ifdef CONFIG_NFS_V4_1
8456
Andy Adamsonfc931582009-04-01 09:22:31 -04008457/*
8458 * Initialize the values to be used by the client in CREATE_SESSION
8459 * If nfs4_init_session set the fore channel request and response sizes,
8460 * use them.
8461 *
8462 * Set the back channel max_resp_sz_cached to zero to force the client to
8463 * always set csa_cachethis to FALSE because the current implementation
8464 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8465 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008466static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8467 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008468{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008469 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008470 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008471 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008472
Andy Adamson18aad3d2013-06-26 12:21:49 -04008473 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8474 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8475
Andy Adamsonfc931582009-04-01 09:22:31 -04008476 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008477 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8478 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008479 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008480 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008481
8482 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008483 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008484 __func__,
8485 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008486 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008487
8488 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008489 args->bc_attrs.max_rqst_sz = max_bc_payload;
8490 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008491 args->bc_attrs.max_resp_sz_cached = 0;
8492 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008493 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008494 if (args->bc_attrs.max_reqs > max_bc_slots)
8495 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008496
8497 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8498 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8499 __func__,
8500 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8501 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8502 args->bc_attrs.max_reqs);
8503}
8504
Trond Myklebust79969dd2015-02-18 11:30:18 -08008505static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8506 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008507{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008508 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008509 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008510
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008511 if (rcvd->max_resp_sz > sent->max_resp_sz)
8512 return -EINVAL;
8513 /*
8514 * Our requested max_ops is the minimum we need; we're not
8515 * prepared to break up compounds into smaller pieces than that.
8516 * So, no point even trying to continue if the server won't
8517 * cooperate:
8518 */
8519 if (rcvd->max_ops < sent->max_ops)
8520 return -EINVAL;
8521 if (rcvd->max_reqs == 0)
8522 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008523 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8524 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008525 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008526}
8527
Trond Myklebust79969dd2015-02-18 11:30:18 -08008528static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8529 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008530{
8531 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008532 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008533
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008534 if (!(res->flags & SESSION4_BACK_CHAN))
8535 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008536 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8537 return -EINVAL;
8538 if (rcvd->max_resp_sz < sent->max_resp_sz)
8539 return -EINVAL;
8540 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8541 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008542 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008543 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008544 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008545 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008546out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008547 return 0;
8548}
Andy Adamson8d353012009-04-01 09:22:32 -04008549
Andy Adamson8d353012009-04-01 09:22:32 -04008550static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008551 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008552{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008553 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008554
Trond Myklebust79969dd2015-02-18 11:30:18 -08008555 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008556 if (ret)
8557 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008558 return nfs4_verify_back_channel_attrs(args, res);
8559}
8560
8561static void nfs4_update_session(struct nfs4_session *session,
8562 struct nfs41_create_session_res *res)
8563{
8564 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008565 /* Mark client id and session as being confirmed */
8566 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8567 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008568 session->flags = res->flags;
8569 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008570 if (res->flags & SESSION4_BACK_CHAN)
8571 memcpy(&session->bc_attrs, &res->bc_attrs,
8572 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008573}
8574
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008575static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008576 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008577{
8578 struct nfs4_session *session = clp->cl_session;
8579 struct nfs41_create_session_args args = {
8580 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008581 .clientid = clp->cl_clientid,
8582 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008583 .cb_program = NFS4_CALLBACK,
8584 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008585 struct nfs41_create_session_res res;
8586
Andy Adamsonfc931582009-04-01 09:22:31 -04008587 struct rpc_message msg = {
8588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8589 .rpc_argp = &args,
8590 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008591 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008592 };
8593 int status;
8594
Chuck Lever6b26cc82016-05-02 14:40:40 -04008595 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008596 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008597
NeilBrown5a0c2572019-05-30 10:41:28 +10008598 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8599 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008600 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008601
Trond Myklebustb519d402016-09-11 14:50:01 -04008602 switch (status) {
8603 case -NFS4ERR_STALE_CLIENTID:
8604 case -NFS4ERR_DELAY:
8605 case -ETIMEDOUT:
8606 case -EACCES:
8607 case -EAGAIN:
8608 goto out;
8609 };
8610
8611 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008612 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008613 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008614 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008615 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008616 if (status)
8617 goto out;
8618 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008619 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008620out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008621 return status;
8622}
8623
8624/*
8625 * Issues a CREATE_SESSION operation to the server.
8626 * It is the responsibility of the caller to verify the session is
8627 * expired before calling this routine.
8628 */
NeilBrowna52458b2018-12-03 11:30:31 +11008629int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008630{
8631 int status;
8632 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008633 struct nfs4_session *session = clp->cl_session;
8634
8635 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8636
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008637 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008638 if (status)
8639 goto out;
8640
Andy Adamsonaacd5532011-11-09 13:58:21 -05008641 /* Init or reset the session slot tables */
8642 status = nfs4_setup_session_slot_tables(session);
8643 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008644 if (status)
8645 goto out;
8646
8647 ptr = (unsigned *)&session->sess_id.data[0];
8648 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8649 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008650out:
8651 dprintk("<-- %s\n", __func__);
8652 return status;
8653}
8654
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008655/*
8656 * Issue the over-the-wire RPC DESTROY_SESSION.
8657 * The caller must serialize access to this routine.
8658 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008659int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008660 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008661{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008662 struct rpc_message msg = {
8663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8664 .rpc_argp = session,
8665 .rpc_cred = cred,
8666 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008667 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008668
8669 dprintk("--> nfs4_proc_destroy_session\n");
8670
8671 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008672 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8673 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008674
NeilBrown5a0c2572019-05-30 10:41:28 +10008675 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8676 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008677 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008678
8679 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008680 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008681 "Session has been destroyed regardless...\n", status);
8682
8683 dprintk("<-- nfs4_proc_destroy_session\n");
8684 return status;
8685}
8686
Trond Myklebust7b38c362012-05-23 13:23:31 -04008687/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008688 * Renew the cl_session lease.
8689 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008690struct nfs4_sequence_data {
8691 struct nfs_client *clp;
8692 struct nfs4_sequence_args args;
8693 struct nfs4_sequence_res res;
8694};
8695
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008696static void nfs41_sequence_release(void *data)
8697{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008698 struct nfs4_sequence_data *calldata = data;
8699 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008700
Elena Reshetova212bf412017-10-20 12:53:38 +03008701 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008702 nfs4_schedule_state_renewal(clp);
8703 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008704 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008705}
8706
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008707static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8708{
8709 switch(task->tk_status) {
8710 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008711 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8712 return -EAGAIN;
8713 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008714 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008715 }
8716 return 0;
8717}
8718
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008719static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008720{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008721 struct nfs4_sequence_data *calldata = data;
8722 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008723
Trond Myklebust14516c32010-07-31 14:29:06 -04008724 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8725 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008726
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008727 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008728 if (task->tk_status < 0) {
8729 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008730 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008731 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008732
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008733 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8734 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008735 return;
8736 }
8737 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008738 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008739out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008740 dprintk("<-- %s\n", __func__);
8741}
8742
8743static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8744{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008745 struct nfs4_sequence_data *calldata = data;
8746 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008747 struct nfs4_sequence_args *args;
8748 struct nfs4_sequence_res *res;
8749
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008750 args = task->tk_msg.rpc_argp;
8751 res = task->tk_msg.rpc_resp;
8752
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008753 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008754}
8755
8756static const struct rpc_call_ops nfs41_sequence_ops = {
8757 .rpc_call_done = nfs41_sequence_call_done,
8758 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008759 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008760};
8761
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008762static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008763 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008764 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008765 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008766{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008767 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008768 struct rpc_message msg = {
8769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8770 .rpc_cred = cred,
8771 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008772 struct rpc_task_setup task_setup_data = {
8773 .rpc_client = clp->cl_rpcclient,
8774 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008775 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008776 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008777 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008778 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008779
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008780 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008781 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008782 goto out_err;
8783
8784 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008785 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008786 if (calldata == NULL)
8787 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008788 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008789 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008790 msg.rpc_argp = &calldata->args;
8791 msg.rpc_resp = &calldata->res;
8792 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008793 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008794
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008795 ret = rpc_run_task(&task_setup_data);
8796 if (IS_ERR(ret))
8797 goto out_err;
8798 return ret;
8799out_put_clp:
8800 nfs_put_client(clp);
8801out_err:
8802 nfs41_release_slot(slot);
8803 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008804}
8805
NeilBrowna52458b2018-12-03 11:30:31 +11008806static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008807{
8808 struct rpc_task *task;
8809 int ret = 0;
8810
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008811 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008812 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008813 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008814 if (IS_ERR(task))
8815 ret = PTR_ERR(task);
8816 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008817 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008818 dprintk("<-- %s status=%d\n", __func__, ret);
8819 return ret;
8820}
8821
NeilBrowna52458b2018-12-03 11:30:31 +11008822static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008823{
8824 struct rpc_task *task;
8825 int ret;
8826
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008827 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008828 if (IS_ERR(task)) {
8829 ret = PTR_ERR(task);
8830 goto out;
8831 }
8832 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008833 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008834 ret = task->tk_status;
8835 rpc_put_task(task);
8836out:
8837 dprintk("<-- %s status=%d\n", __func__, ret);
8838 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008839}
8840
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008841struct nfs4_reclaim_complete_data {
8842 struct nfs_client *clp;
8843 struct nfs41_reclaim_complete_args arg;
8844 struct nfs41_reclaim_complete_res res;
8845};
8846
8847static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8848{
8849 struct nfs4_reclaim_complete_data *calldata = data;
8850
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008851 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008852 &calldata->arg.seq_args,
8853 &calldata->res.seq_res,
8854 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008855}
8856
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008857static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8858{
8859 switch(task->tk_status) {
8860 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008861 wake_up_all(&clp->cl_lock_waitq);
8862 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008863 case -NFS4ERR_COMPLETE_ALREADY:
8864 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8865 break;
8866 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008867 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008868 /* fall through */
8869 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008870 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008871 case -NFS4ERR_BADSESSION:
8872 case -NFS4ERR_DEADSESSION:
8873 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8874 nfs4_schedule_session_recovery(clp->cl_session,
8875 task->tk_status);
8876 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008877 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008878 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008879 }
8880 return 0;
8881}
8882
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008883static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8884{
8885 struct nfs4_reclaim_complete_data *calldata = data;
8886 struct nfs_client *clp = calldata->clp;
8887 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8888
8889 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008890 if (!nfs41_sequence_done(task, res))
8891 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008892
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008893 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008894 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8895 rpc_restart_call_prepare(task);
8896 return;
8897 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008898 dprintk("<-- %s\n", __func__);
8899}
8900
8901static void nfs4_free_reclaim_complete_data(void *data)
8902{
8903 struct nfs4_reclaim_complete_data *calldata = data;
8904
8905 kfree(calldata);
8906}
8907
8908static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8909 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8910 .rpc_call_done = nfs4_reclaim_complete_done,
8911 .rpc_release = nfs4_free_reclaim_complete_data,
8912};
8913
8914/*
8915 * Issue a global reclaim complete.
8916 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008917static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008918 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008919{
8920 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008921 struct rpc_message msg = {
8922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008923 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008924 };
8925 struct rpc_task_setup task_setup_data = {
8926 .rpc_client = clp->cl_rpcclient,
8927 .rpc_message = &msg,
8928 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04008929 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008930 };
8931 int status = -ENOMEM;
8932
8933 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008934 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008935 if (calldata == NULL)
8936 goto out;
8937 calldata->clp = clp;
8938 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008939
Anna Schumakerfba83f32018-05-04 16:22:50 -04008940 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008941 msg.rpc_argp = &calldata->arg;
8942 msg.rpc_resp = &calldata->res;
8943 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04008944 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008945out:
8946 dprintk("<-- %s status=%d\n", __func__, status);
8947 return status;
8948}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008949
8950static void
8951nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8952{
8953 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008954 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008955
8956 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008957 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008958 &lgp->res.seq_res, task);
8959 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008960}
8961
8962static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8963{
8964 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008965
8966 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008967 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008968 dprintk("<-- %s\n", __func__);
8969}
8970
8971static int
8972nfs4_layoutget_handle_exception(struct rpc_task *task,
8973 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8974{
Trond Myklebustee314c22012-10-01 17:25:48 -07008975 struct inode *inode = lgp->args.inode;
8976 struct nfs_server *server = NFS_SERVER(inode);
8977 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008978 int nfs4err = task->tk_status;
8979 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008980 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008981
Boaz Harroshed7e5422014-01-22 20:34:54 +02008982 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008983
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008984 nfs4_sequence_free_slot(&lgp->res.seq_res);
8985
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008986 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008987 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008988 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008989
8990 /*
8991 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8992 * on the file. set tk_status to -ENODATA to tell upper layer to
8993 * retry go inband.
8994 */
8995 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008996 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008997 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008998 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008999 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9000 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9001 */
9002 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009003 status = -EOVERFLOW;
9004 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009005 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009006 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009007 * (or clients) writing to the same RAID stripe except when
9008 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009009 *
9010 * Treat it like we would RECALLCONFLICT -- we retry for a little
9011 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009012 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009013 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009014 if (lgp->args.minlength == 0) {
9015 status = -EOVERFLOW;
9016 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009017 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009018 status = -EBUSY;
9019 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009020 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009021 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009022 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009023 case -NFS4ERR_DELEG_REVOKED:
9024 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009025 case -NFS4ERR_EXPIRED:
9026 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009027 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009028 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009029 lo = NFS_I(inode)->layout;
9030 /* If the open stateid was bad, then recover it. */
9031 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009032 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009033 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009034 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009035 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009036 break;
9037 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009038
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009039 /*
9040 * Mark the bad layout state as invalid, then retry
9041 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009042 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009043 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009044 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009045 pnfs_free_lseg_list(&head);
9046 status = -EAGAIN;
9047 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009048 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009049
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009050 err = nfs4_handle_exception(server, nfs4err, exception);
9051 if (!status) {
9052 if (exception->retry)
9053 status = -EAGAIN;
9054 else
9055 status = err;
9056 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009057out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009058 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009059 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009060}
9061
Fred Isamandacb4522016-09-19 17:47:09 -04009062size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009063{
9064 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9065 return nfs_page_array_len(0, max_resp_sz);
9066}
9067
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009068static void nfs4_layoutget_release(void *calldata)
9069{
9070 struct nfs4_layoutget *lgp = calldata;
9071
9072 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009073 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009074 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009075 dprintk("<-- %s\n", __func__);
9076}
9077
9078static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9079 .rpc_call_prepare = nfs4_layoutget_prepare,
9080 .rpc_call_done = nfs4_layoutget_done,
9081 .rpc_release = nfs4_layoutget_release,
9082};
9083
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009084struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009085nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009086{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009087 struct inode *inode = lgp->args.inode;
9088 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009089 struct rpc_task *task;
9090 struct rpc_message msg = {
9091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9092 .rpc_argp = &lgp->args,
9093 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009094 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009095 };
9096 struct rpc_task_setup task_setup_data = {
9097 .rpc_client = server->client,
9098 .rpc_message = &msg,
9099 .callback_ops = &nfs4_layoutget_call_ops,
9100 .callback_data = lgp,
9101 .flags = RPC_TASK_ASYNC,
9102 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009103 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009104 struct nfs4_exception exception = {
9105 .inode = inode,
9106 .timeout = *timeout,
9107 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009108 int status = 0;
9109
9110 dprintk("--> %s\n", __func__);
9111
Peng Tao4bd5a982014-11-17 11:05:17 +08009112 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9113 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9114
Anna Schumakerfba83f32018-05-04 16:22:50 -04009115 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009116
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009117 task = rpc_run_task(&task_setup_data);
9118 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009119 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05009120 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009121 if (status != 0)
9122 goto out;
9123
Trond Myklebust18c07782019-02-13 07:55:31 -05009124 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009125 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9126 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009127 } else if (lgp->res.layoutp->len == 0) {
9128 status = -EAGAIN;
9129 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009130 } else
9131 lseg = pnfs_layout_process(lgp);
9132out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009133 trace_nfs4_layoutget(lgp->args.ctx,
9134 &lgp->args.range,
9135 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009136 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009137 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009138
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009139 rpc_put_task(task);
9140 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009141 if (status)
9142 return ERR_PTR(status);
9143 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009144}
9145
Benny Halevycbe82602011-05-22 19:52:37 +03009146static void
9147nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9148{
9149 struct nfs4_layoutreturn *lrp = calldata;
9150
9151 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009152 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009153 &lrp->args.seq_args,
9154 &lrp->res.seq_res,
9155 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009156 if (!pnfs_layout_is_valid(lrp->args.layout))
9157 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009158}
9159
9160static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9161{
9162 struct nfs4_layoutreturn *lrp = calldata;
9163 struct nfs_server *server;
9164
9165 dprintk("--> %s\n", __func__);
9166
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009167 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009168 return;
9169
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009170 /*
9171 * Was there an RPC level error? Assume the call succeeded,
9172 * and that we need to release the layout
9173 */
9174 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9175 lrp->res.lrs_present = 0;
9176 return;
9177 }
9178
Benny Halevycbe82602011-05-22 19:52:37 +03009179 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009180 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009181 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009182 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009183 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009184 lrp->args.inode))
9185 goto out_restart;
9186 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009187 default:
9188 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05009189 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009190 case 0:
9191 break;
9192 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009193 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009194 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009195 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009196 }
Benny Halevycbe82602011-05-22 19:52:37 +03009197 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009198 return;
9199out_restart:
9200 task->tk_status = 0;
9201 nfs4_sequence_free_slot(&lrp->res.seq_res);
9202 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009203}
9204
9205static void nfs4_layoutreturn_release(void *calldata)
9206{
9207 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009208 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009209
9210 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009211 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009212 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009213 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009214 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9215 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009216 pnfs_put_layout_hdr(lrp->args.layout);
9217 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009218 kfree(calldata);
9219 dprintk("<-- %s\n", __func__);
9220}
9221
9222static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9223 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9224 .rpc_call_done = nfs4_layoutreturn_done,
9225 .rpc_release = nfs4_layoutreturn_release,
9226};
9227
Peng Tao6c166052014-11-17 09:30:40 +08009228int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009229{
9230 struct rpc_task *task;
9231 struct rpc_message msg = {
9232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9233 .rpc_argp = &lrp->args,
9234 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009235 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009236 };
9237 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009238 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009239 .rpc_message = &msg,
9240 .callback_ops = &nfs4_layoutreturn_call_ops,
9241 .callback_data = lrp,
9242 };
Peng Tao6c166052014-11-17 09:30:40 +08009243 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009244
Andrew Elble99ade3c2015-12-02 09:39:51 -05009245 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9246 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9247 &task_setup_data.rpc_client, &msg);
9248
Benny Halevycbe82602011-05-22 19:52:37 +03009249 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009250 if (!sync) {
9251 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9252 if (!lrp->inode) {
9253 nfs4_layoutreturn_release(lrp);
9254 return -EAGAIN;
9255 }
9256 task_setup_data.flags |= RPC_TASK_ASYNC;
9257 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009258 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009259 task = rpc_run_task(&task_setup_data);
9260 if (IS_ERR(task))
9261 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009262 if (sync)
9263 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009264 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009265 dprintk("<-- %s status=%d\n", __func__, status);
9266 rpc_put_task(task);
9267 return status;
9268}
9269
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009270static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009271_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9272 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009273 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009274{
9275 struct nfs4_getdeviceinfo_args args = {
9276 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009277 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9278 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009279 };
9280 struct nfs4_getdeviceinfo_res res = {
9281 .pdev = pdev,
9282 };
9283 struct rpc_message msg = {
9284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9285 .rpc_argp = &args,
9286 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009287 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009288 };
9289 int status;
9290
9291 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009292 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009293 if (res.notification & ~args.notify_types)
9294 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009295 if (res.notification != args.notify_types)
9296 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009297
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009298 dprintk("<-- %s status=%d\n", __func__, status);
9299
9300 return status;
9301}
9302
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009303int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9304 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009305 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009306{
9307 struct nfs4_exception exception = { };
9308 int err;
9309
9310 do {
9311 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009312 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009313 &exception);
9314 } while (exception.retry);
9315 return err;
9316}
9317EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9318
Andy Adamson863a3c62011-03-23 13:27:54 +00009319static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9320{
9321 struct nfs4_layoutcommit_data *data = calldata;
9322 struct nfs_server *server = NFS_SERVER(data->args.inode);
9323
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009324 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009325 &data->args.seq_args,
9326 &data->res.seq_res,
9327 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009328}
9329
9330static void
9331nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9332{
9333 struct nfs4_layoutcommit_data *data = calldata;
9334 struct nfs_server *server = NFS_SERVER(data->args.inode);
9335
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009336 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009337 return;
9338
9339 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009340 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9341 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9342 case -NFS4ERR_BADLAYOUT: /* no layout */
9343 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009344 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009345 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009346 break;
9347 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009348 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009349 rpc_restart_call_prepare(task);
9350 return;
9351 }
9352 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009353}
9354
9355static void nfs4_layoutcommit_release(void *calldata)
9356{
9357 struct nfs4_layoutcommit_data *data = calldata;
9358
Andy Adamsondb29c082011-07-30 20:52:38 -04009359 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009360 nfs_post_op_update_inode_force_wcc(data->args.inode,
9361 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009362 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009363 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009364 kfree(data);
9365}
9366
9367static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9368 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9369 .rpc_call_done = nfs4_layoutcommit_done,
9370 .rpc_release = nfs4_layoutcommit_release,
9371};
9372
9373int
Andy Adamsonef311532011-03-12 02:58:10 -05009374nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009375{
9376 struct rpc_message msg = {
9377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9378 .rpc_argp = &data->args,
9379 .rpc_resp = &data->res,
9380 .rpc_cred = data->cred,
9381 };
9382 struct rpc_task_setup task_setup_data = {
9383 .task = &data->task,
9384 .rpc_client = NFS_CLIENT(data->args.inode),
9385 .rpc_message = &msg,
9386 .callback_ops = &nfs4_layoutcommit_ops,
9387 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009388 };
9389 struct rpc_task *task;
9390 int status = 0;
9391
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009392 dprintk("NFS: initiating layoutcommit call. sync %d "
9393 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009394 data->args.lastbytewritten,
9395 data->args.inode->i_ino);
9396
Trond Myklebust472e2592015-02-05 16:50:30 -05009397 if (!sync) {
9398 data->inode = nfs_igrab_and_active(data->args.inode);
9399 if (data->inode == NULL) {
9400 nfs4_layoutcommit_release(data);
9401 return -EAGAIN;
9402 }
9403 task_setup_data.flags = RPC_TASK_ASYNC;
9404 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009405 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009406 task = rpc_run_task(&task_setup_data);
9407 if (IS_ERR(task))
9408 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009409 if (sync)
9410 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009411 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009412 dprintk("%s: status %d\n", __func__, status);
9413 rpc_put_task(task);
9414 return status;
9415}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009416
Trond Myklebust302fad72019-02-18 13:32:38 -05009417/*
Andy Adamson97431202013-08-08 10:57:56 -04009418 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9419 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9420 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009421static int
9422_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009423 struct nfs_fsinfo *info,
9424 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009425{
9426 struct nfs41_secinfo_no_name_args args = {
9427 .style = SECINFO_STYLE_CURRENT_FH,
9428 };
9429 struct nfs4_secinfo_res res = {
9430 .flavors = flavors,
9431 };
9432 struct rpc_message msg = {
9433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9434 .rpc_argp = &args,
9435 .rpc_resp = &res,
9436 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009437 struct rpc_clnt *clnt = server->client;
Anna Schumakercc15e242019-08-14 16:22:31 -04009438 struct nfs4_call_sync_data data = {
9439 .seq_server = server,
9440 .seq_args = &args.seq_args,
9441 .seq_res = &res.seq_res,
9442 };
9443 struct rpc_task_setup task_setup = {
9444 .rpc_client = server->client,
9445 .rpc_message = &msg,
9446 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9447 .callback_data = &data,
9448 .flags = RPC_TASK_NO_ROUND_ROBIN,
9449 };
NeilBrowna52458b2018-12-03 11:30:31 +11009450 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009451 int status;
9452
9453 if (use_integrity) {
9454 clnt = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009455 task_setup.rpc_client = clnt;
9456
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009457 cred = nfs4_get_clid_cred(server->nfs_client);
9458 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009459 }
9460
9461 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009462 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9463 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009464 dprintk("<-- %s status=%d\n", __func__, status);
9465
NeilBrowna52458b2018-12-03 11:30:31 +11009466 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009467
9468 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009469}
9470
9471static int
9472nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9473 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9474{
Trond Myklebust0688e642019-04-07 13:59:09 -04009475 struct nfs4_exception exception = {
9476 .interruptible = true,
9477 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009478 int err;
9479 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009480 /* first try using integrity protection */
9481 err = -NFS4ERR_WRONGSEC;
9482
9483 /* try to use integrity protection with machine cred */
9484 if (_nfs4_is_integrity_protected(server->nfs_client))
9485 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9486 flavors, true);
9487
9488 /*
9489 * if unable to use integrity protection, or SECINFO with
9490 * integrity protection returns NFS4ERR_WRONGSEC (which is
9491 * disallowed by spec, but exists in deployed servers) use
9492 * the current filesystem's rpc_client and the user cred.
9493 */
9494 if (err == -NFS4ERR_WRONGSEC)
9495 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9496 flavors, false);
9497
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009498 switch (err) {
9499 case 0:
9500 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009501 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009502 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009503 default:
9504 err = nfs4_handle_exception(server, err, &exception);
9505 }
9506 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009507out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009508 return err;
9509}
9510
9511static int
9512nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9513 struct nfs_fsinfo *info)
9514{
9515 int err;
9516 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009517 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009518 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009519 struct nfs4_secinfo4 *secinfo;
9520 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009521
9522 page = alloc_page(GFP_KERNEL);
9523 if (!page) {
9524 err = -ENOMEM;
9525 goto out;
9526 }
9527
9528 flavors = page_address(page);
9529 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9530
9531 /*
9532 * Fall back on "guess and check" method if
9533 * the server doesn't support SECINFO_NO_NAME
9534 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009535 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009536 err = nfs4_find_root_sec(server, fhandle, info);
9537 goto out_freepage;
9538 }
9539 if (err)
9540 goto out_freepage;
9541
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009542 for (i = 0; i < flavors->num_flavors; i++) {
9543 secinfo = &flavors->flavors[i];
9544
9545 switch (secinfo->flavor) {
9546 case RPC_AUTH_NULL:
9547 case RPC_AUTH_UNIX:
9548 case RPC_AUTH_GSS:
9549 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9550 &secinfo->flavor_info);
9551 break;
9552 default:
9553 flavor = RPC_AUTH_MAXFLAVOR;
9554 break;
9555 }
9556
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009557 if (!nfs_auth_info_match(&server->auth_info, flavor))
9558 flavor = RPC_AUTH_MAXFLAVOR;
9559
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009560 if (flavor != RPC_AUTH_MAXFLAVOR) {
9561 err = nfs4_lookup_root_sec(server, fhandle,
9562 info, flavor);
9563 if (!err)
9564 break;
9565 }
9566 }
9567
9568 if (flavor == RPC_AUTH_MAXFLAVOR)
9569 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009570
9571out_freepage:
9572 put_page(page);
9573 if (err == -EACCES)
9574 return -EPERM;
9575out:
9576 return err;
9577}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009578
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009579static int _nfs41_test_stateid(struct nfs_server *server,
9580 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009581 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009582{
9583 int status;
9584 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009585 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009586 };
9587 struct nfs41_test_stateid_res res;
9588 struct rpc_message msg = {
9589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9590 .rpc_argp = &args,
9591 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009592 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009593 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009594 struct rpc_clnt *rpc_client = server->client;
9595
9596 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9597 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009598
Chuck Lever38527b12012-07-11 16:30:23 -04009599 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009600 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009601 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009602 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009603 if (status != NFS_OK) {
9604 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009605 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009606 }
9607 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009608 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009609}
9610
Trond Myklebust43912bb2016-09-22 13:38:56 -04009611static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9612 int err, struct nfs4_exception *exception)
9613{
9614 exception->retry = 0;
9615 switch(err) {
9616 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009617 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009618 nfs4_handle_exception(server, err, exception);
9619 break;
9620 case -NFS4ERR_BADSESSION:
9621 case -NFS4ERR_BADSLOT:
9622 case -NFS4ERR_BAD_HIGH_SLOT:
9623 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9624 case -NFS4ERR_DEADSESSION:
9625 nfs4_do_handle_exception(server, err, exception);
9626 }
9627}
9628
Chuck Lever38527b12012-07-11 16:30:23 -04009629/**
9630 * nfs41_test_stateid - perform a TEST_STATEID operation
9631 *
9632 * @server: server / transport on which to perform the operation
9633 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009634 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009635 *
9636 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9637 * Otherwise a negative NFS4ERR value is returned if the operation
9638 * failed or the state ID is not currently valid.
9639 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009640static int nfs41_test_stateid(struct nfs_server *server,
9641 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009642 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009643{
Trond Myklebust0688e642019-04-07 13:59:09 -04009644 struct nfs4_exception exception = {
9645 .interruptible = true,
9646 };
Bryan Schumaker7d974792011-06-02 14:59:08 -04009647 int err;
9648 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009649 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009650 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009651 } while (exception.retry);
9652 return err;
9653}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009654
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009655struct nfs_free_stateid_data {
9656 struct nfs_server *server;
9657 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009658 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009659};
9660
9661static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9662{
9663 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009664 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009665 &data->args.seq_args,
9666 &data->res.seq_res,
9667 task);
9668}
9669
9670static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9671{
9672 struct nfs_free_stateid_data *data = calldata;
9673
9674 nfs41_sequence_done(task, &data->res.seq_res);
9675
9676 switch (task->tk_status) {
9677 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009678 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009679 rpc_restart_call_prepare(task);
9680 }
9681}
9682
9683static void nfs41_free_stateid_release(void *calldata)
9684{
9685 kfree(calldata);
9686}
9687
Trond Myklebust17f26b12013-08-21 15:48:42 -04009688static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009689 .rpc_call_prepare = nfs41_free_stateid_prepare,
9690 .rpc_call_done = nfs41_free_stateid_done,
9691 .rpc_release = nfs41_free_stateid_release,
9692};
9693
Anna Schumaker2f261022018-05-15 13:03:39 -04009694/**
9695 * nfs41_free_stateid - perform a FREE_STATEID operation
9696 *
9697 * @server: server / transport on which to perform the operation
9698 * @stateid: state ID to release
9699 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009700 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009701 *
9702 * Note: this function is always asynchronous.
9703 */
9704static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009705 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009706 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009707 bool privileged)
9708{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009709 struct rpc_message msg = {
9710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009711 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009712 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009713 struct rpc_task_setup task_setup = {
9714 .rpc_client = server->client,
9715 .rpc_message = &msg,
9716 .callback_ops = &nfs41_free_stateid_ops,
9717 .flags = RPC_TASK_ASYNC,
9718 };
9719 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009720 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009721
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009722 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9723 &task_setup.rpc_client, &msg);
9724
Chuck Lever38527b12012-07-11 16:30:23 -04009725 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009726 data = kmalloc(sizeof(*data), GFP_NOFS);
9727 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009728 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009729 data->server = server;
9730 nfs4_stateid_copy(&data->args.stateid, stateid);
9731
9732 task_setup.callback_data = data;
9733
9734 msg.rpc_argp = &data->args;
9735 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009736 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009737 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009738 if (IS_ERR(task))
9739 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009740 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009741 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009742}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009743
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009744static void
9745nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009746{
NeilBrowna52458b2018-12-03 11:30:31 +11009747 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009748
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009749 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009750 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009751}
9752
Trond Myklebust36281ca2012-03-04 18:13:56 -05009753static bool nfs41_match_stateid(const nfs4_stateid *s1,
9754 const nfs4_stateid *s2)
9755{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009756 if (s1->type != s2->type)
9757 return false;
9758
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009759 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009760 return false;
9761
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009762 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009763 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009764
Anna Schumaker045c5512017-01-11 16:59:48 -05009765 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009766}
9767
Andy Adamson557134a2009-04-01 09:21:53 -04009768#endif /* CONFIG_NFS_V4_1 */
9769
Trond Myklebust36281ca2012-03-04 18:13:56 -05009770static bool nfs4_match_stateid(const nfs4_stateid *s1,
9771 const nfs4_stateid *s2)
9772{
Trond Myklebustf597c532012-03-04 18:13:56 -05009773 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009774}
9775
9776
Trond Myklebust17280172012-03-11 13:11:00 -04009777static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009778 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009779 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780 .recover_open = nfs4_open_reclaim,
9781 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009782 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009783 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784};
9785
Andy Adamson591d71c2009-04-01 09:22:47 -04009786#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009787static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009788 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9789 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9790 .recover_open = nfs4_open_reclaim,
9791 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009792 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009793 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009794 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009795};
9796#endif /* CONFIG_NFS_V4_1 */
9797
Trond Myklebust17280172012-03-11 13:11:00 -04009798static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009799 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009800 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009801 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009803 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009804};
9805
Andy Adamson591d71c2009-04-01 09:22:47 -04009806#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009807static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009808 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9809 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009810 .recover_open = nfs41_open_expired,
9811 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009812 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009813};
9814#endif /* CONFIG_NFS_V4_1 */
9815
Trond Myklebust17280172012-03-11 13:11:00 -04009816static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009817 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009818 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009819 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009820};
9821
9822#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009823static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009824 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009825 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009826 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009827};
9828#endif
9829
Chuck Leverec011fe2013-10-17 14:12:39 -04009830static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009831 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009832 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009833};
9834
9835#if defined(CONFIG_NFS_V4_1)
9836static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009837 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009838 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009839};
9840#endif /* CONFIG_NFS_V4_1 */
9841
Trond Myklebust97dc1352010-06-16 09:52:26 -04009842static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9843 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009844 .init_caps = NFS_CAP_READDIRPLUS
9845 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009846 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009847 .init_client = nfs40_init_client,
9848 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009849 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009850 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009851 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009852 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009853 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009854 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009855 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9856 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9857 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009858 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009859};
9860
9861#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009862static struct nfs_seqid *
9863nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9864{
9865 return NULL;
9866}
9867
Trond Myklebust97dc1352010-06-16 09:52:26 -04009868static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9869 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009870 .init_caps = NFS_CAP_READDIRPLUS
9871 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009872 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009873 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009874 | NFS_CAP_ATOMIC_OPEN_V1
9875 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009876 .init_client = nfs41_init_client,
9877 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009878 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009879 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009880 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009881 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009882 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009883 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009884 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009885 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9886 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9887 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009888 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009889};
9890#endif
9891
Steve Dickson42c2c422013-05-22 12:50:38 -04009892#if defined(CONFIG_NFS_V4_2)
9893static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9894 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009895 .init_caps = NFS_CAP_READDIRPLUS
9896 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009897 | NFS_CAP_POSIX_LOCK
9898 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009899 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009900 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009901 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009902 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009903 | NFS_CAP_OFFLOAD_CANCEL
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009904 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009905 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009906 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -05009907 | NFS_CAP_CLONE
9908 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009909 .init_client = nfs41_init_client,
9910 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009911 .match_stateid = nfs41_match_stateid,
9912 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009913 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009914 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009915 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009916 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009917 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009918 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9919 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9920 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009921 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009922};
9923#endif
9924
Trond Myklebust97dc1352010-06-16 09:52:26 -04009925const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9926 [0] = &nfs_v4_0_minor_ops,
9927#if defined(CONFIG_NFS_V4_1)
9928 [1] = &nfs_v4_1_minor_ops,
9929#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009930#if defined(CONFIG_NFS_V4_2)
9931 [2] = &nfs_v4_2_minor_ops,
9932#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009933};
9934
Trond Myklebust13997822016-07-24 17:10:52 -04009935static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009936{
9937 ssize_t error, error2;
9938
9939 error = generic_listxattr(dentry, list, size);
9940 if (error < 0)
9941 return error;
9942 if (list) {
9943 list += error;
9944 size -= error;
9945 }
9946
9947 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9948 if (error2 < 0)
9949 return error2;
9950 return error + error2;
9951}
9952
Trond Myklebust17f26b12013-08-21 15:48:42 -04009953static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009954 .create = nfs_create,
9955 .lookup = nfs_lookup,
9956 .atomic_open = nfs_atomic_open,
9957 .link = nfs_link,
9958 .unlink = nfs_unlink,
9959 .symlink = nfs_symlink,
9960 .mkdir = nfs_mkdir,
9961 .rmdir = nfs_rmdir,
9962 .mknod = nfs_mknod,
9963 .rename = nfs_rename,
9964 .permission = nfs_permission,
9965 .getattr = nfs_getattr,
9966 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009967 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009968};
9969
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009970static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009971 .permission = nfs_permission,
9972 .getattr = nfs_getattr,
9973 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009974 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009975};
9976
David Howells509de812006-08-22 20:06:11 -04009977const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009978 .version = 4, /* protocol version */
9979 .dentry_ops = &nfs4_dentry_operations,
9980 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009981 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009982 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009983 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009984 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009985 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009986 .getattr = nfs4_proc_getattr,
9987 .setattr = nfs4_proc_setattr,
9988 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009989 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009990 .access = nfs4_proc_access,
9991 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009992 .create = nfs4_proc_create,
9993 .remove = nfs4_proc_remove,
9994 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009995 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009996 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009997 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009998 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009999 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010000 .link = nfs4_proc_link,
10001 .symlink = nfs4_proc_symlink,
10002 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010003 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010004 .readdir = nfs4_proc_readdir,
10005 .mknod = nfs4_proc_mknod,
10006 .statfs = nfs4_proc_statfs,
10007 .fsinfo = nfs4_proc_fsinfo,
10008 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010009 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010010 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010011 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010013 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010014 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010015 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010016 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010017 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010018 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010019 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010020 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010021 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010022 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010023 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010024 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010025 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -040010026 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010027 .create_server = nfs4_create_server,
10028 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010029};
10030
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010031static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010032 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010033 .list = nfs4_xattr_list_nfs4_acl,
10034 .get = nfs4_xattr_get_nfs4_acl,
10035 .set = nfs4_xattr_set_nfs4_acl,
10036};
10037
10038const struct xattr_handler *nfs4_xattr_handlers[] = {
10039 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010040#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10041 &nfs4_xattr_nfs4_label_handler,
10042#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010043 NULL
10044};
10045
Linus Torvalds1da177e2005-04-16 15:20:36 -070010046/*
10047 * Local variables:
10048 * c-basic-offset: 8
10049 * End:
10050 */