blob: 33a8e53e976c67e30ef0b4f094deb1689c0d4f12 [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);
6201 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006202 case -NFS4ERR_ACCESS:
6203 if (data->args.bitmask) {
6204 data->args.bitmask = NULL;
6205 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006206 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006207 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006208 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006209 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006210 task->tk_status = nfs4_async_handle_exception(task,
6211 data->res.server, task->tk_status,
6212 &exception);
6213 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006214 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006215 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006216 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006217 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006218 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006219out_restart:
6220 task->tk_status = 0;
6221 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006222}
6223
6224static void nfs4_delegreturn_release(void *calldata)
6225{
Peng Tao039b7562014-07-03 13:05:02 +08006226 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006227 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006228
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006229 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006230 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006231 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6232 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006233 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006234 nfs_iput_and_deactive(inode);
6235 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006236 kfree(calldata);
6237}
6238
Andy Adamson938e1012009-04-01 09:22:28 -04006239static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6240{
6241 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006242 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006243
6244 d_data = (struct nfs4_delegreturndata *)data;
6245
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006246 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08006247 return;
6248
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006249 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6250 if (lo && !pnfs_layout_is_valid(lo)) {
6251 d_data->args.lr_args = NULL;
6252 d_data->res.lr_res = NULL;
6253 }
6254
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006255 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006256 &d_data->args.seq_args,
6257 &d_data->res.seq_res,
6258 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006259}
Andy Adamson938e1012009-04-01 09:22:28 -04006260
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006261static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006262 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006263 .rpc_call_done = nfs4_delegreturn_done,
6264 .rpc_release = nfs4_delegreturn_release,
6265};
6266
NeilBrowna52458b2018-12-03 11:30:31 +11006267static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006268{
6269 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006270 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006271 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006272 struct rpc_message msg = {
6273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6274 .rpc_cred = cred,
6275 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006276 struct rpc_task_setup task_setup_data = {
6277 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006278 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006279 .callback_ops = &nfs4_delegreturn_ops,
6280 .flags = RPC_TASK_ASYNC,
6281 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006282 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006283
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006284 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006285 if (data == NULL)
6286 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006287 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006288
6289 nfs4_state_protect(server->nfs_client,
6290 NFS_SP4_MACH_CRED_CLEANUP,
6291 &task_setup_data.rpc_client, &msg);
6292
Trond Myklebustfe650402006-01-03 09:55:18 +01006293 data->args.fhandle = &data->fh;
6294 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006295 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006296 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006297 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006298 data->res.fattr = &data->fattr;
6299 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006300 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006301 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006302 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006303 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006304 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006305 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006306 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006307 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006308 if (data->lr.roc) {
6309 data->args.lr_args = &data->lr.arg;
6310 data->res.lr_res = &data->lr.res;
6311 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006312 } else if (data->lr.roc) {
6313 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6314 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006315 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006316
Trond Myklebustc970aa82007-07-14 15:39:59 -04006317 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006318 msg.rpc_argp = &data->args;
6319 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006320 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006321 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006322 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006323 if (!issync)
6324 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006325 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006326 if (status != 0)
6327 goto out;
6328 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006329out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006330 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006331 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332}
6333
NeilBrowna52458b2018-12-03 11:30:31 +11006334int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335{
6336 struct nfs_server *server = NFS_SERVER(inode);
6337 struct nfs4_exception exception = { };
6338 int err;
6339 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006340 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006341 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 switch (err) {
6343 case -NFS4ERR_STALE_STATEID:
6344 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 case 0:
6346 return 0;
6347 }
6348 err = nfs4_handle_exception(server, err, &exception);
6349 } while (exception.retry);
6350 return err;
6351}
6352
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6354{
6355 struct inode *inode = state->inode;
6356 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006357 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006358 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006360 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006362 struct nfs_lockt_res res = {
6363 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 };
6365 struct rpc_message msg = {
6366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006367 .rpc_argp = &arg,
6368 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 .rpc_cred = state->owner->so_cred,
6370 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 struct nfs4_lock_state *lsp;
6372 int status;
6373
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006374 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006375 status = nfs4_set_lock_state(state, request);
6376 if (status != 0)
6377 goto out;
6378 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006379 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006380 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006381 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006382 switch (status) {
6383 case 0:
6384 request->fl_type = F_UNLCK;
6385 break;
6386 case -NFS4ERR_DENIED:
6387 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006389 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006390 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006391out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 return status;
6393}
6394
6395static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6396{
Trond Myklebust0688e642019-04-07 13:59:09 -04006397 struct nfs4_exception exception = {
6398 .interruptible = true,
6399 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 int err;
6401
6402 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006403 err = _nfs4_proc_getlk(state, cmd, request);
6404 trace_nfs4_get_lock(request, state, cmd, err);
6405 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 &exception);
6407 } while (exception.retry);
6408 return err;
6409}
6410
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006411/*
6412 * Update the seqid of a lock stateid after receiving
6413 * NFS4ERR_OLD_STATEID
6414 */
6415static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6416 struct nfs4_lock_state *lsp)
6417{
6418 struct nfs4_state *state = lsp->ls_state;
6419 bool ret = false;
6420
6421 spin_lock(&state->state_lock);
6422 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6423 goto out;
6424 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6425 nfs4_stateid_seqid_inc(dst);
6426 else
6427 dst->seqid = lsp->ls_stateid.seqid;
6428 ret = true;
6429out:
6430 spin_unlock(&state->state_lock);
6431 return ret;
6432}
6433
6434static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6435 struct nfs4_lock_state *lsp)
6436{
6437 struct nfs4_state *state = lsp->ls_state;
6438 bool ret;
6439
6440 spin_lock(&state->state_lock);
6441 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6442 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6443 spin_unlock(&state->state_lock);
6444 return ret;
6445}
6446
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006447struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006448 struct nfs_locku_args arg;
6449 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006450 struct nfs4_lock_state *lsp;
6451 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006452 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006453 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006454 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006455 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006456};
6457
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006458static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6459 struct nfs_open_context *ctx,
6460 struct nfs4_lock_state *lsp,
6461 struct nfs_seqid *seqid)
6462{
6463 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006464 struct nfs4_state *state = lsp->ls_state;
6465 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006466
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006467 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006468 if (p == NULL)
6469 return NULL;
6470 p->arg.fh = NFS_FH(inode);
6471 p->arg.fl = &p->fl;
6472 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006473 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006474 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006475 /* Ensure we don't close file until we're done freeing locks! */
6476 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006477 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006478 locks_init_lock(&p->fl);
6479 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006480 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006481 spin_lock(&state->state_lock);
6482 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6483 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006484 return p;
6485}
6486
Trond Myklebust06f814a2006-01-03 09:55:07 +01006487static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006488{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006489 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006490 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006491 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006492 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006493 put_nfs_open_context(calldata->ctx);
6494 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006495}
6496
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006497static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006498{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006499 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006500 struct nfs4_exception exception = {
6501 .inode = calldata->lsp->ls_state->inode,
6502 .stateid = &calldata->arg.stateid,
6503 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006504
Trond Myklebust14516c32010-07-31 14:29:06 -04006505 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6506 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006507 switch (task->tk_status) {
6508 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006509 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006510 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006511 if (nfs4_update_lock_stateid(calldata->lsp,
6512 &calldata->res.stateid))
6513 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006514 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006515 case -NFS4ERR_ADMIN_REVOKED:
6516 case -NFS4ERR_EXPIRED:
6517 nfs4_free_revoked_stateid(calldata->server,
6518 &calldata->arg.stateid,
6519 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006520 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006521 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006522 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006523 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6524 calldata->lsp))
6525 rpc_restart_call_prepare(task);
6526 break;
6527 case -NFS4ERR_OLD_STATEID:
6528 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6529 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006530 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006531 break;
6532 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006533 task->tk_status = nfs4_async_handle_exception(task,
6534 calldata->server, task->tk_status,
6535 &exception);
6536 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006537 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006538 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006539 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006540}
6541
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006542static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006543{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006544 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006546 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6547 nfs_async_iocounter_wait(task, calldata->l_ctx))
6548 return;
6549
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006550 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006551 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006552 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006553 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006554 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006555 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006556 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006557 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006558 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006559 &calldata->res.seq_res,
6560 task) != 0)
6561 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006562 return;
6563out_no_action:
6564 task->tk_action = NULL;
6565out_wait:
6566 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567}
6568
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006569static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006570 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006571 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006572 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006573};
6574
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006575static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6576 struct nfs_open_context *ctx,
6577 struct nfs4_lock_state *lsp,
6578 struct nfs_seqid *seqid)
6579{
6580 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006581 struct rpc_message msg = {
6582 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6583 .rpc_cred = ctx->cred,
6584 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006585 struct rpc_task_setup task_setup_data = {
6586 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006587 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006588 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006589 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006590 .flags = RPC_TASK_ASYNC,
6591 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006592
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006593 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6594 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6595
Frank Filz137d6ac2007-07-09 15:32:29 -07006596 /* Ensure this is an unlock - when canceling a lock, the
6597 * canceled lock is passed in, and it won't be an unlock.
6598 */
6599 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006600 if (fl->fl_flags & FL_CLOSE)
6601 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006602
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006603 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6604 if (data == NULL) {
6605 nfs_free_seqid(seqid);
6606 return ERR_PTR(-ENOMEM);
6607 }
6608
Anna Schumakerfba83f32018-05-04 16:22:50 -04006609 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006610 msg.rpc_argp = &data->arg;
6611 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006612 task_setup_data.callback_data = data;
6613 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006614}
6615
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6617{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006618 struct inode *inode = state->inode;
6619 struct nfs4_state_owner *sp = state->owner;
6620 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006621 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006622 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006623 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006624 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006625 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006626 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627
Trond Myklebust9b073572006-06-29 16:38:34 -04006628 status = nfs4_set_lock_state(state, request);
6629 /* Unlock _before_ we do the RPC call */
6630 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006631 /* Exclude nfs_delegation_claim_locks() */
6632 mutex_lock(&sp->so_delegreturn_mutex);
6633 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006634 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006635 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006636 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006637 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006638 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006639 }
6640 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 if (status != 0)
6643 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006644 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006645 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006646 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6647 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006648 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6649 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006650 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006651 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006652 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006653 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006654 status = PTR_ERR(task);
6655 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006656 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006657 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006658 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006659out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006660 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006661 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006662 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663}
6664
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006665struct nfs4_lockdata {
6666 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006667 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006668 struct nfs4_lock_state *lsp;
6669 struct nfs_open_context *ctx;
6670 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006671 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006672 int rpc_status;
6673 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006674 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006675};
6676
6677static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006678 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6679 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006680{
6681 struct nfs4_lockdata *p;
6682 struct inode *inode = lsp->ls_state->inode;
6683 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006684 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006685
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006686 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006687 if (p == NULL)
6688 return NULL;
6689
6690 p->arg.fh = NFS_FH(inode);
6691 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006692 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006693 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006694 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006695 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6696 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006697 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006698 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006699 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006700 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006701 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006702 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006703 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006704 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006705 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006706 locks_init_lock(&p->fl);
6707 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006708 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006709out_free_seqid:
6710 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006711out_free:
6712 kfree(p);
6713 return NULL;
6714}
6715
6716static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6717{
6718 struct nfs4_lockdata *data = calldata;
6719 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720
Harvey Harrison3110ff82008-05-02 13:42:44 -07006721 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006722 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006723 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006724 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006725 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006726 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006727 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006728 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006729 nfs4_stateid_copy(&data->arg.open_stateid,
6730 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006731 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006732 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006733 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006734 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006735 nfs4_stateid_copy(&data->arg.lock_stateid,
6736 &data->lsp->ls_stateid);
6737 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006738 if (!nfs4_valid_open_stateid(state)) {
6739 data->rpc_status = -EBADF;
6740 task->tk_action = NULL;
6741 goto out_release_open_seqid;
6742 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006743 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006744 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006745 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006746 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006747 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006748 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006749out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006750 nfs_release_seqid(data->arg.open_seqid);
6751out_release_lock_seqid:
6752 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006753out_wait:
6754 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006755 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006756}
6757
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006758static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6759{
6760 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006761 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006762
Harvey Harrison3110ff82008-05-02 13:42:44 -07006763 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006764
Trond Myklebust14516c32010-07-31 14:29:06 -04006765 if (!nfs4_sequence_done(task, &data->res.seq_res))
6766 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006767
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006768 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006769 switch (task->tk_status) {
6770 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006771 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006772 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006773 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006774 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006775 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006776 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006777 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006778 if (data->arg.new_lock_owner != 0) {
6779 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6780 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6781 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006782 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6783 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006784 break;
6785 case -NFS4ERR_BAD_STATEID:
6786 case -NFS4ERR_OLD_STATEID:
6787 case -NFS4ERR_STALE_STATEID:
6788 case -NFS4ERR_EXPIRED:
6789 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006790 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006791 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006792 goto out_restart;
6793 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006794 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006795 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006796 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006797out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006798 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006799 return;
6800out_restart:
6801 if (!data->cancelled)
6802 rpc_restart_call_prepare(task);
6803 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006804}
6805
6806static void nfs4_lock_release(void *calldata)
6807{
6808 struct nfs4_lockdata *data = calldata;
6809
Harvey Harrison3110ff82008-05-02 13:42:44 -07006810 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006811 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006812 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006813 struct rpc_task *task;
6814 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6815 data->arg.lock_seqid);
6816 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006817 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006818 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006819 } else
6820 nfs_free_seqid(data->arg.lock_seqid);
6821 nfs4_put_lock_state(data->lsp);
6822 put_nfs_open_context(data->ctx);
6823 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006824 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006825}
6826
6827static const struct rpc_call_ops nfs4_lock_ops = {
6828 .rpc_call_prepare = nfs4_lock_prepare,
6829 .rpc_call_done = nfs4_lock_done,
6830 .rpc_release = nfs4_lock_release,
6831};
6832
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006833static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6834{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006835 switch (error) {
6836 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006837 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006838 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006839 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006840 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006841 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006842 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006843 break;
6844 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006845 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006846 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006847 };
6848}
6849
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006850static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006851{
6852 struct nfs4_lockdata *data;
6853 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006854 struct rpc_message msg = {
6855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6856 .rpc_cred = state->owner->so_cred,
6857 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006858 struct rpc_task_setup task_setup_data = {
6859 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006860 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006861 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006862 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006863 .flags = RPC_TASK_ASYNC,
6864 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006865 int ret;
6866
Harvey Harrison3110ff82008-05-02 13:42:44 -07006867 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006868 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006869 fl->fl_u.nfs4_fl.owner,
6870 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006871 if (data == NULL)
6872 return -ENOMEM;
6873 if (IS_SETLKW(cmd))
6874 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006875 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6876 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006877 msg.rpc_argp = &data->arg;
6878 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006879 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006880 if (recovery_type > NFS_LOCK_NEW) {
6881 if (recovery_type == NFS_LOCK_RECLAIM)
6882 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006883 } else
6884 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006885 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006886 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006887 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006888 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006889 if (ret == 0) {
6890 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006891 if (ret)
6892 nfs4_handle_setlk_error(data->server, data->lsp,
6893 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006894 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006895 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006896 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006897 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006898 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006899 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900}
6901
6902static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6903{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006904 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006905 struct nfs4_exception exception = {
6906 .inode = state->inode,
6907 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006908 int err;
6909
6910 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006911 /* Cache the lock if possible... */
6912 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6913 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006914 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006915 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006916 break;
6917 nfs4_handle_exception(server, err, &exception);
6918 } while (exception.retry);
6919 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920}
6921
6922static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6923{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006924 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006925 struct nfs4_exception exception = {
6926 .inode = state->inode,
6927 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006928 int err;
6929
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006930 err = nfs4_set_lock_state(state, request);
6931 if (err != 0)
6932 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006933 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006934 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6935 return 0;
6936 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006937 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006938 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6939 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006940 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006941 switch (err) {
6942 default:
6943 goto out;
6944 case -NFS4ERR_GRACE:
6945 case -NFS4ERR_DELAY:
6946 nfs4_handle_exception(server, err, &exception);
6947 err = 0;
6948 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006949 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006950out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006951 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952}
6953
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006954#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006955static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6956{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006957 struct nfs4_lock_state *lsp;
6958 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006959
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006960 status = nfs4_set_lock_state(state, request);
6961 if (status != 0)
6962 return status;
6963 lsp = request->fl_u.nfs4_fl.owner;
6964 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6965 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6966 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006967 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006968}
6969#endif
6970
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6972{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006973 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006974 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006975 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006976 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977
Trond Myklebust01c3b862006-06-29 16:38:39 -04006978 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006979 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006980 if (status < 0)
6981 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006982 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006983 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006984 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006985 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006986 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006987 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006988 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006989 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006990 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006991 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006992 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006993 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006994 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006995 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006996out:
6997 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 return status;
6999}
7000
7001static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7002{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007003 struct nfs4_exception exception = {
7004 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007005 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007006 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007007 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 int err;
7009
7010 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007011 err = _nfs4_proc_setlk(state, cmd, request);
7012 if (err == -NFS4ERR_DENIED)
7013 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007015 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 } while (exception.retry);
7017 return err;
7018}
7019
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007020#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7021#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7022
7023static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007024nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7025 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007026{
7027 int status = -ERESTARTSYS;
7028 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7029
7030 while(!signalled()) {
7031 status = nfs4_proc_setlk(state, cmd, request);
7032 if ((status != -EAGAIN) || IS_SETLK(cmd))
7033 break;
7034 freezable_schedule_timeout_interruptible(timeout);
7035 timeout *= 2;
7036 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7037 status = -ERESTARTSYS;
7038 }
7039 return status;
7040}
7041
Jeff Laytona1d617d82016-09-17 18:17:39 -04007042#ifdef CONFIG_NFS_V4_1
7043struct nfs4_lock_waiter {
7044 struct task_struct *task;
7045 struct inode *inode;
7046 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007047};
7048
7049static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007050nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007051{
7052 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007053 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007054
Jeff Layton57174592018-03-18 08:37:03 -04007055 /* NULL key means to wake up everyone */
7056 if (key) {
7057 struct cb_notify_lock_args *cbnl = key;
7058 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7059 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007060
Jeff Layton57174592018-03-18 08:37:03 -04007061 /* Only wake if the callback was for the same owner. */
7062 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7063 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007064
Jeff Layton57174592018-03-18 08:37:03 -04007065 /* Make sure it's for the right inode */
7066 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7067 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007068 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007069
7070 /* override "private" so we can use default_wake_function */
7071 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007072 ret = woken_wake_function(wait, mode, flags, key);
7073 if (ret)
7074 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007075 wait->private = waiter;
7076 return ret;
7077}
7078
7079static int
7080nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7081{
7082 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007083 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7084 struct nfs_server *server = NFS_SERVER(state->inode);
7085 struct nfs_client *clp = server->nfs_client;
7086 wait_queue_head_t *q = &clp->cl_lock_waitq;
7087 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7088 .id = lsp->ls_seqid.owner_id,
7089 .s_dev = server->s_dev };
7090 struct nfs4_lock_waiter waiter = { .task = current,
7091 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007092 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007093 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007094
7095 /* Don't bother with waitqueue if we don't expect a callback */
7096 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7097 return nfs4_retry_setlk_simple(state, cmd, request);
7098
7099 init_wait(&wait);
7100 wait.private = &waiter;
7101 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007102
7103 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007104 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007105 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007106 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7107 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007108 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007109 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007110
7111 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007112 freezer_do_not_count();
7113 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7114 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007115 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007116 }
7117
Jeff Laytona1d617d82016-09-17 18:17:39 -04007118 return status;
7119}
7120#else /* !CONFIG_NFS_V4_1 */
7121static inline int
7122nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7123{
7124 return nfs4_retry_setlk_simple(state, cmd, request);
7125}
7126#endif
7127
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128static int
7129nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7130{
7131 struct nfs_open_context *ctx;
7132 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 int status;
7134
7135 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007136 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 state = ctx->state;
7138
Trond Myklebustd9531262009-07-21 19:22:38 -04007139 if (IS_GETLK(cmd)) {
7140 if (state != NULL)
7141 return nfs4_proc_getlk(state, F_GETLK, request);
7142 return 0;
7143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144
7145 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7146 return -EINVAL;
7147
Trond Myklebustd9531262009-07-21 19:22:38 -04007148 if (request->fl_type == F_UNLCK) {
7149 if (state != NULL)
7150 return nfs4_proc_unlck(state, cmd, request);
7151 return 0;
7152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153
Trond Myklebustd9531262009-07-21 19:22:38 -04007154 if (state == NULL)
7155 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007156
7157 if ((request->fl_flags & FL_POSIX) &&
7158 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7159 return -ENOLCK;
7160
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007161 /*
7162 * Don't rely on the VFS having checked the file open mode,
7163 * since it won't do this for flock() locks.
7164 */
7165 switch (request->fl_type) {
7166 case F_RDLCK:
7167 if (!(filp->f_mode & FMODE_READ))
7168 return -EBADF;
7169 break;
7170 case F_WRLCK:
7171 if (!(filp->f_mode & FMODE_WRITE))
7172 return -EBADF;
7173 }
7174
Jeff Layton1ea67db2016-09-17 18:17:37 -04007175 status = nfs4_set_lock_state(state, request);
7176 if (status != 0)
7177 return status;
7178
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007179 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180}
7181
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007182int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007183{
7184 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007185 int err;
7186
7187 err = nfs4_set_lock_state(state, fl);
7188 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007189 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007190 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007191 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007192}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007193
Trond Myklebustcf470c32012-03-07 13:49:12 -05007194struct nfs_release_lockowner_data {
7195 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007196 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007197 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007198 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007199 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007200};
7201
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007202static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7203{
7204 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007205 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007206 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7207 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007208 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007209 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007210}
7211
7212static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7213{
7214 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007215 struct nfs_server *server = data->server;
7216
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007217 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007218
7219 switch (task->tk_status) {
7220 case 0:
7221 renew_lease(server, data->timestamp);
7222 break;
7223 case -NFS4ERR_STALE_CLIENTID:
7224 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007225 nfs4_schedule_lease_recovery(server->nfs_client);
7226 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007227 case -NFS4ERR_LEASE_MOVED:
7228 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007229 if (nfs4_async_handle_error(task, server,
7230 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007231 rpc_restart_call_prepare(task);
7232 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007233}
7234
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007235static void nfs4_release_lockowner_release(void *calldata)
7236{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007237 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007238 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007239 kfree(calldata);
7240}
7241
Trond Myklebust17280172012-03-11 13:11:00 -04007242static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007243 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7244 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007245 .rpc_release = nfs4_release_lockowner_release,
7246};
7247
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007248static void
7249nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007250{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007251 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007252 struct rpc_message msg = {
7253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7254 };
7255
7256 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007257 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007258
Trond Myklebustcf470c32012-03-07 13:49:12 -05007259 data = kmalloc(sizeof(*data), GFP_NOFS);
7260 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007261 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007262 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007263 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007264 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7265 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7266 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007267
Trond Myklebustcf470c32012-03-07 13:49:12 -05007268 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007269 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007270 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007271 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007272}
7273
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007274#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7275
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007276static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007277 struct dentry *unused, struct inode *inode,
7278 const char *key, const void *buf,
7279 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007280{
Al Viro59301222016-05-27 10:19:30 -04007281 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007282}
7283
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007284static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007285 struct dentry *unused, struct inode *inode,
7286 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007287{
Al Virob2968212016-04-10 20:48:24 -04007288 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007289}
7290
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007291static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007292{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007293 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007294}
7295
David Quigleyc9bccef2013-05-22 12:50:45 -04007296#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007297
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007298static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007299 struct dentry *unused, struct inode *inode,
7300 const char *key, const void *buf,
7301 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007302{
7303 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007304 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007305
7306 return -EOPNOTSUPP;
7307}
7308
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007309static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007310 struct dentry *unused, struct inode *inode,
7311 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007312{
7313 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007314 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007315 return -EOPNOTSUPP;
7316}
7317
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007318static ssize_t
7319nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007320{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007321 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007322
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007323 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7324 len = security_inode_listsecurity(inode, list, list_len);
7325 if (list_len && len > list_len)
7326 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007327 }
7328 return len;
7329}
7330
7331static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7332 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007333 .get = nfs4_xattr_get_nfs4_label,
7334 .set = nfs4_xattr_set_nfs4_label,
7335};
David Quigleyc9bccef2013-05-22 12:50:45 -04007336
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007337#else
7338
7339static ssize_t
7340nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7341{
7342 return 0;
7343}
7344
7345#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007346
Andy Adamson533eb462011-06-13 18:25:56 -04007347/*
7348 * nfs_fhget will use either the mounted_on_fileid or the fileid
7349 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007350static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7351{
Andy Adamson533eb462011-06-13 18:25:56 -04007352 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7353 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7354 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007355 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007356 return;
7357
7358 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007359 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007360 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7361 fattr->nlink = 2;
7362}
7363
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007364static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7365 const struct qstr *name,
7366 struct nfs4_fs_locations *fs_locations,
7367 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007368{
7369 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007370 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007371 struct nfs4_fs_locations_arg args = {
7372 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007373 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007374 .page = page,
7375 .bitmask = bitmask,
7376 };
Benny Halevy22958462009-04-01 09:22:02 -04007377 struct nfs4_fs_locations_res res = {
7378 .fs_locations = fs_locations,
7379 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007380 struct rpc_message msg = {
7381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7382 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007383 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007384 };
7385 int status;
7386
Harvey Harrison3110ff82008-05-02 13:42:44 -07007387 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007388
Chuck Leverc05cefc2017-11-05 15:45:22 -05007389 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7390 bitmask[1] = nfs4_fattr_bitmap[1];
7391
Andy Adamson533eb462011-06-13 18:25:56 -04007392 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7393 * is not supported */
7394 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007395 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007396 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007397 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007398
Trond Myklebustc228fd32007-01-13 02:28:11 -05007399 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007400 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007401 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007402 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007403 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007404 return status;
7405}
7406
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007407int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7408 const struct qstr *name,
7409 struct nfs4_fs_locations *fs_locations,
7410 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007411{
Trond Myklebust0688e642019-04-07 13:59:09 -04007412 struct nfs4_exception exception = {
7413 .interruptible = true,
7414 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007415 int err;
7416 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007417 err = _nfs4_proc_fs_locations(client, dir, name,
7418 fs_locations, page);
7419 trace_nfs4_get_fs_locations(dir, name, err);
7420 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007421 &exception);
7422 } while (exception.retry);
7423 return err;
7424}
7425
Chuck Leverb03d7352013-10-17 14:12:50 -04007426/*
7427 * This operation also signals the server that this client is
7428 * performing migration recovery. The server can stop returning
7429 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7430 * appended to this compound to identify the client ID which is
7431 * performing recovery.
7432 */
7433static int _nfs40_proc_get_locations(struct inode *inode,
7434 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007435 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007436{
7437 struct nfs_server *server = NFS_SERVER(inode);
7438 struct rpc_clnt *clnt = server->client;
7439 u32 bitmask[2] = {
7440 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7441 };
7442 struct nfs4_fs_locations_arg args = {
7443 .clientid = server->nfs_client->cl_clientid,
7444 .fh = NFS_FH(inode),
7445 .page = page,
7446 .bitmask = bitmask,
7447 .migration = 1, /* skip LOOKUP */
7448 .renew = 1, /* append RENEW */
7449 };
7450 struct nfs4_fs_locations_res res = {
7451 .fs_locations = locations,
7452 .migration = 1,
7453 .renew = 1,
7454 };
7455 struct rpc_message msg = {
7456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7457 .rpc_argp = &args,
7458 .rpc_resp = &res,
7459 .rpc_cred = cred,
7460 };
7461 unsigned long now = jiffies;
7462 int status;
7463
7464 nfs_fattr_init(&locations->fattr);
7465 locations->server = server;
7466 locations->nlocations = 0;
7467
Anna Schumakerfba83f32018-05-04 16:22:50 -04007468 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007469 status = nfs4_call_sync_sequence(clnt, server, &msg,
7470 &args.seq_args, &res.seq_res);
7471 if (status)
7472 return status;
7473
7474 renew_lease(server, now);
7475 return 0;
7476}
7477
7478#ifdef CONFIG_NFS_V4_1
7479
7480/*
7481 * This operation also signals the server that this client is
7482 * performing migration recovery. The server can stop asserting
7483 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7484 * performing this operation is identified in the SEQUENCE
7485 * operation in this compound.
7486 *
7487 * When the client supports GETATTR(fs_locations_info), it can
7488 * be plumbed in here.
7489 */
7490static int _nfs41_proc_get_locations(struct inode *inode,
7491 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007492 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007493{
7494 struct nfs_server *server = NFS_SERVER(inode);
7495 struct rpc_clnt *clnt = server->client;
7496 u32 bitmask[2] = {
7497 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7498 };
7499 struct nfs4_fs_locations_arg args = {
7500 .fh = NFS_FH(inode),
7501 .page = page,
7502 .bitmask = bitmask,
7503 .migration = 1, /* skip LOOKUP */
7504 };
7505 struct nfs4_fs_locations_res res = {
7506 .fs_locations = locations,
7507 .migration = 1,
7508 };
7509 struct rpc_message msg = {
7510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7511 .rpc_argp = &args,
7512 .rpc_resp = &res,
7513 .rpc_cred = cred,
7514 };
7515 int status;
7516
7517 nfs_fattr_init(&locations->fattr);
7518 locations->server = server;
7519 locations->nlocations = 0;
7520
Anna Schumakerfba83f32018-05-04 16:22:50 -04007521 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007522 status = nfs4_call_sync_sequence(clnt, server, &msg,
7523 &args.seq_args, &res.seq_res);
7524 if (status == NFS4_OK &&
7525 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7526 status = -NFS4ERR_LEASE_MOVED;
7527 return status;
7528}
7529
7530#endif /* CONFIG_NFS_V4_1 */
7531
7532/**
7533 * nfs4_proc_get_locations - discover locations for a migrated FSID
7534 * @inode: inode on FSID that is migrating
7535 * @locations: result of query
7536 * @page: buffer
7537 * @cred: credential to use for this operation
7538 *
7539 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7540 * operation failed, or a negative errno if a local error occurred.
7541 *
7542 * On success, "locations" is filled in, but if the server has
7543 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7544 * asserted.
7545 *
7546 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7547 * from this client that require migration recovery.
7548 */
7549int nfs4_proc_get_locations(struct inode *inode,
7550 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007551 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007552{
7553 struct nfs_server *server = NFS_SERVER(inode);
7554 struct nfs_client *clp = server->nfs_client;
7555 const struct nfs4_mig_recovery_ops *ops =
7556 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007557 struct nfs4_exception exception = {
7558 .interruptible = true,
7559 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007560 int status;
7561
7562 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7563 (unsigned long long)server->fsid.major,
7564 (unsigned long long)server->fsid.minor,
7565 clp->cl_hostname);
7566 nfs_display_fhandle(NFS_FH(inode), __func__);
7567
7568 do {
7569 status = ops->get_locations(inode, locations, page, cred);
7570 if (status != -NFS4ERR_DELAY)
7571 break;
7572 nfs4_handle_exception(server, status, &exception);
7573 } while (exception.retry);
7574 return status;
7575}
7576
Chuck Lever44c99932013-10-17 14:13:30 -04007577/*
7578 * This operation also signals the server that this client is
7579 * performing "lease moved" recovery. The server can stop
7580 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7581 * is appended to this compound to identify the client ID which is
7582 * performing recovery.
7583 */
NeilBrowna52458b2018-12-03 11:30:31 +11007584static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007585{
7586 struct nfs_server *server = NFS_SERVER(inode);
7587 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7588 struct rpc_clnt *clnt = server->client;
7589 struct nfs4_fsid_present_arg args = {
7590 .fh = NFS_FH(inode),
7591 .clientid = clp->cl_clientid,
7592 .renew = 1, /* append RENEW */
7593 };
7594 struct nfs4_fsid_present_res res = {
7595 .renew = 1,
7596 };
7597 struct rpc_message msg = {
7598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7599 .rpc_argp = &args,
7600 .rpc_resp = &res,
7601 .rpc_cred = cred,
7602 };
7603 unsigned long now = jiffies;
7604 int status;
7605
7606 res.fh = nfs_alloc_fhandle();
7607 if (res.fh == NULL)
7608 return -ENOMEM;
7609
Anna Schumakerfba83f32018-05-04 16:22:50 -04007610 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007611 status = nfs4_call_sync_sequence(clnt, server, &msg,
7612 &args.seq_args, &res.seq_res);
7613 nfs_free_fhandle(res.fh);
7614 if (status)
7615 return status;
7616
7617 do_renew_lease(clp, now);
7618 return 0;
7619}
7620
7621#ifdef CONFIG_NFS_V4_1
7622
7623/*
7624 * This operation also signals the server that this client is
7625 * performing "lease moved" recovery. The server can stop asserting
7626 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7627 * this operation is identified in the SEQUENCE operation in this
7628 * compound.
7629 */
NeilBrowna52458b2018-12-03 11:30:31 +11007630static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007631{
7632 struct nfs_server *server = NFS_SERVER(inode);
7633 struct rpc_clnt *clnt = server->client;
7634 struct nfs4_fsid_present_arg args = {
7635 .fh = NFS_FH(inode),
7636 };
7637 struct nfs4_fsid_present_res res = {
7638 };
7639 struct rpc_message msg = {
7640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7641 .rpc_argp = &args,
7642 .rpc_resp = &res,
7643 .rpc_cred = cred,
7644 };
7645 int status;
7646
7647 res.fh = nfs_alloc_fhandle();
7648 if (res.fh == NULL)
7649 return -ENOMEM;
7650
Anna Schumakerfba83f32018-05-04 16:22:50 -04007651 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007652 status = nfs4_call_sync_sequence(clnt, server, &msg,
7653 &args.seq_args, &res.seq_res);
7654 nfs_free_fhandle(res.fh);
7655 if (status == NFS4_OK &&
7656 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7657 status = -NFS4ERR_LEASE_MOVED;
7658 return status;
7659}
7660
7661#endif /* CONFIG_NFS_V4_1 */
7662
7663/**
7664 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7665 * @inode: inode on FSID to check
7666 * @cred: credential to use for this operation
7667 *
7668 * Server indicates whether the FSID is present, moved, or not
7669 * recognized. This operation is necessary to clear a LEASE_MOVED
7670 * condition for this client ID.
7671 *
7672 * Returns NFS4_OK if the FSID is present on this server,
7673 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7674 * NFS4ERR code if some error occurred on the server, or a
7675 * negative errno if a local failure occurred.
7676 */
NeilBrowna52458b2018-12-03 11:30:31 +11007677int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007678{
7679 struct nfs_server *server = NFS_SERVER(inode);
7680 struct nfs_client *clp = server->nfs_client;
7681 const struct nfs4_mig_recovery_ops *ops =
7682 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007683 struct nfs4_exception exception = {
7684 .interruptible = true,
7685 };
Chuck Lever44c99932013-10-17 14:13:30 -04007686 int status;
7687
7688 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7689 (unsigned long long)server->fsid.major,
7690 (unsigned long long)server->fsid.minor,
7691 clp->cl_hostname);
7692 nfs_display_fhandle(NFS_FH(inode), __func__);
7693
7694 do {
7695 status = ops->fsid_present(inode, cred);
7696 if (status != -NFS4ERR_DELAY)
7697 break;
7698 nfs4_handle_exception(server, status, &exception);
7699 } while (exception.retry);
7700 return status;
7701}
7702
Trond Myklebust302fad72019-02-18 13:32:38 -05007703/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007704 * If 'use_integrity' is true and the state managment nfs_client
7705 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7706 * and the machine credential as per RFC3530bis and RFC5661 Security
7707 * Considerations sections. Otherwise, just use the user cred with the
7708 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007709 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007710static 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 +00007711{
7712 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04007713 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7714 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007715 struct nfs4_secinfo_arg args = {
7716 .dir_fh = NFS_FH(dir),
7717 .name = name,
7718 };
7719 struct nfs4_secinfo_res res = {
7720 .flavors = flavors,
7721 };
7722 struct rpc_message msg = {
7723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7724 .rpc_argp = &args,
7725 .rpc_resp = &res,
7726 };
Anna Schumaker50493362019-08-14 15:30:16 -04007727 struct nfs4_call_sync_data data = {
7728 .seq_server = NFS_SERVER(dir),
7729 .seq_args = &args.seq_args,
7730 .seq_res = &res.seq_res,
7731 };
7732 struct rpc_task_setup task_setup = {
7733 .rpc_client = clnt,
7734 .rpc_message = &msg,
7735 .callback_ops = clp->cl_mvops->call_sync_ops,
7736 .callback_data = &data,
7737 .flags = RPC_TASK_NO_ROUND_ROBIN,
7738 };
NeilBrowna52458b2018-12-03 11:30:31 +11007739 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007740
7741 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04007742 clnt = clp->cl_rpcclient;
7743 task_setup.rpc_client = clnt;
7744
7745 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007746 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007747 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007748
7749 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007750
Anna Schumaker50493362019-08-14 15:30:16 -04007751 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7752 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7753 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007754
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007755 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007756
NeilBrowna52458b2018-12-03 11:30:31 +11007757 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007758 return status;
7759}
7760
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007761int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7762 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007763{
Trond Myklebust0688e642019-04-07 13:59:09 -04007764 struct nfs4_exception exception = {
7765 .interruptible = true,
7766 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007767 int err;
7768 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007769 err = -NFS4ERR_WRONGSEC;
7770
7771 /* try to use integrity protection with machine cred */
7772 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7773 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7774
7775 /*
7776 * if unable to use integrity protection, or SECINFO with
7777 * integrity protection returns NFS4ERR_WRONGSEC (which is
7778 * disallowed by spec, but exists in deployed servers) use
7779 * the current filesystem's rpc_client and the user cred.
7780 */
7781 if (err == -NFS4ERR_WRONGSEC)
7782 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7783
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007784 trace_nfs4_secinfo(dir, name, err);
7785 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007786 &exception);
7787 } while (exception.retry);
7788 return err;
7789}
7790
Andy Adamson557134a2009-04-01 09:21:53 -04007791#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007792/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007793 * Check the exchange flags returned by the server for invalid flags, having
7794 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7795 * DS flags set.
7796 */
7797static int nfs4_check_cl_exchange_flags(u32 flags)
7798{
7799 if (flags & ~EXCHGID4_FLAG_MASK_R)
7800 goto out_inval;
7801 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7802 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7803 goto out_inval;
7804 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7805 goto out_inval;
7806 return NFS_OK;
7807out_inval:
7808 return -NFS4ERR_INVAL;
7809}
7810
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007811static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007812nfs41_same_server_scope(struct nfs41_server_scope *a,
7813 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007814{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007815 if (a->server_scope_sz != b->server_scope_sz)
7816 return false;
7817 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007818}
7819
Andy Adamson02a95de2016-02-05 16:08:37 -05007820static void
7821nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7822{
7823}
7824
7825static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7826 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7827};
7828
Andy Adamson357f54d2010-12-14 10:11:57 -05007829/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007830 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007831 *
7832 * The 4.1 client currently uses the same TCP connection for the
7833 * fore and backchannel.
7834 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007835static
7836int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7837 struct rpc_xprt *xprt,
7838 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007839 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007840{
7841 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007842 struct nfs41_bind_conn_to_session_args args = {
7843 .client = clp,
7844 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7845 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007846 struct nfs41_bind_conn_to_session_res res;
7847 struct rpc_message msg = {
7848 .rpc_proc =
7849 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007850 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007851 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007852 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007853 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007854 struct rpc_task_setup task_setup_data = {
7855 .rpc_client = clnt,
7856 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007857 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007858 .rpc_message = &msg,
7859 .flags = RPC_TASK_TIMEOUT,
7860 };
7861 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007862
Trond Myklebust71a097c2015-02-18 09:27:18 -08007863 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7864 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7865 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007866
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007867 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7868 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7869 args.dir = NFS4_CDFC4_FORE;
7870
7871 task = rpc_run_task(&task_setup_data);
7872 if (!IS_ERR(task)) {
7873 status = task->tk_status;
7874 rpc_put_task(task);
7875 } else
7876 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007877 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007878 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007879 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007880 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7881 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007882 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007883 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007884 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007885 dprintk("NFS: %s: Unexpected direction from server\n",
7886 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007887 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007888 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007889 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007890 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7891 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007892 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007893 }
7894 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007895
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007896 return status;
7897}
7898
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007899struct rpc_bind_conn_calldata {
7900 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007901 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007902};
7903
7904static int
7905nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7906 struct rpc_xprt *xprt,
7907 void *calldata)
7908{
7909 struct rpc_bind_conn_calldata *p = calldata;
7910
7911 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7912}
7913
NeilBrowna52458b2018-12-03 11:30:31 +11007914int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007915{
7916 struct rpc_bind_conn_calldata data = {
7917 .clp = clp,
7918 .cred = cred,
7919 };
7920 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7921 nfs4_proc_bind_conn_to_session_callback, &data);
7922}
7923
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007924/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007925 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7926 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007927 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007928static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7929 .how = SP4_MACH_CRED,
7930 .enforce.u.words = {
7931 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7932 1 << (OP_EXCHANGE_ID - 32) |
7933 1 << (OP_CREATE_SESSION - 32) |
7934 1 << (OP_DESTROY_SESSION - 32) |
7935 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007936 },
7937 .allow.u.words = {
7938 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007939 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007940 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007941 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007942 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007943 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007944 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007945 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007946 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007947 1 << (OP_FREE_STATEID - 32) |
7948 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007949 }
7950};
7951
7952/*
7953 * Select the state protection mode for client `clp' given the server results
7954 * from exchange_id in `sp'.
7955 *
7956 * Returns 0 on success, negative errno otherwise.
7957 */
7958static int nfs4_sp4_select_mode(struct nfs_client *clp,
7959 struct nfs41_state_protection *sp)
7960{
7961 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7962 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7963 1 << (OP_EXCHANGE_ID - 32) |
7964 1 << (OP_CREATE_SESSION - 32) |
7965 1 << (OP_DESTROY_SESSION - 32) |
7966 1 << (OP_DESTROY_CLIENTID - 32)
7967 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007968 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007969 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007970 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007971
7972 if (sp->how == SP4_MACH_CRED) {
7973 /* Print state protect result */
7974 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7975 for (i = 0; i <= LAST_NFS4_OP; i++) {
7976 if (test_bit(i, sp->enforce.u.longs))
7977 dfprintk(MOUNT, " enforce op %d\n", i);
7978 if (test_bit(i, sp->allow.u.longs))
7979 dfprintk(MOUNT, " allow op %d\n", i);
7980 }
7981
7982 /* make sure nothing is on enforce list that isn't supported */
7983 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7984 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7985 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007986 ret = -EINVAL;
7987 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007988 }
7989 }
7990
7991 /*
7992 * Minimal mode - state operations are allowed to use machine
7993 * credential. Note this already happens by default, so the
7994 * client doesn't have to do anything more than the negotiation.
7995 *
7996 * NOTE: we don't care if EXCHANGE_ID is in the list -
7997 * we're already using the machine cred for exchange_id
7998 * and will never use a different cred.
7999 */
8000 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8001 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8002 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8003 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8004 dfprintk(MOUNT, "sp4_mach_cred:\n");
8005 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008006 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008007 } else {
8008 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008009 ret = -EINVAL;
8010 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008011 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008012
8013 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008014 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8015 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008016 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8017 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008018 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008019 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008020
Andrew Elble99ade3c2015-12-02 09:39:51 -05008021 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8022 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008023 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008024 }
8025
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008026 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8027 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8028 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008029 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008030 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008031
8032 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8033 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8034 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008035 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008036 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008037
8038 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8039 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008040 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008041 }
8042
8043 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8044 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008045 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008046 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008047 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008048out:
8049 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008050 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008051}
8052
Andy Adamson8d89bd72016-09-09 09:22:18 -04008053struct nfs41_exchange_id_data {
8054 struct nfs41_exchange_id_res res;
8055 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008056};
8057
Andy Adamson8d89bd72016-09-09 09:22:18 -04008058static void nfs4_exchange_id_release(void *data)
8059{
8060 struct nfs41_exchange_id_data *cdata =
8061 (struct nfs41_exchange_id_data *)data;
8062
Olga Kornievskaia63513232017-03-13 10:36:19 -04008063 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008064 kfree(cdata->res.impl_id);
8065 kfree(cdata->res.server_scope);
8066 kfree(cdata->res.server_owner);
8067 kfree(cdata);
8068}
8069
8070static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008071 .rpc_release = nfs4_exchange_id_release,
8072};
8073
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008074/*
8075 * _nfs4_proc_exchange_id()
8076 *
8077 * Wrapper for EXCHANGE_ID operation.
8078 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008079static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008080nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008081 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008082{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008083 struct rpc_message msg = {
8084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008085 .rpc_cred = cred,
8086 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008087 struct rpc_task_setup task_setup_data = {
8088 .rpc_client = clp->cl_rpcclient,
8089 .callback_ops = &nfs4_exchange_id_call_ops,
8090 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008091 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008092 };
8093 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008094 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008095
Elena Reshetova212bf412017-10-20 12:53:38 +03008096 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008097 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008098
Trond Myklebust9c760d12017-07-31 18:38:50 -04008099 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008100 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008101 if (!calldata)
8102 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008103
Trond Myklebustfd405592017-08-01 16:02:47 -04008104 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008105
8106 status = nfs4_init_uniform_client_string(clp);
8107 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008108 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008109
Andy Adamson8d89bd72016-09-09 09:22:18 -04008110 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8111 GFP_NOFS);
8112 status = -ENOMEM;
8113 if (unlikely(calldata->res.server_owner == NULL))
8114 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008115
Andy Adamson8d89bd72016-09-09 09:22:18 -04008116 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008117 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008118 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008119 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008120
Andy Adamson8d89bd72016-09-09 09:22:18 -04008121 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8122 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008123 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008124
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008125 switch (sp4_how) {
8126 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008127 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008128 break;
8129
8130 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008131 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008132 break;
8133
8134 default:
8135 /* unsupported! */
8136 WARN_ON_ONCE(1);
8137 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008138 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008139 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008140 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008141 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008142 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008143 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8144 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008145 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008146 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008147 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8148 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008149#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008150 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008151#endif
8152 msg.rpc_argp = &calldata->args;
8153 msg.rpc_resp = &calldata->res;
8154 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008155
Trond Myklebust9c760d12017-07-31 18:38:50 -04008156 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008157
8158out_impl_id:
8159 kfree(calldata->res.impl_id);
8160out_server_scope:
8161 kfree(calldata->res.server_scope);
8162out_server_owner:
8163 kfree(calldata->res.server_owner);
8164out_calldata:
8165 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008166out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008167 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008168 return ERR_PTR(status);
8169}
8170
8171/*
8172 * _nfs4_proc_exchange_id()
8173 *
8174 * Wrapper for EXCHANGE_ID operation.
8175 */
NeilBrowna52458b2018-12-03 11:30:31 +11008176static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008177 u32 sp4_how)
8178{
8179 struct rpc_task *task;
8180 struct nfs41_exchange_id_args *argp;
8181 struct nfs41_exchange_id_res *resp;
8182 int status;
8183
8184 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8185 if (IS_ERR(task))
8186 return PTR_ERR(task);
8187
8188 argp = task->tk_msg.rpc_argp;
8189 resp = task->tk_msg.rpc_resp;
8190 status = task->tk_status;
8191 if (status != 0)
8192 goto out;
8193
8194 status = nfs4_check_cl_exchange_flags(resp->flags);
8195 if (status != 0)
8196 goto out;
8197
8198 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8199 if (status != 0)
8200 goto out;
8201
8202 clp->cl_clientid = resp->clientid;
8203 clp->cl_exchange_flags = resp->flags;
8204 clp->cl_seqid = resp->seqid;
8205 /* Client ID is not confirmed */
8206 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8207 clear_bit(NFS4_SESSION_ESTABLISHED,
8208 &clp->cl_session->session_state);
8209
8210 if (clp->cl_serverscope != NULL &&
8211 !nfs41_same_server_scope(clp->cl_serverscope,
8212 resp->server_scope)) {
8213 dprintk("%s: server_scope mismatch detected\n",
8214 __func__);
8215 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8216 }
8217
8218 swap(clp->cl_serverowner, resp->server_owner);
8219 swap(clp->cl_serverscope, resp->server_scope);
8220 swap(clp->cl_implid, resp->impl_id);
8221
8222 /* Save the EXCHANGE_ID verifier session trunk tests */
8223 memcpy(clp->cl_confirm.data, argp->verifier.data,
8224 sizeof(clp->cl_confirm.data));
8225out:
8226 trace_nfs4_exchange_id(clp, status);
8227 rpc_put_task(task);
8228 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008229}
8230
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008231/*
8232 * nfs4_proc_exchange_id()
8233 *
8234 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8235 *
8236 * Since the clientid has expired, all compounds using sessions
8237 * associated with the stale clientid will be returning
8238 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8239 * be in some phase of session reset.
8240 *
8241 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8242 */
NeilBrowna52458b2018-12-03 11:30:31 +11008243int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008244{
8245 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8246 int status;
8247
8248 /* try SP4_MACH_CRED if krb5i/p */
8249 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8250 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008251 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008252 if (!status)
8253 return 0;
8254 }
8255
8256 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008257 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008258}
8259
Andy Adamson04fa2c62016-09-09 09:22:29 -04008260/**
8261 * nfs4_test_session_trunk
8262 *
8263 * This is an add_xprt_test() test function called from
8264 * rpc_clnt_setup_test_and_add_xprt.
8265 *
8266 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8267 * and is dereferrenced in nfs4_exchange_id_release
8268 *
8269 * Upon success, add the new transport to the rpc_clnt
8270 *
8271 * @clnt: struct rpc_clnt to get new transport
8272 * @xprt: the rpc_xprt to test
8273 * @data: call data for _nfs4_proc_exchange_id.
8274 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308275void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008276 void *data)
8277{
8278 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008279 struct rpc_task *task;
8280 int status;
8281
Andy Adamson04fa2c62016-09-09 09:22:29 -04008282 u32 sp4_how;
8283
8284 dprintk("--> %s try %s\n", __func__,
8285 xprt->address_strings[RPC_DISPLAY_ADDR]);
8286
8287 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8288
8289 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008290 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8291 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308292 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008293
8294 status = task->tk_status;
8295 if (status == 0)
8296 status = nfs4_detect_session_trunking(adata->clp,
8297 task->tk_msg.rpc_resp, xprt);
8298
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308299 if (status == 0)
8300 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8301
Trond Myklebust9c760d12017-07-31 18:38:50 -04008302 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008303}
8304EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8305
Trond Myklebust66245532012-05-25 17:18:09 -04008306static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008307 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008308{
8309 struct rpc_message msg = {
8310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8311 .rpc_argp = clp,
8312 .rpc_cred = cred,
8313 };
8314 int status;
8315
NeilBrown5a0c2572019-05-30 10:41:28 +10008316 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8317 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008318 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008319 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008320 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008321 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8322 return status;
8323}
8324
8325static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008326 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008327{
8328 unsigned int loop;
8329 int ret;
8330
8331 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8332 ret = _nfs4_proc_destroy_clientid(clp, cred);
8333 switch (ret) {
8334 case -NFS4ERR_DELAY:
8335 case -NFS4ERR_CLIENTID_BUSY:
8336 ssleep(1);
8337 break;
8338 default:
8339 return ret;
8340 }
8341 }
8342 return 0;
8343}
8344
8345int nfs4_destroy_clientid(struct nfs_client *clp)
8346{
NeilBrowna52458b2018-12-03 11:30:31 +11008347 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008348 int ret = 0;
8349
8350 if (clp->cl_mvops->minor_version < 1)
8351 goto out;
8352 if (clp->cl_exchange_flags == 0)
8353 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008354 if (clp->cl_preserve_clid)
8355 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008356 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008357 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008358 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008359 switch (ret) {
8360 case 0:
8361 case -NFS4ERR_STALE_CLIENTID:
8362 clp->cl_exchange_flags = 0;
8363 }
8364out:
8365 return ret;
8366}
8367
Donald Buczek0efb01b2019-07-07 21:26:08 +02008368#endif /* CONFIG_NFS_V4_1 */
8369
Andy Adamson2050f0c2009-04-01 09:22:30 -04008370struct nfs4_get_lease_time_data {
8371 struct nfs4_get_lease_time_args *args;
8372 struct nfs4_get_lease_time_res *res;
8373 struct nfs_client *clp;
8374};
8375
8376static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8377 void *calldata)
8378{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008379 struct nfs4_get_lease_time_data *data =
8380 (struct nfs4_get_lease_time_data *)calldata;
8381
8382 dprintk("--> %s\n", __func__);
8383 /* just setup sequence, do not trigger session recovery
8384 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008385 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008386 &data->args->la_seq_args,
8387 &data->res->lr_seq_res,
8388 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008389 dprintk("<-- %s\n", __func__);
8390}
8391
8392/*
8393 * Called from nfs4_state_manager thread for session setup, so don't recover
8394 * from sequence operation or clientid errors.
8395 */
8396static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8397{
8398 struct nfs4_get_lease_time_data *data =
8399 (struct nfs4_get_lease_time_data *)calldata;
8400
8401 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008402 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008403 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008404 switch (task->tk_status) {
8405 case -NFS4ERR_DELAY:
8406 case -NFS4ERR_GRACE:
8407 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8408 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8409 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008410 /* fall through */
8411 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008412 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008413 return;
8414 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008415 dprintk("<-- %s\n", __func__);
8416}
8417
Trond Myklebust17280172012-03-11 13:11:00 -04008418static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008419 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8420 .rpc_call_done = nfs4_get_lease_time_done,
8421};
8422
8423int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8424{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008425 struct nfs4_get_lease_time_args args;
8426 struct nfs4_get_lease_time_res res = {
8427 .lr_fsinfo = fsinfo,
8428 };
8429 struct nfs4_get_lease_time_data data = {
8430 .args = &args,
8431 .res = &res,
8432 .clp = clp,
8433 };
8434 struct rpc_message msg = {
8435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8436 .rpc_argp = &args,
8437 .rpc_resp = &res,
8438 };
8439 struct rpc_task_setup task_setup = {
8440 .rpc_client = clp->cl_rpcclient,
8441 .rpc_message = &msg,
8442 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008443 .callback_data = &data,
8444 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008445 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008446
Anna Schumakerfba83f32018-05-04 16:22:50 -04008447 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008448 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008449}
8450
Donald Buczek0efb01b2019-07-07 21:26:08 +02008451#ifdef CONFIG_NFS_V4_1
8452
Andy Adamsonfc931582009-04-01 09:22:31 -04008453/*
8454 * Initialize the values to be used by the client in CREATE_SESSION
8455 * If nfs4_init_session set the fore channel request and response sizes,
8456 * use them.
8457 *
8458 * Set the back channel max_resp_sz_cached to zero to force the client to
8459 * always set csa_cachethis to FALSE because the current implementation
8460 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8461 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008462static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8463 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008464{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008465 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008466 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008467 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008468
Andy Adamson18aad3d2013-06-26 12:21:49 -04008469 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8470 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8471
Andy Adamsonfc931582009-04-01 09:22:31 -04008472 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008473 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8474 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008475 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008476 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008477
8478 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008479 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008480 __func__,
8481 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008482 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008483
8484 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008485 args->bc_attrs.max_rqst_sz = max_bc_payload;
8486 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008487 args->bc_attrs.max_resp_sz_cached = 0;
8488 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008489 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008490 if (args->bc_attrs.max_reqs > max_bc_slots)
8491 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008492
8493 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8494 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8495 __func__,
8496 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8497 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8498 args->bc_attrs.max_reqs);
8499}
8500
Trond Myklebust79969dd2015-02-18 11:30:18 -08008501static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8502 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008503{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008504 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008505 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008506
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008507 if (rcvd->max_resp_sz > sent->max_resp_sz)
8508 return -EINVAL;
8509 /*
8510 * Our requested max_ops is the minimum we need; we're not
8511 * prepared to break up compounds into smaller pieces than that.
8512 * So, no point even trying to continue if the server won't
8513 * cooperate:
8514 */
8515 if (rcvd->max_ops < sent->max_ops)
8516 return -EINVAL;
8517 if (rcvd->max_reqs == 0)
8518 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008519 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8520 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008521 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008522}
8523
Trond Myklebust79969dd2015-02-18 11:30:18 -08008524static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8525 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008526{
8527 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008528 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008529
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008530 if (!(res->flags & SESSION4_BACK_CHAN))
8531 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008532 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8533 return -EINVAL;
8534 if (rcvd->max_resp_sz < sent->max_resp_sz)
8535 return -EINVAL;
8536 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8537 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008538 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008539 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008540 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008541 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008542out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008543 return 0;
8544}
Andy Adamson8d353012009-04-01 09:22:32 -04008545
Andy Adamson8d353012009-04-01 09:22:32 -04008546static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008547 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008548{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008549 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008550
Trond Myklebust79969dd2015-02-18 11:30:18 -08008551 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008552 if (ret)
8553 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008554 return nfs4_verify_back_channel_attrs(args, res);
8555}
8556
8557static void nfs4_update_session(struct nfs4_session *session,
8558 struct nfs41_create_session_res *res)
8559{
8560 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008561 /* Mark client id and session as being confirmed */
8562 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8563 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008564 session->flags = res->flags;
8565 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008566 if (res->flags & SESSION4_BACK_CHAN)
8567 memcpy(&session->bc_attrs, &res->bc_attrs,
8568 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008569}
8570
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008571static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008572 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008573{
8574 struct nfs4_session *session = clp->cl_session;
8575 struct nfs41_create_session_args args = {
8576 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008577 .clientid = clp->cl_clientid,
8578 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008579 .cb_program = NFS4_CALLBACK,
8580 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008581 struct nfs41_create_session_res res;
8582
Andy Adamsonfc931582009-04-01 09:22:31 -04008583 struct rpc_message msg = {
8584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8585 .rpc_argp = &args,
8586 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008587 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008588 };
8589 int status;
8590
Chuck Lever6b26cc82016-05-02 14:40:40 -04008591 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008592 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008593
NeilBrown5a0c2572019-05-30 10:41:28 +10008594 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8595 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008596 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008597
Trond Myklebustb519d402016-09-11 14:50:01 -04008598 switch (status) {
8599 case -NFS4ERR_STALE_CLIENTID:
8600 case -NFS4ERR_DELAY:
8601 case -ETIMEDOUT:
8602 case -EACCES:
8603 case -EAGAIN:
8604 goto out;
8605 };
8606
8607 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008608 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008609 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008610 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008611 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008612 if (status)
8613 goto out;
8614 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008615 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008616out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008617 return status;
8618}
8619
8620/*
8621 * Issues a CREATE_SESSION operation to the server.
8622 * It is the responsibility of the caller to verify the session is
8623 * expired before calling this routine.
8624 */
NeilBrowna52458b2018-12-03 11:30:31 +11008625int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008626{
8627 int status;
8628 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008629 struct nfs4_session *session = clp->cl_session;
8630
8631 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8632
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008633 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008634 if (status)
8635 goto out;
8636
Andy Adamsonaacd5532011-11-09 13:58:21 -05008637 /* Init or reset the session slot tables */
8638 status = nfs4_setup_session_slot_tables(session);
8639 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008640 if (status)
8641 goto out;
8642
8643 ptr = (unsigned *)&session->sess_id.data[0];
8644 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8645 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008646out:
8647 dprintk("<-- %s\n", __func__);
8648 return status;
8649}
8650
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008651/*
8652 * Issue the over-the-wire RPC DESTROY_SESSION.
8653 * The caller must serialize access to this routine.
8654 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008655int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008656 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008657{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008658 struct rpc_message msg = {
8659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8660 .rpc_argp = session,
8661 .rpc_cred = cred,
8662 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008663 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008664
8665 dprintk("--> nfs4_proc_destroy_session\n");
8666
8667 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008668 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8669 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008670
NeilBrown5a0c2572019-05-30 10:41:28 +10008671 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8672 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008673 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008674
8675 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008676 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008677 "Session has been destroyed regardless...\n", status);
8678
8679 dprintk("<-- nfs4_proc_destroy_session\n");
8680 return status;
8681}
8682
Trond Myklebust7b38c362012-05-23 13:23:31 -04008683/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008684 * Renew the cl_session lease.
8685 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008686struct nfs4_sequence_data {
8687 struct nfs_client *clp;
8688 struct nfs4_sequence_args args;
8689 struct nfs4_sequence_res res;
8690};
8691
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008692static void nfs41_sequence_release(void *data)
8693{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008694 struct nfs4_sequence_data *calldata = data;
8695 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008696
Elena Reshetova212bf412017-10-20 12:53:38 +03008697 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008698 nfs4_schedule_state_renewal(clp);
8699 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008700 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008701}
8702
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008703static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8704{
8705 switch(task->tk_status) {
8706 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008707 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8708 return -EAGAIN;
8709 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008710 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008711 }
8712 return 0;
8713}
8714
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008715static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008716{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008717 struct nfs4_sequence_data *calldata = data;
8718 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008719
Trond Myklebust14516c32010-07-31 14:29:06 -04008720 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8721 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008722
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008723 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008724 if (task->tk_status < 0) {
8725 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008726 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008727 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008728
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008729 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8730 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008731 return;
8732 }
8733 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008734 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008735out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008736 dprintk("<-- %s\n", __func__);
8737}
8738
8739static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8740{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008741 struct nfs4_sequence_data *calldata = data;
8742 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008743 struct nfs4_sequence_args *args;
8744 struct nfs4_sequence_res *res;
8745
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008746 args = task->tk_msg.rpc_argp;
8747 res = task->tk_msg.rpc_resp;
8748
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008749 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008750}
8751
8752static const struct rpc_call_ops nfs41_sequence_ops = {
8753 .rpc_call_done = nfs41_sequence_call_done,
8754 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008755 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008756};
8757
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008758static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008759 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008760 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008761 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008762{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008763 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008764 struct rpc_message msg = {
8765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8766 .rpc_cred = cred,
8767 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008768 struct rpc_task_setup task_setup_data = {
8769 .rpc_client = clp->cl_rpcclient,
8770 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008771 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008772 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008773 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008774 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008775
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008776 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008777 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008778 goto out_err;
8779
8780 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008781 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008782 if (calldata == NULL)
8783 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008784 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008785 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008786 msg.rpc_argp = &calldata->args;
8787 msg.rpc_resp = &calldata->res;
8788 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008789 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008790
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008791 ret = rpc_run_task(&task_setup_data);
8792 if (IS_ERR(ret))
8793 goto out_err;
8794 return ret;
8795out_put_clp:
8796 nfs_put_client(clp);
8797out_err:
8798 nfs41_release_slot(slot);
8799 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008800}
8801
NeilBrowna52458b2018-12-03 11:30:31 +11008802static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008803{
8804 struct rpc_task *task;
8805 int ret = 0;
8806
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008807 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008808 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008809 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008810 if (IS_ERR(task))
8811 ret = PTR_ERR(task);
8812 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008813 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008814 dprintk("<-- %s status=%d\n", __func__, ret);
8815 return ret;
8816}
8817
NeilBrowna52458b2018-12-03 11:30:31 +11008818static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008819{
8820 struct rpc_task *task;
8821 int ret;
8822
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008823 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008824 if (IS_ERR(task)) {
8825 ret = PTR_ERR(task);
8826 goto out;
8827 }
8828 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008829 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008830 ret = task->tk_status;
8831 rpc_put_task(task);
8832out:
8833 dprintk("<-- %s status=%d\n", __func__, ret);
8834 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008835}
8836
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008837struct nfs4_reclaim_complete_data {
8838 struct nfs_client *clp;
8839 struct nfs41_reclaim_complete_args arg;
8840 struct nfs41_reclaim_complete_res res;
8841};
8842
8843static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8844{
8845 struct nfs4_reclaim_complete_data *calldata = data;
8846
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008847 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008848 &calldata->arg.seq_args,
8849 &calldata->res.seq_res,
8850 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008851}
8852
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008853static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8854{
8855 switch(task->tk_status) {
8856 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008857 wake_up_all(&clp->cl_lock_waitq);
8858 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008859 case -NFS4ERR_COMPLETE_ALREADY:
8860 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8861 break;
8862 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008863 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008864 /* fall through */
8865 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008866 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008867 case -NFS4ERR_BADSESSION:
8868 case -NFS4ERR_DEADSESSION:
8869 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8870 nfs4_schedule_session_recovery(clp->cl_session,
8871 task->tk_status);
8872 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008873 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008874 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008875 }
8876 return 0;
8877}
8878
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008879static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8880{
8881 struct nfs4_reclaim_complete_data *calldata = data;
8882 struct nfs_client *clp = calldata->clp;
8883 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8884
8885 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008886 if (!nfs41_sequence_done(task, res))
8887 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008888
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008889 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008890 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8891 rpc_restart_call_prepare(task);
8892 return;
8893 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008894 dprintk("<-- %s\n", __func__);
8895}
8896
8897static void nfs4_free_reclaim_complete_data(void *data)
8898{
8899 struct nfs4_reclaim_complete_data *calldata = data;
8900
8901 kfree(calldata);
8902}
8903
8904static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8905 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8906 .rpc_call_done = nfs4_reclaim_complete_done,
8907 .rpc_release = nfs4_free_reclaim_complete_data,
8908};
8909
8910/*
8911 * Issue a global reclaim complete.
8912 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008913static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008914 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008915{
8916 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008917 struct rpc_message msg = {
8918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008919 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008920 };
8921 struct rpc_task_setup task_setup_data = {
8922 .rpc_client = clp->cl_rpcclient,
8923 .rpc_message = &msg,
8924 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04008925 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008926 };
8927 int status = -ENOMEM;
8928
8929 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008930 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008931 if (calldata == NULL)
8932 goto out;
8933 calldata->clp = clp;
8934 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008935
Anna Schumakerfba83f32018-05-04 16:22:50 -04008936 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008937 msg.rpc_argp = &calldata->arg;
8938 msg.rpc_resp = &calldata->res;
8939 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04008940 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008941out:
8942 dprintk("<-- %s status=%d\n", __func__, status);
8943 return status;
8944}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008945
8946static void
8947nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8948{
8949 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008950 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008951
8952 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008953 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008954 &lgp->res.seq_res, task);
8955 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008956}
8957
8958static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8959{
8960 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008961
8962 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008963 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008964 dprintk("<-- %s\n", __func__);
8965}
8966
8967static int
8968nfs4_layoutget_handle_exception(struct rpc_task *task,
8969 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8970{
Trond Myklebustee314c22012-10-01 17:25:48 -07008971 struct inode *inode = lgp->args.inode;
8972 struct nfs_server *server = NFS_SERVER(inode);
8973 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008974 int nfs4err = task->tk_status;
8975 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008976 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008977
Boaz Harroshed7e5422014-01-22 20:34:54 +02008978 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008979
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008980 nfs4_sequence_free_slot(&lgp->res.seq_res);
8981
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008982 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008983 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008984 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008985
8986 /*
8987 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8988 * on the file. set tk_status to -ENODATA to tell upper layer to
8989 * retry go inband.
8990 */
8991 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008992 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008993 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008994 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008995 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8996 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8997 */
8998 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008999 status = -EOVERFLOW;
9000 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009001 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009002 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009003 * (or clients) writing to the same RAID stripe except when
9004 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009005 *
9006 * Treat it like we would RECALLCONFLICT -- we retry for a little
9007 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009008 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009009 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009010 if (lgp->args.minlength == 0) {
9011 status = -EOVERFLOW;
9012 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009013 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009014 status = -EBUSY;
9015 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009016 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009017 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009018 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009019 case -NFS4ERR_DELEG_REVOKED:
9020 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009021 case -NFS4ERR_EXPIRED:
9022 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009023 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009024 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009025 lo = NFS_I(inode)->layout;
9026 /* If the open stateid was bad, then recover it. */
9027 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009028 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009029 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009030 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009031 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009032 break;
9033 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009034
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009035 /*
9036 * Mark the bad layout state as invalid, then retry
9037 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009038 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009039 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009040 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009041 pnfs_free_lseg_list(&head);
9042 status = -EAGAIN;
9043 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009044 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009045
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009046 err = nfs4_handle_exception(server, nfs4err, exception);
9047 if (!status) {
9048 if (exception->retry)
9049 status = -EAGAIN;
9050 else
9051 status = err;
9052 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009053out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009054 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009055 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009056}
9057
Fred Isamandacb4522016-09-19 17:47:09 -04009058size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009059{
9060 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9061 return nfs_page_array_len(0, max_resp_sz);
9062}
9063
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009064static void nfs4_layoutget_release(void *calldata)
9065{
9066 struct nfs4_layoutget *lgp = calldata;
9067
9068 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009069 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009070 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009071 dprintk("<-- %s\n", __func__);
9072}
9073
9074static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9075 .rpc_call_prepare = nfs4_layoutget_prepare,
9076 .rpc_call_done = nfs4_layoutget_done,
9077 .rpc_release = nfs4_layoutget_release,
9078};
9079
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009080struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009081nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009082{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009083 struct inode *inode = lgp->args.inode;
9084 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009085 struct rpc_task *task;
9086 struct rpc_message msg = {
9087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9088 .rpc_argp = &lgp->args,
9089 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009090 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009091 };
9092 struct rpc_task_setup task_setup_data = {
9093 .rpc_client = server->client,
9094 .rpc_message = &msg,
9095 .callback_ops = &nfs4_layoutget_call_ops,
9096 .callback_data = lgp,
9097 .flags = RPC_TASK_ASYNC,
9098 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009099 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009100 struct nfs4_exception exception = {
9101 .inode = inode,
9102 .timeout = *timeout,
9103 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009104 int status = 0;
9105
9106 dprintk("--> %s\n", __func__);
9107
Peng Tao4bd5a982014-11-17 11:05:17 +08009108 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9109 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9110
Anna Schumakerfba83f32018-05-04 16:22:50 -04009111 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009112
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009113 task = rpc_run_task(&task_setup_data);
9114 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009115 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05009116 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009117 if (status != 0)
9118 goto out;
9119
Trond Myklebust18c07782019-02-13 07:55:31 -05009120 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009121 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9122 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009123 } else if (lgp->res.layoutp->len == 0) {
9124 status = -EAGAIN;
9125 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009126 } else
9127 lseg = pnfs_layout_process(lgp);
9128out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009129 trace_nfs4_layoutget(lgp->args.ctx,
9130 &lgp->args.range,
9131 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009132 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009133 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009134
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009135 rpc_put_task(task);
9136 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009137 if (status)
9138 return ERR_PTR(status);
9139 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009140}
9141
Benny Halevycbe82602011-05-22 19:52:37 +03009142static void
9143nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9144{
9145 struct nfs4_layoutreturn *lrp = calldata;
9146
9147 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009148 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009149 &lrp->args.seq_args,
9150 &lrp->res.seq_res,
9151 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009152 if (!pnfs_layout_is_valid(lrp->args.layout))
9153 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009154}
9155
9156static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9157{
9158 struct nfs4_layoutreturn *lrp = calldata;
9159 struct nfs_server *server;
9160
9161 dprintk("--> %s\n", __func__);
9162
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009163 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009164 return;
9165
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009166 /*
9167 * Was there an RPC level error? Assume the call succeeded,
9168 * and that we need to release the layout
9169 */
9170 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9171 lrp->res.lrs_present = 0;
9172 return;
9173 }
9174
Benny Halevycbe82602011-05-22 19:52:37 +03009175 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009176 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009177 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009178 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009179 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009180 lrp->args.inode))
9181 goto out_restart;
9182 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009183 default:
9184 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05009185 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009186 case 0:
9187 break;
9188 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009189 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009190 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009191 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009192 }
Benny Halevycbe82602011-05-22 19:52:37 +03009193 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009194 return;
9195out_restart:
9196 task->tk_status = 0;
9197 nfs4_sequence_free_slot(&lrp->res.seq_res);
9198 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009199}
9200
9201static void nfs4_layoutreturn_release(void *calldata)
9202{
9203 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009204 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009205
9206 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009207 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009208 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009209 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009210 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9211 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009212 pnfs_put_layout_hdr(lrp->args.layout);
9213 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009214 kfree(calldata);
9215 dprintk("<-- %s\n", __func__);
9216}
9217
9218static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9219 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9220 .rpc_call_done = nfs4_layoutreturn_done,
9221 .rpc_release = nfs4_layoutreturn_release,
9222};
9223
Peng Tao6c166052014-11-17 09:30:40 +08009224int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009225{
9226 struct rpc_task *task;
9227 struct rpc_message msg = {
9228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9229 .rpc_argp = &lrp->args,
9230 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009231 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009232 };
9233 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009234 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009235 .rpc_message = &msg,
9236 .callback_ops = &nfs4_layoutreturn_call_ops,
9237 .callback_data = lrp,
9238 };
Peng Tao6c166052014-11-17 09:30:40 +08009239 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009240
Andrew Elble99ade3c2015-12-02 09:39:51 -05009241 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9242 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9243 &task_setup_data.rpc_client, &msg);
9244
Benny Halevycbe82602011-05-22 19:52:37 +03009245 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009246 if (!sync) {
9247 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9248 if (!lrp->inode) {
9249 nfs4_layoutreturn_release(lrp);
9250 return -EAGAIN;
9251 }
9252 task_setup_data.flags |= RPC_TASK_ASYNC;
9253 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009254 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009255 task = rpc_run_task(&task_setup_data);
9256 if (IS_ERR(task))
9257 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009258 if (sync)
9259 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009260 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009261 dprintk("<-- %s status=%d\n", __func__, status);
9262 rpc_put_task(task);
9263 return status;
9264}
9265
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009266static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009267_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9268 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009269 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009270{
9271 struct nfs4_getdeviceinfo_args args = {
9272 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009273 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9274 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009275 };
9276 struct nfs4_getdeviceinfo_res res = {
9277 .pdev = pdev,
9278 };
9279 struct rpc_message msg = {
9280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9281 .rpc_argp = &args,
9282 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009283 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009284 };
9285 int status;
9286
9287 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009288 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009289 if (res.notification & ~args.notify_types)
9290 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009291 if (res.notification != args.notify_types)
9292 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009293
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009294 dprintk("<-- %s status=%d\n", __func__, status);
9295
9296 return status;
9297}
9298
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009299int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9300 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009301 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009302{
9303 struct nfs4_exception exception = { };
9304 int err;
9305
9306 do {
9307 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009308 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009309 &exception);
9310 } while (exception.retry);
9311 return err;
9312}
9313EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9314
Andy Adamson863a3c62011-03-23 13:27:54 +00009315static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9316{
9317 struct nfs4_layoutcommit_data *data = calldata;
9318 struct nfs_server *server = NFS_SERVER(data->args.inode);
9319
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009320 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009321 &data->args.seq_args,
9322 &data->res.seq_res,
9323 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009324}
9325
9326static void
9327nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9328{
9329 struct nfs4_layoutcommit_data *data = calldata;
9330 struct nfs_server *server = NFS_SERVER(data->args.inode);
9331
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009332 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009333 return;
9334
9335 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009336 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9337 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9338 case -NFS4ERR_BADLAYOUT: /* no layout */
9339 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009340 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009341 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009342 break;
9343 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009344 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009345 rpc_restart_call_prepare(task);
9346 return;
9347 }
9348 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009349}
9350
9351static void nfs4_layoutcommit_release(void *calldata)
9352{
9353 struct nfs4_layoutcommit_data *data = calldata;
9354
Andy Adamsondb29c082011-07-30 20:52:38 -04009355 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009356 nfs_post_op_update_inode_force_wcc(data->args.inode,
9357 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009358 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009359 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009360 kfree(data);
9361}
9362
9363static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9364 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9365 .rpc_call_done = nfs4_layoutcommit_done,
9366 .rpc_release = nfs4_layoutcommit_release,
9367};
9368
9369int
Andy Adamsonef311532011-03-12 02:58:10 -05009370nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009371{
9372 struct rpc_message msg = {
9373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9374 .rpc_argp = &data->args,
9375 .rpc_resp = &data->res,
9376 .rpc_cred = data->cred,
9377 };
9378 struct rpc_task_setup task_setup_data = {
9379 .task = &data->task,
9380 .rpc_client = NFS_CLIENT(data->args.inode),
9381 .rpc_message = &msg,
9382 .callback_ops = &nfs4_layoutcommit_ops,
9383 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009384 };
9385 struct rpc_task *task;
9386 int status = 0;
9387
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009388 dprintk("NFS: initiating layoutcommit call. sync %d "
9389 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009390 data->args.lastbytewritten,
9391 data->args.inode->i_ino);
9392
Trond Myklebust472e2592015-02-05 16:50:30 -05009393 if (!sync) {
9394 data->inode = nfs_igrab_and_active(data->args.inode);
9395 if (data->inode == NULL) {
9396 nfs4_layoutcommit_release(data);
9397 return -EAGAIN;
9398 }
9399 task_setup_data.flags = RPC_TASK_ASYNC;
9400 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009401 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009402 task = rpc_run_task(&task_setup_data);
9403 if (IS_ERR(task))
9404 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009405 if (sync)
9406 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009407 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009408 dprintk("%s: status %d\n", __func__, status);
9409 rpc_put_task(task);
9410 return status;
9411}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009412
Trond Myklebust302fad72019-02-18 13:32:38 -05009413/*
Andy Adamson97431202013-08-08 10:57:56 -04009414 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9415 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9416 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009417static int
9418_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009419 struct nfs_fsinfo *info,
9420 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009421{
9422 struct nfs41_secinfo_no_name_args args = {
9423 .style = SECINFO_STYLE_CURRENT_FH,
9424 };
9425 struct nfs4_secinfo_res res = {
9426 .flavors = flavors,
9427 };
9428 struct rpc_message msg = {
9429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9430 .rpc_argp = &args,
9431 .rpc_resp = &res,
9432 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009433 struct rpc_clnt *clnt = server->client;
Anna Schumakercc15e242019-08-14 16:22:31 -04009434 struct nfs4_call_sync_data data = {
9435 .seq_server = server,
9436 .seq_args = &args.seq_args,
9437 .seq_res = &res.seq_res,
9438 };
9439 struct rpc_task_setup task_setup = {
9440 .rpc_client = server->client,
9441 .rpc_message = &msg,
9442 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9443 .callback_data = &data,
9444 .flags = RPC_TASK_NO_ROUND_ROBIN,
9445 };
NeilBrowna52458b2018-12-03 11:30:31 +11009446 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009447 int status;
9448
9449 if (use_integrity) {
9450 clnt = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009451 task_setup.rpc_client = clnt;
9452
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009453 cred = nfs4_get_clid_cred(server->nfs_client);
9454 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009455 }
9456
9457 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009458 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9459 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009460 dprintk("<-- %s status=%d\n", __func__, status);
9461
NeilBrowna52458b2018-12-03 11:30:31 +11009462 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009463
9464 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009465}
9466
9467static int
9468nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9469 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9470{
Trond Myklebust0688e642019-04-07 13:59:09 -04009471 struct nfs4_exception exception = {
9472 .interruptible = true,
9473 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009474 int err;
9475 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009476 /* first try using integrity protection */
9477 err = -NFS4ERR_WRONGSEC;
9478
9479 /* try to use integrity protection with machine cred */
9480 if (_nfs4_is_integrity_protected(server->nfs_client))
9481 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9482 flavors, true);
9483
9484 /*
9485 * if unable to use integrity protection, or SECINFO with
9486 * integrity protection returns NFS4ERR_WRONGSEC (which is
9487 * disallowed by spec, but exists in deployed servers) use
9488 * the current filesystem's rpc_client and the user cred.
9489 */
9490 if (err == -NFS4ERR_WRONGSEC)
9491 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9492 flavors, false);
9493
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009494 switch (err) {
9495 case 0:
9496 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009497 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009498 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009499 default:
9500 err = nfs4_handle_exception(server, err, &exception);
9501 }
9502 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009503out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009504 return err;
9505}
9506
9507static int
9508nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9509 struct nfs_fsinfo *info)
9510{
9511 int err;
9512 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009513 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009514 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009515 struct nfs4_secinfo4 *secinfo;
9516 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009517
9518 page = alloc_page(GFP_KERNEL);
9519 if (!page) {
9520 err = -ENOMEM;
9521 goto out;
9522 }
9523
9524 flavors = page_address(page);
9525 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9526
9527 /*
9528 * Fall back on "guess and check" method if
9529 * the server doesn't support SECINFO_NO_NAME
9530 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009531 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009532 err = nfs4_find_root_sec(server, fhandle, info);
9533 goto out_freepage;
9534 }
9535 if (err)
9536 goto out_freepage;
9537
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009538 for (i = 0; i < flavors->num_flavors; i++) {
9539 secinfo = &flavors->flavors[i];
9540
9541 switch (secinfo->flavor) {
9542 case RPC_AUTH_NULL:
9543 case RPC_AUTH_UNIX:
9544 case RPC_AUTH_GSS:
9545 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9546 &secinfo->flavor_info);
9547 break;
9548 default:
9549 flavor = RPC_AUTH_MAXFLAVOR;
9550 break;
9551 }
9552
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009553 if (!nfs_auth_info_match(&server->auth_info, flavor))
9554 flavor = RPC_AUTH_MAXFLAVOR;
9555
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009556 if (flavor != RPC_AUTH_MAXFLAVOR) {
9557 err = nfs4_lookup_root_sec(server, fhandle,
9558 info, flavor);
9559 if (!err)
9560 break;
9561 }
9562 }
9563
9564 if (flavor == RPC_AUTH_MAXFLAVOR)
9565 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009566
9567out_freepage:
9568 put_page(page);
9569 if (err == -EACCES)
9570 return -EPERM;
9571out:
9572 return err;
9573}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009574
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009575static int _nfs41_test_stateid(struct nfs_server *server,
9576 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009577 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009578{
9579 int status;
9580 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009581 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009582 };
9583 struct nfs41_test_stateid_res res;
9584 struct rpc_message msg = {
9585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9586 .rpc_argp = &args,
9587 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009588 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009589 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009590 struct rpc_clnt *rpc_client = server->client;
9591
9592 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9593 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009594
Chuck Lever38527b12012-07-11 16:30:23 -04009595 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009596 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009597 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009598 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009599 if (status != NFS_OK) {
9600 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009601 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009602 }
9603 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009604 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009605}
9606
Trond Myklebust43912bb2016-09-22 13:38:56 -04009607static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9608 int err, struct nfs4_exception *exception)
9609{
9610 exception->retry = 0;
9611 switch(err) {
9612 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009613 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009614 nfs4_handle_exception(server, err, exception);
9615 break;
9616 case -NFS4ERR_BADSESSION:
9617 case -NFS4ERR_BADSLOT:
9618 case -NFS4ERR_BAD_HIGH_SLOT:
9619 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9620 case -NFS4ERR_DEADSESSION:
9621 nfs4_do_handle_exception(server, err, exception);
9622 }
9623}
9624
Chuck Lever38527b12012-07-11 16:30:23 -04009625/**
9626 * nfs41_test_stateid - perform a TEST_STATEID operation
9627 *
9628 * @server: server / transport on which to perform the operation
9629 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009630 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009631 *
9632 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9633 * Otherwise a negative NFS4ERR value is returned if the operation
9634 * failed or the state ID is not currently valid.
9635 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009636static int nfs41_test_stateid(struct nfs_server *server,
9637 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009638 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009639{
Trond Myklebust0688e642019-04-07 13:59:09 -04009640 struct nfs4_exception exception = {
9641 .interruptible = true,
9642 };
Bryan Schumaker7d974792011-06-02 14:59:08 -04009643 int err;
9644 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009645 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009646 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009647 } while (exception.retry);
9648 return err;
9649}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009650
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009651struct nfs_free_stateid_data {
9652 struct nfs_server *server;
9653 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009654 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009655};
9656
9657static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9658{
9659 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009660 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009661 &data->args.seq_args,
9662 &data->res.seq_res,
9663 task);
9664}
9665
9666static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9667{
9668 struct nfs_free_stateid_data *data = calldata;
9669
9670 nfs41_sequence_done(task, &data->res.seq_res);
9671
9672 switch (task->tk_status) {
9673 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009674 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009675 rpc_restart_call_prepare(task);
9676 }
9677}
9678
9679static void nfs41_free_stateid_release(void *calldata)
9680{
9681 kfree(calldata);
9682}
9683
Trond Myklebust17f26b12013-08-21 15:48:42 -04009684static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009685 .rpc_call_prepare = nfs41_free_stateid_prepare,
9686 .rpc_call_done = nfs41_free_stateid_done,
9687 .rpc_release = nfs41_free_stateid_release,
9688};
9689
Anna Schumaker2f261022018-05-15 13:03:39 -04009690/**
9691 * nfs41_free_stateid - perform a FREE_STATEID operation
9692 *
9693 * @server: server / transport on which to perform the operation
9694 * @stateid: state ID to release
9695 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009696 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009697 *
9698 * Note: this function is always asynchronous.
9699 */
9700static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009701 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009702 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009703 bool privileged)
9704{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009705 struct rpc_message msg = {
9706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009707 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009708 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009709 struct rpc_task_setup task_setup = {
9710 .rpc_client = server->client,
9711 .rpc_message = &msg,
9712 .callback_ops = &nfs41_free_stateid_ops,
9713 .flags = RPC_TASK_ASYNC,
9714 };
9715 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009716 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009717
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009718 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9719 &task_setup.rpc_client, &msg);
9720
Chuck Lever38527b12012-07-11 16:30:23 -04009721 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009722 data = kmalloc(sizeof(*data), GFP_NOFS);
9723 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009724 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009725 data->server = server;
9726 nfs4_stateid_copy(&data->args.stateid, stateid);
9727
9728 task_setup.callback_data = data;
9729
9730 msg.rpc_argp = &data->args;
9731 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009732 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009733 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009734 if (IS_ERR(task))
9735 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009736 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009737 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009738}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009739
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009740static void
9741nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009742{
NeilBrowna52458b2018-12-03 11:30:31 +11009743 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009744
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009745 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009746 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009747}
9748
Trond Myklebust36281ca2012-03-04 18:13:56 -05009749static bool nfs41_match_stateid(const nfs4_stateid *s1,
9750 const nfs4_stateid *s2)
9751{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009752 if (s1->type != s2->type)
9753 return false;
9754
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009755 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009756 return false;
9757
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009758 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009759 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009760
Anna Schumaker045c5512017-01-11 16:59:48 -05009761 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009762}
9763
Andy Adamson557134a2009-04-01 09:21:53 -04009764#endif /* CONFIG_NFS_V4_1 */
9765
Trond Myklebust36281ca2012-03-04 18:13:56 -05009766static bool nfs4_match_stateid(const nfs4_stateid *s1,
9767 const nfs4_stateid *s2)
9768{
Trond Myklebustf597c532012-03-04 18:13:56 -05009769 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009770}
9771
9772
Trond Myklebust17280172012-03-11 13:11:00 -04009773static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009774 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009775 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776 .recover_open = nfs4_open_reclaim,
9777 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009778 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009779 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780};
9781
Andy Adamson591d71c2009-04-01 09:22:47 -04009782#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009783static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009784 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9785 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9786 .recover_open = nfs4_open_reclaim,
9787 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009788 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009789 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009790 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009791};
9792#endif /* CONFIG_NFS_V4_1 */
9793
Trond Myklebust17280172012-03-11 13:11:00 -04009794static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009795 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009796 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009797 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009799 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800};
9801
Andy Adamson591d71c2009-04-01 09:22:47 -04009802#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009803static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009804 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9805 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009806 .recover_open = nfs41_open_expired,
9807 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009808 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009809};
9810#endif /* CONFIG_NFS_V4_1 */
9811
Trond Myklebust17280172012-03-11 13:11:00 -04009812static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009813 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009814 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009815 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009816};
9817
9818#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009819static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009820 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009821 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009822 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009823};
9824#endif
9825
Chuck Leverec011fe2013-10-17 14:12:39 -04009826static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009827 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009828 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009829};
9830
9831#if defined(CONFIG_NFS_V4_1)
9832static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009833 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009834 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009835};
9836#endif /* CONFIG_NFS_V4_1 */
9837
Trond Myklebust97dc1352010-06-16 09:52:26 -04009838static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9839 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009840 .init_caps = NFS_CAP_READDIRPLUS
9841 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009842 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009843 .init_client = nfs40_init_client,
9844 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009845 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009846 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009847 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009848 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009849 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009850 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009851 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9852 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9853 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009854 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009855};
9856
9857#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009858static struct nfs_seqid *
9859nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9860{
9861 return NULL;
9862}
9863
Trond Myklebust97dc1352010-06-16 09:52:26 -04009864static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9865 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009866 .init_caps = NFS_CAP_READDIRPLUS
9867 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009868 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009869 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009870 | NFS_CAP_ATOMIC_OPEN_V1
9871 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009872 .init_client = nfs41_init_client,
9873 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009874 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009875 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009876 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009877 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009878 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009879 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009880 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009881 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9882 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9883 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009884 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009885};
9886#endif
9887
Steve Dickson42c2c422013-05-22 12:50:38 -04009888#if defined(CONFIG_NFS_V4_2)
9889static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9890 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009891 .init_caps = NFS_CAP_READDIRPLUS
9892 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009893 | NFS_CAP_POSIX_LOCK
9894 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009895 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009896 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009897 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009898 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009899 | NFS_CAP_OFFLOAD_CANCEL
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009900 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009901 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009902 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -05009903 | NFS_CAP_CLONE
9904 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009905 .init_client = nfs41_init_client,
9906 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009907 .match_stateid = nfs41_match_stateid,
9908 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009909 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009910 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009911 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009912 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009913 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009914 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9915 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9916 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009917 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009918};
9919#endif
9920
Trond Myklebust97dc1352010-06-16 09:52:26 -04009921const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9922 [0] = &nfs_v4_0_minor_ops,
9923#if defined(CONFIG_NFS_V4_1)
9924 [1] = &nfs_v4_1_minor_ops,
9925#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009926#if defined(CONFIG_NFS_V4_2)
9927 [2] = &nfs_v4_2_minor_ops,
9928#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009929};
9930
Trond Myklebust13997822016-07-24 17:10:52 -04009931static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009932{
9933 ssize_t error, error2;
9934
9935 error = generic_listxattr(dentry, list, size);
9936 if (error < 0)
9937 return error;
9938 if (list) {
9939 list += error;
9940 size -= error;
9941 }
9942
9943 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9944 if (error2 < 0)
9945 return error2;
9946 return error + error2;
9947}
9948
Trond Myklebust17f26b12013-08-21 15:48:42 -04009949static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009950 .create = nfs_create,
9951 .lookup = nfs_lookup,
9952 .atomic_open = nfs_atomic_open,
9953 .link = nfs_link,
9954 .unlink = nfs_unlink,
9955 .symlink = nfs_symlink,
9956 .mkdir = nfs_mkdir,
9957 .rmdir = nfs_rmdir,
9958 .mknod = nfs_mknod,
9959 .rename = nfs_rename,
9960 .permission = nfs_permission,
9961 .getattr = nfs_getattr,
9962 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009963 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009964};
9965
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009966static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009967 .permission = nfs_permission,
9968 .getattr = nfs_getattr,
9969 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009970 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009971};
9972
David Howells509de812006-08-22 20:06:11 -04009973const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974 .version = 4, /* protocol version */
9975 .dentry_ops = &nfs4_dentry_operations,
9976 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009977 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009978 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009979 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009980 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009981 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009982 .getattr = nfs4_proc_getattr,
9983 .setattr = nfs4_proc_setattr,
9984 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009985 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009986 .access = nfs4_proc_access,
9987 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009988 .create = nfs4_proc_create,
9989 .remove = nfs4_proc_remove,
9990 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009991 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009992 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009993 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009994 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009995 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009996 .link = nfs4_proc_link,
9997 .symlink = nfs4_proc_symlink,
9998 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009999 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010000 .readdir = nfs4_proc_readdir,
10001 .mknod = nfs4_proc_mknod,
10002 .statfs = nfs4_proc_statfs,
10003 .fsinfo = nfs4_proc_fsinfo,
10004 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010005 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010007 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010008 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010009 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010010 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010011 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010012 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010013 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010014 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010016 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010017 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010018 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010019 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010020 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010021 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -040010022 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010023 .create_server = nfs4_create_server,
10024 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010025};
10026
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010027static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010028 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010029 .list = nfs4_xattr_list_nfs4_acl,
10030 .get = nfs4_xattr_get_nfs4_acl,
10031 .set = nfs4_xattr_set_nfs4_acl,
10032};
10033
10034const struct xattr_handler *nfs4_xattr_handlers[] = {
10035 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010036#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10037 &nfs4_xattr_nfs4_label_handler,
10038#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010039 NULL
10040};
10041
Linus Torvalds1da177e2005-04-16 15:20:36 -070010042/*
10043 * Local variables:
10044 * c-basic-offset: 8
10045 * End:
10046 */